/
QLOptionPriceModel.cs
282 lines (243 loc) · 14.1 KB
/
QLOptionPriceModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QLNet;
namespace QuantConnect.Securities.Option
{
using Logging;
using PricingEngineFunc = Func<GeneralizedBlackScholesProcess, IPricingEngine>;
using PricingEngineFuncEx = Func<Symbol, GeneralizedBlackScholesProcess, IPricingEngine>;
/// <summary>
/// Provides QuantLib(QL) implementation of <see cref="IOptionPriceModel"/> to support major option pricing models, available in QL.
/// </summary>
public class QLOptionPriceModel : IOptionPriceModel
{
private static readonly OptionStyle[] _defaultAllowedOptionStyles = new[] { OptionStyle.European, OptionStyle.American };
private readonly IQLUnderlyingVolatilityEstimator _underlyingVolEstimator;
private readonly IQLRiskFreeRateEstimator _riskFreeRateEstimator;
private readonly IQLDividendYieldEstimator _dividendYieldEstimator;
private readonly PricingEngineFuncEx _pricingEngineFunc;
/// <summary>
/// When enabled, approximates Greeks if corresponding pricing model didn't calculate exact numbers.
/// The default value is true.
/// </summary>
public bool EnableGreekApproximation { get; set; } = true;
/// <summary>
/// True if volatility model is warmed up, i.e. has generated volatility value different from zero, otherwise false.
/// </summary>
public bool VolatilityEstimatorWarmedUp => _underlyingVolEstimator.IsReady;
/// <summary>
/// List of option styles supported by the pricing model.
/// By default, both American and European option styles are supported.
/// </summary>
public OptionStyle[] AllowedOptionStyles { get; }
/// <summary>
/// Method constructs QuantLib option price model with necessary estimators of underlying volatility, risk free rate, and underlying dividend yield
/// </summary>
/// <param name="pricingEngineFunc">Function modeled stochastic process, and returns new pricing engine to run calculations for that option</param>
/// <param name="underlyingVolEstimator">The underlying volatility estimator</param>
/// <param name="riskFreeRateEstimator">The risk free rate estimator</param>
/// <param name="dividendYieldEstimator">The underlying dividend yield estimator</param>
/// <param name="allowedOptionStyles">List of option styles supported by the pricing model. It defaults to both American and European option styles</param>
public QLOptionPriceModel(PricingEngineFunc pricingEngineFunc, IQLUnderlyingVolatilityEstimator underlyingVolEstimator, IQLRiskFreeRateEstimator riskFreeRateEstimator, IQLDividendYieldEstimator dividendYieldEstimator, OptionStyle[] allowedOptionStyles = null)
: this((option, process) => pricingEngineFunc(process), underlyingVolEstimator, riskFreeRateEstimator, dividendYieldEstimator, allowedOptionStyles)
{}
/// <summary>
/// Method constructs QuantLib option price model with necessary estimators of underlying volatility, risk free rate, and underlying dividend yield
/// </summary>
/// <param name="pricingEngineFunc">Function takes option and modeled stochastic process, and returns new pricing engine to run calculations for that option</param>
/// <param name="underlyingVolEstimator">The underlying volatility estimator</param>
/// <param name="riskFreeRateEstimator">The risk free rate estimator</param>
/// <param name="dividendYieldEstimator">The underlying dividend yield estimator</param>
/// <param name="allowedOptionStyles">List of option styles supported by the pricing model. It defaults to both American and European option styles</param>
public QLOptionPriceModel(PricingEngineFuncEx pricingEngineFunc, IQLUnderlyingVolatilityEstimator underlyingVolEstimator, IQLRiskFreeRateEstimator riskFreeRateEstimator, IQLDividendYieldEstimator dividendYieldEstimator, OptionStyle[] allowedOptionStyles = null)
{
_pricingEngineFunc = pricingEngineFunc;
_underlyingVolEstimator = underlyingVolEstimator ?? new ConstantQLUnderlyingVolatilityEstimator();
_riskFreeRateEstimator = riskFreeRateEstimator ?? new ConstantQLRiskFreeRateEstimator();
_dividendYieldEstimator = dividendYieldEstimator ?? new ConstantQLDividendYieldEstimator();
AllowedOptionStyles = allowedOptionStyles ?? _defaultAllowedOptionStyles;
}
/// <summary>
/// Evaluates the specified option contract to compute a theoretical price, IV and greeks
/// </summary>
/// <param name="security">The option security object</param>
/// <param name="slice">The current data slice. This can be used to access other information
/// available to the algorithm</param>
/// <param name="contract">The option contract to evaluate</param>
/// <returns>An instance of <see cref="OptionPriceModelResult"/> containing the theoretical
/// price of the specified option contract</returns>
public OptionPriceModelResult Evaluate(Security security, Slice slice, OptionContract contract)
{
if (!AllowedOptionStyles.Contains(contract.Symbol.ID.OptionStyle))
{
throw new ArgumentException($"{contract.Symbol.ID.OptionStyle} style options are not supported by option price model '{this.GetType().Name}'");
}
try
{
// expired options has no price
if (contract.Time > contract.Expiry)
{
return OptionPriceModelResult.None;
}
// setting up option pricing parameters
var calendar = new UnitedStates();
var dayCounter = new Actual365Fixed();
var optionSecurity = (Option)security;
var settlementDate = contract.Time.Date.AddDays(Option.DefaultSettlementDays);
var maturityDate = contract.Expiry.Date.AddDays(Option.DefaultSettlementDays);
var underlyingQuoteValue = new SimpleQuote((double)optionSecurity.Underlying.Price);
var dividendYieldValue = new SimpleQuote(_dividendYieldEstimator.Estimate(security, slice, contract));
var dividendYield = new Handle<YieldTermStructure>(new FlatForward(0, calendar, dividendYieldValue, dayCounter));
var riskFreeRateValue = new SimpleQuote(_riskFreeRateEstimator.Estimate(security, slice, contract));
var riskFreeRate = new Handle<YieldTermStructure>(new FlatForward(0, calendar, riskFreeRateValue, dayCounter));
var underlyingVolValue = new SimpleQuote(_underlyingVolEstimator.Estimate(security, slice, contract));
var underlyingVol = new Handle<BlackVolTermStructure>(new BlackConstantVol(0, calendar, new Handle<Quote>(underlyingVolValue), dayCounter));
if (!_underlyingVolEstimator.IsReady)
{
return OptionPriceModelResult.None;
}
// preparing stochastic process and payoff functions
var stochasticProcess = new BlackScholesMertonProcess(new Handle<Quote>(underlyingQuoteValue), dividendYield, riskFreeRate, underlyingVol);
var payoff = new PlainVanillaPayoff(contract.Right == OptionRight.Call ? QLNet.Option.Type.Call : QLNet.Option.Type.Put, (double)contract.Strike);
// creating option QL object
var option = contract.Symbol.ID.OptionStyle == OptionStyle.American ?
new VanillaOption(payoff, new AmericanExercise(settlementDate, maturityDate)) :
new VanillaOption(payoff, new EuropeanExercise(maturityDate));
Settings.setEvaluationDate(settlementDate);
// preparing pricing engine QL object
option.setPricingEngine(_pricingEngineFunc(contract.Symbol, stochasticProcess));
// running calculations
// can return negative value in neighbourhood of 0
var npv = Math.Max(0, EvaluateOption(option));
// function extracts QL greeks catching exception if greek is not generated by the pricing engine and reevaluates option to get numerical estimate of the seisitivity
Func<Func<double>, Func<double>, decimal> tryGetGreekOrReevaluate = (greek, reevalFunc) =>
{
try
{
return (decimal)greek();
}
catch (Exception)
{
return EnableGreekApproximation ? (decimal)reevalFunc() : 0.0m;
}
};
// function extracts QL greeks catching exception if greek is not generated by the pricing engine
Func<Func<double>, decimal> tryGetGreek = greek => tryGetGreekOrReevaluate(greek, () => 0.0);
// function extracts QL IV catching exception if IV is not generated by the pricing engine
Func<decimal> tryGetImpliedVol = () =>
{
try
{
return (decimal)option.impliedVolatility((double)optionSecurity.Price, stochasticProcess);
}
catch (Exception err)
{
Log.Debug($"tryGetImpliedVol() error: {err.Message}");
return 0m;
}
};
Func<Tuple<decimal, decimal>> evalDeltaGamma = () =>
{
try
{
return Tuple.Create((decimal)option.delta(), (decimal)option.gamma());
}
catch (Exception)
{
if (EnableGreekApproximation)
{
var step = 0.01;
var initial = underlyingQuoteValue.value();
underlyingQuoteValue.setValue(initial - step);
var npvMinus = EvaluateOption(option);
underlyingQuoteValue.setValue(initial + step);
var npvPlus = EvaluateOption(option);
underlyingQuoteValue.setValue(initial);
return Tuple.Create((decimal)((npvPlus - npvMinus) / (2 * step)),
(decimal)((npvPlus - 2 * npv + npvMinus) / (step * step)));
}
else
return Tuple.Create(0.0m, 0.0m);
}
};
Func<double> reevalVega = () =>
{
var step = 0.001;
var initial = underlyingVolValue.value();
underlyingVolValue.setValue(initial + step);
var npvPlus = EvaluateOption(option);
underlyingVolValue.setValue(initial);
return (npvPlus - npv) / step;
};
Func<double> reevalTheta = () =>
{
var step = 1.0 / 365.0;
Settings.setEvaluationDate(settlementDate.AddDays(-1));
var npvMinus = EvaluateOption(option);
Settings.setEvaluationDate(settlementDate);
return (npv - npvMinus) / step;
};
Func<double> reevalRho = () =>
{
var step = 0.001;
var initial = riskFreeRateValue.value();
riskFreeRateValue.setValue(initial + step);
var npvPlus = EvaluateOption(option);
riskFreeRateValue.setValue(initial);
return (npvPlus - npv) / step;
};
// producing output with lazy calculations of IV and greeks
return new OptionPriceModelResult((decimal)npv,
tryGetImpliedVol,
() => new Greeks(evalDeltaGamma,
() => tryGetGreekOrReevaluate(() => option.vega(), reevalVega),
() => tryGetGreekOrReevaluate(() => option.theta(), reevalTheta),
() => tryGetGreekOrReevaluate(() => option.rho(), reevalRho),
() => tryGetGreek(() => option.elasticity())));
}
catch (Exception err)
{
Log.Debug($"QLOptionPriceModel.Evaluate() error: {err.Message}");
return OptionPriceModelResult.None;
}
}
/// <summary>
/// Runs option evaluation and logs exceptions
/// </summary>
/// <param name="option"></param>
/// <returns></returns>
private static double EvaluateOption(VanillaOption option)
{
try
{
var npv = option.NPV();
if (double.IsNaN(npv) ||
double.IsInfinity(npv))
npv = 0.0;
return npv;
}
catch (Exception err)
{
Log.Debug($"QLOptionPriceModel.EvaluateOption() error: {err.Message}");
return 0.0;
}
}
}
}