Mariner Backtesting - Accounting for Unrealized Profit & Loss

account[symbol].unrealized_pl

account[symbol].unrealized_pl Overview:

calculates the profit loss for a currently held position for the specified symbol.

Attributes:

Sample - Access data attribute

 account[self.symbol].unrealized_pl.entry_pl
Name Type Information
entry_pl float Profit or Loss for unmatched trades (i.e. open position) by entry price for symbol. entry_pl will change throughout the day as the current market price changes.
mtm_pl float Profit or Loss for unmatched trades (i.e. open position) by mtm price for symbol. For trades held overnight, the mtm_price is the mark from the clearing firm. On intra-day trades, mtm_price and entry_price will match. mtm_pl will change throughout the day as the current market price changes.
Remarks:
  • Works like a dictionary filtering on the account object.
  • mtm_price and entry_price will match on intra-day trades.
  • Will return 0.00 if no shares are held in the specified symbol.
Working Example:

Backtest for TQQQ on 11/01/16 from 9:30-9:32.

 from cloudquant.interfaces import Strategy


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

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

        # two marketable buy orders
        order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)

    def on_minute_bar(self, event, md, order, service, account, bar):

        if event.timestamp > service.time(9, 31) and event.timestamp < service.time(9, 32):
            print '\n\n.............................\non_minute_bar\n\t%s\n' % service.time_to_string(event.timestamp,
                                                                                                      '%H:%M:%S.%f')
            print('\nShares currently held in ' + self.symbol + ':\t' + str(account[self.symbol].position.shares))
            print("\n\naccount[self.symbol].unrealized_pl")
            print account[self.symbol].unrealized_pl
            order.algo_sell(self.symbol, "market", intent="none", order_quantity=100, allow_multiple_pending=True)

        elif event.timestamp > service.time(9, 32):
            print '\n\n.............................\non_minute_bar\n\t%s\n' % service.time_to_string(event.timestamp,
                                                                                                      '%H:%M:%S.%f')
            print 'account'
            print account

            print("\n\naccount[self.symbol].unrealized_pl")
            print account[self.symbol].unrealized_pl

            print '\n\naccount[self.symbol].unrealized_pl.entry_pl  -   %s' % account[
                self.symbol].unrealized_pl.entry_pl
            print 'account[self.symbol].unrealized_pl.mtm_pl  -   %s' % account[self.symbol].unrealized_pl.mtm_pl

            service.terminate()

Console

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


Shares currently held in TQQQ:  100


account[self.symbol].unrealized_pl
        entry_pl        :           9.00
        mtm_pl          :           9.00


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

account
account_id:                                 0
entry_pl:                                1.00
mtm_pl:                                  1.00
buying_power:                            None
open_capital_long:                       0.00
open_capital_short:                      0.00
pending_capital_long:                    0.00
pending_capital_short:              -12776.00
max_capital_used:                    12775.00
max_capital_used_timestamp:  1481553005000001
open_symbols:                []
pending_symbols:             []
TQQQ:
    Execution count     :              2
    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


account[self.symbol].unrealized_pl
        entry_pl        :           0.00
        mtm_pl          :           0.00


account[self.symbol].unrealized_pl.entry_pl  -   0.0
account[self.symbol].unrealized_pl.mtm_pl  -   0.0