Back

Put option pricing example

I just created this algorithm to answer this question.

I think it might be of interest as a seperate post, so I have hereby created a special dicussion for it.

The minimal code that you need to plot the price of a stock and a put option (with the price derived by the Black-Scholes formula) are as follows:

/*
* Example of how to use Put pricing with Black-Scholes in an algorithm
* by: Jean-Paul van Brakel
*/
public class BasicTemplateAlgorithm : QCAlgorithm
{
// ticker to be used
private readonly string _ticker = "AAPL";

// number of periods to be used in volatility calculation
private static int _vol_periods = 14;
private readonly RollingWindow PriceHistory = new RollingWindow(_vol_periods);

// define option maturity date
private readonly DateTime _maturityDate = new DateTime(2015, 1, 16); // third friday of the month

//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Start and End Date range for the backtest:
SetStartDate(2015, 1, 1);
SetWarmup(TimeSpan.FromDays(_vol_periods));
SetEndDate(2015, 1, 2);

//Cash allocation
SetCash(25000);

//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, _ticker, Resolution.Minute);

//Initialise plot
Chart plotter = new Chart("DerivativePlot", ChartType.Stacked);
plotter.AddSeries(new Series("Price", SeriesType.Line));
plotter.AddSeries(new Series("Put price", SeriesType.Line));
AddChart(plotter);
}

//Data Event Handler: New data arrives here.
public void OnData(TradeBars data)
{
PriceHistory.Add(data[_ticker]);
if (!PriceHistory.IsReady) return;

// specify option settings here:
double price = (double)PriceHistory[0].Close;
double strike = 110; // strike price of option
double rate = 0.05; // risk-free rate of return to use in calculation
// recalculate annualised time to maturity
double maturity = (BlackScholes.CountWeekDays(data.Time,_maturityDate)/250);
double[] _p_history = new double[PriceHistory.Count];
for (int i = 0; i < PriceHistory.Count; i++)
// copy close (you can change this to your liking)
_p_history[i] = (double)PriceHistory[i].Close;
// approximate volatility with historical volatility of the underlying
double volatility = BlackScholes.HistoricalVolatility(_p_history);
double yield = 0.01; // approximation to the annualised dividend yields(%) for AAPL

// calculate Black-Scholes option value for a European PUT (also approximation to American PUT)
double _optionPrice = BlackScholes.blsPut(price, strike, rate, maturity, volatility, yield);

if (data.Time >= StartDate) {
Plot("DerivativePlot", "Price", price);
Plot("DerivativePlot", "Put price", _optionPrice);
}

// put your actual trading logic here:
if (!Portfolio.HoldStock) {
Order(_ticker, -100);
}
}
}


For this to work, you also need to add the class BlackScholes which I've put together. Simply clone the project below and you're ready to go.

To see the resulting plot of both the stock price and the put option price, clone the algorithm and backtest it. Then on the right side you can click on the plot DerivativePlot. This will show the plot of both the stock and derivative price.

I hope it is of interest to some :)
Update Backtest






The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



Just a little detail: calculating the estimate of the historical volatility works best if you use the daily closes of the last two weeks of the underlying. Currently the code uses the last 14 periods of the resolution that you use. So when using the minute resolution, the volatility estimate is based on the last 14 minutes and therefore likely to be inaccurate. But you can easily change the code to use the last 14 daily closes instead (or actually 10: two weeks in working days). This is true for other parts of the code as well: you can improve estimates to increase the Black-Scholes precision.
0

Great share, it's super cool seeing option pricing charts in QC!
1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.



You samples are truly needed!
Thank you JP B !
1

Really great to see the formula for calculating options pricing. Great share!
1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed