Mariner Backtesting - TrackValue()

TrackValue() tracks desired value to determine when new highs and lows happen. It also tracks other information that can help to find unique patterns

TrackValue()

Creates the instance.

 TrackValue( InitialValue, InitialHigh, InitialLow, CurrentTime )

Parameters:

Name Type Default Information
InitialValue float required the value that is going to be tracked in function. This can be any value, a function that returns a value (like P&L), market data object (like md.Last or chart bar), or any combination of these used in a calculation.
InitialHigh float required for scripts started late, value can be set when script is initialized. "None" lets the script create the high as data is passed into the script.
InitialLow float required for scripts started late, value can be set when script is initialized. "None" lets the script create the low as data is passed into the script.
CurrentTime MUTS required system time tracking

Attributes:

Name Type Default Information
InitialValue float parameter InitialValue initial value to be tracked
HighValue float calculated Set initial high/low to inital value if 0 or not provided. ```python if (InitialHigh == None and InitialLow == None): self.HighValue = self.InitialValue else: #Test high/low value so that high is greater than low if InitialLow > InitialHigh: self.HighValue = InitialHigh else: self.HighValue = InitialHigh ```
LowValue float calculated Set initial high/low to inital value if 0 or not provided. ```python # Set initial high/low to inital value if 0 or not provided if (InitialHigh == None and InitialLow == None): self.LowValue = self.InitialValue else: #Test high/low value so that high is greater than low if InitialLow > InitialHigh: self.LowValue = InitialLow else: self.LowValue = InitialLow ```
PassHighLowTest boolean calculated ```python # Set initial high/low to inital value if 0 or not provided if (InitialHigh == None and InitialLow == None): pass else: #Test high/low value so that high is greater than low if InitialLow > InitialHigh: self.PassHighLowTest = False else: self.PassHighLowTest = True ```
IsNewHigh boolean False last evaluation set new high
IsNewLow boolean False last evaluation set new low
HighTime MUTS service.system_time time of latest new high
LowTime MUTS service.system_time time of latest new low
ValueRange float calculated ```python HighValue - LowValue```
NewHighCount integer 0 count of new highs
NewLowCount integer 0 count of new lows
PullBackFromHigh float 0.0 max pullback from last new high
PreviousPullBackFromHigh float 0.0 previous pullback from new high
PullBackFromLow float 0.0 max pullback from last new low
PreviousPullBackFromLow low 0.0 previous pullback from new low
SecondsSinceHigh integer 0 seconds since last new high
SecondsSinceLow integer 0 seconds since last new low
NewHighSinceLowCount integer 0 count of new highs since the last new low was set. when new low is set ```python NewHighSinceLowCount``` is reset to 0
NewLowSinceHighCount integer 0 count of new lows since the last new low was set. when new high is set ```python NewLowSinceHighCount``` is reset to 0
PercentFromHigh float 0.0 percent away from new high
PercentFromLow float 0.0 percent away from new low

Calculate()

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

 Calculate( CurrentValue, CurrentTime )

Parameters:

Name Type Default Information
CurrentValue float required current value of the value being tracked
CurrentTime MUTS required current system time
Working Example:
 from cloudquant.interfaces import Strategy
import ktgfunc


class TrackValueExample(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol in ['AAPL', 'AMZN', 'FB', 'SPY', 'GOOGL', 'SBUX']
    def on_start(self, md, order, service, account):

        # create an object of the trackValue class
        # all of the initial values are set to the previous close
        self.trackValue = ktgfunc.TrackValue(InitialValue=md.stat.prev_close, InitialHigh=md.stat.prev_close,
                                             InitialLow=md.stat.prev_close, CurrentTime=service.system_time)

    def on_minute_bar(self, event, md, order, service, account, bar):
        # every minute, track the movement of the current symbol, the highs, lows
        self.trackValue.Calculate(md.L1.last, event.timestamp)

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

        print("\nThe values tracked today for " + self.symbol + ":")

        # Use the TrackValue attributes to print information gathered in on_minute_bar
        print("\n\tHighest price : " + str(self.trackValue.HighValue) + " at " + service.time_to_string(self.trackValue.HighTime) )
        print("\tLowest price: " + str(self.trackValue.LowValue) + " at " + service.time_to_string(self.trackValue.LowTime))
        print ("\tNew high count: " + str(self.trackValue.NewHighCount))
        print ("\tNew low count: " + str(self.trackValue.NewLowCount) + "\n\n\n")

Console

The values tracked today for AAPL:

    Highest price : 132.427001953 at 2017-02-09 15:12:02.584000
    Lowest price: 131.190002441 at 2017-02-09 10:15:00.059000
    New high count: 16
    New low count: 9




The values tracked today for AMZN:

    Highest price : 824.826782227 at 2017-02-09 09:47:00.020000
    Lowest price: 819.710021973 at 2017-02-09 09:30:00.000000
    New high count: 8
    New low count: 0




The values tracked today for FB:

    Highest price : 134.479995728 at 2017-02-09 09:30:00.091000
    Lowest price: 133.311004639 at 2017-02-09 10:15:00.092000
    New high count: 1
    New low count: 12




The values tracked today for GOOGL:

    Highest price : 832.609985352 at 2017-02-09 09:30:00.010000
    Lowest price: 827.650024414 at 2017-02-09 10:15:00.268000
    New high count: 1
    New low count: 9




The values tracked today for SBUX:

    Highest price : 56.1001014709 at 2017-02-09 13:15:02.037000
    Lowest price: 55.2200012207 at 2017-02-09 09:30:00.000000
    New high count: 40
    New low count: 0




The values tracked today for SPY:

    Highest price : 230.935699463 at 2017-02-09 14:42:00.997000
    Lowest price: 229.240005493 at 2017-02-09 09:30:00.000000
    New high count: 38
    New low count: 0