Hi,
I've beginning toying around with strategies, which typically require each day a trailing window of past prices. Attempting to be efficient, I wrote a simple function (below) to store the price data in context, so that I do not have to retrieve each day the whole window from the database. But now I'm worried that the data obtained in this way will probably not be adjusted for mergers/splits/dividends...
So here are a few questions:
- Will I encounter problem with mergers/splits/dividends by storing each day the current data? I suspect I will.
- Is it good practice to make daily calls to retrieve a price trailing window using the data.history function? (This slows down the backtest considerably...)
- If not, what is the proper way of retrieving the price trailing window?
- In the help, what do you mean exactly when you say that in data.history, "price is always forward-filled"?
Thanks and best regards,
Samuel Monnier
# Stores the price history in a variable context.price_data.
def store_price_data(context,data):
# Retrieves the price data
new_price_data = pd.DataFrame(data.current(context.security_list, 'price').to_dict(), index = [0])
# Appends it to the data already stored
context.price_data = new_price_data.append(context.price_data, ignore_index = True)
# If the data stored is longer than the lookback period, truncate it.
if len(context.price_data) > context.lookback:
context.price_data = context.price_data[0: context.lookback]