Back to Community
multiple day historical calculation in pipeline

I am trying to add a factor to a pipeline that requires multiple days of data in a row, I can achieve it outside of pipeline using data.history and then performing a calculation with that data, but is there a way to do something like this in the pipeline.

import pandas as pd  
context.lookback = data.history(context.security_list, 'price', 7, '1d')  
df = context.lookback  
df['price_calculated'] = df['price_calculated'].rolling(5).sum()

2 responses

Are you wishing to calculate/return a single value? In the example you are actually returning multiple values. Do you just want the sum of the last 5 days of prices? Factors by definition return a single real number per output. You can have multiple outputs but the quantity of outputs is fixed,

yes so for a given day it will be a single real number output which is the sum of the last 5 days of prices.