This tutorial is a part of the series “Building your automated strategy with IBKR: From Back Testing to Paper Trading to Live Execution.” See below for the previous installments:
- Part I provides instructions on how to setup the development and trading environment.
- Part II demonstrates how to use IBKR historical data within Backtrader.
- Part III showcases how to use various types of real-time data to run trading strategies.
- Part IV walks you through how to transition to paper trading using live data.
Paper trading can help to validate the efficacy of a strategy, so it is prudent to continue paper trading until a stable performance has been achieved.
Sometimes a successful paper trading strategy may not translate to the same performance in real trading. This can be for a myriad of internal or external factors (market impact, price slippage, market microstructure etc.) or simple programming issues.
To help isolate and identify these issues it is important to have a robust monitoring and debugging facility built into the trading process.
The simplest and most effective method is to have comprehensive and accurate logging. The logger should record trades and actions such as opening and closing prices and trigger conditions and parameters pertinent to the strategy.
The Backtrader and TWS API also has debugging built in and can be accessed by setting the appropriate configuration parameters.
We will explore logging by enhancing the strategy we developed in Part 4 of this series.
The table below shows the configuration for both live and paper trading.
Transition from paper to live trading is simply a matter of changing the port number from 7497 to 7496. This will result in live orders going out to the market so be careful when modifying the configuration.
An Example of a Simple Buy/Sell Strategy With Logging
Not that in this strategy we generate Buy and Sell market orders related to a simple moving average calculation. This strategy has been adapted from (https://www.backtrader.com/docu/quickstart/quickstart) with minor updates.
Setting Up A Log File
There is a example custom logger supplied in the atreyu_backtrader_api module, and can be referenced in the code as follows:
The logger has the following properties:
- It can write a named log file into a named directory thereby keeping all the logs together.
- It has log rotation, e.g. it does not overwrite logs on repeated restarts but backs each log keeping the specified logfile as the latest log.
- Each log line can be echoed to the terminal as well as written to the logfile
The logger can be setup as follows:
Configuring each sub-logger for the module and source file that is of interest, can control the logfile output.
Enable Logging in Backtrader
To enable logging in the Backtrader framework the _debug = True variable can be passed when setting up the IBStore and IBData classes.
Onward To Live Trading
You now have implemented logging and have and good understanding of how to debug your strategy in case of programming issues.
It is suggested that live trading starts small, and that as confidence is gained trading sizes/frequency can be increased within your prescribed risk appetite. At each stage it is recommended that you review the logs for anomalies that you may not notice from looking at the TWS screens.
Switching the strategy to live trading is as simple as logging in with TWS for trading and changing the port designator in the code below:
This concludes our series “Building your automated strategy with IBKR: From Back Testing to Paper Trading to Live Execution.”
Visit Atreyu Trading Services https://www.atreyutrading.com/ for additional insight on Backtrader.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from Atreyu Trading Services and is being posted with its permission. The views expressed in this material are solely those of the author and/or Atreyu Trading Services and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Disclosure: API Examples Discussed
Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.
Disclosure: Order Types / TWS
The order types available through Interactive Brokers LLC’s Trader Workstation are designed to help you limit your loss and/or lock in a profit. Market conditions and other factors may affect execution. In general, orders guarantee a fill or guarantee a price, but not both. In extreme market conditions, an order may either be executed at a different price than anticipated or may not be filled in the marketplace.