Mariner Backtesting - init - How to Use Init

Passing Parameters Using Init
When Passing Parameters:
  • Make sure to use double quotes around the strings.
  • __init__() must contain **params as a parameter.
  • A larger dictionary can be passed as a parameter, for example: {"side": "long", "time": "9:55", "quantity": 100}
  • More information on init.
Parameters passed in:

{"side": "long"} {"side": "short"}

Code Used in Video:
 # Copyright Cloudquant, LLC. All right reserved.
from cloudquant.interfaces import Strategy


class PassingParameters(Strategy):
    # note that this doesn't start with "self" because it's a @staticmethod
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'AAL'

    # used to pass external parameters for each instance (same for values for every instance)
    def __init__(self, **params):  # , **params - if passing in parameters is desired
        if params is not None:
            self.side = params['side']

    # called at the beginning of each instance
    def on_start(self, md, order, service, account):
        if self.side == 'long':
            order.algo_buy(symbol=self.symbol, algorithm='market', intent='init', order_quantity=100)
            print '100 shares have been purchased.'

        elif self.side == 'short':
            order.algo_sell(symbol=self.symbol, algorithm='market', intent='init', order_quantity=100)
            print '100 shares have been sold.'

        service.terminate()

Console when {"side": "long"} is passed

100 shares have been purchased.

Console when {"side": "short"} is passed

100 shares have been sold.