Mariner Backtesting - Account Positions

account[symbol].position

account[symbol].position Overview:

is a special way of filtering the account object by symbol for attributes about the account's positions .

Attributes:

Sample - Access data attribute

 account[self.symbol].position.shares
Name Type Information
capital_long float Capital relative to long positions.
capital_short float Capital relative to short positions. capital_short are negative numbers.
entry_price float VWAP Entry price for the open inventory (FIFO).
inventory list[UnmatchedTrade] List of inventory item (UnmatchedTrade object - see below) that make up the position. Inventory is managed in first in first out (FIFO) order.
mtm_price float VWAP position price for open inventory (FIFO) based on entry of the day initiated and the clearing firms marked to market price if held overnight.
shares integer Remaining shares open. Long positions are positive numbers and short positions are negative numbers.

UnmatchedTrade object

Name Type Information
execution tuple Link to the entry Execution. See below.
mtm_price float VWAP position price for open inventory (FIFO) based on entry of the day initiated and the clearing firms market to market price if held overnight.
remaining_shares integer Shares remaining of the open position. Long positions are positive numbers. Short positions are negative numbers

Execution object - tuple

Name Type Information
clordid string Client Order Id in FIX. Is generated when the order is sent. Will be the same on executions.
collect None is a json dictionary of information collected at the time of the order.
instruction_id string Unique GUID for the instance of the script that generated the initial order. Will be None if the initial order wasn't started from same machine (e.g. from trading client)
order_id None Unique GUID of order being filled (very similar clordid). Will be None if the initial order wasn't started from same machine (e.g. from trading client)
price float Execution price
script_id string GUID of the script. Will be None if the initial order wasn't started from same machine (e.g. from trading client)
shares integer Shares executed on the trade. Always positive number, whether buy or sell execution
side integer Side of the trade
  • buy = 1
  • sell = 2
  • shortsell = 5
symbol string Symbol of trade
time muts Timestamp of trade
user_key string user_key set on the order. Can be used to identify trades that go together (pairs/batch/strategy). account object can be filtered on user_key
Remarks:
  • Works like a dictionary filtering on the account object.
  • Rather than summing long and short pending order (should it be absolute or netted), there are two separate attributes for tracking long/short capital and shares. Manipulate as needed.
  • capital_short and shares_short are negative numbers.
  • mtm_price is an attribute of UnmatchedTrade while entry_price is an attribute of the Execution object.
Working Example:

Backtest for QLD on 7/29/16 from 9:31-9:32. Extra Symbols: [SPY]

 from cloudquant.interfaces import Strategy


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

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

        # generate positions
        order.algo_buy(self.symbol, "market", intent="increase", order_quantity=100)
        order.algo_buy(self.symbol, "market", intent="increase", order_quantity=100, allow_multiple_pending=True)
        order.algo_buy(self.symbol, "market", intent="increase", order_quantity=100, allow_multiple_pending=True)

    def on_finish(self, md, order, service, account):
        print '\n\n.............................\non_finish\n\t%s\n' % service.time_to_string(service.system_time,
                                                                                              '%H:%M:%S.%f')
        print account

        for symbol in sorted(account):
            print '\n\naccount[%s].position' % symbol
            print account[symbol].position

            print '\n\n\taccount[symbol].position.capital_long  -   ', account[symbol].position.capital_long
            print '\taccount[symbol].position.capital_short -   ', account[symbol].position.capital_short
            print '\taccount[symbol].position.entry_price   -   ', account[symbol].position.entry_price
            print '\taccount[symbol].position.mtm_price -   ', account[symbol].position.mtm_price
            print '\taccount[symbol].position.shares    -   ', account[symbol].position.shares

            print '\n\n\tfor items in account[symbol].position.inventory:'
            for items in account[symbol].position.inventory:
                name = items._fields
                for key, value in enumerate(items):
                    if name[key] == 'execution':
                        print '\t\texecution'
                        fields = value._fields
                        for index, keys in enumerate(fields):
                            print '\t\t\t%s\t%s' % (keys, value[index])
                    else:
                        print '\t\t%s\t%s' % (name[key], value)

Console

.............................
on_finish
    16:00:00.000000

account_id:                                 0
entry_pl:                                0.00
mtm_pl:                                  0.00
buying_power:                            None
open_capital_long:                   38325.00
open_capital_short:                      0.00
pending_capital_long:                38325.00
pending_capital_short:                   0.00
max_capital_used:                    38325.00
max_capital_used_timestamp:  1481553005000001
open_symbols:                ['TQQQ']
pending_symbols:             []
SPY:
    Execution count     :              0
    realized_pl         :
        entry_pl        :           0.00
        mtm_pl          :           0.00
        matched_trades  :              0
    unrealized_pl       :
        entry_pl        :           0.00
        mtm_pl          :           0.00
    position            :
        shares          :              0
        entry_price     :           0.00
        mtm_price       :           0.00
        capital_long    :           0.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
TQQQ:
    Execution count     :              3
    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          :            300
        entry_price     :         127.75
        mtm_price       :         127.75
        capital_long    :       38325.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


account[SPY].position
        shares          :              0
        entry_price     :           0.00
        mtm_price       :           0.00
        capital_long    :           0.00
        capital_short   :           0.00


    account[symbol].position.capital_long   -   0.0
    account[symbol].position.capital_short  -   0.0
    account[symbol].position.entry_price    -   0.0
    account[symbol].position.mtm_price  -   0.0
    account[symbol].position.shares -   0


    for items in account[symbol].position.inventory:


account[TQQQ].position
        shares          :            300
        entry_price     :         127.75
        mtm_price       :         127.75
        capital_long    :       38325.00
        capital_short   :           0.00


    account[symbol].position.capital_long   -   38325.0
    account[symbol].position.capital_short  -   0.0
    account[symbol].position.entry_price    -   127.75
    account[symbol].position.mtm_price  -   127.75
    account[symbol].position.shares -   300


    for items in account[symbol].position.inventory:
        remaining_shares    100
        mtm_price   127.75
        execution
            symbol  TQQQ
            side    1
            shares  100
            price   127.75
            time    1481553005000001
            collect None
            order_id    0000000000000001
            clordid None
            instruction_id  ba6c969c-7155-570a-9b9a-cecc3a125b99
            script_id   ecb70f5e-e91a-43b2-a51d-10a6d52f19f6
            user_key    ba6c969c-7155-570a-9b9a-cecc3a125b99
        remaining_shares    100
        mtm_price   127.75
        execution
            symbol  TQQQ
            side    1
            shares  100
            price   127.75
            time    1481553005000001
            collect None
            order_id    0000000000000002
            clordid None
            instruction_id  ba6c969c-7155-570a-9b9a-cecc3a125b99
            script_id   ecb70f5e-e91a-43b2-a51d-10a6d52f19f6
            user_key    ba6c969c-7155-570a-9b9a-cecc3a125b99
        remaining_shares    100
        mtm_price   127.75
        execution
            symbol  TQQQ
            side    1
            shares  100
            price   127.75
            time    1481553005000001
            collect None
            order_id    0000000000000003
            clordid None
            instruction_id  ba6c969c-7155-570a-9b9a-cecc3a125b99
            script_id   ecb70f5e-e91a-43b2-a51d-10a6d52f19f6
            user_key    ba6c969c-7155-570a-9b9a-cecc3a125b99