Skip to content

Under The Hood

Trading System

This is the main system that runs your backtest. All the parameters for the trading system are supplied in class MyTradingParams()

Raw data (intraday tick data or end of day data) is read from the datasource for all instruments and sent to the trading system. At every such update of data, the trading system does the following things and then updates the state of that instrument in the system :

  • Checks whether it is time to update the features and execute. It updates features at a frequency determined by trading parameters.
  • If it is, it first loops over all instruments and updates the features for each instrument (specified by trading parameters). Each instrument stores its features, which can be later extracted out.
  • Then, the trading system computes all the market features (specified by trading parameters). The system also stores such market features at every update features interval. An example of market feature is 'prediction' which the user needs to provide - This feaure specifies our prediction probability that the specified instrument is a buy
  • Then the trading system looks at the prediction value and passes it to an execution system. This converts uses the prediction value (and current capital, risk limits etc) to decide what positions should he trading system take for various instruments.
  • Trade executions generated by Execution System are then passed into the orderPlacer, which tries to place these orders into the market.

Trading System also keeps checking for confirmation of orders which are placed. It updates instrument positions for trades that are confirmed.

For trading system to work, following need to be specified:

DataSource

Read Above

Benchmark

The market instrument to benchmark your strategy's perfromancy. Strategies that perform better than the benchmark are considered successful. Make sure that you specify the benchmark instrumentID in list of instruments to get data for.

Starting Capital

The initial amount of money you're putting into your trading system. This is set to 1 million notional by default.

Frequency Of Feature Updates

Frequency of updates to features. Any updates within this frequncy to instruments do not trigger feature updates. Consequently any trading decisions that need to take place happen with the same frequency This is important when you are working with frequently updating data - for example intraday tick data

PriceFeatureKey

You may have multiple measures of price for an instrument at the same time, for example open price, close price etc. Specify which price to use for pnl calculations. By default, this is set to 'close' for daily calculations.

Lookback Size

How far back do you want the historical data for your calculations. The historical market features and instrument features are only stored upto this amount. The more data you request, the slower your system will be.

Instrument Features, Market Features and Custom Features

See Getting Started

Prediction Function

Read Above

Execution System

Takes a prediction value and converts it into possible trades for each instrument. It takes into account current positions, risk limits, current capital and value of the prediction.

All execution systems take the following arguments:

  • 'longLimit': Max long position. No buys if instrument position == longLimit, can be specified as dollar or lot size
  • 'shortLimit': Max short position. No sells if instrument position == shortLimit, can be specified as dollar or lot size
  • 'capitalUsageLimit': Minimum capital threshold, specified as % of initial capital. No trades if currentCapital/initialCapital < capitalUsageLimit
  • 'lotSize': Size to trade per trade, can be specified as dollar or lot size
  • 'limitType': 'D' for dollar(monetary) limit, 'L' for lot size limit
  • 'price': Feature to use as price

Right now we support following executions:

System Description Parameters Snippet
SimpleExecutionSystem Simplest type, takes prediction in the form of probability that price will go up. Instruments with probability of price increasing above enter_threshold are bought and below (1-enter_threshold) are sold. Instrument positions with probability predictions values between (1-exit_threshold) and exit_threshold are closed 'enter_threshold', 'exit_threshold', 'longLimit', 'shortLimit', 'capitalUsageLimit', 'lotSize', 'limitType', 'price'
SimpleExecutionSystemWithFairValue Takes prediction in the form of FairValue of price. Instruments with CurrentPrice - FairValue > enter_threshold_deviation are sold and FairValue - CurrentPrice > enter_threshold_deviation are bought. Instrument positions with abs(CurrentPrice - FairValue) < exit_threshold_deviation are closed 'enter_threshold_deviation', 'exit_threshold_deviation', 'longLimit', 'shortLimit', 'capitalUsageLimit', 'lotSize', 'limitType', 'price'
PairExecutionSystem , PairExecutionSystemWithFairValue Behaves the same as above two, except predictions are made on ratio of prices between two instruments(pair) 'pair', 'pairRatio','enter_threshold_deviation', 'exit_threshold_deviation', 'longLimit', 'shortLimit', 'capitalUsageLimit', 'lotSize', 'limitType', 'price'

OrderPlacer

Execution System decided what trades you want to do, order placer actually makes those trades (place an order), and also reads confirmations of orders being placed. For Backtesting, you can just use the BacktestingOrderPlacer, which places the order and automatically trades at 'PriceFeatureKey' price at the same time.