Building Backtest Engine From Scratch

I’ve been relying on tools available to public for backtesting models, however there’re limitations on such tools (stability, availability, performance and scope of markets) so that I’ve decided to build a back test engine for my own purpose.

The tools that I’ve explored includes the following:
– markets: ashare stocks; futures listed in SHFE/DCE/ZCE.
– gtja: momentum strategies for ashare stocks.
– goldminer: futures strategies.
– tradeblazer: futures strategies (this tool has issues on simulation trading, and customer support is terrible so I’ve moved away from it).
I’ve tuned a stock strategy under gtja’s platform; however it can’t backtest for HKSE and US stocks. And gtja is taking their platform offline soon.

This entry tracks the key design components of back test engine.

  • IEngine
    An engine is the market, which accepts orders, match orders and performs settlement processes. There’re three types of engines of my interest: backtest, simulation and live. My aim is to work on backtest and simulation for stock markets. Another entry explores more details related to engine.
  • IAccount & AccountService
    An account acts like a broker account. IAccount is POJO so that it can save/restore information via JSON, where informaciton includes trades, orders, cash, NAVs, and related portfolios.
    AccountService facilities operations to submit orders (to market), receives events on orders (e.g. the submited order is rejected, accepted, filled and etc), receives events on trades and performs EOD settlement activities.
    Details are explored here.
  • IStrategy
    An strategy has access to IAccount, and also listens to market for price ticks, and bar updates. Functions for IStrategy:
    1. OnTick/OnBarUpdate
    A strategy listens to TickEvent/BarEvent. When bar is ticked, the strategy will calculate indicators, and generate BUY/SELL signals, and leverage IAccount to send orders to interact with market.
    2. OnMarketOpen/Close
    A strategy listens to market status event, so that before market open, it can generate orders. A scenario is when market opens at 09:25 for pre-market auction, the strategy can send out orders based on market data as of T-1, in order to increase/decrease risks.
  • Putting Things Together
    – Define Engine
    – Define AccountService
    – Define Strategy
    – Register account/strategies to engine.
    – Run backtest
    – Generate report
  • Spring Shell Integration

IContext
For simulation, this components provides configuration details such as commission fees, slippage, initial account balance, and etc. During simulation, context tracks the simulated market time, so that context/engine/account will share a single set of timestamp.

  • Design On Strategy Model

Published by

wofong

三千娑婆世界,三千难忘遗憾;回头乃是岸,此岸在何方;堪忍不能忍,万般看不穿;何时放得下,始得自在心。 I'm a programmer, a quantitative analyst, a photography hobbyist, a traveler, a runner, and a nature lover.

One thought on “Building Backtest Engine From Scratch”

Leave a comment