Mariner Backtesting - Anatomy of a CQ Elite Script

Event-Driven Backtesting (every tick)

Anatomy of a CloudQuant Elite Script High Level Strategy - Start/Finish

on_stategy_start: called once when the strategy starts (aka before anything else)

 @classmethod
def on_strategy_start(cls, md, service, account):
    pass

on_strategy_finish: called once when the strategy finishes (aka after everything else has stopped)

 @classmethod
def on_strategy_finish(cls, md, service, account):
    pass
Symbol Universe

is_symbol_qualified: this determines which symbols the script will run on if you do not set up specific symbols when you schedule the backtest. You can choose symbols dynamically, for example choosing those with a certain number of shares. This is called once when the strategy starts. Note that this doesn't start with "self" because it's a @classmethod

 @classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
    return True

backtesting_extra_symbols: used to load data for other symbols that are not in is_symbol_qualified, for example if you want to compare your symbol to an index. Only used in backtesting

 @classmethod
def backtesting_extra_symbols(cls, symbol, md, service, account):
    return False
Start/Finish Instance Related Methods

__init__: used to pass external parameters for each instance (same for values for every instance)

 def __init__(self): #, **params - if passing in parameters is desired
    pass

on_start: called at the beginning of each instance

 def on_start(self, md, order, service, account):
    pass

on_finish: if running with an instance per symbol, call when an instance is terminated

 def on_finish(self, md, order, service, account):
    pass

on_save: called by the system at the end of a trading day when a script is run as a "Single Symbol Script" (not using is_symbol_qualified) and hasn't been terminated

 def on_save(self, state_dict):
    pass

on_restore: called by the system after on_start when a script is run as a "Single Symbol Script" (not using is_symbol_qualified) and has been restored

 def on_restore(self, state_dict):
    pass
Timer Method

on_timer: called when a timer event is received

 def on_timer(self, event, md, order, service, account):
    pass
Market Data Related Methods

on_minute_bar: called every minute before the first on_trade of every new minute, or 5 seconds after a new minute starts

 def on_minute_bar(self, event, md, order, service, account, bar):
    pass

on_trade: called whenever one of the symbols the script is registered for gets traded on an exchange

 def on_trade(self, event, md, order, service, account):
    pass

on_nbbo_price: called when national best bid offer (nbbo) prices change (not size)

 def on_nbbo_price( self, event, md, order, service, account ):
    pass

on_arca_imbalance: called when arca imbalance message is received

 def on_arca_imbalance(self, event, md, order, service, account):
    pass

on_nasdaq_imbalance: called when nasdaq imbalance message is received

 def on_nasdaq_imbalance(self, event, md, order, service, account):
    pass

on_nyse_imbalance: called when nyse/amex/nsye mkt/openbook message is received

 def on_nyse_imbalance(self, event, md, order, service, account):
    pass
Order Related Methods

on_ack: called when order considered pending by the system

 def on_ack(self, event, md, order, service, account):
    pass

on_reject: called when an order is rejected (locally or other parts of the order processes e.g. the market)

 def on_reject(self, event, md, order, service, account):
    pass

on_fill: called when the position changes in this account and symbol (whether manually or automated)

 def on_fill(self, event, md, order, service, account):
    pass

on_cancel: called when the market has confirmed an order has been canceled

 def on_cancel(self, event, md, order, service, account):
    pass

on_cancel_reject: called when the market has rejected a cancel request

 def on_cancel_reject(self, event, md, order, service, account):
    pass
Market/Trading Disruption Methods

on_trading_halt: called when the market halts trading in a registered symbol

 def on_trading_halt(self, event, md, order, service, account):
    pass

on_trading_resume: called when trading resumes after being halted in a regisered symbol

 def on_trading_resume(self, event, md, order, service, account):
    pass