Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
simple moving average 1 day back

I looked thru the postings and the last post I saw regarding this was 2016....and it stated there was not a simple way to reference the simple moving average 1 day back ?

has this changed ?

2 responses

Hi Blaine,

This may help:

# Displaced moving average  
import talib  
# ---------------------------------------  
STK, ma, days_ago = symbol('SPY'), 10, 5  
# ---------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_close())

def trade(context,data):  
    bars = ma + days_ago 

    prices = data.history(STK, 'close', bars, '1d')  
    mavg = talib.SMA(prices, ma)

    mavg_today = round(mavg[-1], 2)  
    mavg_days_ago = round(mavg[-days_ago-1], 2)

    record(mavg_today = mavg_today, mavg_days_ago = mavg_days_ago)  

Thank you very much.....data manipulation with python is more challening than many platforms I've worked on before.... the curve continues !