Mariner Backtesting - MovingAverage

A simple moving average over the number of seconds defined by the user.

(class) Overview:

MovingAverage{} is a simple moving average over the number of seconds defined by the user. This expression breaks down to three parts: the moving average, operator, and compare value. The moving average can be based on any value over the number of seconds desired.

Interfaces:

MovingAverage()

Creates the instance.

 MovingAverage( MaxSummaryItems )

Parameters:

Name Type Default Information
MaxSummaryItems integer required max number of summary items ```python if MaxSummaryItems > 1: MaxSummaryItems = 1 ```

Attributes:

Name Type Default Information
MovingAverage float 0.0 simple moving average over the number of seconds defined by the user. 1 is the minimum, max of number of seconds in a day. calculation doesn't cover more than the current day.

CalcMovingAverage()

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

 CalcMovingAverage( MarketTime, Value )

Parameters:

Name Type Default Information
MarketTime MUTS required system recognized time
Value float require 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
Working Example:
 from cloudquant.interfaces import Strategy
import ktgfunc


class MovingAverageExample(Strategy):

    @classmethod
    def is_symbol_qualified(cls, symbol, md, service, account):
        return symbol == "AAL"

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

        # print symbol and timestamp
        print(self.symbol + "\n"+ service.time_to_string(service.system_time) + "\tin on start")

        # create the MovingAverage object with the parameter of 23400
        # which is the number of seconds in the trading day (from 9:30 to 4:00)
        self.avg = ktgfunc.MovingAverage( MaxSummaryItems=23400)

        # counts the number of bars
        self.bar_count = 0


    def on_minute_bar(self, event, md, order, service, account, bar):
        # calculate the moving average every minute
        self.avg.CalcMovingAverage(md.L1.timestamp, md.L1.last)

        # increment counter
        self.bar_count += 1

        if self.bar_count % 40 == 0:

            # print timestamp
            print("\n\n" + service.time_to_string(event.timestamp))

            print("In on_minute_bar #" + str(self.bar_count))

            # print the moving average
            print("The moving average is "+ str(self.avg.MovingAverage))

Console

AAL
2016-08-09 09:30:00.000000  in on start

2016-08-09 10:09:05.005000
In on_minute_bar #40
The moving average is 35.186942482


2016-08-09 10:49:00.129000
In on_minute_bar #80
The moving average is 35.0094862938


2016-08-09 11:29:01.065000
In on_minute_bar #120
The moving average is 34.9184167226


2016-08-09 12:09:00.020000
In on_minute_bar #160
The moving average is 34.8629537821


2016-08-09 12:49:02.280000
In on_minute_bar #200
The moving average is 34.8414395142


2016-08-09 13:29:04.877000
In on_minute_bar #240
The moving average is 34.8344128927


2016-08-09 14:09:00.330000
In on_minute_bar #280
The moving average is 34.8397799628


2016-08-09 14:49:00.804000
In on_minute_bar #320
The moving average is 34.836629343


2016-08-09 15:29:01.631000
In on_minute_bar #360
The moving average is 34.8283235656