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 :)

Author