- MQL5 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
GKPrice returned spot intrinsic whenever sigma was non-positive. With time still to run the payoff is deterministic on the forward, not the spot, so the value is that intrinsic discounted at the domestic rate, which is also the sigma -> 0 limit of the formula. The old branch left a step discontinuity at the boundary and understated a 30-day EURUSD call struck at spot by about 16 pips. GKPrice also now guards non-positive S and K, as every other function in the header already did. CGKProviderCSV::Load validates each row before accepting it. StringToDouble returns zero for anything it cannot parse, so a garbled or column-shifted row previously loaded as a tidy set of zeros and reconstructed into a plausible-looking smile; days, spot and ATM cannot legitimately be zero, and a row failing that is now named in the log and dropped. GKDeriveDeltaQuotes no longer describes itself as the exact inverse of the reconstruction. Aggregating a listed chain into five delta-space numbers is lossy by construction, so it runs the reconstruction backwards without being invertible. README records the cross-check against the independent Python reference, the measured CDF error, and what the build does not model. |
||
| Files/GK | ||
| Include/GK | ||
| Indicators/GK | ||
| README.md | ||
GK FX Smile
The FX volatility smile, reconstructed in MQL5 from the three quotes the market actually publishes, priced with Garman-Kohlhagen.
Companion code for the MQL5 article: https://www.mql5.com/en/articles/23807
What it does
FX options are not quoted the way equity options are. The market quotes in delta space, not strikes, and the core set is three numbers: at-the-money volatility, a risk reversal, and a butterfly. Turning those into a smile is the work. Most desks also publish 10-delta wings, which the sample sheets carry as optional columns.
Garman-Kohlhagen is Black-Scholes with two interest rates, one per currency, which is what a currency pair needs. On top of that sits the part that catches people out: delta is a convention. Spot or forward, premium-adjusted or not, and the choice changes which strike a quoted delta refers to. The article works through the conventions rather than picking one silently.
From those quotes the smile is reconstructed across strikes, and GKSmile.mq5
draws it. The feed can be a CSV or native MetaTrader 5 options. Be clear on
what comes back: the reconstruction is exact at the pillars and a
piecewise-linear interpolation everywhere else. Three or five quotes pin
points, not a curve, and a fuller model such as SABR or Vanna-Volga is what
would fill the gaps.
The engine is checked against an independent Python reference built from the
formulas rather than translated from the header. Across two sample sheets,
five tenors and all four delta conventions, 200 reconstructed pillars agree
bit for bit once both sides use the same normal CDF, which places the entire
residual on that approximation and nothing else. Measured directly against
math.erfc over a fine grid, the Abramowitz and Stegun CDF in the header is
worst by 7.45e-8, matching its published bound.
What it does not model is worth stating. Rates are one flat pair per tenor rather than a bootstrapped curve. Time is calendar days over 365 throughout, with the same value reused for the discount factors, and settlement lags, the spot date and the expiry cut are absent. There is no arbitrage check, so five pillars joined by straight lines can produce a call-price curve that is not convex in the strike. The butterfly is read in its simplified form; a market-strangle quote needs an extra calibration step first. The native provider summarises a listed chain into delta-space quotes, which is lossy by construction and therefore not an inverse of the reconstruction.
Layout
Include/GK/GarmanKohlhagen.mqh two-rate pricing and greeks
Include/GK/GKData.mqh quote handling and smile reconstruction
Include/GK/GKProviderNative.mqh native MT5 options feed
Indicators/GK/GKSmile.mq5 the smile indicator
Files/GK/EURUSD.csv sample quotes
Files/GK/USDJPY.csv sample quotes
Start with GKSmile.mq5 on any chart, left on its CSV source: the sample
sheets let it run without a live options feed. The one input that matters is
the delta convention, which is a property of the pair and of whoever supplies
your quotes, not a preference. EURUSD wants the unadjusted spot delta, USDJPY
the premium-adjusted one; getting it wrong raises no error and simply places
every pillar on a slightly different strike. To use your own marks, replace
the CSV rows, keeping volatilities in percent and rates as continuously
compounded decimals, since a money-market deposit quote needs converting
first.
Disclaimer
Educational code. The sample volatility sheets carry illustrative quotes, not live market data. This is a reconstruction tool, not a pricing library: the checks above bound its arithmetic, and say nothing about whether the conventions and rates you feed it are the ones your counterparty uses. Test on your own data and broker conditions before drawing conclusions.