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 - Real Trading

Now that we've built various algorithmic trading strategies and successfully backtested them with satisfactory results and paper traded them in live markets, it is finally time for real trading.

Real trading is where we execute a trading strategy in the live market hours with real money. If your strategy has performed well in backtesting and paper trading, you can expect similar results with real money. Please note that your strategy may not perform as expected in the real market, despite giving good backtesting and paper trading results. Profitable backtesting and paper trading results are prerequisites for a profitable real trading experience but are not sufficient to guarantee a profit for every session.

For real trading, a strategy configuration is required. It consists of multiple parameters, some of which are as follows:

  • Start and...

Technical requirements

You will need the following to successfully execute the recipes in this chapter:

  • Python 3.7+
  • Python package:
  • pyalgotrading ( $ pip install pyalgotrading )

Ensure you have added and bound your broking details on https://algobulls.com. Refer to Appendix II for more details. You can use any broker supported by the AlgoBulls platform for this chapter.

The latest Jupyter notebook for this chapter can be found on GitHub at https://github.com/PacktPublishing/Python-Algorithmic-Trading-Cookbook/tree/master/Chapter11.

EMA–Regular–Order strategy fetching the strategy

In this recipe, you will fetch the StrategyEMARegularOrder strategy class from your account on the AlgoBulls platform. 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; that is, 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 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:
>>> algobulls_connection = AlgoBullsConnection()
  1. Fetch the authorization URL:
>>> algobulls_connection.get_authorization_url()

We get...

EMA–Regular–Order strategy real trading the strategy

In this recipe, you will perform real trading on the EMA-Regular-Order 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 real trading functionality facilitated by pyalgotrading for this recipe, which, in turn, submits a real trading job on the AlgoBulls platform.

Once submitted, real trading will be run by the AlgoBulls real trading engine. You can query its status any time to find out about the state of the real trading 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 real trading engine fetches the strategy and gets the execution environment ready, which may take a couple of minutes. Once...

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

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

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

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

After submitting a real trading job on the AlgoBulls Platform, the AlgoBulls real trading engine starts executing the strategy. During its execution, along with the logs, the AlgoBulls real trading engine also generates a P&L table in real time. This table holds information on every trade 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 insight 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 real...

EMA–Regular–Order strategy fetching a real trading report statistics table

After submitting a real trading job on the AlgoBulls platform, the AlgoBulls real trading engine starts executing the strategy. During its execution, along with the logs and P&L table, the AlgoBulls real 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), count of total trades, winning trades, losing trades, long trades and short trades, the 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 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...

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 must have uploaded while going through the last recipe in 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 about the required strategy class; that is, 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 we will be using; that is, StrategyMACDBracketOrder.

How to do it…

Execute the following steps to complete this recipe:

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

MACD–Bracket–Order strategy real trading the strategy

In this recipe, you will perform real trading on the MACD-Bracket-Order 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 real trading functionality facilitated by pyalgotrading for this recipe, which, in turn, submits a real trading job on the AlgoBulls platform.

Once submitted, real trading will be run by the AlgoBulls real trading engine. You can query the status anytime to find out the state of the real trading 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 real trading engine fetches the strategy and get the execution environment ready, which may take a couple of minutes...

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

After submitting a real trading job on the AlgoBulls platform, the AlgoBulls real trading engine starts executing the strategy. During its execution, every event that occurs and every decision that's been made by the AlgoBulls real trading engine is 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 and debugging behavior or performance issues that are frequently encountered while developing a strategy.

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

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

After submitting a real trading job on the AlgoBulls platform, the AlgoBulls real trading engine starts executing the strategy. During its execution, along with the logs, the AlgoBulls real trading engine also generates a P&L table in real time. This table holds information on every trade 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 insight 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 real...

MACD–Bracket–Order strategy fetching a real trading report statistics table

After submitting a real trading job on the AlgoBulls platform, the AlgoBulls real trading engine starts executing the strategy. During its execution, along with the logs and P&L table, the AlgoBulls real 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), count of total trades, winning trades, losing trades, long trades and short trades, the maximum gain and minimum gain (or maximum loss), and 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...

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