Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
blackdeltautilities.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/experimental/fx/blackdeltacalculator.hpp>
22
23namespace QuantExt {
24
25Real getStrikeFromDelta(Option::Type optionType, Real delta, DeltaVolQuote::DeltaType dt, Real spot, Real domDiscount,
26 Real forDiscount, QuantLib::ext::shared_ptr<BlackVolTermStructure> vol, Real t, Real accuracy,
27 Size maxIterations) {
28 Real forward = spot / domDiscount * forDiscount;
29 Real result = forward, lastResult;
30 Size iterations = 0;
31 do {
32 Real stddev = std::sqrt(vol->blackVariance(t, result));
33 try {
34 BlackDeltaCalculator bdc(optionType, dt, spot, domDiscount, forDiscount, stddev);
35 lastResult = result;
36 result = bdc.strikeFromDelta(delta);
37 } catch (const std::exception& e) {
38 QL_FAIL("getStrikeFromDelta("
39 << (optionType == Option::Call ? 1.0 : -1.0) * delta << ") could not be computed for spot=" << spot
40 << ", forward=" << spot / domDiscount * forDiscount << " (domRate=" << -std::log(domDiscount) / t
41 << ", forRate=" << -std::log(forDiscount) / t << "), vol=" << stddev / std::sqrt(t)
42 << ", expiry=" << t << ": " << e.what());
43 }
44 } while (std::abs((result - lastResult) / lastResult) > accuracy && ++iterations < maxIterations);
45 QL_REQUIRE(iterations < maxIterations, "getStrikeFromDelta: max iterations ("
46 << maxIterations << "), no solution found for accuracy " << accuracy
47 << ", last iterations: " << lastResult << "/" << result
48 << ", spot=" << spot << ", forward=" << spot / domDiscount * forDiscount
49 << " (domRate=" << -std::log(domDiscount) / t
50 << ", forRate=" << -std::log(forDiscount) / t << "), expiry=" << t);
51
52 return result;
53}
54
55Real getAtmStrike(DeltaVolQuote::DeltaType dt, DeltaVolQuote::AtmType at, Real spot, Real domDiscount, Real forDiscount,
56 QuantLib::ext::shared_ptr<BlackVolTermStructure> vol, Real t, Real accuracy, Size maxIterations) {
57 Real forward = spot / domDiscount * forDiscount;
58 Real result = forward, lastResult;
59 Size iterations = 0;
60 do {
61 Real stddev = std::sqrt(vol->blackVariance(t, result));
62 try {
63 BlackDeltaCalculator bdc(Option::Call, dt, spot, domDiscount, forDiscount, stddev);
64 lastResult = result;
65 result = bdc.atmStrike(at);
66 } catch (const std::exception& e) {
67 QL_FAIL("getAtmStrike() could not be computed for spot="
68 << spot << ", forward=" << spot / domDiscount * forDiscount
69 << " (domRate=" << -std::log(domDiscount) / t << ", forRate=" << -std::log(forDiscount) / t
70 << "), vol=" << stddev / std::sqrt(t) << ", expiry=" << t << ": " << e.what());
71 }
72 } while (std::abs((result - lastResult) / lastResult) > accuracy && ++iterations < maxIterations);
73 QL_REQUIRE(iterations < maxIterations, "getAtmStrike: max iterations ("
74 << maxIterations << "), no solution found for accuracy " << accuracy
75 << ", last iterations: " << lastResult << "/" << result
76 << ", spot=" << spot << ", forward=" << spot / domDiscount * forDiscount
77 << " (domRate=" << -std::log(domDiscount) / t
78 << ", forRate=" << -std::log(forDiscount) / t << "), expiry=" << t);
79 return result;
80}
81
82} // namespace QuantExt
utilities to calculate strikes from deltas and atm strikes on smiles
Real getStrikeFromDelta(Option::Type optionType, Real delta, DeltaVolQuote::DeltaType dt, Real spot, Real domDiscount, Real forDiscount, QuantLib::ext::shared_ptr< BlackVolTermStructure > vol, Real t, Real accuracy, Size maxIterations)
Real getAtmStrike(DeltaVolQuote::DeltaType dt, DeltaVolQuote::AtmType at, Real spot, Real domDiscount, Real forDiscount, QuantLib::ext::shared_ptr< BlackVolTermStructure > vol, Real t, Real accuracy, Size maxIterations)