Designing, Building and Deploying a Fully Automated Algorithmic Trading System

Designing, Building and Deploying a Fully Automated Algorithmic Trading System

As I developed several inter-day trading/ portfolio management algorithms, I also embarked on a journey in parallel to develop a fully automated execution framework that could satisfy my requirements.

Previously orders were executed manually after signals are generated automatically.

Requirements

  • A relatively slow trading system triggered by an hourly task scheduler during trading hours. I’m using Linux cron jobs for this.
  • A non event-driven framework. I’m relying on ib_sync framework for my algorithms executed in interactive brokers. The alternative is an event-driven framework (e.g. official IB framework based on asyncio) based on my experience is hard to debug and would be an overkill for my low to medium frequency trading/ portfolio management system.
  • Automated trading execution and portfolio adjustments in relation to risks, signals and leverage cap.
  • Data ingestion from multiple data sources/ APIs for signal processing, trade executions and logs.
  • Inclusion of safeguards and circuit breakers in place before firing any trades.
  • Notification through email for errors and daily updates @ start, middle and end of trading day. -Able to integrate the above steps through Python.

Steps taken

  1. Without diving into the details of the code, here is the the general flow of my framework. The ‘main’ programs and execution flowcharts are appended at the end of the article.
  2. Connect to my brokerage account as a new session. Cancel any open orders. I adopt this option instead of ‘modifying orders’ as it’s much easier. If I were to modify orders, I would have to keep track of the number of filled orders and unfilled orders. As my algorithm is triggered hourly, any unfilled orders in that hourly slot are deemed to deviate significantly from the current price and the chance of being executed at the limit price is pretty low. That being said, in my next iteration, I would prefer to invest time in writing code to ‘modify orders’ instead of a simplistic ‘cancel orders’.
  3. Generate forecast i.e. Required leverage and weightage from different tickers. I currently write them as local files instead of committing them to a database or storing them as variables as they are more robust during live trading. I do not have to worry about concurrency issues (else codes have to consider mutex and locks), database downtime.
  4. Obtain net liquidation value of account.
  5. Read in ticker weights and leverage from file written out in step 3.
  6. Create order files for different tickers including actions such as ‘Buy’, ‘Sell’ or ‘Do nothing’, Quantity and Limit price. At the moment, I’m using Adaptive Limit 7. Order from Interactive Brokers — which based on my understanding is a discretionary order with limit price as an upper bound. At the moment, I’m unwilling to invest more time on this to gain any further execution alpha as it could be a non-trivial task to dig into the bid-ask order books.
  7. Check if there are any errors from step 1 to 4 through an error flag. If there is any error, orders will not be sent. If everything cleared, the program will send the orders to the exchange.
  8. Trades, if any are recorded into a csv file.
  9. Save current snapshot which includes current positions, trades, net liquidation value and leverage utilized and margins.
  10. If errors are encountered, an alert will be sent to my gmail.
  11. Daily updates/ snapshots are sent to my gmail at the start, middle and end of session.
  12. If the program is triggered at 5am SGT (after US trading hours), the snapshots and trades information are committed to my database.
  13. Disconnect from current session.

Technology stack

If you are keen to know my technology stack, I’m currently using the following,

  • Linux Mint
  • Mint Box, 8GB RAM (but any laptop or virtual private server would suffice)
  • Interactive brokers brokerage account (but the framework could be adapted to any brokerages with APIs)
  • Interactive brokers gateway (IB Trade Worker Station is an alternative but I do not require any graphical user interface for my purpose)
  • ib_insync framework

Word of Caution

  • This framework is not ideal for tick by tick or even bar by bar (seconds or minutes) trading algorithm since it’s not predicated on speed. An event-driven framework would be a better alternative if speed is required.
  • I’m relying on brokerage server side to keep track of the current positions, margins, leverage and orders. In the next stage of development beyond this Minimum Viable Product, I’m keen to develop my own accounting system.

Appendix

/post/img/prog1.png /post/img/prog2.png /post/img/flow1.jpeg

Related

comments powered by Disqus