Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Python Algorithmic Trading Cookbook

You're reading from  Python Algorithmic Trading Cookbook

Product type Book
Published in Aug 2020
Publisher Packt
ISBN-13 9781838989354
Pages 542 pages
Edition 1st Edition
Languages
Author (1):
Pushpak Dagade Pushpak Dagade
Profile icon Pushpak Dagade

Table of Contents (16) Chapters

Preface Handling and Manipulating Date, Time, and Time Series Data Stock Markets - Primer on Trading Fetching Financial Data Computing Candlesticks and Historical Data Computing and Plotting Technical Indicators Placing Regular Orders on the Exchange Placing Bracket and Cover Orders on the Exchange Algorithmic Trading Strategies - Coding Step by Step Algorithmic Trading - Backtesting Algorithmic Trading - Paper Trading Algorithmic Trading - Real Trading Other Books You May Enjoy Appendix I
Appendix II
Appendix III
Algorithmic Trading - Paper Trading

After building algorithmic trading strategies in Chapter 8, Algorithmic Trading Strategies – Coding Step by Step, and successfully backtesting them with satisfactory results in the previous chapter, the next step is to paper trade the strategies in live markets.

Paper trading is the method of executing a trading strategy in the live market hours by simply recording trades coming from the strategy execution in real time. The trades are not executed with real money via a broker. Earlier, this recording of trades was done on paper, hence the name paper trading. These virtual trades can be used for analyzing the risk and return metrics. Typical paper trading metrics include profit and loss (P&L), maximum drawdown, the count of total trades, winning trades, losing trades, long trades and short trades, average profit per winning and losing...

Technical requirements

EMA-Regular-Order strategy – fetching the strategy

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

Make sure you have gone through the first six recipes of Chapter 8, Algorithmic Trading Strategies – Coding Step by Step, to get a complete picture of the strategy class used, 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...

EMA-Regular-Order strategy – paper trading the strategy

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

Once submitted, paper trading will be run by the AlgoBulls paper trading engine. You can query the status any time to know the state of the paper trading job. The job goes through the following states, in the following 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 paper trading engine will fetch the strategy and get the execution environment ready,...

EMA-Regular-Order strategy – fetching paper trading logs in real time

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, every event that occurs and decisions taken by the AlgoBulls paper trading engine are recorded with exact timestamps in the form of textual logs. Some 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 the positions created by these trades, waits for new candles, and so on. These logs are quintessential in validating the strategy behavior and debugging behavioral or performance issues that are frequently encountered while developing a strategy.

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

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

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, along with the logs, the AlgoBulls paper trading engine also generates a P&L table in real time. This table holds information on every trade punched by the strategy. It also has details on the mapping between entry and exit orders and the trade P&L and cumulative P&L, sorted chronologically, with the latest order first. This table gives an insight into the overall strategy 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 by your strategy after you submit a paper trading job. The AlgoBulls platform...

EMA-Regular-Order strategy – fetching a paper trading report – statistics table

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, along with the logs and the P&L table, the AlgoBulls paper trading 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), the 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 an instant overview of the overall strategy 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 by your strategy after you submit a paper...

EMA-Regular-Order strategy – fetching a paper trading report – order history

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, along with the logs, the P&L table, and statistics table, the AlgoBulls paper trading engine also generates 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 in understanding how long it has taken for a trade to go from 'OPEN' to 'COMPLETE' or to the 'CANCELLED' state. For example, the MARKET orders would immediately go from an 'OPEN' to 'COMPLETE' state but the LIMIT orders may take a while, based on the market conditions, to go from an 'OPEN' to 'COMPLETE' state, or they may even get to 'CANCELLED'. All this information...

MACD-Bracket-Order strategy – fetching the strategy

In this recipe, you will fetch the strategy class, StrategyMACDBracketOrder, from your account on the AlgoBulls platform, which you must have uploaded while going through the last recipe of Chapter 8, Algorithmic Trading Strategies – Coding Step by Step. 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 Chapter 8, Algorithmic Trading Strategies – Coding Step by Step, to get a complete picture of the strategy class used, 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...

MACD-Bracket-Order strategy – paper trading the strategy

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

Once submitted, paper trading will be run by the AlgoBulls paper trading engine. You can query the status any time to know the state of the paper trading job. The job goes through the following states, in the following 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 paper trading engine will fetch the strategy and get the...

MACD-Bracket-Order strategy – fetching paper trading logs in real time

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, every event that occurs and decisions taken by the AlgoBulls paper trading 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 behavioral or performance issues that are frequently encountered while developing a strategy.

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

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

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, along with the logs, the AlgoBulls paper trading engine also generates a P&L table in real time. This table holds information on every trade punched by the strategy. It also has details on the mapping between entry and exit orders and the trade P&L and cumulative P&L, sorted chronologically, with the latest order first. This table gives an insight into the overall strategy 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 by your strategy after you submit a paper trading job. The AlgoBulls platform...

MACD-Bracket-Order strategy – fetching a paper trading report – statistics table

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, along with the logs and P&L table, the AlgoBulls paper trading 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), the 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 an instant overview of the overall strategy 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 by your strategy after you submit a paper trading...

MACD-Bracket-Order strategy – fetching a paper trading report – order history

After submitting a paper trading job on the AlgoBulls platform, the AlgoBulls paper trading engine starts executing the strategy. During the execution, along with the logs, P&L table, and statistics table, the AlgoBulls paper trading engine also generates 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 in understanding how long it has taken for a trade to go from 'OPEN' to 'COMPLETE' or to the 'CANCELLED' state. For example, the MARKET orders would immediately go from an 'OPEN' to 'COMPLETE' state but the LIMIT orders may take a while, based on the market conditions, to go from an 'OPEN' to 'COMPLETE' state, or they may even get to the 'CANCELLED' state. All...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020 Publisher: Packt ISBN-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.
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}