Back to Community
How to get the past N day rolling return?

A trading signal I have requires the past 60 day rolling % change for a stock. For example, I have:

def initialize(context):  
    context.spy = sid(8554)  
def handle_data(context, data):

    spy = context.spy

    spy60dayreturn = ???

How do you calculate this?

2 responses

This may help:

# --------------------------------  
stock = symbol('SPY'); PERIOD = 60  
# --------------------------------  
def initialize(context):  
    schedule_function(period_return_indicator, date_rules.every_day(), time_rules.market_close())  

def period_return_indicator(context, data):  
    prices = data.history(stock, 'price',  PERIOD + 1, '1d')  
    period_return = prices[-1]/prices[0] - 1.0 

    roc = data.history(stock, 'price',  PERIOD + 1, '1d').pct_change(PERIOD)[-1]  

    record(period_return = period_return, roc = roc)  
    print( period_return, roc )  

thank you Vladimir