Reader small image

You're reading from  Python Algorithmic Trading Cookbook

Product typeBook
Published inAug 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838989354
Edition1st Edition
Languages
Right arrow
Author (1)
Pushpak Dagade
Pushpak Dagade
author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade

Right arrow
Algorithmic Trading - Backtesting

After building algorithmic trading strategies, as we did in the previous chapter, the first step is to backtest them over a given duration of time for a given strategy configuration.

Backtesting is a method of evaluating the performance of a trading strategy by virtually executing it over past data and analyzing its risk and return metrics. Real money is not used here. Typical backtesting metrics include Profit and Loss (P&L), maximum drawdown, count of total trades, winning trades, losing trades, long trades and short trades, average profit per winning and losing trade, and more. Until these metrics meet the necessary requirements, the entire process should be repeated with incremental changes being made to strategy parameters and/or strategy implementation.

If a strategy performs well on past data, it is likely to perform well on live data...

Technical requirements

EMA-Regular-Order strategy – fetching the strategy

In this recipe, you will fetch the StrategyEMARegularOrder strategy class from your account on the AlgoBulls platform, which you uploaded while going through the EMA-Regular-Order strategy – uploading the strategy recipe in Chapter 8, Algorithmic Trading Strategies – Coding Step by Step, on the AlgoBulls trading platform. This recipe starts with setting up a connection to the AlgoBulls platform, querying all available strategies in your account, and fetching the details of the required strategy class, StrategyEMARegularOrder.

Make sure you have gone through the first six recipes of the previous chapter to get a complete picture of the strategy class we will be using; that is, StrategyEMARegularOrder.

How to do it…

We execute the following steps for this recipe:

  1. Import the necessary modules:
>>> from pyalgotrading.algobulls import AlgoBullsConnection
  1. Create a new AlgoBulls connection object:
>...

EMA-Regular-Order strategy – backtesting the strategy

In this recipe, you will perform backtesting on the EMA-Regular-Order strategy. You must have fetched this strategy from your account in the AlgoBulls platform in the preceding recipe. You will leverage the backtesting functionality facilitated by pyalgotrading for this recipe, which, in turn, submits a backtesting job on the AlgoBulls platform.

Once submitted, backtesting will be run by the AlgoBulls backtesting engine. You can query the status anytime to find the state of the backtesting job. The job goes through the following states, in the given order:

  • STARTING (intermediate state)
  • STARTED (stable state)
  • STOPPING (intermediate state)
  • STOPPED (stable state)

On submitting a job, it starts with an intermediate state, STARTING. In this state, the AlgoBulls backtesting engine fetches the strategy and get the execution environment ready, which may take a couple of minutes. Once done, the job moves to the STARTED state. Strategy...

EMA-Regular-Order strategy – fetching backtesting logs in real time

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During the execution, every event that occurs and every decision taken by the AlgoBulls backtesting engine are recorded with exact timestamps in the form of textual logs. Examples of recorded activities include the given strategy config, every new candle generated at regular intervals, trades punched by your strategy, the entry and exit of positions created by these trades, waits for new candles, and so on. These logs are quintessential in validating the strategy behavior and debugging behavior or performance issues that are frequently encountered while developing a strategy.

In this recipe, you will fetch the backtesting logs for your strategy. The logs start coming up as soon as your submitted backtesting job reaches the 'STARTED' state (refer to the previous recipe for more...

EMA-Regular-Order strategy – fetching a backtesting report profit and loss table

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, along with the logs, the AlgoBulls backtesting engine also generates a P&L table in real time. This table holds information on every trade that's been punched by the strategy. It also contains details on the mappings between entry and exit orders, the trade P&L, and the cumulative P&L, sorted chronologically, with the latest order first. This table gives us an insight into the overall strategy's performance with the help of individual and cumulative P&L numbers. The entry-exit order mapping also helps validate the strategy behavior.

In this recipe, you will fetch the P&L table report for your strategy. This report is available as soon as the first trade is punched in by your strategy after you submit a backtesting...

EMA-Regular-Order strategy — fetching a backtesting report statistics table

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, along with the logs and P&L table, the AlgoBulls backtesting engine also generates a summary from the P&L table in real time. This summary is a table of statistics containing various statistical numbers such as Net P&L (absolute and percentage), Max Drawdown (absolute and percentage), count of total trades, winning trades, losing trades, long trades and short trades, maximum gain and minimum gain (or maximum loss), and average profit per winning and losing trade. This table gives an instant overview of the overall strategy's performance.

In this recipe, you will fetch the statistics table report for your strategy. This report is available as soon as the first trade is punched in by your strategy after you submit a backtesting job. The...

EMA-Regular-Order strategy – fetching a backtesting report order history

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, along with the logs, the P&L table and the statistics table of the AlgoBulls backtesting engine generate an order history log in real time. This log contains state transitions of every order, along with the timestamps and additional information (if any) for each order state. The order history log is crucial for understanding how long it has taken for a trade to go from an 'OPEN' state to 'COMPLETE' or 'CANCELLED'. For example, the MARKET orders would immediately go from 'OPEN' to 'COMPLETE' but the LIMIT orders may take a while, based on the market conditions, to go from 'OPEN' to 'COMPLETE' they may even get to 'CANCELLED'. All this information is available in...

MACD-Bracket-Order strategy – fetching the strategy

In this recipe, you will fetch the StrategyMACDBracketOrder strategy class from your account on the AlgoBulls platform, which you uploaded while going through the last recipe in the previous chapter. This recipe starts with setting up a connection to the AlgoBulls platform, querying all available strategies in your account, and fetching details of the required strategy class, StrategyMACDBracketOrder.

Make sure you have gone through the last six recipes of the previous chapter to get a complete picture of the strategy class we will be using; that is, StrategyMACDBracketOrder.

How to do it…

We execute the following steps for this recipe:

  1. Import the necessary modules:
>>> from pyalgotrading.algobulls import AlgoBullsConnection
  1. Create a new AlgoBulls connection object:
>>> algobulls_connection = AlgoBullsConnection()
  1. Fetch the authorization URL:
>>> algobulls_connection.get_authorization_url...

MACD-Bracket-Order strategy – backtesting the strategy

In this recipe, you will perform backtesting on the MACD-Bracket-Order strategy. You must have fetched this strategy from your account on the AlgoBulls platform in the previous recipe of this chapter. You will leverage the backtesting functionality facilitated by pyalgotrading for this recipe, which, in turn, submits a backtesting job on the AlgoBulls platform.

Once submitted, backtesting will be run by the AlgoBulls backtesting engine. You can query the status at any time to find out the state of the backtesting job. The job goes through the following states, in the given order:

  • STARTING (Intermediate state)
  • STARTED (Stable state)
  • STOPPING (Intermediate state)
  • STOPPED (Stable state)

On submitting a job, it starts with an intermediate state, 'STARTING'. In this state, the AlgoBulls backtesting engine fetches the strategy and get the execution environment ready, which may take a couple of minutes. Once done,...

MACD-Bracket-Order strategy – fetching backtesting logs in real time

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, every event that occurs and the decisions that have been made by the AlgoBulls backtesting engine are recorded with exact timestamps in the form of textual logs. Examples of recorded activities include the given strategy config, every new candle generated at regular intervals, trades punched in by your strategy, the entry and exit of positions created by these trades, waits for new candles, and so on. These logs are quintessential for validating the strategy's behavior and debugging behavioral or performance issues that are frequently encountered while developing a strategy.

In this recipe, you will fetch backtesting logs for your strategy. The logs start coming up as soon as your submitted backtesting job reaches the 'STARTED' state. The AlgoBulls platform...

MACD-Bracket-Order strategy – fetching a backtesting report profit and loss table

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, along with the logs, the AlgoBulls backtesting engine also generates a P&L table in real time. This table holds information on every trade that's been punched in by the strategy. It also contains details on the mappings between entry and exit orders, the trade P&L, and the cumulative P&L, sorted chronologically, with the latest order first.

This table gives us insights into the strategy's overall performance with the help of individual and cumulative P&L numbers. The entry-exit order mapping also helps validate the strategy's behavior.

In this recipe, you will fetch the P&L table report for your strategy. This report is available as soon as the first trade is punched in by your strategy after you submit a backtesting...

MACD-Bracket-Order strategy – fetching a backtesting report statistics table

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, along with the logs and P&L table, the AlgoBulls backtesting engine also generates a summary from the P&L table in real time. This summary is a table of statistics containing various statistical numbers such as Net P&L (absolute and percentage), Max Drawdown (absolute and percentage), count of total trades, winning trades, losing trades, long trades and short trades, maximum gain and minimum gain (or maximum loss), and the average profit per winning and losing trade. This table gives us an instant overview of the strategy's overall performance.

In this recipe, you will fetch the statistics table report for your strategy. This report is available as soon as the first trade is punched in by your strategy after you submit a backtesting...

MACD-Bracket-Order strategy – fetching a backtesting report order history

After submitting a backtesting job on the AlgoBulls platform, the AlgoBulls backtesting engine starts executing the strategy. During its execution, along with the logs, the P&L table, and the statistics table, the AlgoBulls backtesting engine also generates an order history log in real time. This log contains the state transitions of every order, along with the timestamps and additional information (if any) for each order state. The order history log is crucial in understanding how long it has taken for a trade to go from an 'OPEN' to 'COMPLETE' or 'CANCELLED' state. For example, MARKET orders would immediately go from 'OPEN' to 'COMPLETE' but LIMIT orders may take a while, based on the market conditions, to go from 'OPEN' to 'COMPLETE' they may even get 'CANCELLED'. All this information is available in the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020Publisher: PacktISBN-13: 9781838989354
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade