I am trying to go through the current NIFTY 100 list of stocks. Quite a few of them did not exist say a year back. This results in an error during backtesting (as shown in the attached screenshot). Currently using try except in the place where we extract price data, but that doesnt seem to catch the error. What could be a better way ?
try:
price_data = data.history(context.securities, 'close',
context.params['indicator_lookback'],
context.params['indicator_freq'])
except:
return
Hi Abhishek,
It seems like you are trying to fetch data of multiple stocks using data.history method. And some of the stocks don't have data.
If you are working with multiple stocks it's recommended to use the blueshift pipeline. In the blueshift pipeline, you can add filters like AverageDollarVolume to filter stocks that have data. You can go to this link to understand the functioning of the blueshift pipeline.
Also, I am adding a code snippet that uses the AverageDollarVolume function to filter stocks.
# The strategy requires context.lookback number of days.
def make_strategy_pipeline(context):
"""
A function to make the pipeline.
"""
dollar_volume_filter = AverageDollarVolume(window_length=252).top(100)
lookback_returns = Returns(window_length=context.lookback)
pipe = Pipeline(
screen = dollar_volume_filter,
columns = {
'lookback_returns': lookback_returns
}
)
return pipe
Hope this helps!