A Black-Scholes-based options backtesting simulator
This is a rough and ready options backtesting model that estimates historical options pricing by plugging in realized implied volatility to the Black-Scholes Model to roughly price options. It is for educational purposes and is useful for those of us who don't have $$$ to spend for historical options data.
backtester.ipynb
Worksutils/get_data.ipynb
(more on this below)'1.05 x'
for 5% OTM at open, or '1.2 SD'
for a position that is 1.2 standard deviations OTM for the given holding period (in days) (based of implied volatility, not realized volatility).hp
(holding period) in the 3rd dimension. The third dimension is used to calculate the price at the end of every day of the holding period so the position can be scalped for a profit of closed at a stop-loss, and for plotting the positions across the hplookback
days on realized volatility and/or implied volatility. For example, you can filter to open a position such that: 30% <= IV < 50% and the mean realized open-close (annualized) volatility for the previous 5 trading days is < 20%50_day_return
(returns from t=-50 to t=0 trading days). A regression line is fitted and can be used to tweak the modelget_data.ipynb
worksIt's worth noting that any data source can be used to supply the columns required by backtester. However, this repository has a file which is explicitly built to request these data and build dataframes with the specific columns, saving the pickle for use by backtester. It is capable of requesting data from scratch as well as updating dataframes to include the latest data
is_update
to False
and specify the parameters you wish such as the underlying's symbol, exchange, and security type, whether to include IV data, the duration of data to grab (eg. 5 years back from today, or 'MAX'
for all available data), and data granulatiry (1 day bars is the default, any size less than this requires more complex logic to avoid exceeding rate limits). Based on these parameters, a file name will be automatically generated that is decodable for updates such that:file_name
and set the flag, is_update
to True
. From there, simply run all the cells. The program will decode the file name, request data from the last known date in the dataframe until present, append that to the existing df, drop duplicates, recalculate columns (shifts must be recalculated else nan
s will show up on every update)..shift()
s introduce future data into current decisions. To address the latter, there are flags training_df
and full_df
for testing and validation, respectively.Options markets are efficient. I have yet to find a combination of parameters that provides risk-adjusted returns in excess of a buy-and-hold strategy in the long run.
Please feel free to contribute in any capacity! Some ideas for simple contributions:
Past returns are NOT indicative of future performance
!