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.
/*
* 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);
}
}
}
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can
continue your Boot Camp training progress from the terminal. We
hope to see you in the community soon!