Back to Community
Noob: How can I backtest the previous open

How can I test the previous day or the previous week's price levels? For example, I want to see if the ES continuous contract open>prev open buy 1 share. If open < prev open, do nothing? I like to do this for previous day high and previous weeks high and other levels such as these. Any help or guidance is greatly appreciated.

1 response

You can crate a custom factor like this:

class MyFactor(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 2 # Days to look back

    def compute(self, today, assets, out, close):  
        if close[-2] > close[-1]:  
            signal = 1  
            signal = 0  
    out[:] = signal  

Then, run a Pipeline on it:

def make_pipeline():  
    myfactor = MyFactor()  
    return Pipeline(columns={'myfactor':myfactor})  

Finally, attach the pipeline and then define a trading logic, for example:

# In Initialize  
algo.attach_pipeline(make_pipeline(), 'pipeline')

# In before_trading_start  
if pipeline == 1:  
        order(asset, amount=1, style)