Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Research to IDE Help

I managed to get the following snippet of code to work in the research environment but have been hitting a wall in the IDE.

    pxTLT = np.log(data.history(context.tlt, 'price', 100, '1d'))  
    weeklyTLT = pxTLT.resample('1M', closed='right', label='right').last().ffill()

    pxSPY = np.log(data.history(context.spy, 'price', 100, '1d'))  
    weeklySPY = pxSPY.resample('1M', closed='right', label='right').last().ffill()  
    px = (weeklySPY[symbols('SPY')] - weeklyTLT[symbols('TLT')]).dropna() 

    fastMA = px.rolling(window = 20).mean().dropna()  
    slowMA = px.rolling(window = 50).mean().dropna()  
    osc = (fastMA - slowMA)/ slowMA

    signal = np.mean(osc[-3:], axis=0)  
    lag = np.mean(osc[-6: -4], axis=0)

I have been getting nan values for the signal and lag variables and not sure where the error might be coming from.

Key difference between this snippet of code and the notebook is the use of np.mean() instead of df.rolling().mean() as there were issues with comparing values of the rolling mean.

Appreciate any help on this issue.

2 responses

There are 2 issues with your code example:
1. You don't request enough data. Resampling 100 daily prices to monthly gives you only 6 values but you want rolling windows of 20 and 50 which return only NaNs. Request more days in data.history, something like 1200.
2. The symbols object is different in research and IDE. In research it's and equity object but in IDE it's a list of equity objects. In your code

weeklySPY[symbols('SPY')]  

is a series in research but a data frame in IDE. To get the series in IDE use symbol without 's' at the end (this is not available in research).
Your code should work like this:

    pxTLT = np.log(data.history(context.tlt, 'price', 1200, '1d'))  
    weeklyTLT = pxTLT.resample('1M', closed='right', label='right').last().ffill()

    pxSPY = np.log(data.history(context.spy, 'price', 1200, '1d'))  
    weeklySPY = pxSPY.resample('1M', closed='right', label='right').last().ffill()  
    px = (weeklySPY[symbol('SPY')] - weeklyTLT[symbol('TLT')])#.dropna()

    fastMA = px.rolling(window = 20).mean().dropna()  
    slowMA = px.rolling(window = 50).mean().dropna()  
    osc = (fastMA - slowMA)/ slowMA

    signal = np.mean(osc[-3:], axis=0)  
    lag = np.mean(osc[-6: -4], axis=0)

I assume you have defined context.tlt as symbols('TLT'). You should keep it like that if you only make those 2 changes. Alternatively you could shorten it a bit like this:

    tlt = symbol('TLT')  
    spy = symbol('SPY')

    hist = np.log(data.history([tlt, spy], 'price', 1200, '1d'))  
    weekly = hist.resample('1M', closed='right', label='right').last().ffill()  
    px = weekly[spy] - weekly[tlt]

    fastMA = px.rolling(20).mean()  
    slowMA = px.rolling(50).mean()  
    osc = (fastMA - slowMA) / slowMA

    signal = osc[-3:].mean()  
    lag = osc[-6: -4].mean()

Thanks Tentor, that was very clear and helpful!