May cohort is now open: How to secure your spot:

Easily power algo trading with the popular pyalgotrading

In today’s guest post by AlgoBulls, we introduce pyalgotrading which is a Python package that helps create execute trading strategies.

Creating trading strategies with pyalgotrading is easy. The highlight of this process is that it can be done on any platform including web app, Jupyter notebooks or command line.

Today, we’ll get started with a simple example to demonstrate how pyalgotrading works.

Let’s go!

Easily power algo trading with the popular pyalgotrading

AlgoBulls is an algorithmic trading platform that makes it easy to backtest and execute strategies.

To set up a workspace for running pyalgotrading package on jupyter notebook, you can follow the steps in the documentation. Once set up is completed and you have launched the notebook, you’re ready!

Imports and set up

First, we need to import pyalgotrading and establish a connection. Instructions to get your access token, are here. After you connect, you can list all your saved strategies.

from pyalgotrading.algobulls import AlgoBullsConnection

connection = AlgoBullsConnection()

connection.set_access_token("<your-access-token>")

connection.get_all_strategies()

For every strategy, you will get a unique ID for the strategy. The ID will be used in other functions for the parameter called “strategy_code” or “strategy”.

Upload a strategy

If you have a strategy, you can upload it to your AlgoBulls account. Let’s use the EMA crossover strategy as an example. Grab the code here on GitHub. Save the code as a .py file, import it into the Notebook, and save into your account.

from ema_crossover import StrategyEMARegularOrder as strategy_cls

response = connection.create_strategy(
    strategy_cls, 
    overwrite=True
)
strategy = response['strategyId']

Execute the backtest

Execute the backtest by setting the parameters for the strategy and calling backtest.

parameters = {
  "TIMEPERIOD1": 12,
  "TIMEPERIOD2": 20,
}

vendor_details = {
    "brokerName": 'ALPACAV2',
    "credentialParameters": {
        "CLIENT_API_KEY": "<your-client-key>", 
        "CLIENT_API_SECRET": "<your-client-key>"
    }
}

connection.backtest(
    strategy=strategy,
    start="2021-08-01 09:15 -0400",
    end="2023-07-31 15:30 -0400",
    instrument="NASDAQ:MSFT",
    lots=5,
    parameters=parameters,
    candle="1 hour",
    vendor_details=vendor_details,
    initial_funds_virtual=5000
)

Once the strategy backtest starts, you’ll be notified with output from pyalgotrading.

Easily power algo trading with the popular pyalgotrading. We introduce pyalgotrading that helps create execute trading strategies.

Output the strategy analysis

Once your strategy’s execution is finished, you can check its results and performance. For that purpose, you can retrieve the performance report as a DataFrame to perform further analytics.

connection.get_backtesting_report_pnl_table(strategy)

You can easily compute industry-standard metrics like Sharpe and Sortino ratios using the above simple function call. It generates these metrics in a tabular format.

Vizualizations, in the form of charts, offer insights into your strategy’s performance. Each chart provides a unique perspective on your trading strategy’s behaviour.

connection.get_backtesting_report_statistics(
		strategy_code,
		report="full",
		html_dump=True
)

This outputs a QuantStats report depicting the strategy performance.

Easily power algo trading with the popular pyalgotrading. We introduce pyalgotrading that helps create execute trading strategies.

Next steps

As a next step, review the comprehensive AlgoBulls documentation and create your own strategy. You can use the same boilerplate code from this example to backtest it.