Mariner Backtesting - account.filter()

account. Overview:

filter() is a method on account that returns attributes of the account object filtered by the user_key. user_key is set when the order is submitted.

Interface:
 filter(user_key)

Sample - Calling method

filtered_acc = account.filter( user_key )
print filtered_acc.open_symbols
Parameters
Name Type Default Information
user_key string required user key from order
Returns:
Type Notes
object An AccountView object that filters all data by the given user_key
Remarks:
  • Filtering on user_key is helpful when a strategy is has multiple moving parts and one symbols orders/executions/position might be related to multiple strategies (e.g. a hedge, basket, pairs trade).
  • Use sparingly. It is a function. The more orders/trades in the account may take longer and longer.
Working Example:

Backtest for AAPL on 7/29/16 from 9:31-9:32. Extra Symbols: [SPY, TQQQ, QQQ]

from cloudquant.interfaces import Strategy


class AccountObjectFilter(Strategy):
    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'TQQQ'

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

        # initiate executions
        order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)
        order.algo_buy(self.symbol, "market", intent="increase", order_quantity=100, allow_multiple_pending=True,
                       user_key='demo')

        # another symbol
        order.algo_buy('SPY', "market", intent="init", order_quantity=100)
        order.algo_buy('SPY', "market", intent="increase", order_quantity=100, allow_multiple_pending=True)

        # test pending with no execution
        order.algo_buy('TQQQ', "limit", intent="init", price=0.01, order_quantity=100)

        # short trade
        order.algo_sell('QQQ', "market", intent="init", order_quantity=100)
        order.algo_sell('QQQ', "limit", intent="increase", price=10000, order_quantity=100, allow_multiple_pending=True)

        def on_minute_bar(self, event, md, order, service, account, bar):
            if event.timestamp > service.time(9,31):
                print '\n\n.............................\non_minute_bar\n\t%s\n' % service.time_to_string(service.system_time,
                                                                                                      '%H:%M:%S.%f')
                print account

                print '\n\naccount[self.symbol].executions'
                print account[self.symbol].executions

                print '\n\naccount.filter("demo")[self.symbol].executions'
                print account.filter("demo")[self.symbol].executions

                print '\n\naccount.open_capital_long'
                print account.open_capital_long

                print '\n\naccount.filter("demo").open_capital_long'
                print account.filter("demo").open_capital_long

                print '\n\nBy Symbol'
                for symbol in account:
                    print symbol
                    for execution in account[symbol].executions:
                        print execution, '\n'
                service.terminate()

Console

.............................
on_minute_bar
    09:31:05.005000

account_id:                                 0
entry_pl:                                0.00
mtm_pl:                                  0.00
buying_power:                            None
open_capital_long:                   70822.00
open_capital_short:                 -11894.00
pending_capital_long:                83597.00
pending_capital_short:              -23788.00
max_capital_used:                    82716.00
max_capital_used_timestamp:  1481553005000001
open_symbols:                ['SPY', 'QQQ', 'TQQQ']
pending_symbols:             ['QQQ', 'TQQQ']
SPY:
    Execution count     :              2
    realized_pl         :
        entry_pl        :           0.00
        mtm_pl          :           0.00
        matched_trades  :              0
    unrealized_pl       :
        entry_pl        :          32.00
        mtm_pl          :          32.00
    position            :
        shares          :            200
        entry_price     :         226.36
        mtm_price       :         226.36
        capital_long    :       45272.00
        capital_short   :           0.00
    pending             :
        count_long      :              0
        shares_long     :              0
        capital_long    :           0.00
        count_short     :              0
        shares_short    :              0
        capital_short   :           0.00
QQQ:
    Execution count     :              1
    realized_pl         :
        entry_pl        :           0.00
        mtm_pl          :           0.00
        matched_trades  :              0
    unrealized_pl       :
        entry_pl        :          -6.00
        mtm_pl          :          -6.00
    position            :
        shares          :           -100
        entry_price     :         118.94
        mtm_price       :         118.94
        capital_long    :           0.00
        capital_short   :      -11894.00
    pending             :
        count_long      :              0
        shares_long     :              0
        capital_long    :           0.00
        count_short     :              1
        shares_short    :           -100
        capital_short   :      -11900.00
TQQQ:
    Execution count     :              2
    realized_pl         :
        entry_pl        :           0.00
        mtm_pl          :           0.00
        matched_trades  :              0
    unrealized_pl       :
        entry_pl        :          18.00
        mtm_pl          :          18.00
    position            :
        shares          :            200
        entry_price     :         127.75
        mtm_price       :         127.75
        capital_long    :       25550.00
        capital_short   :           0.00
    pending             :
        count_long      :              1
        shares_long     :            100
        capital_long    :       12784.00
        count_short     :              0
        shares_short    :              0
        capital_short   :           0.00


account[self.symbol].executions
[Execution(symbol='TQQQ', side=1, shares=100, price=127.75, time=1481553005000001, collect=None, order_id='0000000000000001', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='29ec22cb-893b-50f2-85d4-6567448f9b03'), Execution(symbol='TQQQ', side=1, shares=100, price=127.75, time=1481553005000001, collect=None, order_id='0000000000000002', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='demo')]


account.filter("demo")[self.symbol].executions
[Execution(symbol='TQQQ', side=1, shares=100, price=127.75, time=1481553005000001, collect=None, order_id='0000000000000002', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='demo')]


account.open_capital_long
70822.0001221


account.filter("demo").open_capital_long
12775.0


By Symbol
SPY
Execution(symbol='SPY', side=1, shares=100, price=226.36000061035156, time=1481553005000001, collect=None, order_id='0000000000000003', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='29ec22cb-893b-50f2-85d4-6567448f9b03')

Execution(symbol='SPY', side=1, shares=100, price=226.36000061035156, time=1481553005000001, collect=None, order_id='0000000000000004', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='29ec22cb-893b-50f2-85d4-6567448f9b03')

QQQ
Execution(symbol='QQQ', side=-1, shares=-100, price=118.94000244140625, time=1481553005000001, collect=None, order_id='0000000000000006', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='29ec22cb-893b-50f2-85d4-6567448f9b03')

TQQQ
Execution(symbol='TQQQ', side=1, shares=100, price=127.75, time=1481553005000001, collect=None, order_id='0000000000000001', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='29ec22cb-893b-50f2-85d4-6567448f9b03')

Execution(symbol='TQQQ', side=1, shares=100, price=127.75, time=1481553005000001, collect=None, order_id='0000000000000002', clordid=None, instruction_id='29ec22cb-893b-50f2-85d4-6567448f9b03', script_id=u'ac9e7c5e-3f35-4f87-a914-fe948412b6fb', user_key='demo')