Mariner Backtesting - TA-LIB Moving average with variable period

MAVP

 real = MAVP(close, periods, minperiod=2, maxperiod=30, matype=0)

Plot

Moving average with variable period

Working Example

from cloudquant.interfaces import Strategy
from collections import OrderedDict
import ktgfunc
import numpy
import talib

class WE_MAVP(Strategy):

    def on_start(self, md, order, service, account):
        # symbol and timestamp
        print(self.symbol + ":  " + service.time_to_string(service.system_time))
        daily_bars = md.bar.daily(start=-200)
        close = daily_bars.close

        # for the first 100 days, have a period of 5 days
        # afterwards, increase the period to 30 days 
        periodList = []

        num = 0
        while num < len(close):
            if num <= 100:
                periodList.append(5)
            elif num > 100:
                periodList.append(30)
            num += 1


        periods = numpy.asarray(periodList, dtype=float)

        real = talib.MAVP(close, periods, minperiod=2, maxperiod=30, matype=0)

        # get the date values
        dates = service._context.market._storage.market_hours.keys()
        dateList = []
        for date in dates:
           dateList.append(str(date.strftime('%Y-%m-%d')))
        dates = sorted(dateList, reverse=True)[1:201]
        dates.sort()

        dict = OrderedDict()
        dict['date'] = dates
        dict['close'] = close
        dict['periods'] = periods
        dict['real'] = real
        symbol = 'MAVP: ' + self.symbol
        symbol = 'MAVP: ' + self.symbol
        print ktgfunc.talib_table(symbol, 1, dict)

Console

MSFT:  2017-02-09 09:30:00.000000
MAVP: MSFT
Input Output
date close periods real
2016-04-26 50.43 5.00 nan
2016-04-27 49.94 5.00 nan
2016-04-28 48.92 5.00 nan
2016-04-29 48.89 5.00 nan
2016-05-02 49.61 5.00 nan
2016-05-03 48.80 5.00 nan
2016-05-04 48.89 5.00 nan
2016-05-05 48.96 5.00 nan
2016-05-06 49.40 5.00 nan
2016-05-09 49.08 5.00 nan
2016-05-10 50.01 5.00 nan
2016-05-11 50.04 5.00 nan
2016-05-12 50.49 5.00 nan
2016-05-13 50.07 5.00 nan
2016-05-16 50.81 5.00 nan
2016-05-17 49.86 5.00 nan
2016-05-18 50.16 5.00 nan
2016-05-19 49.67 5.00 nan
2016-05-20 49.97 5.00 nan
2016-05-23 49.39 5.00 nan
2016-05-24 50.93 5.00 nan
2016-05-25 51.45 5.00 nan
2016-05-26 51.22 5.00 nan
2016-05-27 51.65 5.00 nan
2016-05-31 52.32 5.00 nan
2016-06-01 52.17 5.00 nan
2016-06-02 51.80 5.00 nan
2016-06-03 51.12 5.00 nan
2016-06-06 51.46 5.00 nan
2016-06-07 51.43 5.00 51.60
2016-06-08 51.37 5.00 51.44
2016-06-09 50.96 5.00 51.27
2016-06-10 50.82 5.00 51.21
2016-06-13 49.49 5.00 50.81
2016-06-14 49.19 5.00 50.37
2016-06-15 49.05 5.00 49.90
2016-06-16 49.74 5.00 49.66
2016-06-17 49.49 5.00 49.39
2016-06-20 49.43 5.00 49.38
2016-06-21 50.53 5.00 49.65
2016-06-22 50.33 5.00 49.90
2016-06-23 51.24 5.00 50.20
2016-06-24 49.19 5.00 50.14
2016-06-27 47.81 5.00 49.82
2016-06-28 48.80 5.00 49.48
2016-06-29 49.89 5.00 49.39
2016-06-30 50.51 5.00 49.24
2016-07-01 50.50 5.00 49.50
2016-07-05 50.51 5.00 50.04
2016-07-06 50.72 5.00 50.43
2016-07-07 50.72 5.00 50.59
2016-07-08 51.63 5.00 50.82
2016-07-11 51.91 5.00 51.10
2016-07-12 52.53 5.00 51.50
2016-07-13 52.82 5.00 51.92
2016-07-14 53.05 5.00 52.39
2016-07-15 53.01 5.00 52.66
2016-07-18 53.27 5.00 52.93
2016-07-19 52.41 5.00 52.91
2016-07-20 55.19 5.00 53.38
2016-07-21 55.08 5.00 53.79
2016-07-22 55.84 5.00 54.36
2016-07-25 56.00 5.00 54.90
2016-07-26 56.03 5.00 55.63
2016-07-27 55.47 5.00 55.68
2016-07-28 55.49 5.00 55.77
2016-07-29 55.95 5.00 55.79
2016-08-01 55.85 5.00 55.76
2016-08-02 55.85 5.00 55.72
2016-08-03 56.24 5.00 55.88
2016-08-04 56.65 5.00 56.11
2016-08-05 57.21 5.00 56.36
2016-08-08 57.31 5.00 56.65
2016-08-09 57.45 5.00 56.97
2016-08-10 57.27 5.00 57.18
2016-08-11 57.55 5.00 57.36
2016-08-12 57.19 5.00 57.36
2016-08-15 57.37 5.00 57.37
2016-08-16 57.05 5.00 57.29
2016-08-17 57.17 5.00 57.27
2016-08-18 57.21 5.00 57.20
2016-08-19 57.23 5.00 57.21
2016-08-22 57.28 5.00 57.19
2016-08-23 57.50 5.00 57.28
2016-08-24 57.56 5.00 57.36
2016-08-25 57.78 5.00 57.47
2016-08-26 57.64 5.00 57.55
2016-08-29 57.71 5.00 57.64
2016-08-30 57.50 5.00 57.64
2016-08-31 57.07 5.00 57.54
2016-09-01 57.20 5.00 57.43
2016-09-02 57.28 5.00 57.35
2016-09-06 57.22 5.00 57.26
2016-09-07 57.27 5.00 57.21
2016-09-08 57.04 5.00 57.21
2016-09-09 55.83 5.00 56.93
2016-09-12 56.67 5.00 56.81
2016-09-13 56.15 5.00 56.59
2016-09-14 55.88 5.00 56.32
2016-09-15 56.81 5.00 56.27
2016-09-16 56.87 5.00 56.47
2016-09-19 56.55 30.00 57.12
2016-09-20 56.43 30.00 57.09
2016-09-21 57.37 30.00 57.09
2016-09-22 57.43 30.00 57.10
2016-09-23 57.04 30.00 57.08
2016-09-26 56.52 30.00 57.06
2016-09-27 57.56 30.00 57.06
2016-09-28 57.64 30.00 57.08
2016-09-29 57.01 30.00 57.08
2016-09-30 57.21 30.00 57.08
2016-10-03 57.03 30.00 57.07
2016-10-04 56.86 30.00 57.06
2016-10-05 57.25 30.00 57.05
2016-10-06 57.35 30.00 57.04
2016-10-07 57.41 30.00 57.03
2016-10-10 57.65 30.00 57.03
2016-10-11 56.81 30.00 57.00
2016-10-12 56.73 30.00 56.97
2016-10-13 56.54 30.00 56.95
2016-10-14 57.03 30.00 56.95
2016-10-17 56.84 30.00 56.93
2016-10-18 57.27 30.00 56.94
2016-10-19 57.14 30.00 56.93
2016-10-20 56.87 30.00 56.93
2016-10-21 59.26 30.00 57.04
2016-10-24 60.59 30.00 57.17
2016-10-25 60.58 30.00 57.32
2016-10-26 60.22 30.00 57.46
2016-10-27 59.70 30.00 57.56
2016-10-28 59.47 30.00 57.65
2016-10-31 59.52 30.00 57.74
2016-11-01 59.40 30.00 57.84
2016-11-02 59.03 30.00 57.90
2016-11-03 58.81 30.00 57.95
2016-11-04 58.32 30.00 57.99
2016-11-07 60.01 30.00 58.10
2016-11-08 60.06 30.00 58.19
2016-11-09 59.77 30.00 58.26
2016-11-10 58.31 30.00 58.30
2016-11-11 58.62 30.00 58.35
2016-11-14 57.73 30.00 58.37
2016-11-15 58.87 30.00 58.44
2016-11-16 59.65 30.00 58.52
2016-11-17 60.64 30.00 58.63
2016-11-18 60.35 30.00 58.73
2016-11-21 60.86 30.00 58.83
2016-11-22 61.12 30.00 58.98
2016-11-23 60.40 30.00 59.10
2016-11-25 60.53 30.00 59.23
2016-11-28 60.61 30.00 59.35
2016-11-29 61.09 30.00 59.49
2016-11-30 60.26 30.00 59.59
2016-12-01 59.20 30.00 59.66
2016-12-02 59.25 30.00 59.74
2016-12-05 60.22 30.00 59.77
2016-12-06 59.95 30.00 59.75
2016-12-07 61.37 30.00 59.78
2016-12-08 61.01 30.00 59.80
2016-12-09 61.97 30.00 59.88
2016-12-12 62.17 30.00 59.97
2016-12-13 62.98 30.00 60.09
2016-12-14 62.68 30.00 60.19
2016-12-15 62.58 30.00 60.31
2016-12-16 62.30 30.00 60.43
2016-12-19 63.62 30.00 60.61
2016-12-20 63.54 30.00 60.72
2016-12-21 63.54 30.00 60.84
2016-12-22 63.55 30.00 60.97
2016-12-23 63.24 30.00 61.13
2016-12-27 63.28 30.00 61.29
2016-12-28 62.99 30.00 61.46
2016-12-29 62.90 30.00 61.60
2016-12-30 62.14 30.00 61.68
2017-01-03 62.58 30.00 61.74
2017-01-04 62.30 30.00 61.81
2017-01-05 62.30 30.00 61.86
2017-01-06 62.84 30.00 61.91
2017-01-09 62.64 30.00 61.99
2017-01-10 62.62 30.00 62.06
2017-01-11 63.19 30.00 62.14
2017-01-12 62.61 30.00 62.19
2017-01-13 62.70 30.00 62.28
2017-01-17 62.53 30.00 62.39
2017-01-18 62.50 30.00 62.49
2017-01-19 62.30 30.00 62.56
2017-01-20 62.74 30.00 62.66
2017-01-23 62.96 30.00 62.71
2017-01-24 63.52 30.00 62.79
2017-01-25 63.68 30.00 62.85
2017-01-26 64.27 30.00 62.92
2017-01-27 65.78 30.00 63.01
2017-01-30 65.13 30.00 63.10
2017-01-31 64.65 30.00 63.16
2017-02-01 63.58 30.00 63.21
2017-02-02 63.17 30.00 63.19
2017-02-03 63.68 30.00 63.20
2017-02-06 63.64 30.00 63.20
2017-02-07 63.43 30.00 63.20
2017-02-08 63.34 30.00 63.20