Mariner Backtesting - Accounting Pending Orders

account[symbol].pending

account[symbol].pending Overview:

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

Attributes:

Sample - Access data attribute

 account[self.symbol].pending.capital_long
Name Type Information
capital_long float Capital floating in the market long. This is calculated by taking the current market price for each order and multiplying by the number of shares. If the order's symbol is opening or closing, the market price will be the last symbol price. Otherwise, the price will be the most recent ask.
capital_short float Capital floating in the market long. capital_short are negative numbers. This is calculated by taking the current market price for each order and multiplying by the number of shares. If the order's symbol is opening or closing, the market price will be the last symbol price. Otherwise, the price will be the most recent bid.
count_long integer Count of long orders pending.
count_short integer Count of short orders pending.
orders list[PendingOrder] List of all pending orders, including those that have passed pre-trade risk checks and exchange checks, and orders that have not been checked yet.
shares_long integer Sum of long shares pending
shares_short integer Sum of short shares pending. shares_short are negative numbers.

PendingOrder object - tuple

 PendingOrder( script_id=u'bd3f152e-6e88-4f70-8ae3-0e44039094f1',  user_key='0ff93b0a-6151-4bf6-8bda-de33259941b1',  filled_shares=0,  timestamp=1469799059915000 )
Name Type Information
clordid string Client Order Id in FIX. Is generated when the order is sent. Will be the same on executions.
collect object json dictionary of information collected at the time of the order. None means nothing collected.
exit_options dictionary Dictionary of system parameters for controlling the exit strategy. None means no system parameters.
exit_parameters object Dictionary of parameters for the exit script. None means no parameters.
exit_script object Guid of the exit script. None means no exit script.
explanation string Defaults to None. Can be used to add a note to the order about why it was placed.
filled_shares integer Tells how many shares from this order have been filled. This is only applicable if partial filling of orders is allowed (by default, partial filling is allowed). Defaults to 0.
flags integer Defaults to 0. flags is used by the simulator engine to determine which and how orders are allowed to be placed. Currently only used by allow_multiple_pending when placing an 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)
intent string Intended position change. One of "none", "init", "increase", "decrease", "exit", "reverse".
  • "init" - Initiate a long or short position. Must be flat.
  • "increase" - Increase a long or short position. Can be flat or in a position.
  • "decrease" - Decrease a current long or short position by the specified shares. (Note: "order_quantity" > 0). Will not cover/liquidate more than the position.
  • "exit" - Liquidates a long position or covers a short position. Exit - covers/liquidates the entire position. Shares not permitted when intent is exit.
  • "reverse" - Reverse a short position to a long position, or a long postion to short. We recommend using "position_size" instead of "order_quantity" to specify desired position.
  • "none" - Skip intent check. In production, very few scripts uses this setting.
order_algorithm string Name of algorithm to process order with.
order_id object 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 Pending order 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 object Pending order's shares. Always positive number, whether buy or sell execution
symbol string Symbol of pending order.
timestamp 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.
Working Example:

Backtest for SPY on 2/16/16 from 9:31-9:32.

 from cloudquant.interfaces import Strategy


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

    def on_start(self, md, order, service, account):
        # generate rejected orders
        order.algo_buy(self.symbol, "limit", intent="increase", order_quantity=100, price=0.01,
                       allow_multiple_pending=True)
        order.algo_buy(self.symbol, "limit", intent="increase", order_quantity=100, price=0.02,
                       allow_multiple_pending=True)
        order.algo_buy(self.symbol, "limit", intent="increase", order_quantity=100, price=0.03,
                       allow_multiple_pending=True)

        order.algo_sell(self.symbol, "limit", intent="increase", order_quantity=100, price=10000.01,
                        allow_multiple_pending=True)
        order.algo_sell(self.symbol, "limit", intent="increase", order_quantity=100, price=10000.02,
                        allow_multiple_pending=True)
        order.algo_sell(self.symbol, "limit", intent="increase", order_quantity=100, price=10000.03,
                        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

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

        print '\n\naccount[self.symbol].pending.capital_long  -   %s' % account[self.symbol].pending.capital_long
        print 'account[self.symbol].pending.capital_short  -   %s' % account[self.symbol].pending.capital_short
        print 'account[self.symbol].pending.count_long  -   %s' % account[self.symbol].pending.count_long
        print 'account[self.symbol].pending.count_short  -   %s' % account[self.symbol].pending.count_short
        print 'account[self.symbol].pending.shares_long  -   %s' % account[self.symbol].pending.shares_long
        print 'account[self.symbol].pending.shares_short  -   %s' % account[self.symbol].pending.shares_short

Console

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

account_id:                                 0
entry_pl:                                0.00
mtm_pl:                                  0.00
buying_power:                            None
open_capital_long:                       0.00
open_capital_short:                      0.00
pending_capital_long:                    0.00
pending_capital_short:                   0.00
max_capital_used:                        0.00
max_capital_used_timestamp:                 0
open_symbols:                []
pending_symbols:             ['TQQQ']
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
QQQ:
    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     :              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      :              3
        shares_long     :            300
        capital_long    :       38343.00
        count_short     :              3
        shares_short    :           -300
        capital_short   :      -38328.00


account[self.symbol].pending
        count_long      :              3
        shares_long     :            300
        capital_long    :       38343.00
        count_short     :              3
        shares_short    :           -300
        capital_short   :      -38328.00


account[self.symbol].pending.capital_long  -   38342.9992676
account[self.symbol].pending.capital_short  -   -38328.0006409
account[self.symbol].pending.count_long  -   3
account[self.symbol].pending.count_short  -   3
account[self.symbol].pending.shares_long  -   300
account[self.symbol].pending.shares_short  -   -300