Mariner Backtesting - Strategy.on_strategy_finish()

Strategy finishing event

on_strategy_finish() is called on after the last instantiation of the class is completed either from the end of the testing time or service.terminate().

Interface:
 @classmethod
def on_strategy_finish(cls,
                       md,
                       service,
                       account):
    pass
Parameters:
Name Type Default Information
cls object required cls is how to read or assign class variables in a class method (compared to reading/assigning class variable in an instance method self.__class__.CLASS_VARIABLE).
md Market required market data object
service Service required service object
account Account required account object
Remarks:
  • No return value.
  • Has access to system objects: md, service and account.
  • Does not have access to order. e.g. you can't cancel orders from on_strategy_finish() (use on_finish() in this case).
  • Class method - @classmethod.
  • Use cls to access/assign class variables (compared to reading/assigning class variable in an instance method self.__class__.CLASS_VARIABLE).
  • on_strategy_finish() is a good place to do end of job activities. e.g. writing data to files or printing the account object
Working Example:
  • 9/7/16 Test date
  • Test time 16:01 - 16:02
import math
from cloudquant.interfaces import Strategy


class OnStrategyFinishExample(Strategy):
    rvol = {}

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return md[symbol].stat.avol > 1000000

    # pass external params to set rvol_check
    def __init__(self, **params):
        self.rvol_check = 2
        if 'rvol_check' in  params:
            self.rvol_check = float(params['rvol_check'])

    def on_finish(self, md, order, service, account):
        # add all symbols that qualified, add to class variable
        rvol = md.L1.rvol if not math.isnan(md.L1.rvol) else 0
        if rvol > 2:
            self.__class__.rvol[self.symbol] = rvol

    @classmethod
    def on_strategy_finish(cls, md, service, account):      
        # print qualifying symbols sorted by rvol
        print '\n\nqualifying symbols sorted by rvol\n\nSymbol\trvol'
        for symbol, rvol in sorted(cls.rvol.items(), key=lambda kv: kv[1], reverse=True):
            print '%s\t%s' % (symbol, rvol)

Console

qualifying symbols sorted by rvol

Symbol  rvol
RTRX    12.469493866
SFM 7.87747573853
HDS 7.87296915054
NAVB    7.40810823441
CTLT    6.91785335541
GEVO    5.78485202789
CPE 4.99618244171
MGT 4.7103767395
WATT    4.57672691345
WDC 4.16538858414
APA 4.16156721115
SMFG    3.67303681374
TGNA    3.6264834404
ENB 3.62280511856
SE  3.20305538177
TNXP    3.09393072128
WFM 3.04853224754
CTRP    3.03863024712
MJN 3.02748680115
CMG 2.88571834564
IMNP    2.83028125763
CPHD    2.73294305801
OLN 2.56712913513
NAV 2.48402833939
BOX 2.46957349777
BBT 2.44723916054
PCAR    2.35924911499
ILF 2.32579946518
STX 2.30630588531
SMH 2.24878120422
XON 2.21858406067
TSM 2.21845245361
DVAX    2.21349358559
NGD 2.20602798462
VMW 2.19120383263
JNUG    2.14205360413
HPE 2.13844227791
GIS 2.11158084869
IGT 2.10876774788
CCI 2.07488059998
CMI 2.07360935211
GRMN    2.00680589676