accessibility of daily moving average: Issue to get the current(today data if period is day) data ?

I have created a daily moving average variable. But I could not get the current moving average data when the period is day. I am able to get yesterday moving average. Here is below for the code.

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

    daily_bars = md.bar.daily(start=-200)        ###moving average  0 for simple, 1 for ema
    dclose     = daily_bars.close

    self.realSMA21  = talib.MA(dclose , timeperiod=21, matype=0)
    self.realSMA50  = talib.MA(dclose , timeperiod=50, matype=0)

I can get access to self.realSMA21[-1] (yesterday MA) but self.realSMA21[0] gives NAN so how to get access to today MA data?

Leon

Comments

  • ptunneyptunney Posts: 246
    edited February 2018

    A daily moving average is based on close prices..

    So a 20 day MA price is the moving average of the previous 20 days.

    It is super simple to calculate, you do not need TALIB, just sum the close prices for the last 20 days and divide by 20 (make sure you have 20 items in your list!!).

    If you want it for the day before that, sum the close prices for the 20 days prior to that and divide by 20, easy.

    Most of the time, for model decision making, when using a 20 day MA you are looking at something like.. current price is above or below the 20 day moving average price. ie the MA of the PREVIOUS 20 days.. You can't include today because you don't know today's close price until 4pm.
    At the start of any day you do not have a close price for that day. If you did you would be forward-looking, that's a no, no.

    So asking for the MA price for today is mute...

    Now, saying that, there is a way of getting a 20 day MA including today.. live... you simply fold in the last traded price, put it as the last price in the list before you do your calculation.

    But your code shows you are doing the calculation in on_start.. and depending on when you start your script and what type of symbol you are looking at you may not have a LAST price.

    I am assuming you are doing some kind of beginning of the day calcuation.. if the price today is above or below the 20 day MA price then do this.

    Hopefully this helps!

    Paul

    Some code!

    from cloudquant.interfaces import Strategy
    import talib
    import numpy
    
    class MovingAverage(Strategy):
    
        @classmethod
        def is_symbol_qualified(cls, symbol, md, service, account):
            return symbol== 'SPY'
    
        def on_start(self, md, order, service, account):
            print self.symbol
    
            # First we need some historic data so lets grab 30 daily bars
            daily_bars = md.bar.daily(start=-30,include_empty=False) 
    
            # most TALIB calls use OHLC (Open, High, Low, Close) in some form so lets pull those out so we have them ready
            open = daily_bars.open   
            high = daily_bars.high   
            low = daily_bars.low     
            close = daily_bars.close 
            volume = daily_bars.volume
    
            # TALIB.MA - Moving Average - matype=0 by default...
            # 0 = SMA (Simple Moving Average) (Default)
            # 1 = EMA (Exponential Moving Average)
            # 2 = WMA (Weighted Moving Average)
            # 3 = DEMA (Double Exponential Moving Average)
            # 4 = TEMA (Triple Exponential Moving Average)
            # 5 = TRIMA (Triangular Moving Average)
            # 6 = KAMA (Kaufman Adaptive Moving Average)
            # 7 = MAMA (MESA Adaptive Moving Average)
            # 8 = T3 (Triple Exponential Moving Average)            
    
            SMA = talib.MA(close,timeperiod=20)
            SMAlist = numpy.ndarray.tolist(SMA) 
            print "Simple Moving Average",SMAlist
    
            EMA = talib.MA(close,timeperiod=20,matype=1)
            EMAlist = numpy.ndarray.tolist(EMA) 
            print "Exponential Moving Average",EMAlist
    
            print "Simple Moving Average      (last 3 entries)",SMAlist[-3:]
            print "Exponential Moving Average (last 3 entries)",EMAlist[-3:]
            print "Average of last 20 close prices",sum(close[-20:])/len(close[-20:])
            print "Last Three close prices...", round(close[-3],2), round(close[-2],2), round(close[-1],2)
            print "last 20 close prices...",close[-20:]
            print
            service.terminate()
            return 
    

    run on 12/23/2018 that that outputs the previous 20 days SPY close prices as follows...

    283.2999878
    286.5799866
    284.6799927
    281.7600098
    281.8999939
    281.5799866
    275.4500122
    263.9299927
    269.1300049
    267.6700134
    257.6300049
    261.5
    265.3399963
    266
    269.5899963
    273.0299988
    273.1099854
    271.3999939
    270.0499878
    270.3999939
    SUM TOTAL 5454.029938 / 20 days = 272.7014969
    
Sign In or Register to comment.