Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to shift a moving average n periods into the future ?

Hi , i try to shift the moving average of , for example the last 2 trading days and project it 3 days into the future

it works in the notebook like so:

import numpy as np  
import pandas as pd  
import matplotlib.pyplot as plt

aapl_daily_low = get_pricing(  
    'AAPL',  
    fields='low', #modify to price, open_price, high, low or volume to change the field  
    start_date='2014-01-01', #customize your pricing date range  
    end_date = '2014-07-01',  
    frequency='daily', #change to daily for daily pricing  
)


aapl_daily_high = get_pricing(  
    'AAPL',  
    fields='high', #modify to price, open_price, high, low or volume to change the field  
    start_date='2014-01-01', #customize your pricing date range  
    end_date = '2014-07-01',  
    frequency='daily', #change to daily for daily pricing  
)


aapl_daily_close = get_pricing(  
    'AAPL',  
    fields='close_price', #modify to price, open_price, high, low or volume to change the field  
    start_date='2014-01-01', #customize your pricing date range  
    end_date = '2014-07-01',  
    frequency='daily', #change to daily for daily pricing  
)



apple_mean_low = np.round(aapl_daily_low.rolling(window = 2, center = False).mean().shift(3),2)  
apple_mean_high = np.round(aapl_daily_high.rolling(window = 2, center = False).mean().shift(3),2)  

iam happy with the output:

Plot of daily low and MA of low

however if i try to run a backtest on an algo with the lines of code for creating the MAs it seems to return NaN values ?

can anybody help a python beginner please ?

thnx

4 responses

Sergej ,

Try this:

    security = symbol('SPY')  
    ma = 2  
    shift_ma = 1  
    bars = ma + shift_ma

    curr_price = data.current(security, 'price')  
    hist_low = data.history(security, 'low' , bars, '1d')  
    mean_low_curr = np.round(hist_low[-ma:-1].mean(), 2)  
    mean_low_shifted = np.round(hist_low[-ma-shift_ma:-shift_ma-1].mean(), 2)

    hist_high = data.history(security, 'high' , bars, '1d')  
    mean_high_curr = np.round( hist_high[-ma:-1].mean(), 2)  
    mean_high_shifted = np.round(hist_high[-ma-shift_ma:-shift_ma-1].mean(), 2)

    print(mean_low_curr,  mean_low_shifted, mean_high_curr,  mean_high_shifted,)  

    record( mean_low_curr = mean_low_curr, mean_low_shifted = mean_low_shifted )  
    record( mean_high_curr=mean_high_curr,mean_high_shifted=mean_high_shifted )  
    record( curr_price = curr_price,)  

Thank you so much Vladimir!!!!

iam gonna check and test and tinker along it over the course of the weekend and report back..

till then all the best!

Sergej can you post your backtest afterward?
I'd love to see how you structured the code. I absolutely understand if you don't want to disclose your code.
Thank you to Vladimir as well, I have also been looking for a solution to switching moving averages in n periods into the future.

Hey Carl ,

well i managed to edit the code (trial and error) , so that the MAs work the way as intended , iam still working on entries and position handling

the settings i use are :
iam pretty sure it may look like a butcherd version of vladimirs code (sorry vlad ;) , but the logs look good when compared to my chart .. so iam happy with the result...

def ma_cross_handling(context,data):  

    security = symbol('SPY')  
    ma = 1  
    shift_ma = 4  
    bars = ma + shift_ma

    curr_price = data.current(security, 'price')  
    curr_high = data.current(security, 'high')  
    curr_low = data.current(security, 'low')  

    hist_low = data.history(security, 'low' , bars, '1d')  
    mean_low_curr = np.round(hist_low[ma].mean(), 3)  


    hist_high = data.history(security, 'high' , bars, '1d')  
    mean_high_curr = np.round( hist_high[ma].mean(), 3)  


    print(mean_low_curr, mean_high_curr)  

    record( mean_low_curr = mean_low_curr )  
    record( mean_high_curr = mean_high_curr )  
    record( curr_high = curr_high, curr_low = curr_low)  

@Vladimir could you may elobarate of what your thought process behind your coding was ?, would defenetly help me learn to understand programming , thank you

@Carl
it may take some while till i post an updated version of the code / algo , as its only the beginning ,will keep you updated

till then all the best