Mariner Backtesting - Tracking Profits & Losses

TrackProfitLossInformation()

(class) Overview:

TrackProfitLossInformation() makes it easy to track PnL information during a trade.

Interfaces:

TrackProfitLossInformation()

Creates the instance.

 TrackProfitLossInformation( md, entryPrice, entryShares )

Parameters:

Name Type Default Information
md object required ktg market data object
entryPrice float required should be the entry price
entryShares float required should be the entry shares

Attributes:

Name Type Default Information
HighInTrade float parameter entryPrice Highest price printed during the trade.
HighInTradeTime MUTS service.system_time Market time at which the highest price printed.
LowInTrade float parameter entryPrice Lowest price printed during the trade.
LowInTradeTime MUTS md.system_time Market time at which the lowest price printed.
EntryPrice float parameter entryPrice should be the entry price
Shares integer parameter entryShares should be the entry shares
MarketPrice float 0.0 Best bid (for long positions) or best ask (for short positions).
ProfitLoss float 0.0 Difference between the corresponding market price (explained above) and the entry price of the trade, multiplied by the number of shares.
Range float 0.0 Difference between the high and low of the range.
LossFromBest float 0.0 Amount trade has pulled back from the best price.
GainFromWorst float 0.0 Amount trade has moved in your favor from the worst price.

Track()

Call to update the instance variables. Call method for checking instance variables.

 Track( md )

Parameters:

Name Type Default Information
md object required ktg market data object
Working Example:
 from cloudquant.interfaces import Strategy
import ktgfunc

class TrackProfitability(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == 'GE'

    def on_start(self, md, order, service, account):
        print 'Start', service.time_to_string(service.system_time), self.symbol, '\n\n'

        # Sell Limit CHX at Script Price abcae79b-ec08-4a81-a736-af5a4868b44e
        order.algo_sell(self.symbol,
               algorithm='abcae79b-ec08-4a81-a736-af5a4868b44e',
               intent='init',
               order_quantity=100,
               price = md.L1.ask - .1)

        self.minute_count = 0

    def on_minute_bar(self, event, md, order, service, account, bar):
        # increment on every minute
        self.minute_count += 1

        # Initialize a TrackProfitLossInformation object passing the entry_price and shares
        # check self.minute_count to ensure that a position has been entered
        if self.minute_count == 2:
            entry_price = account[self.symbol].position.entry_price
            shares = account[self.symbol].position.shares
            self.tpl = ktgfunc.TrackProfitLossInformation(md, entry_price, abs(shares))

        if self.minute_count > 1:

            # update the TrackProfitLossInformation object
            self.tpl.Track(md)

            print service.time_to_string(service.system_time), self.symbol
            print 'EntryPrice - %s' % self.tpl.EntryPrice
            print 'Shares - %s' % self.tpl.Shares
            print 'MarketPrice - %s\n' % self.tpl.MarketPrice

            print 'HighInTrade - %s' % self.tpl.HighInTrade
            print 'HighInTradeTime - %s\n' % service.time_to_string(self.tpl.HighInTradeTime)
            print 'LowInTrade - %s' % self.tpl.LowInTrade
            print 'LowInTradeTime - %s\n' % service.time_to_string(self.tpl.LowInTradeTime)

            print 'ProfitLoss - %s' % self.tpl.ProfitLoss
            print 'Range - %s' % self.tpl.Range
            print 'LossFromBest - %s' % self.tpl.LossFromBest
            print 'GainFromWorst - %s\n\n\n' % self.tpl.GainFromWorst

        # terminate on the 6th minute
        if self.minute_count == 6:
            service.terminate()

Console

Start 2016-03-01 09:30:00.000000 GE

2016-03-01 09:31:00.675000 GE
EntryPrice - 29.2899993896
Shares - 100
MarketPrice - 29.4300003052

HighInTrade - 29.4300003052
HighInTradeTime - 2016-03-01 09:30:59.447000

LowInTrade - 29.2899993896
LowInTradeTime - 2016-03-01 09:30:59.447000

ProfitLoss - 14.0000915527
Range - 0.140000915527
LossFromBest - 0.0
GainFromWorst - 14.0000915527



2016-03-01 09:32:00.516000 GE
EntryPrice - 29.2899993896
Shares - 100
MarketPrice - 29.4300003052

HighInTrade - 29.4300003052
HighInTradeTime - 2016-03-01 09:30:59.447000

LowInTrade - 29.2899993896
LowInTradeTime - 2016-03-01 09:30:59.447000

ProfitLoss - 14.0000915527
Range - 0.140000915527
LossFromBest - 0.0
GainFromWorst - 14.0000915527



2016-03-01 09:33:03.183000 GE
EntryPrice - 29.2899993896
Shares - 100
MarketPrice - 29.4689006805

HighInTrade - 29.4689006805
HighInTradeTime - 2016-03-01 09:32:59.916000

LowInTrade - 29.2899993896
LowInTradeTime - 2016-03-01 09:30:59.447000

ProfitLoss - 17.8901290894
Range - 0.178901290894
LossFromBest - 0.0
GainFromWorst - 17.8901290894



2016-03-01 09:34:00.027000 GE
EntryPrice - 29.2899993896
Shares - 100
MarketPrice - 29.4274997711

HighInTrade - 29.4689006805
HighInTradeTime - 2016-03-01 09:32:59.916000

LowInTrade - 29.2899993896
LowInTradeTime - 2016-03-01 09:30:59.447000

ProfitLoss - 13.750038147
Range - 0.178901290894
LossFromBest - -4.14009094238
GainFromWorst - 13.750038147



2016-03-01 09:35:00.749000 GE
EntryPrice - 29.2899993896
Shares - 100
MarketPrice - 29.4279003143

HighInTrade - 29.4689006805
HighInTradeTime - 2016-03-01 09:32:59.916000

LowInTrade - 29.2899993896
LowInTradeTime - 2016-03-01 09:30:59.447000

ProfitLoss - 13.7900924683
Range - 0.178901290894
LossFromBest - -4.10003662109
GainFromWorst - 13.7900924683