Event-driven backtest systems

An event-driven backtester uses almost all the components of the trading system. Most of the time, this type of backtester encompass all the trading system components (such as the order manager system, the position manager, and the risk manager). Since more components are involved, the backtester is more realistic.

The event-driven backtester is close to the trading system we implemented in Chapter 7Building a Trading System in Python. We left the code of the TradingSimulation.py file empty. In this section, we will see how to code that missing code.

We will have a loop calling all the components one by one. The components will read the input one after the other and will then generate events if needed. All these events will be inserted into a queue (we'll use the Python deque object). The events we encountered when we coded the trading system were the following:

  • Tick events – When we read a new line of market data
  • Book events – When the top of the book is modified
  • Signal events – When it is possible to go long or short
  • Order events – When orders are sent to the market
  • Market response events – When the market response comes to the trading system

The pseudo code for an event-driven backtesting system is as follows:

from chapter7.LiquidityProvider import LiquidityProvider
from chapter7.TradingStrategy import TradingStrategy
from chapter7.MarketSimulator import MarketSimulator
from chapter7.OrderManager import OrderManager
from chapter7.OrderBook import OrderBook
from collections import deque

def main():
lp_2_gateway = deque()
ob_2_ts = deque()
ts_2_om = deque()
ms_2_om = deque()
om_2_ts = deque()
gw_2_om = deque()
om_2_gw = deque()

lp = LiquidityProvider(lp_2_gateway)
ob = OrderBook(lp_2_gateway, ob_2_ts)
ts = TradingStrategy(ob_2_ts, ts_2_om, om_2_ts)
ms = MarketSimulator(om_2_gw, gw_2_om)
om = OrderManager(ts_2_om, om_2_ts, om_2_gw, gw_2_om)

lp.read_tick_data_from_data_source()
while len(lp_2_gateway)>0:
ob.handle_order_from_gateway()
ts.handle_input_from_bb()
om.handle_input_from_ts()
ms.handle_order_from_gw()
om.handle_input_from_market()
ts.handle_response_from_om()
lp.read_tick_data_from_data_source()

if __name__ == '__main__':
main()

We can see that all the components of the trading system are called. If we had a service checking the position, this service would be called.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.89