<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>blog</title>
<link>https://your-website-url.example.com/</link>
<atom:link href="https://your-website-url.example.com/index.xml" rel="self" type="application/rss+xml"/>
<description>A blog built with Quarto</description>
<generator>quarto-1.8.27</generator>
<lastBuildDate>Wed, 11 Feb 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Hedging a book of exotic options with different maturities</title>
  <link>https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol.html</link>
  <description><![CDATA[ 





<p>In this notebook, we continue on the track to study the track of hedging in the world of FX option still from a trader sitting in a exotic desk in a investment bank. As we’ve seen in the previous notebooks (<a href="../../posts/post-with-code/exotic-book.html">Hedging a book of exotics options</a>) we have cover the basics on how to hedge a book of FX exotics, both in the case where we are hedging under the Black-Scholes model and in the case where we have a volatility smile and hedge options with the same expiration using the VV method (<a href="../../posts/post-with-code/exotic-book-vanna-volga.html">Hedging a book of exotics options using the Vanna-Volga</a>), in this notebook we are going to take a even further step since we are going to leverage the whole IV surface to hedge our portfolio made of different options with different strikes and expirations. In this case, I’ve decided to leverage the local volatility model and price using the Finite Difference method. The assets that we are considering are thus under the following dynamics:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0AdS_t%20&amp;=%20(r_d%20-%20r_f)%20S_t%20%5C,%20dt%20+%20%5Csigma(S,%20t)%20S_t%20%5C,%20dW_t%5Ed,%20%5C%5C%0AdB%5Ed_t%20&amp;=%20r_d%20B%5Ed_t%5C,%20dt,%20%5C%5C%0AdB%5Ef_t%20&amp;=%20r_f%20B%5Ef_t%5C,%20dt%0A%5Cend%7Balign*%7D%0A"></p>
<section id="market-data" class="level1">
<h1>Market Data</h1>
<p>Let’s assume that we are an USD based desk and we are dealing with EURUSD exotics. The initial market data at time <img src="https://latex.codecogs.com/png.latex?t%20=%200"> is the following</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20S_0%20&amp;=%201.18%20%5C%5C%0A%20%20%20%20Date(Today)%20&amp;=%2012/12/2025%20%5C%5C%0A%20%20%20%20r_%7BUSD%7D%20&amp;=%200.03%20%5C%5C%0A%20%20%20%20r_%7BEUR%7D%20&amp;=%200.02%20%5C%5C%0A%5Cend%7Balign*%7D%0A"></p>
<p>With the following Implied Vol surface</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?10%5CDelta%20p"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?25%5CDelta%20p"></th>
<th style="text-align: left;">ATM</th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?25%5CDelta%20c"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?10%5CDelta%20c"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><strong>1W</strong></td>
<td style="text-align: left;">13.50%</td>
<td style="text-align: left;">12.00%</td>
<td style="text-align: left;">9.50%</td>
<td style="text-align: left;">8.50%</td>
<td style="text-align: left;">9.50%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>1M</strong></td>
<td style="text-align: left;">13.00%</td>
<td style="text-align: left;">11.50%</td>
<td style="text-align: left;">9.00%</td>
<td style="text-align: left;">8.00%</td>
<td style="text-align: left;">9.00%</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>3M</strong></td>
<td style="text-align: left;">12.50%</td>
<td style="text-align: left;">11.00%</td>
<td style="text-align: left;">9.50%</td>
<td style="text-align: left;">8.50%</td>
<td style="text-align: left;">9.50%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>1Y</strong></td>
<td style="text-align: left;">12.00%</td>
<td style="text-align: left;">10.50%</td>
<td style="text-align: left;">10.00%</td>
<td style="text-align: left;">9.50%</td>
<td style="text-align: left;">10.50%</td>
</tr>
</tbody>
</table>
<p>For the sake of simplicity we consider that the interest rate term structures for the USD and EUR are both flat</p>
<blockquote class="blockquote">
<p>Note in the FX option world, IVs are quoted as a delta-vol pairs for each maturity.</p>
</blockquote>
<div id="3cba25b6" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">EUR_USD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.18</span></span>
<span id="cb1-2">today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, ql.December, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>)</span>
<span id="cb1-3">ref_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today</span>
<span id="cb1-4">calendar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.NullCalendar()</span>
<span id="cb1-5">dc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Actual365Fixed()</span>
<span id="cb1-6">r_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0325</span></span>
<span id="cb1-7">r_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span></span>
<span id="cb1-8"></span>
<span id="cb1-9">ql.Settings.instance().evaluationDate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today</span></code></pre></div></div>
</div>
<div id="e31befb0" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">spot_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(EUR_USD)</span>
<span id="cb2-2">spot_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(spot_quote)</span>
<span id="cb2-3"></span>
<span id="cb2-4">deltas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>]</span>
<span id="cb2-5">option_types <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Option.Put, ql.Option.Put, ql.Option.Call, ql.Option.Call, ql.Option.Call]</span>
<span id="cb2-6">maturity_1y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calendar.advance(today, ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ql.Years))</span>
<span id="cb2-7">maturity_3m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calendar.advance(today, ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, ql.Months))</span>
<span id="cb2-8">maturity_6m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calendar.advance(today, ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, ql.Months))</span>
<span id="cb2-9">maturity_1m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calendar.advance(today, ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ql.Months))</span>
<span id="cb2-10">maturity_1w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calendar.advance(today, ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ql.Weeks))</span>
<span id="cb2-11">maturities <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb2-12">    maturity_1w,</span>
<span id="cb2-13">    maturity_1m,</span>
<span id="cb2-14">    maturity_3m,</span>
<span id="cb2-15">    maturity_1y</span>
<span id="cb2-16">]</span>
<span id="cb2-17">times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [dc.yearFraction(today, m) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> maturities]</span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Vol surface</span></span>
<span id="cb2-19">ivs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb2-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.10P, 0.25P, ATM,  0.25C, 0.10C</span></span>
<span id="cb2-21">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.135</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.095</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.090</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.095</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1W </span></span>
<span id="cb2-22">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.130</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.115</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.090</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.080</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.090</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1M</span></span>
<span id="cb2-23">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.125</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.110</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.095</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.085</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.095</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3M</span></span>
<span id="cb2-24">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.120</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.105</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.095</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.105</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1Y</span></span>
<span id="cb2-25">])</span></code></pre></div></div>
</div>
<div id="9fa6180a" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rates TS</span></span>
<span id="cb3-2">domestic_ts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(ql.FlatForward(today, r_d, dc))</span>
<span id="cb3-3">foreign_ts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(ql.FlatForward(today, r_f, dc))</span></code></pre></div></div>
</div>
<div id="30d25e89" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Discount curves</span></span>
<span id="cb4-2">eur_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span>)]</span>
<span id="cb4-3">eur_dfs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>]</span>
<span id="cb4-4">eur_curve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DiscountCurve(eur_dates, eur_dfs, dc)</span>
<span id="cb4-5">eur_curve.enableExtrapolation()</span>
<span id="cb4-6">eur_disc_ts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(eur_curve)</span>
<span id="cb4-7"></span>
<span id="cb4-8">usd_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span>)]</span>
<span id="cb4-9">usd_dfs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.985</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.96</span>]</span>
<span id="cb4-10">usd_curve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DiscountCurve(usd_dates, usd_dfs, dc)</span>
<span id="cb4-11">usd_curve.enableExtrapolation()</span>
<span id="cb4-12">usd_disc_ts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(usd_curve)</span></code></pre></div></div>
</div>
<p>Let’s also suppose that the following discount curve:</p>
<div id="edc62631" class="cell" data-execution_count="181">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol_files/figure-html/cell-12-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Since we have a sticky delta volatility surface, we need to turn the surface into a sticky strike volatility surface in order to make <code>QuantLib</code> make use of those IVs when pricing the exotics. To do that let’s use the <code>create_strike_iv_surf</code> helper function defined above. <code>create_strike_iv_surf</code> uses the <code>ql.BlackDeltaCalculator</code> class which allows us to get the strike from the delta-vol quote according the specific delta quote convetion that we are using.</p>
<div id="12be16fe" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">strikes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> create_strike_iv_surf(EUR_USD, usd_curve, eur_curve, deltas, option_types, times, ivs)</span></code></pre></div></div>
</div>
<p>The corresponding strikes for each <img src="https://latex.codecogs.com/png.latex?(%5CDelta,T)"> tuple are:</p>
<div id="a18bf630" class="cell" data-execution_count="183">
<div class="cell-output cell-output-display" data-execution_count="183">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">10D P</th>
<th data-quarto-table-cell-role="th">25D P</th>
<th data-quarto-table-cell-role="th">ATM</th>
<th data-quarto-table-cell-role="th">25D C</th>
<th data-quarto-table-cell-role="th">10D C</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">1W</th>
<td>1.152382</td>
<td>1.167127</td>
<td>1.180212</td>
<td>1.190167</td>
<td>1.200282</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1M</th>
<td>1.125401</td>
<td>1.154811</td>
<td>1.180866</td>
<td>1.199518</td>
<td>1.221262</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">3M</th>
<td>1.093388</td>
<td>1.140628</td>
<td>1.182536</td>
<td>1.216533</td>
<td>1.256383</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1Y</th>
<td>1.025369</td>
<td>1.112431</td>
<td>1.189692</td>
<td>1.268784</td>
<td>1.363115</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Since, now we have a set of IV with different strikes for each expiration, we need to find a way to build the IV surface using this data. One efficient and common way to do that is using the Andersen-Huge interpolation <span class="citation" data-cites="vol-interpol">(see Andreasen and Huge 2010)</span> method, which allows us to achieve this goal. In a nuthsell, the Andreasen and Huge have introduced an efficient and arbitrage free volatility interpolation method based on a one step finite difference implicit Euler scheme applied to a local volatility parametrization. Probably the most notable use case is the generation of a local volatility surface from a set of option quotes.</p>
<p>QuantLib provides us with a series of classes that can help us to build both the IV and the local surface from the IV data, using the Andersen-Huge method. First and foremost, we need to build a calibration set, which is basically a list of Tuple made by the option and the iv relative to that option <code>(option, ivs_quote)</code>, and then we can create an instance of the <code>ql.AndersenHugeVolatilityInterpl</code> with the calibration set created, that is the class that is going to handle the interpolation. Then we need to create the relative Implied vol and Local vol adapter the <code>ql.AndersenHugeVolatilityAdapter</code> and the <code>ql.AndersenHugeLocalAdapter</code> respectively. Those are the instances that resprents the BlackVarianceTermStructure and the LocalVolTermStructure that we need to further wrap into a <code>RelinkableHandle</code>.</p>
<p>Why a <code>RenilableHandle</code> you might ask? Well it’s because it will come in handyh to us when calculating the sensitivities, rembember a <code>RelinkableHandle</code> can be link to another term structure after it is first created, unlike a plain handle.</p>
<div id="a3ff9c3f" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calibration of vol surfaces</span></span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> calibrate_vol_surface(strikes: Union[List[List[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>]], np.ndarray], </span>
<span id="cb6-3">                          options: List[ql.Option],</span>
<span id="cb6-4">                          maturities: List[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>], </span>
<span id="cb6-5">                          ivs: np.array</span>
<span id="cb6-6">                        ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Tuple[ql.BlackVolTermStructure, ql.LocalVolTermStructure]:</span>
<span id="cb6-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Calibrate the given ivs quotes and build a BlackVolTermStructure and a LocalVolTermStructure</span></span>
<span id="cb6-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Parameters:</span></span>
<span id="cb6-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        strikes (Union[List[List[float]], np.ndarray]): the strikes of the volatility surface</span></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        options (List[ql.Option]): the types of option a specific strike is referring to</span></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        maturities (List[float]): list of maturieties of the vol surface</span></span>
<span id="cb6-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        ivs (np.ndarray): the iv surface data</span></span>
<span id="cb6-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Return:</span></span>
<span id="cb6-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        ql.Tuple[ql.BlackVolTermStructure, ql.LocalVolTermStructure]: tuple contaning the calibrate black vol term structure </span></span>
<span id="cb6-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                                                                    and the local vol term structure</span></span>
<span id="cb6-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb6-20">    calibration_set <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.CalibrationSet()</span>
<span id="cb6-21"></span>
<span id="cb6-22">    n, m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(maturities), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(options)</span>
<span id="cb6-23"></span>
<span id="cb6-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n):</span>
<span id="cb6-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(m):</span>
<span id="cb6-26">            payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.PlainVanillaPayoff(options[i], strikes[i][j])</span>
<span id="cb6-27">            maturity <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(maturities[i])</span>
<span id="cb6-28"></span>
<span id="cb6-29">            calibration_set.push_back((ql.VanillaOption(payoff, maturity), ql.SimpleQuote(ivs[i][j])))</span>
<span id="cb6-30"></span>
<span id="cb6-31">    ah_interpolation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.AndreasenHugeVolatilityInterpl(calibration_set, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb6-32">                            spot_handle, domestic_ts, foreign_ts)</span>
<span id="cb6-33">    ah_vol_surf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.AndreasenHugeVolatilityAdapter(ah_interpolation) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># IV surface</span></span>
<span id="cb6-34">    ah_local_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.AndreasenHugeLocalVolAdapter(ah_interpolation) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LV surface</span></span>
<span id="cb6-35"></span>
<span id="cb6-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ah_vol_surf, ah_local_vol</span>
<span id="cb6-37"></span>
<span id="cb6-38">ah_vol_surf, ah_local_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calibrate_vol_surface(strikes, option_types, maturities, ivs)</span>
<span id="cb6-39">black_vol_surf_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.RelinkableBlackVolTermStructureHandle(ah_vol_surf)</span>
<span id="cb6-40">local_vol_surf_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.RelinkableLocalVolTermStructureHandle(ah_local_vol)</span></code></pre></div></div>
</div>
<p>Now that we have constructed the vol surfaces, let’s now visualized how they look:</p>
<div id="820268ae" class="cell" data-execution_count="185">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol_files/figure-html/cell-16-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Just a reminder <img src="https://latex.codecogs.com/png.latex?IV_%7B%5Ctext%7Bsurface%7D%7D%20%5Cne%20LV_%7B%5Ctext%7Bsurface%7D%7D">, they are related but they are not the same thing. IVs represent a sort of estimate of the average future volatility of the underlying during the option’s lifetime. In a way, IVs is a “global” measure of volatility, in contrast to the local volatility <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7BLV%7D"> at any spot price and time.</p>
<p>Since we are using the local vol model, one way to price option is by using the Finite Difference method. QuantLib provides us with the <code>FdBlackScholesBarrierEngine</code> class to achieve that, keep in mind that we need to set <code>useLocalVol=True</code>, thus we can use the local volatility function when running the pricing engine.</p>
<div id="a6adddb4" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">engine <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.FdBlackScholesBarrierEngine(</span>
<span id="cb7-2">    process,</span>
<span id="cb7-3">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>,</span>
<span id="cb7-4">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>,</span>
<span id="cb7-5">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb7-6">    ql.FdmSchemeDesc.Douglas(),</span>
<span id="cb7-7">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># localVol has to be True</span></span>
<span id="cb7-8">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span></code></pre></div></div>
</div>
<section id="exotics-book" class="level2">
<h2 class="anchored" data-anchor-id="exotics-book">Exotics Book</h2>
<p>Here’s how the trader book is composed of:</p>
<p>1st Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20T%20&amp;=%201.0%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.30%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="6799165d" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 1</span></span>
<span id="cb8-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb8-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.30</span></span>
<span id="cb8-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpOut</span>
<span id="cb8-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(maturity_1y)</span>
<span id="cb8-6">strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strike_from_delta(</span>
<span id="cb8-7">    EUR_USD,</span>
<span id="cb8-8">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb8-9">    times[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb8-10">    ql.Option.Call,</span>
<span id="cb8-11">    ivs[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb8-12">    usd_curve,</span>
<span id="cb8-13">    eur_curve</span>
<span id="cb8-14">)</span>
<span id="cb8-15"></span>
<span id="cb8-16">barrier_25d_130b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(strike, exercise, barrier, barrier_type)</span>
<span id="cb8-17">barrier_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_130b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>2nd Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$2000000%20%5C%5C%0A%20%20%20%20T%20&amp;=%200.5%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.08%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BDown%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="04b66cca" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 2</span></span>
<span id="cb9-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2_000_000</span></span>
<span id="cb9-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.08</span></span>
<span id="cb9-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.DownOut</span>
<span id="cb9-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(maturity_6m)</span>
<span id="cb9-6">strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strike_from_delta(</span>
<span id="cb9-7">    EUR_USD,</span>
<span id="cb9-8">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb9-9">    dc.yearFraction(today, maturity_6m),</span>
<span id="cb9-10">    ql.Option.Call,</span>
<span id="cb9-11">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.09</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interpolated value</span></span>
<span id="cb9-12">    usd_curve,</span>
<span id="cb9-13">    eur_curve</span>
<span id="cb9-14">)</span>
<span id="cb9-15"></span>
<span id="cb9-16">barrier_25d_108b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(strike, exercise, barrier, barrier_type)</span>
<span id="cb9-17">barrier_2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_108b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>3rd Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20T%20&amp;=%200.25%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.20%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20In%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="8b63faea" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 3</span></span>
<span id="cb10-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb10-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.20</span></span>
<span id="cb10-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpIn</span>
<span id="cb10-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(maturity_3m)</span>
<span id="cb10-6">strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strike_from_delta(</span>
<span id="cb10-7">    EUR_USD,</span>
<span id="cb10-8">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb10-9">    dc.yearFraction(today, maturity_3m),</span>
<span id="cb10-10">    ql.Option.Call,</span>
<span id="cb10-11">    ivs[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb10-12">    usd_curve,</span>
<span id="cb10-13">    eur_curve</span>
<span id="cb10-14">)</span>
<span id="cb10-15">call_vanilla_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxVanillaOptionFactory(spot_handle, maturity_1y, ql.Option.Call)</span>
<span id="cb10-16"></span>
<span id="cb10-17">barrier_25d_120b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(strike, exercise, barrier, barrier_type)</span>
<span id="cb10-18">backup_vanilla <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_vanilla_factory.get_option(strike)</span>
<span id="cb10-19">FxVanillaOptionFactory.assign_analyitcal_price_engine(backup_vanilla, process_25c)</span>
<span id="cb10-20">barrier_3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_120b, notional, backup_vanilla)</span></code></pre></div></div>
</div>
<p>4th Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BPut%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20T%20&amp;=%200.25%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.05%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20p%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BDown%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="d269693c" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 4</span></span>
<span id="cb11-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb11-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span></span>
<span id="cb11-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.DownOut</span>
<span id="cb11-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(maturity_3m)</span>
<span id="cb11-6">strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strike_from_delta(</span>
<span id="cb11-7">    EUR_USD,</span>
<span id="cb11-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb11-9">    dc.yearFraction(today, maturity_3m),</span>
<span id="cb11-10">    ql.Option.Put,</span>
<span id="cb11-11">    ivs[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb11-12">    usd_curve,</span>
<span id="cb11-13">    eur_curve</span>
<span id="cb11-14">)</span>
<span id="cb11-15"></span>
<span id="cb11-16">barrier_25d_105b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> put_barrier_factory.get_option(strike, exercise, barrier, barrier_type)</span>
<span id="cb11-17">barrier_4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_105b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>5th Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BPut%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.25%20%5C%5C%0A%20%20%20%20T%20&amp;=%200.0833%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20p%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="c7b5ce4d" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 5</span></span>
<span id="cb12-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2_000_000</span></span>
<span id="cb12-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span></span>
<span id="cb12-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpOut</span>
<span id="cb12-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(maturity_1m)</span>
<span id="cb12-6">strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strike_from_delta(</span>
<span id="cb12-7">    EUR_USD,</span>
<span id="cb12-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb12-9">    dc.yearFraction(today, maturity_1m),</span>
<span id="cb12-10">    ql.Option.Put,</span>
<span id="cb12-11">    ivs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb12-12">    usd_curve,</span>
<span id="cb12-13">    eur_curve</span>
<span id="cb12-14">)</span>
<span id="cb12-15"></span>
<span id="cb12-16">barrier_25d_105b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> put_barrier_factory.get_option(strike, exercise, barrier, barrier_type)</span>
<span id="cb12-17">barrier_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_105b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<div id="259f3ccc" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">book: List[BarrierPosition] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [barrier_1, barrier_2, barrier_3, barrier_4, barrier_5]</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ins <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book:</span>
<span id="cb13-4">    FxBarrierOptionFactory.assign_price_engine(ins.instrument, engine)</span></code></pre></div></div>
</div>
<p>Now we have setup our book, let’s see what’s the premium on those options.</p>
<div id="2e7047b1" class="cell" data-execution_count="195">
<div class="cell-output cell-output-stdout">
<pre><code>Barrier 1, price $260.39
Barrier 2, price $26721.44
Barrier 3, price $7822.55
Barrier 4, price $4510.61
Barrier 5, price $13603.77</code></pre>
</div>
</div>
<div id="06ea7785" class="cell" data-execution_count="196">
<div class="cell-output cell-output-stdout">
<pre><code>The total book value is ${book_value}</code></pre>
</div>
</div>
</section>
</section>
<section id="bucketed-sensitivities" class="level1">
<h1>Bucketed sensitivities</h1>
<p>When dealing with vol surfaces or smiles, to calculate the sensitivity w.r.t. the volatility does not result trival as when we are dealing with a single vol like in BS model. As shown in the Hedging a book of exotic options with VV method, in case we have a smile we can calculate the sensitivities to the various movements of the volatility smile (paralallel shift, varying the skew, and the curvature), and those ideas can be also applied to the whole vol surface.</p>
<p>Another possible way, to get those vega sensitivities is to calculate how the prices are sensible to each single IV variation, in this way we get the so called bucket sensitivites.</p>
<p>For example we want to know how the price of an exotic is sensible to the variation to the <img src="https://latex.codecogs.com/png.latex?%5Csigma_%7B(0.10%5CDelta%20P,%201M)%7D">. To the get that we can use the forward finte difference formula:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cpartial%20V%7D%7B%5Cpartial%20%5Csigma_%7B(0.10%5CDelta%20P,%201M)%7D%7D%20=%20%5Cfrac%7BV(%5Csigma_%7B(0.10%5CDelta%20P,%201M)%7D%20+%20%5Cvarepsilon,%20%5Ccdot)%20-%20V(%5Csigma_%7B(0.10%5CDelta%20P,%201M)%7D,%20%5Ccdot)%7D%7B%5Cvarepsilon%7D%0A"></p>
<p>By repeating this process for all vols of the IV surface we get a matrix of the bucket sensitivities:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BBucket%20Sensitivities%7D%20=%20%5Cleft%5B%0A%5Cbegin%7Barray%7D%7Bc%7Ccccc%7D%0A%20%20%20%20&amp;%20%5Ctext%7B10$%5CDelta$%20P%7D%20&amp;%20%5Ctext%7B25$%5CDelta$%20P%7D%20&amp;%20%5Ctext%7BATM%7D%20&amp;%20%5Ctext%7B25$%5CDelta$%20C%7D%20&amp;%20%5Ctext%7B10$%5CDelta$%20C%7D%20%5C%5C%0A%20%20%20%20%5Chline%0A%20%20%20%20%5Ctext%7B1W%7D%20%20%20&amp;%20s_%7B11%7D%20&amp;%20s_%7B12%7D%20&amp;%20s_%7B13%7D%20&amp;%20s_%7B14%7D%20&amp;%20s_%7B15%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7B1M%7D%20%20%20&amp;%20s_%7B21%7D%20&amp;%20s_%7B22%7D%20&amp;%20s_%7B23%7D%20&amp;%20s_%7B24%7D%20&amp;%20s_%7B25%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7B3M%7D%20%20&amp;%20s_%7B31%7D%20&amp;%20s_%7B32%7D%20&amp;%20s_%7B33%7D%20&amp;%20s_%7B34%7D%20&amp;%20s_%7B35%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7B1Y%7D%20%20&amp;%20s_%7B41%7D%20&amp;%20s_%7B42%7D%20&amp;%20s_%7B43%7D%20&amp;%20s_%7B44%7D%20%20&amp;%20s_%7B45%7D%20%5C%5C%0A%5Cend%7Barray%7D%0A%5Cright%5D%0A"></p>
<p>The methodology to use to calculate the sensitivities w.r.t. the volatility surface is up the trader/trading desk, on how they want those sensitivities …</p>
<blockquote class="blockquote">
<p>TODO: add risk surface explanation</p>
</blockquote>
<p>To visualized the effect of those single bumps of the single IVs, let’s for example bump by <img src="https://latex.codecogs.com/png.latex?1%5C%25"> the ATM vol with maturity 1 year, and visualized how the IV and LV surfaces are changing.</p>
<div id="9b9f00bd" class="cell" data-execution_count="198">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol_files/figure-html/cell-29-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>The IV surface does change a bit, but where we can see a big changment is in LV surface where a new hump appears at the end of the surface. Let’s now see how the prices of the exotics have changed with this new vol surfaces.</p>
<div id="e4d50989" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">new_premiums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [opt.NPV() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book]</span>
<span id="cb16-2">new_premiums</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="199">
<pre><code>[555.7614339685688,
 31789.433532155144,
 7822.54562563613,
 4510.6110117102935,
 13603.771705059622]</code></pre>
</div>
</div>
<div id="5950367b" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">np.array(premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.array(new_premiums)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="200">
<pre><code>array([ -295.3701985 , -5067.99548249,     0.        ,     0.        ,
           0.        ])</code></pre>
</div>
</div>
<p>As we can see the only options where the price has varied is the 1st barrier, which is the only barrier that has expiration time 1Y. … You might wonder what does bump another vol with shorted maturity, will that affect also the exotics with maturities greater than that maturity? Let’s bump for example by 1bp the ATM, 1M implied vol and see the effects on the other exotics prices.</p>
<div id="891a84a8" class="cell" data-execution_count="202">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol_files/figure-html/cell-33-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="dd51d9b2" class="cell" data-execution_count="203">
<div class="cell-output cell-output-stdout">
<pre><code>Barrier 1, ATM 1M IV bump sensitivity: $ -0.10
Barrier 2, ATM 1M IV bump sensitivity: $ 17.19
Barrier 3, ATM 1M IV bump sensitivity: $ 4.48
Barrier 4, ATM 1M IV bump sensitivity: $ -22.66
Barrier 5, ATM 1M IV bump sensitivity: $ -565.39</code></pre>
</div>
</div>
<p>Now we see how all options in the book have been impacted by this vol bump, that’s because all the options in the book have an expiration after 1Y, thus this will impact their price. Of course Barrier 5 will be the one with more price impact, since it expires in 1 month, but we can see that also in options expirying in 3 months and 6 months there is a price impact.</p>
<p>Let’s apply the process above for all the IVs in the original IV surface and calculate those bucket sensitivities.</p>
<div id="e315fecb" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">bucket_sens <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros_like(ivs)</span>
<span id="cb21-2">delta_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0e-08</span></span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(ivs)):</span>
<span id="cb21-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(ivs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])):</span>
<span id="cb21-6">        ivs[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> delta_vol</span>
<span id="cb21-7"></span>
<span id="cb21-8">        tmp_strikes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> create_strike_iv_surf(EUR_USD, usd_curve, eur_curve, deltas, option_types, times, ivs)</span>
<span id="cb21-9"></span>
<span id="cb21-10">        new_ah_vol_surf, new_ah_local_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calibrate_vol_surface(tmp_strikes, option_types, maturities, ivs)</span>
<span id="cb21-11">        black_vol_surf_handle.linkTo(new_ah_vol_surf)</span>
<span id="cb21-12">        local_vol_surf_handle.linkTo(new_ah_local_vol)</span>
<span id="cb21-13"></span>
<span id="cb21-14">        new_book_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([opt.NPV() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb21-15"></span>
<span id="cb21-16">        bucket_sens[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (new_book_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> book_value) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> delta_vol</span>
<span id="cb21-17"></span>
<span id="cb21-18">        ivs[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> delta_vol</span></code></pre></div></div>
</div>
<div id="fc9a0139" class="cell" data-execution_count="205">
<div class="cell-output cell-output-display" data-execution_count="205">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">10D P</th>
<th data-quarto-table-cell-role="th">25D P</th>
<th data-quarto-table-cell-role="th">ATM</th>
<th data-quarto-table-cell-role="th">25D C</th>
<th data-quarto-table-cell-role="th">10D C</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">1W</th>
<td>-1878.530020</td>
<td>5996.552500</td>
<td>-7096.302579</td>
<td>3011.398803</td>
<td>974.274008</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1M</th>
<td>-11133.617227</td>
<td>168080.302683</td>
<td>41107.244760</td>
<td>-11645.406630</td>
<td>1430.459088</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">3M</th>
<td>-153599.417536</td>
<td>173253.199318</td>
<td>40819.460264</td>
<td>250610.804505</td>
<td>49760.293768</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1Y</th>
<td>15323.454136</td>
<td>-176753.786945</td>
<td>341937.783378</td>
<td>346704.848926</td>
<td>-71231.295442</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>How do we hedge those bucketed sensitivities? Well, we can do it with plain vanilla options, …</p>
<p><img src="https://latex.codecogs.com/png.latex?%0As_%7Bij%7D%20=%20w_%7Bij%7D%20%5Cfrac%7B%5Cpartial%20C_%7BBS%7D(%5Csigma%7Bij%7D)%7D%7B%5Cpartial%20%5Csigma_%7Bij%7D%7D%20%20%5CRightarrow%20w_%7Bij%7D%20=%20%5Cfrac%7Bs_%7Bij%7D%7D%7B%5Cfrac%7B%5Cpartial%20C_%7BBS%7D(%5Csigma%7Bij%7D)%7D%7B%5Cpartial%20%5Csigma_%7Bij%7D%7D%7D%0A"></p>
<div id="c81f9a23" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">vanilla_options <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros_like(bucket_sens).tolist()</span>
<span id="cb22-2"></span>
<span id="cb22-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(vanilla_options)):</span>
<span id="cb22-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(vanilla_options[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])):</span>
<span id="cb22-5">        maturity <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> maturities[i]</span>
<span id="cb22-6">        option_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> option_types[j]</span>
<span id="cb22-7">        strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> strikes[i, j]</span>
<span id="cb22-8">        process <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.GarmanKohlagenProcess(</span>
<span id="cb22-9">            spot_handle,</span>
<span id="cb22-10">            foreign_ts,</span>
<span id="cb22-11">            domestic_ts,</span>
<span id="cb22-12">            ql.BlackVolTermStructureHandle(</span>
<span id="cb22-13">                ql.BlackConstantVol(ref_date, calendar, ivs[i, j], dc)</span>
<span id="cb22-14">            )</span>
<span id="cb22-15">        )</span>
<span id="cb22-16"></span>
<span id="cb22-17">        payoff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.PlainVanillaPayoff(option_type, strike)</span>
<span id="cb22-18">        vanilla_options[i][j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.VanillaOption(payoff, ql.EuropeanExercise(maturity))</span>
<span id="cb22-19">        vanilla_options[i][j].setPricingEngine(ql.AnalyticEuropeanEngine(process))</span></code></pre></div></div>
</div>
<div id="01eab1c0" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">vegas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([[opt.vega() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> row] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> vanilla_options])</span></code></pre></div></div>
</div>
<div id="2a6969f0" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">vega_weights <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bucket_sens <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> vegas</span>
<span id="cb24-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> np.printoptions(precision<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>):</span>
<span id="cb24-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(vega_weights)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[-0.033  0.058 -0.054  0.029  0.017]
 [-0.094  0.78   0.15  -0.052  0.012]
 [-0.778  0.477  0.088  0.656  0.231]
 [ 0.04  -0.251  0.372  0.446 -0.159]]</code></pre>
</div>
</div>
<p>Now that we have found the weight for all the vanillas, let’s now reduce the total <img src="https://latex.codecogs.com/png.latex?%5CDelta"> exposure of our portfolio. The weight for the spot position hedge is given by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aw_%7B%5CDelta%7D%20=%20%5Cfrac%7B%5Cfrac%7B%5Cpartial%20%5Ctext%7Bbook%7D%7D%7B%5Cpartial%20S%7D%7D%7B%5Csum_%7Bij%7D%20%5Cfrac%7B%5Cpartial%20C_%7BBS%7D%5E%7Bij%7D%7D%7B%5Cpartial%20S%7D%7D%0A"></p>
<div id="54aa06bb" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1">spot_change <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb26-2"></span>
<span id="cb26-3">delta_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(delta, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb26-4">delta_vanillas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([opt.delta() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt, v_w <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(row, v_row)]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row, v_row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(vanilla_options, vega_weights)])</span></code></pre></div></div>
</div>
<div id="62820f2c" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1">hedging_delta_weight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> delta_vanillas</span>
<span id="cb27-2">hedging_delta_weight</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="212">
<pre><code>np.float64(-0.23937887076554554)</code></pre>
</div>
</div>
<div id="767b1cac" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">vanillas_premiums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [[opt.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt, v_w <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(row, v_row)] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row, v_row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(vanilla_options, vega_weights)]</span>
<span id="cb29-2">spot_hedge_usd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hedging_delta_weight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_handle.value()</span>
<span id="cb29-3">hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(vanillas_premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> spot_hedge_usd</span>
<span id="cb29-4">bank_account_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(premiums)</span></code></pre></div></div>
</div>
<div id="1c41b5e8" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1">port_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bank_account_value]</span>
<span id="cb30-2"></span>
<span id="cb30-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The portfolio value at time t = 0 is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>port_value[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>The portfolio value at time t = 0 is 0.0</code></pre>
</div>
</div>
<section id="changes-of-the-hedging-portfolio-with-the-changes-of-the-market-conditions" class="level2">
<h2 class="anchored" data-anchor-id="changes-of-the-hedging-portfolio-with-the-changes-of-the-market-conditions">Changes of the hedging portfolio with the changes of the market conditions</h2>
<p>Now that we have setup our hedging portfolio see verify how its will change as the spot and vol surface change. Let’s assume that in a 2-day period the spot went go down by 200 pips, the ATM vol went up by <img src="https://latex.codecogs.com/png.latex?0.5%5C%25"> while the the 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> Put vol went up <img src="https://latex.codecogs.com/png.latex?0.8%5C%25"> (since the price to hedge from downside has went up because of the underlying so there is more demand for puts).</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20S_0%20&amp;=%201.16%20%5C%5C%0A%20%20%20%20Date(Today)%20&amp;=%2016/12/2025%20%5C%5C%0A%20%20%20%20r_d%20&amp;=%200.03%20%5C%5C%0A%20%20%20%20r_f%20&amp;=%200.02%20%5C%5C%0A%5Cend%7Balign*%7D%0A"></p>
<p>With the following Implied Vol surface</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?10%5CDelta%20p"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?25%5CDelta%20p"></th>
<th style="text-align: left;">ATM</th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?25%5CDelta%20c"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?10%5CDelta%20c"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><strong>3D</strong></td>
<td style="text-align: left;">14.00%</td>
<td style="text-align: left;">13.00%</td>
<td style="text-align: left;">12.00%</td>
<td style="text-align: left;">11.50%</td>
<td style="text-align: left;">10.50%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>1M</strong></td>
<td style="text-align: left;">14.00%</td>
<td style="text-align: left;">13.50%</td>
<td style="text-align: left;">10.00%</td>
<td style="text-align: left;">9.00%</td>
<td style="text-align: left;">9.75%</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>3M</strong></td>
<td style="text-align: left;">12.75%</td>
<td style="text-align: left;">11.50%</td>
<td style="text-align: left;">10.00%</td>
<td style="text-align: left;">9.00%</td>
<td style="text-align: left;">9.50%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>1Y</strong></td>
<td style="text-align: left;">12.50%</td>
<td style="text-align: left;">10.75%</td>
<td style="text-align: left;">10.25%</td>
<td style="text-align: left;">9.75%</td>
<td style="text-align: left;">9.50%</td>
</tr>
</tbody>
</table>
<p>We are now interested to see what’s the new exposure of our hedging portfolio.</p>
<div id="b47c8352" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1">ref_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, ql.Days)</span>
<span id="cb32-2"></span>
<span id="cb32-3">spot_quote.setValue(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.16</span>)</span>
<span id="cb32-4"></span>
<span id="cb32-5">new_times <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [dc.yearFraction(ref_date, m) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> maturities]</span>
<span id="cb32-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Vol surface</span></span>
<span id="cb32-7">new_ivs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb32-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.10P, 0.25P, ATM,  0.25C, 0.10C</span></span>
<span id="cb32-9">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.140</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.13</span>,  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.115</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.105</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1W </span></span>
<span id="cb32-10">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.140</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.135</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.110</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.105</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1M</span></span>
<span id="cb32-11">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1275</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.115</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.090</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.095</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3M</span></span>
<span id="cb32-12">    [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.125</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1075</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1025</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0975</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.105</span>],  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1Y</span></span>
<span id="cb32-13">])</span>
<span id="cb32-14"></span>
<span id="cb32-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move vols and expiry time</span></span>
<span id="cb32-16">ql.Settings.instance().evaluationDate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ref_date</span></code></pre></div></div>
</div>
<p>Let’s calculate the new strikes needed to build the new calibrated local vol surface.</p>
<div id="ef96aa49" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1">new_strikes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> create_strike_iv_surf(spot_quote.value(), usd_curve, eur_curve, deltas, option_types, new_times, new_ivs)</span></code></pre></div></div>
</div>
<div id="9cf12bdc" class="cell" data-execution_count="217">
<div class="cell-output cell-output-display" data-execution_count="217">
<pre><code>array([[1.13612881, 1.14837208, 1.16019107, 1.17076404, 1.17858337],
       [1.10412947, 1.13192316, 1.16097287, 1.18435339, 1.20361041],
       [1.07412444, 1.11998689, 1.16256386, 1.19758045, 1.23419363],
       [1.00256954, 1.09219685, 1.16972097, 1.24936665, 1.33944933]])</code></pre>
</div>
</div>
<div id="2c4e457e" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1">ah_vol_surf, ah_local_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calibrate_vol_surface(new_strikes, option_types, maturities, new_ivs)</span>
<span id="cb35-2">black_vol_surf_handle.linkTo(ah_vol_surf)</span>
<span id="cb35-3">local_vol_surf_handle.linkTo(ah_local_vol)</span></code></pre></div></div>
</div>
<div id="5e767378" class="cell" data-execution_count="219">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol_files/figure-html/cell-49-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>We now have to recalculate the price of the hedging vanillas and the value of the new exotics to calculate the <img src="https://latex.codecogs.com/png.latex?P%5C&amp;L"> from time <img src="https://latex.codecogs.com/png.latex?t"> to time <img src="https://latex.codecogs.com/png.latex?t+1"></p>
<div id="dc6d8113" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(vanilla_options)):</span>
<span id="cb36-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(vanilla_options[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])):</span>
<span id="cb36-3">        process <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.GarmanKohlagenProcess(</span>
<span id="cb36-4">            spot_handle,</span>
<span id="cb36-5">            foreign_ts,</span>
<span id="cb36-6">            domestic_ts,</span>
<span id="cb36-7">            ql.BlackVolTermStructureHandle(</span>
<span id="cb36-8">                ql.BlackConstantVol(ref_date, calendar, new_ivs[i, j], dc)</span>
<span id="cb36-9">            )</span>
<span id="cb36-10">        )</span>
<span id="cb36-11">        vanilla_options[i][j].setPricingEngine(ql.AnalyticEuropeanEngine(process))</span></code></pre></div></div>
</div>
<div id="f445bc9c" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1">new_premiums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [opt.NPV() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book]</span>
<span id="cb37-2">new_book_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(new_premiums)</span>
<span id="cb37-3">new_vanilla_premiums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [[opt.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> opt, v_w <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(row, v_row)] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row, v_row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(vanilla_options, vega_weights)]</span>
<span id="cb37-4">spot_premium_eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hedging_delta_weight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional </span>
<span id="cb37-5">new_spot_hedge_usd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hedging_delta_weight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_handle.value()</span>
<span id="cb37-6">new_hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(new_vanilla_premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> new_spot_hedge_usd</span></code></pre></div></div>
</div>
<div id="6b86709f" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc.yearFraction(today, ref_date)</span>
<span id="cb38-2">usd_interest_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt</span>
<span id="cb38-3">eur_interest_accr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spot_premium_eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt</span></code></pre></div></div>
</div>
<div id="326ea5be" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">new_total_portfolio_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(new_premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bank_account_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> usd_interest_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> eur_interest_accr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb39-2"></span>
<span id="cb39-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Portfolio value at time t = 1 is: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>new_total_portfolio_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Portfolio value at time t = 1 is: $202.86605298389892</code></pre>
</div>
</div>
<p>Thus the variation from time 0 to time 1 is just about <img src="https://latex.codecogs.com/png.latex?%5C$200">, this confirms that our hedge worked by limiting the variation of the whole portfolio value.</p>



</section>
</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-vol-interpol" class="csl-entry">
Andreasen, Jesper, and Brian Huge. 2010. <span>“Volatility Interpolation.”</span> <em>Risk Magazine</em>, March. <a href="https://doi.org/10.2139/ssrn.1694972">https://doi.org/10.2139/ssrn.1694972</a>.
</div>
</div></section></div> ]]></description>
  <category>Quantitative finance</category>
  <category>Hedging</category>
  <category>Exotic options</category>
  <guid>https://your-website-url.example.com/posts/post-with-code/exotic-book-local-vol.html</guid>
  <pubDate>Wed, 11 Feb 2026 00:00:00 GMT</pubDate>
  <media:content url="https://your-website-url.example.com/posts/post-with-code/Local-vs-implied-volatility-surface.webp" medium="image" type="image/webp"/>
</item>
<item>
  <title>Hedging a book of exotic options using the Vanna-Volga method</title>
  <link>https://your-website-url.example.com/posts/post-with-code/exotic-book-vanna-volga.html</link>
  <description><![CDATA[ 





<p>In this notebook, we continue to study the track of hedging in the world of FX options still from a trader sitting in a exotic desk in the S&amp;T branch of a bank. As we’ve seen in the previous notebook (<a href="../../posts/post-with-code/exotic-book.html">Hedging a book of exotics options</a>) we have cover the basics on how to hedge a book of FX exotics, in this notebook we are going to take a further step to what is used in practice since, as you might know volatilities in practice are not flat across different strikes and across different time to expirations. Thus we are going to introduce a method that is going to take into account also for those volatilities as well to produce market consistent prices. This method is called the <strong>Vanna-Volga</strong> method.</p>
<p>Again as a library we are going to use <code>QuantLib</code>, because, well why not, right?</p>
<section id="market-data" class="level2">
<h2 class="anchored" data-anchor-id="market-data">Market Data</h2>
<p>Let’s assume that we are an USD based desk and we are dealing with EURUSD exotics. The initial market data at time <img src="https://latex.codecogs.com/png.latex?t%20=%200"> is the following</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20S_0%20&amp;=%201.18%20%5C%5C%0A%20%20%20%20Date(Today)%20&amp;=%2016/09/2025%20%5C%5C%0A%20%20%20%20T%20&amp;=%200.5%20%5Ctext%7B%20(time%20to%20expiration%20in%20years)%7D%20%5C%5C%0A%20%20%20%20r_d%20&amp;=%200.045%20%5C%5C%0A%20%20%20%20r_f%20&amp;=%200.02%20%5C%5C%0A%20%20%20%20%5Csigma%20&amp;=%200.13%20%5C%5C%0A%5Cend%7Balign*%7D%0A"></p>
<p>Let’s further assume that in our book we have all options expirying in 6 months (thus <img src="https://latex.codecogs.com/png.latex?T%20=%200.5">).</p>
<div id="0a1d2ecd" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">EUR_USD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.18</span></span>
<span id="cb1-2">spot_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(EUR_USD)</span>
<span id="cb1-3">today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, ql.September, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>)</span>
<span id="cb1-4">ref_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today</span>
<span id="cb1-5">r_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.045</span></span>
<span id="cb1-6">r_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span></span>
<span id="cb1-7">vol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.13</span>)</span>
<span id="cb1-8">spot_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(spot_quote)</span>
<span id="cb1-9">vol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(vol_quote)</span>
<span id="cb1-10"></span>
<span id="cb1-11">dc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Actual365Fixed()</span>
<span id="cb1-12">calendar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.JointCalendar(ql.Italy(), ql.UnitedStates(ql.UnitedStates.NYSE))</span>
<span id="cb1-13"></span>
<span id="cb1-14">expiration_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ref_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, ql.Months)</span>
<span id="cb1-15">expiration_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc.yearFraction(ref_date, expiration_date)</span>
<span id="cb1-16">domestic_rf_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(ql.FlatForward(today, r_d, dc))</span>
<span id="cb1-17">foreign_rf_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(ql.FlatForward(today, r_f, dc))</span>
<span id="cb1-18">black_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackConstantVol(today, ql.NullCalendar(), vol_handle, dc)</span>
<span id="cb1-19">vol_ts_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackVolTermStructureHandle(black_vol)</span>
<span id="cb1-20"></span>
<span id="cb1-21">exp_time_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(expiration_time)</span>
<span id="cb1-22">exp_time_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(exp_time_quote)</span>
<span id="cb1-23">exp_time_sqrt_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DerivedQuote(exp_time_handle, function<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: np.sqrt(x))</span>
<span id="cb1-24">exp_time_sqrt_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(exp_time_sqrt_quote)</span>
<span id="cb1-25"></span>
<span id="cb1-26">delta_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuote.Spot</span>
<span id="cb1-27">atm_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuote.AtmSpot</span>
<span id="cb1-28">atm_vol, delta25_call_vol, delta25_put_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.11</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.125</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.14</span></span>
<span id="cb1-29">atm_vol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(atm_vol)</span>
<span id="cb1-30">delta25_call_vol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(delta25_call_vol)</span>
<span id="cb1-31">delta25_put_vol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(delta25_put_vol)</span>
<span id="cb1-32"></span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Handle creations</span></span>
<span id="cb1-35">spot_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(spot_quote)</span>
<span id="cb1-36">atm_vol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(atm_vol_quote)</span>
<span id="cb1-37">delta25_c_vol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(delta25_call_vol_quote)</span>
<span id="cb1-38">delta25_p_vol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(delta25_put_vol_quote)</span>
<span id="cb1-39"></span>
<span id="cb1-40">atm_dvol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuote(atm_vol_handle, delta_type, expiration_time, atm_type)</span>
<span id="cb1-41">delta25_c_dvol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuote(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, delta25_c_vol_handle, expiration_time, delta_type)</span>
<span id="cb1-42">delta25_p_dvol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuote(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, delta25_p_vol_handle, expiration_time, delta_type)</span>
<span id="cb1-43">atm_dvol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuoteHandle(atm_dvol_quote)</span>
<span id="cb1-44">delta25_c_dvol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuoteHandle(delta25_c_dvol_quote)</span>
<span id="cb1-45">delta25_p_dvol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DeltaVolQuoteHandle(delta25_p_dvol_quote)</span>
<span id="cb1-46"></span>
<span id="cb1-47">atm_std_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(ql.CompositeQuote(atm_vol_handle, exp_time_sqrt_handle, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x, y: x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y))</span>
<span id="cb1-48">delta25_c_std_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(ql.CompositeQuote(delta25_c_vol_handle, exp_time_sqrt_handle, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x, y: x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y))</span>
<span id="cb1-49">delta25_p_std_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(ql.CompositeQuote(delta25_p_vol_handle, exp_time_sqrt_handle, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x, y: x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y))</span>
<span id="cb1-50"></span>
<span id="cb1-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting the global evaluation date</span></span>
<span id="cb1-52">ql.Settings.instance().evaluationDate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today</span></code></pre></div></div>
</div>
<div id="51b0bd58" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">eur_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span>)]</span>
<span id="cb2-2">eur_dfs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>]</span>
<span id="cb2-3">eur_curve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DiscountCurve(eur_dates, eur_dfs, dc)</span>
<span id="cb2-4">eur_curve.enableExtrapolation()</span>
<span id="cb2-5">eur_disc_ts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(eur_curve)</span>
<span id="cb2-6"></span>
<span id="cb2-7">usd_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span>)]</span>
<span id="cb2-8">usd_dfs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.985</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.96</span>]</span>
<span id="cb2-9">usd_curve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DiscountCurve(usd_dates, usd_dfs, dc)</span>
<span id="cb2-10">usd_curve.enableExtrapolation()</span>
<span id="cb2-11">usd_disc_ts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(usd_curve)</span></code></pre></div></div>
</div>
<p>We know, from empirical experience, that the volatilities across a specific time to expiration are not the same for all the strikes, this is due a number of different reasons: market demand dynamics, skewness of returns, the presence of jumps, …</p>
<p>Thus let’s define the smile for the barrier options expiring in 6 months, assuming that we know the values of the vols for the ATM, 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> puts, and 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> calls. To do that, QuantLib come in handy with ready classes to define a smile across different strikes with a fixed tenor.</p>
<p>The <a href="https://quantlib-python-docs.readthedocs.io/en/latest/termstructures.html#smilesections">SmileSection class</a>, is an object provided by the QuantLib library that represent a volatility smile for a specific tenor. QuantLib export some subclasses of <code>SmileSection</code> using different interpolation methods, thus you can choose some of them according to you needs.</p>
<div id="26adaaa2" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">call_barrier_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxBarrierOptionFactory(spot_handle, ql.Option.Call)</span>
<span id="cb3-2">put_barrier_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxBarrierOptionFactory(spot_handle, ql.Option.Put)</span>
<span id="cb3-3">put_vanilla_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxVanillaOptionFactory(spot_handle, expiration_date, ql.Option.Put)</span>
<span id="cb3-4">call_vanilla_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxVanillaOptionFactory(spot_handle, expiration_date, ql.Option.Call)</span>
<span id="cb3-5">delta_call_calc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackDeltaCalculator(</span>
<span id="cb3-6">    ql.Option.Call, </span>
<span id="cb3-7">    ql.DeltaVolQuote.Spot, </span>
<span id="cb3-8">    EUR_USD, </span>
<span id="cb3-9">    eur_curve.discount(expiration_time), </span>
<span id="cb3-10">    usd_curve.discount(expiration_time), </span>
<span id="cb3-11">    np.sqrt(black_vol.blackVariance(expiration_time, EUR_USD)))</span>
<span id="cb3-12">delta_put_calc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackDeltaCalculator(</span>
<span id="cb3-13">    ql.Option.Put, </span>
<span id="cb3-14">    ql.DeltaVolQuote.Spot, </span>
<span id="cb3-15">    EUR_USD, </span>
<span id="cb3-16">    eur_curve.discount(expiration_time), </span>
<span id="cb3-17">    usd_curve.discount(expiration_time), </span>
<span id="cb3-18">    np.sqrt(black_vol.blackVariance(expiration_time, EUR_USD)))</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the strikes equivalent for the 0.25 delta for call and put options</span></span>
<span id="cb3-21">d25_call_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_call_calc.strikeFromDelta(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb3-22">d25_put_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_put_calc.strikeFromDelta(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb3-23">atm_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_call_calc.atmStrike(ql.DeltaVolQuote.AtmDeltaNeutral)</span></code></pre></div></div>
</div>
<div id="d5c17e48" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the smile using a Spline Cubic interpolation</span></span>
<span id="cb4-2">smile <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SplineCubicInterpolatedSmileSection(</span>
<span id="cb4-3">    expiration_time, </span>
<span id="cb4-4">    [d25_put_strike, atm_strike, d25_call_strike],</span>
<span id="cb4-5">    [delta25_p_std_handle, atm_std_handle, delta25_c_std_handle],</span>
<span id="cb4-6">    ql.makeQuoteHandle(atm_strike)</span>
<span id="cb4-7">)</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_smile_vols(smile: ql.SmileSection, strikes: Union[np.array, List[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>]]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> List[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>]:</span>
<span id="cb4-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [smile.volatility(s) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> strikes]</span></code></pre></div></div>
</div>
<div id="a5be9dd1" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">strikes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(d25_put_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, d25_call_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.04</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb5-2">smile_vols <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_smile_vols(smile, strikes)</span></code></pre></div></div>
</div>
<div id="eae3cc19" class="cell">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-vanna-volga_files/figure-html/cell-12-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>As mentioned above, the pricing method used in this notebook resembles what is acctually used in real life to be consistent with market prices. To take into account also for the smile a pricing method has been developed by Castagna and Mercurio <span class="citation" data-cites="vv-article">(see Castagna and Mercurio 2007)</span>.</p>
<p>The <strong>Vanna-Volga</strong> method takes into account the volatility smile when pricing FX options by adjusting the BS price. It is based on the construction of a locally replicating portfolio whose associate hedging costs are added to the corresponding BS price so as to produce smile-consisent values.</p>
<p>Assume that we construct our hedging portfolio with the following options: 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> put, ATM call and 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> call where the strikes are denoted by <img src="https://latex.codecogs.com/png.latex?K_i,%20i%20=%201,%202,%203%20%5Ctext%7B%20and%20%7D%20K_1%20%3C%20K_2%20%3C%20K_3"> and market implied volatilities are denoted by <img src="https://latex.codecogs.com/png.latex?%5Csigma_i,%20i%20=%201,%202,%203">. Using these options we can build a portfolio that zeros the partial derivatives up to the second order. In fact, denoting respectively by <img src="https://latex.codecogs.com/png.latex?%5CDelta_t"> and <img src="https://latex.codecogs.com/png.latex?x_i"> the units of underlying assets and options with strikes <img src="https://latex.codecogs.com/png.latex?K_i"> held at time <img src="https://latex.codecogs.com/png.latex?t"> and selling <img src="https://latex.codecogs.com/png.latex?C_i%5E%7BBS%7D(t)%20=%20C%5E%7BBS%7D(t,%20K_i)"> under diffusion dynamics both for <img src="https://latex.codecogs.com/png.latex?S_t"> and <img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%20%5Csigma_t">, we have by Ito’s Lemma:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ad%20C%5E%7BBS%7D(t,%20K)%20-%20%5CDelta_t%20d%20S_t%20-%20%5Csum_%7Bi%20=%201%7D%5E3%20x_i%20d%20C_i%5E%7BBS%7D(t)%20%20&amp;%20=%20%5B%20%5Cfrac%7B%5Cpartial%20C%5E%7BBS%7D(t;%20K)%7D%7B%5Cpartial%20t%7D%20-%20%5Csum%20x_i%20%5Cfrac%7B%5Cpartial%20C_%7Bi%7D%5E%7BBS%7D(t)%7D%7B%5Cpartial%20t%7D%20%5D%20dt%20+%20%5B%20%5Cfrac%7B%5Cpartial%20C%5E%7BBS%7D(t;%20K)%7D%7B%5Cpartial%20S%7D%20-%20%5CDelta_t%20-%20%5Csum%20x_i%20%5Cfrac%7B%5Cpartial%20C_%7Bi%7D%5E%7BBS%7D(t)%7D%7B%5Cpartial%20S%7D%20%5D%20dS%20%5C%5C%0A&amp;%20+%20%5B%20%5Cfrac%7B%5Cpartial%20C%5E%7BBS%7D(t;%20K)%7D%7B%5Cpartial%20%5Csigma%7D%20-%20%5Csum%20x_i%20%5Cfrac%7B%5Cpartial%20C_%7Bi%7D%5E%7BBS%7D(t)%7D%7B%5Cpartial%20%5Csigma%7D%20%5D%20d%20%5Csigma_t%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5B%20%5Cfrac%7B%5Cpartial%5E2%20C%5E%7BBS%7D(t;%20K)%7D%7B%5Cpartial%20S%5E2%7D%20-%20%5Csum%20x_i%20%5Cfrac%7B%5Cpartial%5E2%20C_%7Bi%7D%5E%7BBS%7D(t)%7D%7B%5Cpartial%20S%5E2%7D%20%5D%20(d%20S)%5E2%20%5C%5C%0A&amp;%20+%20%5Cfrac%7B1%7D%7B2%7D%20%5B%20%5Cfrac%7B%5Cpartial%5E2%20C%5E%7BBS%7D(t;%20K)%7D%7B%5Cpartial%20%5Csigma%5E2%7D%20-%20%5Csum%20x_i%20%5Cfrac%7B%5Cpartial%5E2%20C_%7Bi%7D%5E%7BBS%7D(t)%7D%7B%5Cpartial%20%5Csigma%5E2%7D%20%5D%20(d%20%5Csigma)%5E2%20+%20%5B%20%5Cfrac%7B%5Cpartial%5E2%20C%5E%7BBS%7D(t;%20K)%7D%7B%5Cpartial%20S%20%5Cpartial%20%5Csigma%20%7D%20-%20%5Csum%20x_i%20%5Cfrac%7B%5Cpartial%5E2%20C_%7Bi%7D%5E%7BBS%7D(t)%7D%7B%5Cpartial%20S%20%5Cpartial%20%5Csigma%20%7D%20%5D%20d%20S_t%20d%20%5Csigma_t%0A%5Cend%7Balign*%7D%0A"></p>
<p>Choosing <img src="https://latex.codecogs.com/png.latex?%5CDelta_t"> and <img src="https://latex.codecogs.com/png.latex?x_i"> so as to zero the coefficient of <img src="https://latex.codecogs.com/png.latex?dS_t,%20d%5Csigma_t,%20(d%5Csigma_t)%5E2,%20%5Ctext%7B%20and%20%7D%20dS_t%20d%5Csigma_t">, the whole portfolio (long position + hedges) is locally riskless at time <img src="https://latex.codecogs.com/png.latex?t">, in what no stochastic terms are involved in its differential:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0Ad%20C%5E%7BBS%7D(t;%20K)%20&amp;-%20%5CDelta_t%20dS_t%20-%20%5Csum%20x_i%20d%20C_i%5E%7BBS%7D(t)%20=%20%5C%5C%0A&amp;%20=%20r%5Ed%20%5B%20C%5E%7BBS%7D(t;%20K)%20-%20%5CDelta_t%20d%20S_t%20-%20%5Csum%20x_i%20C_i%5E%7BBS%7D(t)%20%5D%20dt%20%5Cquad%20(1)%0A%5Cend%7Balign*%7D%0A"></p>
<p>To find the weight of the hedging portfolio we need to solve the following system</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bequation*%7D%0A%5Cleft%5C%7B%0A%5Cbegin%7Baligned%7D%0A%5Cfrac%7B%5Cpartial%20C%5E%7B%5Ctext%7BBS%7D%7D%7D%7B%5Cpartial%20%5Csigma%7D%20(K)%20&amp;=%20%5Csum_%7Bi=1%7D%5E%7B3%7D%20x_i%20(K)%20%5Cfrac%7B%5Cpartial%20C%5E%7B%5Ctext%7BBS%7D%7D%7D%7B%5Cpartial%20%5Csigma%7D%20(K_i)%20%5C%5C%0A%5Cfrac%7B%5Cpartial%5E2%20C%5E%7B%5Ctext%7BBS%7D%7D%7D%7B%5Cpartial%20%5Csigma%5E2%7D%20(K)%20&amp;=%20%5Csum_%7Bi=1%7D%5E%7B3%7D%20x_i%20(K)%20%5Cfrac%7B%5Cpartial%5E2%20C%5E%7B%5Ctext%7BBS%7D%7D%7D%7B%5Cpartial%20%5Csigma%5E2%7D%20(K_i)%20%5C%5C%0A%5Cfrac%7B%5Cpartial%5E2%20C%5E%7B%5Ctext%7BBS%7D%7D%7D%7B%5Cpartial%20%5Csigma%20%5Cpartial%20S_0%7D%20(K)%20&amp;=%20%5Csum_%7Bi=1%7D%5E%7B3%7D%20x_i%20(K)%20%5Cfrac%7B%5Cpartial%5E2%20C%5E%7B%5Ctext%7BBS%7D%7D%7D%7B%5Cpartial%20%5Csigma%20%5Cpartial%20S_0%7D%20(K_i)%0A%5Cend%7Baligned%7D%0A%5Cright.%0A%5Cend%7Bequation*%7D%0A"></p>
<p>Now, for the model to be useful it has to be consistent with market prices, thus the hedging costs at prevaling market prices has to be included in the option price to produce an arbitrage-free price taht is consistent with the quoted option prices <img src="https://latex.codecogs.com/png.latex?C%5E%7BMKT%7D(K_1),%20C%5E%7BMKT%7D(K_2)%20%5Ctext%7B%20and%20%7D%20C%5E%7BMKT%7D(K_3)">. In case of a short maturity, <em>i.e.</em> for a small <img src="https://latex.codecogs.com/png.latex?T"> equation <img src="https://latex.codecogs.com/png.latex?(1)"> can be approximated as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(S_T%20-%20K)%5E+%20-%20C%5E%7BBS%7D(K)%20&amp;-%20%5CDelta_0%20%5B%20S_T%20-%20S_0%20%5D%20-%20%5Csum%20x_i%20%5B%20(S_T%20-%20K_i)%5E+%20-%20C_i%5E%7BBS%7D(K_i)%20%5D%20%5C%5C%0A&amp;%5Capprox%20r%5Ed%20%5B%20C%5E%7BBS%7D(K)%20-%20%5CDelta_0%20S_0%20-%20%5Csum%20x_i%20C_i%5E%7BBS%7D(K_i)%20%5D%20T%0A%5Cend%7Balign*%7D%0A"></p>
<p>by adjusting the BS price with Market prices we have</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC(K)%20=%20C%5E%7BBS%7D(K)%20+%20%5Csum%20x_i(K)%20%5B%20C%5E%7BMKT%7D(K_i)%20-%20C%5E%7BBS%7D(K_i)%20%5D%20%5Cquad%20(2)%20%5C%5C%5B10pt%5D%0A%5CDownarrow%20%5C%5C%5B10pt%5D%0AC%5E%7BBS%7D(K)%20=%20C(K)%20-%20%5Csum%20x_i(K)%20%5B%20C%5E%7BMKT%7D(K_i)%20-%20C%5E%7BBS%7D(K_i)%20%5D%20%5C%5C%5B10pt%5D%0A"></p>
<p>substitute <img src="https://latex.codecogs.com/png.latex?C%5E%7BBS%7D(K)"> yields to</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(S_T%20-%20K)%5E+%20-%20C(K)%20&amp;+%20%5Csum%20x_i(K)%20%5B%20C%5E%7BMKT%7D(K_i)%20-%20C%5E%7BBS%7D(K_i)%20%5D%20-%20%5CDelta_0%20%5B%20S_T%20-%20S_0%20%5D%20%5C%5C%0A&amp;-%20%5Csum%20x_i%20%5B%20(S_T%20-%20K_i)%5E+%20-%20C%5E%7BBS%7D(K_i)%20%5D%20%5C%5C%0A&amp;%5Capprox%20r%5Ed%20%5B%20C(K)%20-%20%5Csum%20x_i(K)%20%5B%20C%5E%7BMKT%7D(K_i)%20-%20C%5E%7BBS%7D(K_i)%20%5D%20-%20%5CDelta_0%20S_0%20-%20%5Csum%20x_i%20C_i%5E%7BBS%7D(K_i)%20%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>which can be simplified to</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(S_T%20-%20K)%5E+%20-%20C(K)%20&amp;-%20%5CDelta_0%20%5B%20S_T%20-%20S_0%20%5D%20-%20%5Csum%20x_i%20%5B%20(S_T%20-%20K_i)%5E+%20-%20C%5E%7BBS%7D(K_i)%20-%20C%5E%7BMKT%7D(K_i)%20+%20C%5E%7BBS%7D(K_i)%20%5D%20%5C%5C%0A&amp;%5Capprox%20r%5Ed%20%5B%20C(K)%20-%20%5CDelta_0%20S_0%20-%20%5Csum%20x_i%20C_i%5E%7BMKT%7D(K_i)%20%5D%0A%5Cend%7Balign*%7D%0A"></p>
<p>by isolating <img src="https://latex.codecogs.com/png.latex?(S_T%20-%20K)%5E+"> we have then that</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A(S_T%20-%20K)%5E+%20%5Capprox%20&amp;%5Coverbrace%7BC(K)%7D%5E%7B%5Ctext%7BVV%20price%7D%7D%20-%20%5Coverbrace%7B%5CDelta_0%20%5BS_T%20-%20S_0%5D%7D%5E%7B%5Ctext%7BDelta%20Hedge%7D%7D%20+%20%5Coverbrace%7B%5Csum_i%20x_i%20%5B%20(S_T%20-%20K_i)%5E+%20-%20C%5E%7BMKT%7D(K_i)%20%5D%7D%5E%7B%5Ctext%7Boption%20positions%7D%7D%20%5C%5C%0A&amp;+%20%5Cunderbrace%7Br%5Ed%20%5B%20C(K)%20-%20%5CDelta_0%20S_0%20-%20%5Csum_i%20x_i%20C_i%5E%7BMKT%7D(K_i)%20%5D%20T%7D_%7B%5Ctext%7Bresidual%20bank%20account%20position%7D%7D%0A%5Cend%7Balign*%7D%0A"></p>
<p>Thus from the above approximation, we are seening that the payoff of the option can still be replicated by shorting <img src="https://latex.codecogs.com/png.latex?%5CDelta_0"> units of the underlying asset, buying <img src="https://latex.codecogs.com/png.latex?x_i"> options with strike <img src="https://latex.codecogs.com/png.latex?K_i"> and investing the resulting cash at rate <img src="https://latex.codecogs.com/png.latex?r%5Ed">.</p>
<p>The quantity <img src="https://latex.codecogs.com/png.latex?C(K)"> is defined as the <strong>VV option’s premium</strong>, implicitly assuming that the replication error is negligible for longer maturities. Such a premium is equal the BS price <img src="https://latex.codecogs.com/png.latex?C%5E%7BBS%7D(K)"> plus the cost difference of the hedging portfolio induced by the market implied volatilities with respect to the constant volatility. Since we set <img src="https://latex.codecogs.com/png.latex?%5Csigma_2%20=%20%5Csigma"> using <img src="https://latex.codecogs.com/png.latex?%5Csigma"> the market volatility for strike <img src="https://latex.codecogs.com/png.latex?K_2">, (2) can be simplified to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AC(K)%20=%20C%5E%7BBS%7D(K)%20+%20x_1%20%5B%20C%5E%7BMKT%7D(K_1)%20-%20C%5E%7BBS%7D(K_1)%20%5D%20%5C%5C%0A+%20x_3%20%5B%20C%5E%7BMKT%7D(K_3)%20-%20C%5E%7BBS%7D(K_3)%20%5D%0A"></p>
<p>The model that extends the Black-Scholes model when dealing with FX options is the Garman-Kohlhagen model, whose dynamics have been defined above. Thus we need to create an instances of the Garman-Kohlhagen process in order to price FX vanilla options.</p>
<p>For the sake of simplicity we assume that all the options on the book have the same expiration, and again since we are under the Garman-Kohlhagen model the vol curve is flat, thus the vol across the different strikes is the same (unrealistic assumption). Here’s how the trader book is composed of:</p>
<p>1st Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.30%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="44556806" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 1</span></span>
<span id="cb6-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb6-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.30</span></span>
<span id="cb6-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpOut</span>
<span id="cb6-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb6-6"></span>
<span id="cb6-7">barrier_25d_130b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(d25_call_strike, exercise, barrier, barrier_type)</span>
<span id="cb6-8">barrier_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_130b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>2nd Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$2000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.08%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BDown%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="63c0e0d4" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 2</span></span>
<span id="cb7-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2_000_000</span></span>
<span id="cb7-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.08</span></span>
<span id="cb7-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.DownOut</span>
<span id="cb7-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb7-6"></span>
<span id="cb7-7">barrier_25d_108b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(d25_call_strike, exercise, barrier, barrier_type)</span>
<span id="cb7-8">barrier_2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_108b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>3rd Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.20%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20In%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="5641b918" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 3</span></span>
<span id="cb8-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb8-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.20</span></span>
<span id="cb8-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpIn</span>
<span id="cb8-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb8-6"></span>
<span id="cb8-7">barrier_25d_120b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(d25_call_strike, exercise, barrier, barrier_type)</span>
<span id="cb8-8">backup_vanilla <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_vanilla_factory.get_option(d25_call_strike)</span>
<span id="cb8-9">FxVanillaOptionFactory.assign_analyitcal_price_engine(backup_vanilla, process_25c)</span>
<span id="cb8-10">barrier_3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_120b, notional, backup_vanilla)</span></code></pre></div></div>
</div>
<p>4th Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BPut%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.05%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20p%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BDown%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="649fac07" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 4</span></span>
<span id="cb9-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb9-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span></span>
<span id="cb9-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.DownOut</span>
<span id="cb9-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb9-6"></span>
<span id="cb9-7">barrier_25d_105b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> put_barrier_factory.get_option(d25_put_strike, exercise, barrier, barrier_type)</span>
<span id="cb9-8">barrier_4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_105b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>5th Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BPut%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.25%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20p%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="b240860f" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 5</span></span>
<span id="cb10-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2_000_000</span></span>
<span id="cb10-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span></span>
<span id="cb10-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpOut</span>
<span id="cb10-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb10-6"></span>
<span id="cb10-7">barrier_25d_105b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> put_barrier_factory.get_option(d25_put_strike, exercise, barrier, barrier_type)</span>
<span id="cb10-8">barrier_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_105b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<div id="3d676d41" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">book: List[BarrierPosition] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [barrier_1, barrier_2, barrier_3, barrier_4, barrier_5]</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ins <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book:</span>
<span id="cb11-4">    FxBarrierOptionFactory.assign_vanna_volga_price_engine(</span>
<span id="cb11-5">        ins.instrument, </span>
<span id="cb11-6">        atm_dvol_handle,</span>
<span id="cb11-7">        delta25_p_dvol_handle,</span>
<span id="cb11-8">        delta25_c_dvol_handle,</span>
<span id="cb11-9">        spot_handle,</span>
<span id="cb11-10">        usd_disc_ts,</span>
<span id="cb11-11">        eur_disc_ts</span>
<span id="cb11-12">    )</span></code></pre></div></div>
</div>
<p>Thus the premium of the option will be the following</p>
<div id="b1f70fa0" class="cell">
<div class="cell-output cell-output-display" data-execution_count="19">
<pre><code>[438.32143676364166,
 23882.627384424337,
 13961.757334147904,
 403.5348985724314,
 27478.97645781696]</code></pre>
</div>
</div>
<p>In the Vanna-Volga case, since we don’t have a single vol but a smile, the sensitivities that we care about in the case of hedging are slightly different from the sensitivity that we look at in the Black-Scholes world. We already know how to calculte those sensitivities using finte differences from the previous <a href="../../posts/post-with-code/exotic-book.html">notebook</a>, what we care now is how the VV price is sensible to the various movements of the volatility smile, in particular we care about how the price will change in case of:</p>
<ul>
<li>a parallel shift of the three volatilities;</li>
<li>a change in the difference between the two 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> wings;</li>
<li>an increase of the two wings with fixed ATM volatility</li>
</ul>
<p>In this way we should be able to capture the effect of a parallel, a twist and a convexity movements of the implied volatility surface. Mathematically speaking using finite differences we can get those sensitivities in the following way:</p>
<ul>
<li>Level</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BLevel%7D%20%5Capprox%20%5Cfrac%7BV(%5Csigma_%7BATM%7D%20+%20%5Cvarepsilon,%20%5Csigma_%7B25C%7D%20+%20%5Cvarepsilon,%20%5Csigma_%7B25P%7D%20+%20%5Cvarepsilon)%20-%20V(%5Csigma_%7BATM%7D%20-%20%5Cvarepsilon,%20%5Csigma_%7B25C%7D%20-%20%5Cvarepsilon,%20%5Csigma_%7B25P%7D%20-%20%5Cvarepsilon)%7D%7B2%20%5Cvarepsilon%7D%0A"></p>
<ul>
<li>Skew Sensitivity</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSkew%7D%20%5Capprox%20%5Cfrac%7BV(%5Csigma_%7BATM%7D,%20%5Csigma_%7B25C%7D%20+%20%5Cvarepsilon%20/%202,%20%5Csigma_%7B25P%7D%20-%20%5Cvarepsilon%20/%202)%20-%20V(%5Csigma_%7BATM%7D,%20%5Csigma_%7B25C%7D%20-%20%5Cvarepsilon%20/%202,%20%5Csigma_%7B25P%7D%20+%20%5Cvarepsilon%20/%202)%7D%7B2%20%5Cvarepsilon%7D%0A"></p>
<ul>
<li>Curvature</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCurvature%7D%20%5Capprox%20%5Cfrac%7BV(%5Csigma_%7BATM%7D,%20%5Csigma_%7B25C%7D%20+%20%5Cvarepsilon,%20%5Csigma_%7B25P%7D%20+%20%5Cvarepsilon)%20-%20V(%5Csigma_%7BATM%7D,%20%5Csigma_%7B25C%7D%20-%20%5Cvarepsilon%20,%20%5Csigma_%7B25P%7D%20-%20%5Cvarepsilon)%7D%7B2%20%5Cvarepsilon%7D%0A"></p>
<div id="c82bc36c" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> central_diff(instrument: ql.Instrument, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, quote: ql.SimpleQuote) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb13-2">    u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote.value()</span>
<span id="cb13-3">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-4">    P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-5">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-6">    P_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-7"></span>
<span id="cb13-8">    quote.setValue(u0)</span>
<span id="cb13-9">    </span>
<span id="cb13-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h)</span>
<span id="cb13-11"></span>
<span id="cb13-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> central_diff_2nd(instrument: ql.Instrument, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, quote: ql.SimpleQuote) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb13-13">    u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote.value()</span>
<span id="cb13-14">    P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-15">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-16">    P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-17">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-18">    P_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-19"></span>
<span id="cb13-20">    quote.setValue(u0)</span>
<span id="cb13-21">    </span>
<span id="cb13-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h)</span>
<span id="cb13-23"></span>
<span id="cb13-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cross_central_diff(instrument: ql.Instrument, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, k: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, quote_1: ql.SimpleQuote, quote_2: ql.SimpleQuote) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb13-25">    u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote_1.value()</span>
<span id="cb13-26">    u2_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote_2.value()</span>
<span id="cb13-27">    </span>
<span id="cb13-28">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-29">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k)</span>
<span id="cb13-30">    P_Plus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-31"></span>
<span id="cb13-32">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-33">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k)</span>
<span id="cb13-34">    P_Plus_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-35"></span>
<span id="cb13-36">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-37">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k)</span>
<span id="cb13-38">    P_Minus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-39"></span>
<span id="cb13-40">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-41">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k)</span>
<span id="cb13-42">    P_Minus_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-43"></span>
<span id="cb13-44">    quote_1.setValue(u1_0)</span>
<span id="cb13-45">    quote_2.setValue(u2_0)</span>
<span id="cb13-46">    </span>
<span id="cb13-47">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Plus_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Minus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_Minus_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k)</span>
<span id="cb13-48"></span>
<span id="cb13-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> parallel_shift(instrument: ql.Instrument, quotes: List[ql.SimpleQuote], h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>):</span>
<span id="cb13-50">    u0s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [quote.value() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> quote <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> quotes]</span>
<span id="cb13-51"></span>
<span id="cb13-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> quote, u0 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(quotes, u0s):</span>
<span id="cb13-53">        quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-54">    </span>
<span id="cb13-55">    P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-56"></span>
<span id="cb13-57">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> quote, u0 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(quotes, u0s):</span>
<span id="cb13-58">        quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-59">    </span>
<span id="cb13-60">    P_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-61"></span>
<span id="cb13-62">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> quote, u0 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(quotes, u0s):</span>
<span id="cb13-63">        quote.setValue(u0)</span>
<span id="cb13-64"></span>
<span id="cb13-65">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>h) </span>
<span id="cb13-66"></span>
<span id="cb13-67"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> skew_sensitivity(instrument: ql.Instrument, d25_c_quote: ql.SimpleQuote, d25_p_quote: ql.SimpleQuote, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb13-68">    u1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d25_c_quote.value()</span>
<span id="cb13-69">    u2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d25_p_quote.value()</span>
<span id="cb13-70"></span>
<span id="cb13-71">    d25_c_quote.setValue(u1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-72">    d25_p_quote.setValue(u2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-73"></span>
<span id="cb13-74">    instrument.recalculate()</span>
<span id="cb13-75">    up_rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-76"></span>
<span id="cb13-77">    d25_c_quote.setValue(u1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-78">    d25_p_quote.setValue(u2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-79"></span>
<span id="cb13-80">    instrument.recalculate()</span>
<span id="cb13-81">    down_rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-82"></span>
<span id="cb13-83">    d25_c_quote.setValue(u1)</span>
<span id="cb13-84">    d25_p_quote.setValue(u2)</span>
<span id="cb13-85"></span>
<span id="cb13-86">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (up_rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> down_rr) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h)</span>
<span id="cb13-87"></span>
<span id="cb13-88"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> curv_sensitivity(instrument: ql.Instrument, d25_c_quote: ql.SimpleQuote, d25_p_quote: ql.SimpleQuote, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb13-89">    u1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d25_c_quote.value()</span>
<span id="cb13-90">    u2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d25_p_quote.value()</span>
<span id="cb13-91"></span>
<span id="cb13-92">    d25_c_quote.setValue(u1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-93">    d25_p_quote.setValue(u2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb13-94"></span>
<span id="cb13-95">    up_bf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-96"></span>
<span id="cb13-97">    d25_c_quote.setValue(u1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-98">    d25_p_quote.setValue(u2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb13-99"></span>
<span id="cb13-100">    down_bf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb13-101"></span>
<span id="cb13-102">    d25_c_quote.setValue(u1)</span>
<span id="cb13-103">    d25_p_quote.setValue(u2)</span>
<span id="cb13-104"></span>
<span id="cb13-105">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (up_bf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> down_bf) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h)</span></code></pre></div></div>
</div>
<p>The plot below gives a visual clue on what those sensitivities measure:</p>
<div id="bae0d0ce" class="cell">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-vanna-volga_files/figure-html/cell-22-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>To give you a more naive mathematical intuition, let’s assume that a vol smile is represented by the following 2nd order polynomial (a very likely assumption):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma(x)%20=%20a%20+%20b%20%5Ccdot%20x%20+%20c%20%5Ccdot%20x%5E2,%20%5Ctext%7B%20where%20%7D%20a,%20b,%20c%20%5Cin%20%5Cmathbb%7BR%7D%0A"></p>
<ul>
<li>in the level sensitivity we are varying <img src="https://latex.codecogs.com/png.latex?a"></li>
<li>when calculating the skew we are varying <img src="https://latex.codecogs.com/png.latex?b"></li>
<li>while when calculating the curvature we are varying <img src="https://latex.codecogs.com/png.latex?c"></li>
</ul>
<p>So we want to know how varying each of the function parameters effect the whole volatility smile, which in turns effected the option price.</p>
<div id="74aae4de" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">delta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote)</span>
<span id="cb14-2">vega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>atm_vol_quote)</span>
<span id="cb14-3">gamma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff_2nd, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote)</span>
<span id="cb14-4">volga <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff_2nd, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>atm_vol_quote)</span>
<span id="cb14-5">vanna <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(cross_central_diff, quote_1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote, quote_2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>atm_vol_quote)</span></code></pre></div></div>
</div>
</section>
<section id="hedging-the-book" class="level2">
<h2 class="anchored" data-anchor-id="hedging-the-book">Hedging the book</h2>
<p>As explained in the <a href="../../posts/post-with-code/exotic-book.html">Hedging a book of exotics options</a>, we want to keep low exposure also to the other high order greeks, but since we don’t have a single vol but smile for a specific tenor we want to hedge ourselves from the possible movements of that smile as described above.</p>
<section id="hedge-methodology" class="level3">
<h3 class="anchored" data-anchor-id="hedge-methodology">Hedge methodology</h3>
<p>Let our exposure vector be:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BE%7D%20=%20%5Cbegin%7Bbmatrix%7D%20%5CDelta%20%5C%5C%20%5Ctext%7BLevel%7D%20%5C%5C%20%5Ctext%7BSkew%7D%20%5C%5C%20%5Ctext%7BCurvature%7D%20%20%5Cend%7Bbmatrix%7D.%0A"></p>
<p>and let our hedge matrix be:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20=%20%5Cleft%5B%0A%5Cbegin%7Barray%7D%7Bc%7Ccccc%7D%0A%20%20%20%20&amp;%20%5Ctext%7BSpot%7D%20&amp;%20%5Ctext%7BStraddle%7D%20&amp;%20%5Ctext%7BRR%7D%20&amp;%20%5Ctext%7BButterfly%7D%20%5C%5C%0A%20%20%20%20%5Chline%0A%20%20%20%20%5CDelta%20%20%20&amp;%20h_%7B11%7D%20&amp;%20h_%7B12%7D%20&amp;%20h_%7B13%7D%20&amp;%20h_%7B14%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BLevel%7D%20%20%20&amp;%20h_%7B21%7D%20&amp;%20h_%7B22%7D%20&amp;%20h_%7B23%7D%20&amp;%20h_%7B24%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BSkew%7D%20%20&amp;%20h_%7B31%7D%20&amp;%20h_%7B32%7D%20&amp;%20h_%7B33%7D%20&amp;%20h_%7B34%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BCurvature%7D%20%20&amp;%20h_%7B41%7D%20&amp;%20h_%7B42%7D%20&amp;%20h_%7B43%7D%20&amp;%20h_%7B44%7D%20%5C%5C%0A%5Cend%7Barray%7D%0A%5Cright%5D%0A"></p>
<p>Then we can solve the following linear system and find the weights <img src="https://latex.codecogs.com/png.latex?x"> (position sizes) just by:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax%20=%20H%5E%7B-1%7D%20E%0A"></p>
<p>When we want to calculate the greeks of our portfolio, we want to do it in meaningful way, thus this requires to us to set the shift operator in a way that makes sense to measure the specific risk of our porfolio. The two main ways that industry professional do that is by using 1% of the spot change and 1% vol change.</p>
<div id="9911c00b" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">spot_change <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb15-2">vol_change <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span></span>
<span id="cb15-3">quotes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [atm_vol_quote, delta25_call_vol_quote, delta25_put_vol_quote]</span>
<span id="cb15-4"></span>
<span id="cb15-5">delta_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(central_diff, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb15-6">level_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(parallel_shift, quotes<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>quotes), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb15-7">skew_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(skew_sensitivity, </span>
<span id="cb15-8">                                         d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote,</span>
<span id="cb15-9">                                         d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb15-10">curv_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(curv_sensitivity, </span>
<span id="cb15-11">                                         d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote,</span>
<span id="cb15-12">                                         d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span></code></pre></div></div>
</div>
<p>The raw Greek exposition for our book is then the following:</p>
<div id="80fb9053" class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Book Delta exposure: -115632.67827744933
Book Level exposure: 718295.615419507
Book Skew exposure: 137760.4070661429
Book Curvature Call exposure: 417234.2386220757</code></pre>
</div>
</div>
<p>To get a more meaningful value of the sensitivities needed for the trader’s perspective, we need to multiply the <img src="https://latex.codecogs.com/png.latex?%5CDelta"> by the spot value and the smile sensitivities by the <img src="https://latex.codecogs.com/png.latex?%5C%25"> of vol that we’ve used to measure those sensitivities (thus in our case by <img src="https://latex.codecogs.com/png.latex?1%5C%25">).</p>
<div id="78c8cd1b" class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Book Delta exposure: $-136446.5603673902
Residual Level exposure: $7182.956154195071
Residual Skew exposure: $1377.6040706614292
Residual Curvature exposure: $4172.342386220757</code></pre>
</div>
</div>
<p>Our exposure vector <img src="https://latex.codecogs.com/png.latex?E%20=%20%5B%20%5CDelta,%20%5Ctext%7BLevel%7D,%20%5Ctext%7BSkew%7D,%20%5Ctext%7BCurvature%7D%20%5D"> is:</p>
<div id="c98d1b8b" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Greek exposure vector</span></span>
<span id="cb18-2">E <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([delta_book, level_book, skew_book, curv_book])</span>
<span id="cb18-3">E</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="27">
<pre><code>array([-115632.67827745,  718295.61541951,  137760.40706614,
        417234.23862208])</code></pre>
</div>
</div>
<p>For each of the base instrument (Spot, ATM call, ATM put, <img src="https://latex.codecogs.com/png.latex?25%5CDelta"> call and <img src="https://latex.codecogs.com/png.latex?25%5CDelta"> put) let’s calculate the basic greeks.</p>
<div id="de221244" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hedge_matrix</span></span>
<span id="cb20-2">base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.float64(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span>)</span>
<span id="cb20-3">spot_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb20-4">atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb20-5">    central_diff(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Delta</span></span>
<span id="cb20-6">    parallel_shift(atm_call, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Level</span></span>
<span id="cb20-7">    skew_sensitivity(atm_call,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skew</span></span>
<span id="cb20-8">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-9">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-10">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-11">    curv_sensitivity(atm_call, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Curvature</span></span>
<span id="cb20-12">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-13">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-14">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb20-15">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb20-16"></span>
<span id="cb20-17">atm_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb20-18">    delta(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb20-19">    parallel_shift(atm_put, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-20">    skew_sensitivity(atm_put, </span>
<span id="cb20-21">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-22">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-23">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-24">    curv_sensitivity(atm_put, </span>
<span id="cb20-25">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-26">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-27">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb20-28">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb20-29"></span>
<span id="cb20-30">delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb20-31">    delta(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb20-32">    parallel_shift(delta25_call, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-33">    skew_sensitivity(delta25_call, </span>
<span id="cb20-34">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-35">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-36">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-37">    curv_sensitivity(delta25_call, </span>
<span id="cb20-38">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-39">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-40">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb20-41">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb20-42"></span>
<span id="cb20-43">delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb20-44">    delta(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb20-45">    parallel_shift(delta25_put, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-46">    skew_sensitivity(delta25_put, </span>
<span id="cb20-47">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-48">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-49">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb20-50">    curv_sensitivity(delta25_put, </span>
<span id="cb20-51">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb20-52">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb20-53">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb20-54">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
</div>
<div id="56e7092f" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's create the straddle, RR, Butterfly from the base vanilla options</span></span>
<span id="cb21-2">atm_straddle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put_greeks</span>
<span id="cb21-3">rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put_greeks</span>
<span id="cb21-4">butterfly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> atm_straddle</span>
<span id="cb21-5"></span>
<span id="cb21-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hedge Matrix</span></span>
<span id="cb21-7">H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.stack([spot_greeks, atm_straddle, rr, butterfly]).T</span></code></pre></div></div>
</div>
<p>Since our system the number of equations is the same as the number of variables, thus can find the optiomal solution of the system by using <code>np.linalg.solve</code></p>
<div id="633ae825" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.solve(H, E)</span></code></pre></div></div>
</div>
<p>Now that we have a solution let’s see what’s our remaining exposure to each of the choosen hedged greeks. We do that by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20x%20-%20E%0A"></p>
<div id="0479b272" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">residual_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E).tolist()</span>
<span id="cb23-2"></span>
<span id="cb23-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Delta exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Level exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Skew exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb23-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Curvature exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Residual Delta exposure: $0.0
Residual Level exposure: $1.1641532182693482e-12
Residual Skew exposure: $0.0
Residual Curvature exposure: $0.0</code></pre>
</div>
</div>
<p>Another crucial thing when hedging a book is, of course, to measure the P&amp;L from one rebalancing period to another. Let’s then calculate the value of the various components of our hedging portfolio. We have the the P&amp;L from time <img src="https://latex.codecogs.com/png.latex?t"> is calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP%5C&amp;L_%7Bt%20%5Crightarrow%20t%20+%201%7D%20=%20(V_%7Bt%20+%201%7D%5E%7Bbook%7D%20+%20V_%7Bt%20+%201%7D%5E%7Bhedge%7D%20+%20C_%7Bt%20+%201%7D)%20-%20(V_%7Bt%7D%5E%7Bbook%7D%20+%20V_%7Bt%7D%5E%7Bhedge%7D%20+%20C_%7Bt%7D)%0A"></p>
<p>where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?V_%7Bt%7D%5E%7Bbook%7D"> is the value of our book at time <img src="https://latex.codecogs.com/png.latex?t">, which is the sum of the premiums that we’ve been collected</li>
<li><img src="https://latex.codecogs.com/png.latex?V_%7Bt%7D%5E%7Bhedge%7D"> is the value of the hedging portfolio, the sum of the premium of the structures that we have bought to hedge our from the various sensistivies</li>
<li><img src="https://latex.codecogs.com/png.latex?C_t"> the cash/bank account of our portfolio which includes financing of cash and trade cashflows</li>
</ul>
<p>A shorter way to express the P&amp;L, is just by considering the variation of the value of the book + the variation of the value of the hedges + cost of borrowing + interest accrued:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP%5C&amp;L_%7Bt%20%5Crightarrow%20t%20+%201%7D%20=%20%5Cdelta%20V_%7Bt,%20t%20+%201%7D%5E%7Bbook%7D%20+%20%5Cdelta%20V_%7Bt,%20t%20+%201%7D%5E%7Bhedge%7D%20+%20%5Ctext%7Bcost%20of%20borrowing%7D%20+%20%5Ctext%7Binterest%20accrued%7D%0A"></p>
<p>since the rebalancing of the hedges of the bank account happend at the end of trading day.</p>
<div id="ca4e1d80" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">spot_premium_eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb25-2">spot_premium_usd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb25-3">atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb25-4">rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb25-5">butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV())) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
</div>
<p>The usd borrowed (which is the sum of the premiums of or hedging instrument) is crucial for use to calculate the interest accrued for borrowing the usd dollars to buy the hedging structures.</p>
<div id="1749d718" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1">hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> spot_premium_usd</span>
<span id="cb26-2">hedging_str_premium</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="33">
<pre><code>np.float64(-543905.6415990698)</code></pre>
</div>
</div>
<p>Thus the value of the cash at time <img src="https://latex.codecogs.com/png.latex?t%20=%200"> is the equal to all the cash that we’ve borrowed to build the hedging structures plus the premiums that we’ve been collected from selling the exotics options</p>
<div id="18d33bd7" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">bank_account_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(premiums)</span>
<span id="cb28-2">bank_account_value</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="34">
<pre><code>np.float64(610070.8591107951)</code></pre>
</div>
</div>
<div id="8868353e" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1">port_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bank_account_value]</span>
<span id="cb30-2"></span>
<span id="cb30-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The portfolio value at time t = 0 is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>port_value[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>The portfolio value at time t = 0 is 0.0</code></pre>
</div>
</div>
</section>
</section>
<section id="changes-of-the-hedging-portfolio-with-the-changes-of-the-market-conditions" class="level2">
<h2 class="anchored" data-anchor-id="changes-of-the-hedging-portfolio-with-the-changes-of-the-market-conditions">Changes of the hedging portfolio with the changes of the market conditions</h2>
<p>Now that we have setup our hedging portfolio, let’s verify how it will change as the spot and vol smile change. Let’s assume that in a week period the spot went go down by 300 pips, the ATM vol went up by <img src="https://latex.codecogs.com/png.latex?0.5%5C%25"> while the the 25 <img src="https://latex.codecogs.com/png.latex?%5CDelta"> Put vol went up <img src="https://latex.codecogs.com/png.latex?0.8%5C%25"> (since the price to hedge from downside has went up because of the underlying so there is more demand for puts).</p>
<div id="46f7f1b5" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1">ref_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, ql.Days)</span>
<span id="cb32-2">expiration_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc.yearFraction(ref_date, expiration_date)</span>
<span id="cb32-3">exp_time_quote.setValue(expiration_time)</span>
<span id="cb32-4"></span>
<span id="cb32-5">spot_quote.setValue(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.15</span>)</span>
<span id="cb32-6">atm_vol_quote.setValue(atm_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.005</span>)</span>
<span id="cb32-7">delta25_put_vol_quote.setValue(delta25_put_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.008</span>)</span>
<span id="cb32-8"></span>
<span id="cb32-9">new_d25_call_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_call_calc.strikeFromDelta(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb32-10">new_d25_put_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_put_calc.strikeFromDelta(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb32-11">new_atm_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_call_calc.atmStrike(ql.DeltaVolQuote.AtmDeltaNeutral)</span>
<span id="cb32-12"></span>
<span id="cb32-13">new_smile <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SplineCubicInterpolatedSmileSection(</span>
<span id="cb32-14">    expiration_time, </span>
<span id="cb32-15">    [new_d25_put_strike, new_atm_strike, new_d25_call_strike],</span>
<span id="cb32-16">    [delta25_p_std_handle, atm_std_handle, delta25_c_std_handle],</span>
<span id="cb32-17">    atm_vol_handle</span>
<span id="cb32-18">)</span>
<span id="cb32-19"></span>
<span id="cb32-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Move vols and expiry time</span></span>
<span id="cb32-21">ql.Settings.instance().evaluationDate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ref_date</span></code></pre></div></div>
</div>
<p>Here’s how the new smile looks like compared to the old the smile:</p>
<div id="ec53cd58" class="cell">
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://your-website-url.example.com/posts/post-with-code/exotic-book-vanna-volga_files/figure-html/cell-38-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="60870507" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1">new_premiums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [pos.NPV() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book]</span></code></pre></div></div>
</div>
<p>With the new spot and vols let’s recalculate the book exposure</p>
<div id="41fbdcb2" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1">delta_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(central_diff, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb34-2">level_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(parallel_shift, quotes<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>quotes), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb34-3">skew_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(skew_sensitivity, </span>
<span id="cb34-4">                                         d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote,</span>
<span id="cb34-5">                                         d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb34-6">curv_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(partial(curv_sensitivity, </span>
<span id="cb34-7">                                         d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote,</span>
<span id="cb34-8">                                         d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote), h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb34-9"></span>
<span id="cb34-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># new Exposure vector</span></span>
<span id="cb34-11">E <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([delta_book, level_book, skew_book, curv_book])</span></code></pre></div></div>
</div>
<div id="01279afc" class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Book Delta exposure: $-426252.06994998624
Residual Level exposure: $6494.259093454982
Residual Skew exposure: $1365.6531019809127
Residual Curvature exposure: $5016.983190482754</code></pre>
</div>
</div>
<p>As before let’s recalculate the greeks for the basic trading instruments, and then combine to obtain the greeks for the structures used in the hedging portfolio.</p>
<div id="691706c9" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hedge_matrix</span></span>
<span id="cb36-2">base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.float64(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span>)</span>
<span id="cb36-3">spot_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb36-4">atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb36-5">    central_diff(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Delta</span></span>
<span id="cb36-6">    parallel_shift(atm_call, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Level</span></span>
<span id="cb36-7">    skew_sensitivity(atm_call,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Skew</span></span>
<span id="cb36-8">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-9">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-10">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-11">    curv_sensitivity(atm_call, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Curvature</span></span>
<span id="cb36-12">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-13">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-14">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb36-15">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb36-16"></span>
<span id="cb36-17">atm_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb36-18">    delta(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb36-19">    parallel_shift(atm_put, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-20">    skew_sensitivity(atm_put, </span>
<span id="cb36-21">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-22">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-23">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-24">    curv_sensitivity(atm_put, </span>
<span id="cb36-25">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-26">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-27">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb36-28">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb36-29"></span>
<span id="cb36-30">delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb36-31">    delta(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb36-32">    parallel_shift(delta25_call, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-33">    skew_sensitivity(delta25_call, </span>
<span id="cb36-34">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-35">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-36">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-37">    curv_sensitivity(delta25_call, </span>
<span id="cb36-38">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-39">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-40">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb36-41">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb36-42"></span>
<span id="cb36-43">delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb36-44">    delta(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb36-45">    parallel_shift(delta25_put, quotes, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-46">    skew_sensitivity(delta25_put, </span>
<span id="cb36-47">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-48">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-49">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb36-50">    curv_sensitivity(delta25_put, </span>
<span id="cb36-51">                     d25_c_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_call_vol_quote, </span>
<span id="cb36-52">                     d25_p_quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>delta25_put_vol_quote,</span>
<span id="cb36-53">                     h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change)</span>
<span id="cb36-54">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
</div>
<div id="ffc3b8b9" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's create the straddle, RR, Butterfly from the base vanilla options</span></span>
<span id="cb37-2">atm_straddle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put_greeks</span>
<span id="cb37-3">rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put_greeks</span>
<span id="cb37-4">butterfly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> atm_straddle</span>
<span id="cb37-5"></span>
<span id="cb37-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hedge Matrix</span></span>
<span id="cb37-7">H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.stack([spot_greeks, atm_straddle, rr, butterfly]).T</span></code></pre></div></div>
</div>
<p>The esposure of our hedging portfolio is now is:</p>
<div id="d7477585" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New exposure of the hedging portfolio</span></span>
<span id="cb38-2">exposure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E).tolist()</span>
<span id="cb38-3"></span>
<span id="cb38-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Delta exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb38-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Level exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb38-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Skew exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb38-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Curvature exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Residual Delta exposure: $2738.4458590258755
Residual Level exposure: $-67.84857193228905
Residual Skew exposure: $-411.47083519745325
Residual Curvature exposure: $-1701.7232883233276</code></pre>
</div>
</div>
<p>To calculate the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BP%5C&amp;L%7D"> from time <img src="https://latex.codecogs.com/png.latex?t"> to <img src="https://latex.codecogs.com/png.latex?t+1"> we need to calculate the change of the value of the hedging portfolio as well</p>
<div id="3b9f7ea7" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb40-1">spot_premium_usd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb40-2">atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb40-3">rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb40-4">butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV())) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb40-5">new_hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> spot_premium_usd</span></code></pre></div></div>
</div>
<div id="32ce846d" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc.yearFraction(today, today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, ql.Days))</span>
<span id="cb41-2">usd_interest_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt</span>
<span id="cb41-3">eur_interest_accr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spot_premium_eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt</span>
<span id="cb41-4">new_total_portfolio_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(new_premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bank_account_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> usd_interest_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> eur_interest_accr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb41-5"></span>
<span id="cb41-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Portfolio value at time t = 1 is: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>new_total_portfolio_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Portfolio value at time t = 1 is: $30021.90322561409</code></pre>
</div>
</div>
<div id="333222b4" class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>The P&amp;L after a week is: $30021.90322561409</code></pre>
</div>
</div>
<p>If we want to reduce our exposure and bring it back to a lower one, we need to recalculate the optimal hedging weights for our hedging portfolio as before:</p>
<div id="4f8d6f2e" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb44-1">x_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.solve(H, E)</span></code></pre></div></div>
</div>
<p>This how our hedging portfolio has to change:</p>
<div id="9641ecf3" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1">(x_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="51">
<pre><code>array([-101803.29731514,  418687.61215821,  208353.40317859,
        322112.47789261])</code></pre>
</div>
</div>
<p>The residual greek exposure after the rebalancing is:</p>
<div id="89c20265" class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>Residual Delta exposure: $1.3387762010097503e-10
Residual Level exposure: $0.0
Residual Skew exposure: $0.0
Residual Curvature exposure: $0.0</code></pre>
</div>
</div>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-vv-article" class="csl-entry">
Castagna, Antonio, and Fabio Mercurio. 2007. <span>“The Vanna-Volga Method for Implied Volatilities.”</span> <em>Risk</em>, January, 106–11.
</div>
</div></section></div> ]]></description>
  <category>Quantitative finance</category>
  <category>Hedging</category>
  <category>Exotic options</category>
  <guid>https://your-website-url.example.com/posts/post-with-code/exotic-book-vanna-volga.html</guid>
  <pubDate>Sat, 24 Jan 2026 00:00:00 GMT</pubDate>
  <media:content url="https://your-website-url.example.com/posts/post-with-code/vv-taylor.png" medium="image" type="image/png" height="50" width="144"/>
</item>
<item>
  <title>Hedging a book of exotic options</title>
  <link>https://your-website-url.example.com/posts/post-with-code/exotic-book.html</link>
  <description><![CDATA[ 





<p>In this notebook, we take on the role of a trader working on an investment bank’s exotic options desk, focusing on FX derivatives. We will explore how such traders hedge their books and how the portfolio’s value and Greek exposures change as the underlying market parameters evolve. To keep things simple, we assume the underlying follows a geometric Brownian motion, with flat term structures for both the volatility surface and the domestic and foreign interest rates. This places us in a Black-Scholes framework (specifically the BS-extended model for FX options, the Garman-Kohlhagen model), described by the following dynamics under the risk-neutral measure:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0AdS_t%20&amp;=%20(r_d%20-%20r_f)%20S_t%20%5C,%20dt%20+%20%5Csigma%20S_t%20%5C,%20dW_t%5Ed,%20%5C%5C%0AdB%5Ed_t%20&amp;=%20r_d%20B%5Ed_t%5C,%20dt,%20%5C%5C%0AdB%5Ef_t%20&amp;=%20r_f%20B%5Ef_t%5C,%20dt%0A%5Cend%7Balign*%7D%0A"></p>
<p>The investments banks are said to be on the “sell-side”, which basically means that they facilitate transactions between institutions, and provide access to financial markets. In our case we are manifacturing exotics options for our clients, who are seeking market opportunities. This leaves the trader with a significant exposition, as the bank take the opposite side of client trades. To manage the risk and ensure the bank meet its obligations when options are exercised, the trader must hedge against market movements.</p>
<p>The hedging of exotics does not only involves hedging first order greeks (the <img src="https://latex.codecogs.com/png.latex?%5CDelta"> and the <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BV%7D">), but its involves hedging high order greeks like <img src="https://latex.codecogs.com/png.latex?%5CGamma">, <img src="https://latex.codecogs.com/png.latex?Vanna"> (DvolDspot) and <img src="https://latex.codecogs.com/png.latex?Volga"> (DvegaDvol). Another completixity when hedging exotics option, especially when dealing with binary or barriers, pin risks needs to be taken into account when the barrier is hit.</p>
<p>The library we use for option pricing is <code>QuantLib</code>, the largest open-source quantitative finance library available. <code>QuantLib</code> offers a comprehensive suite of tools for modeling, pricing, and risk management of financial derivatives. Its active community and extensive documentation make it a popular choice among both practitioners and researchers in the field.</p>
<section id="market-data" class="level2">
<h2 class="anchored" data-anchor-id="market-data">Market Data</h2>
<p>Let’s assume that we are an USD based desk and we are dealing with EURUSD exotics. The initial market data at time <img src="https://latex.codecogs.com/png.latex?t%20=%200"> is the following</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20S_0%20&amp;=%201.18%20%5C%5C%0A%20%20%20%20Date(Today)%20&amp;=%2016/09/2025%20%5C%5C%0A%20%20%20%20T%20&amp;=%200.5%20%5Ctext%7B%20(time%20to%20expiration%20in%20years)%7D%20%5C%5C%0A%20%20%20%20r_d%20&amp;=%200.045%20%5C%5C%0A%20%20%20%20r_f%20&amp;=%200.02%20%5C%5C%0A%20%20%20%20%5Csigma%20&amp;=%200.13%20%5C%5C%0A%5Cend%7Balign*%7D%0A"></p>
<div id="2557c9e5" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">EUR_USD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.18</span></span>
<span id="cb1-2">spot_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(EUR_USD)</span>
<span id="cb1-3">today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, ql.September, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>)</span>
<span id="cb1-4">r_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.045</span></span>
<span id="cb1-5">r_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.02</span></span>
<span id="cb1-6">vol_quote <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.SimpleQuote(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.13</span>)</span>
<span id="cb1-7">spot_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(spot_quote)</span>
<span id="cb1-8">vol_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.QuoteHandle(vol_quote)</span>
<span id="cb1-9"></span>
<span id="cb1-10">dc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Actual365Fixed()</span>
<span id="cb1-11">calendar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.JointCalendar(ql.Italy(), ql.UnitedStates(ql.UnitedStates.NYSE))</span>
<span id="cb1-12"></span>
<span id="cb1-13">expiration_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, ql.Months)</span>
<span id="cb1-14">expiration_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc.yearFraction(today, expiration_date)</span>
<span id="cb1-15">domestic_rf_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(ql.FlatForward(today, r_d, dc))</span>
<span id="cb1-16">foreign_rf_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.YieldTermStructureHandle(ql.FlatForward(today, r_f, dc))</span>
<span id="cb1-17">black_vol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackConstantVol(today, ql.NullCalendar(), vol_handle, dc)</span>
<span id="cb1-18">vol_ts_handle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackVolTermStructureHandle(black_vol)</span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting the global evaluation date</span></span>
<span id="cb1-21">ql.Settings.instance().evaluationDate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today</span></code></pre></div></div>
</div>
<div id="b3ed7f4e" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">eur_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span>)]</span>
<span id="cb2-2">eur_dfs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>]</span>
<span id="cb2-3">eur_curve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DiscountCurve(eur_dates, eur_dfs, dc)</span>
<span id="cb2-4">eur_curve.enableExtrapolation()</span>
<span id="cb2-5"></span>
<span id="cb2-6">usd_dates <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2026</span>), ql.Date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, ql.August, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span>)]</span>
<span id="cb2-7">usd_dfs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.985</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.96</span>]</span>
<span id="cb2-8">usd_curve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.DiscountCurve(usd_dates, usd_dfs, dc)</span>
<span id="cb2-9">usd_curve.enableExtrapolation()</span></code></pre></div></div>
</div>
<p>The forex option market has a particular way of quoting options, the strike prices are quoted in terms of the Delta of the option: this means that before closing the deal, the strike level is not determined yet in absolute terms. Once the deal is closed, given the level of FX spot rate and the IV agreed upon, the strike will be set at a level yielding the BS Delta the two counterparties were dealing. This way of quoting is smart: it allows us not to worry about small movements of the underlying during the bargaining process, because the absolute strike will be defined only after the agreement on the price, so that the trader is sure to trade an option with given features in terms of exposures both to the underlying asset and to the implied volatility.</p>
<p>The most liquid FX optios are usually the 25-<img src="https://latex.codecogs.com/png.latex?%5CDelta"> calls and put and the ATM calls and puts, thus those are going to be our basic hedging instruments and the strikes that we are going to refer to for the exotic in our book.</p>
<p>Here is an example on how FX volatility are quoted:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?10%5CDelta%20p"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?25%5CDelta%20p"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?35%5CDelta%20p"></th>
<th style="text-align: left;">ATM</th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?35%5CDelta%20c"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?25%5CDelta%20c"></th>
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?10%5CDelta%20c"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><strong>1W</strong></td>
<td style="text-align: left;">11.96%</td>
<td style="text-align: left;">11.69%</td>
<td style="text-align: left;">11.67%</td>
<td style="text-align: left;">11.75%</td>
<td style="text-align: left;">11.94%</td>
<td style="text-align: left;">12.19%</td>
<td style="text-align: left;">12.93%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>2W</strong></td>
<td style="text-align: left;">11.81%</td>
<td style="text-align: left;">11.54%</td>
<td style="text-align: left;">11.52%</td>
<td style="text-align: left;">11.60%</td>
<td style="text-align: left;">11.79%</td>
<td style="text-align: left;">12.04%</td>
<td style="text-align: left;">12.78%</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>1M</strong></td>
<td style="text-align: left;">11.60%</td>
<td style="text-align: left;">11.39%</td>
<td style="text-align: left;">11.39%</td>
<td style="text-align: left;">11.50%</td>
<td style="text-align: left;">11.72%</td>
<td style="text-align: left;">11.99%</td>
<td style="text-align: left;">12.77%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>2M</strong></td>
<td style="text-align: left;">11.43%</td>
<td style="text-align: left;">11.16%</td>
<td style="text-align: left;">11.15%</td>
<td style="text-align: left;">11.25%</td>
<td style="text-align: left;">11.48%</td>
<td style="text-align: left;">11.76%</td>
<td style="text-align: left;">12.60%</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>3M</strong></td>
<td style="text-align: left;">11.22%</td>
<td style="text-align: left;">10.92%</td>
<td style="text-align: left;">10.90%</td>
<td style="text-align: left;">11.00%</td>
<td style="text-align: left;">11.23%</td>
<td style="text-align: left;">11.52%</td>
<td style="text-align: left;">12.39%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>6M</strong></td>
<td style="text-align: left;">11.12%</td>
<td style="text-align: left;">10.78%</td>
<td style="text-align: left;">10.76%</td>
<td style="text-align: left;">10.87%</td>
<td style="text-align: left;">11.12%</td>
<td style="text-align: left;">11.43%</td>
<td style="text-align: left;">12.39%</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>9M</strong></td>
<td style="text-align: left;">11.04%</td>
<td style="text-align: left;">10.72%</td>
<td style="text-align: left;">10.71%</td>
<td style="text-align: left;">10.83%</td>
<td style="text-align: left;">11.09%</td>
<td style="text-align: left;">11.41%</td>
<td style="text-align: left;">12.39%</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>1Y</strong></td>
<td style="text-align: left;">11.00%</td>
<td style="text-align: left;">10.69%</td>
<td style="text-align: left;">10.68%</td>
<td style="text-align: left;">10.80%</td>
<td style="text-align: left;">11.06%</td>
<td style="text-align: left;">11.39%</td>
<td style="text-align: left;">12.38%</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>2Y</strong></td>
<td style="text-align: left;">11.02%</td>
<td style="text-align: left;">10.63%</td>
<td style="text-align: left;">10.60%</td>
<td style="text-align: left;">10.70%</td>
<td style="text-align: left;">10.94%</td>
<td style="text-align: left;">11.28%</td>
<td style="text-align: left;">12.34%</td>
</tr>
</tbody>
</table>
<p>To get the strike from the Delta-Vol quote QuantLib comes in handy with a <code>BlackDeltaCalculator</code> that from a given delta (and option type, discount factor, spot value, and delta-vol quote value) it returns the strike associated with it.</p>
<div id="0a5a4f4f" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">call_barrier_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxBarrierOptionFactory(spot_handle, ql.Option.Call)</span>
<span id="cb3-2">put_barrier_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxBarrierOptionFactory(spot_handle, ql.Option.Put)</span>
<span id="cb3-3">put_vanilla_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxVanillaOptionFactory(spot_handle, expiration_date, ql.Option.Put)</span>
<span id="cb3-4">call_vanilla_factory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FxVanillaOptionFactory(spot_handle, expiration_date, ql.Option.Call)</span>
<span id="cb3-5">delta_call_calc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackDeltaCalculator(</span>
<span id="cb3-6">    ql.Option.Call, </span>
<span id="cb3-7">    ql.DeltaVolQuote.Spot, </span>
<span id="cb3-8">    EUR_USD, </span>
<span id="cb3-9">    eur_curve.discount(expiration_time), </span>
<span id="cb3-10">    usd_curve.discount(expiration_time), </span>
<span id="cb3-11">    np.sqrt(black_vol.blackVariance(expiration_time, EUR_USD)))</span>
<span id="cb3-12">delta_put_calc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.BlackDeltaCalculator(</span>
<span id="cb3-13">    ql.Option.Put, </span>
<span id="cb3-14">    ql.DeltaVolQuote.Spot, </span>
<span id="cb3-15">    EUR_USD, </span>
<span id="cb3-16">    eur_curve.discount(expiration_time), </span>
<span id="cb3-17">    usd_curve.discount(expiration_time), </span>
<span id="cb3-18">    np.sqrt(black_vol.blackVariance(expiration_time, EUR_USD)))</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the strikes equivalent for the 0.25 delta for call and put options</span></span>
<span id="cb3-21">d25_call_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_call_calc.strikeFromDelta(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb3-22">d25_put_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_put_calc.strikeFromDelta(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb3-23">atm_strike <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_call_calc.atmStrike(ql.DeltaVolQuote.AtmDeltaNeutral)</span></code></pre></div></div>
</div>
<p>The model that extends the Black-Scholes model when dealing with FX options is the Garman-Kohlhagen model, whose dynamics have been defined above. Thus we need to create an instance of the Garman-Kohlhagen process in order to price FX options.</p>
<p>For the sake of simplicity we assume that all the options on the book have the same expiration, and again since we are under the Garman-Kohlhagen model the vol curve is flat, thus the vol across the different strikes is the same (unrealistic assumption). Here’s how the trader book is composed of:</p>
<p>1st Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.30%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="c96a95dd" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 1</span></span>
<span id="cb4-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb4-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.30</span></span>
<span id="cb4-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpOut</span>
<span id="cb4-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb4-6"></span>
<span id="cb4-7">barrier_25d_130b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(d25_call_strike, exercise, barrier, barrier_type)</span>
<span id="cb4-8">barrier_1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_130b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>2nd Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$2000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.08%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BDown%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="68f232a0" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 2</span></span>
<span id="cb5-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2_000_000</span></span>
<span id="cb5-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.08</span></span>
<span id="cb5-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.DownOut</span>
<span id="cb5-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb5-6"></span>
<span id="cb5-7">barrier_25d_108b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(d25_call_strike, exercise, barrier, barrier_type)</span>
<span id="cb5-8">barrier_2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_108b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>3rd Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BCall%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.20%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20c%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20In%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="047d8639" class="cell" data-execution_count="11">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 3</span></span>
<span id="cb6-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb6-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.20</span></span>
<span id="cb6-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpIn</span>
<span id="cb6-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb6-6"></span>
<span id="cb6-7">barrier_25d_120b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_barrier_factory.get_option(d25_call_strike, exercise, barrier, barrier_type)</span>
<span id="cb6-8">backup_vanilla <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> call_vanilla_factory.get_option(d25_call_strike)</span>
<span id="cb6-9">FxVanillaOptionFactory.assign_analyitcal_price_engine(backup_vanilla, process)</span>
<span id="cb6-10">barrier_3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_120b, notional, backup_vanilla)</span></code></pre></div></div>
</div>
<p>4th Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BPut%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.05%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20p%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BDown%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="f4ab3929" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 4</span></span>
<span id="cb7-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span></span>
<span id="cb7-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span></span>
<span id="cb7-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.DownOut</span>
<span id="cb7-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb7-6"></span>
<span id="cb7-7">barrier_25d_105b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> put_barrier_factory.get_option(d25_put_strike, exercise, barrier, barrier_type)</span>
<span id="cb7-8">barrier_4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_105b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<p>5th Barrier</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%20%20%20%20%5Ctext%7BOption%20type%7D%20&amp;=%20%5Ctext%7BPut%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BNotional%7D%20&amp;=%20%5C$1000000%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%7D%20&amp;=%201.25%20%5C%5C%0A%20%20%20%20%5Ctext%7BStrike%7D%20&amp;=%2025%5CDelta%20p%20%5C%5C%0A%20%20%20%20%5Ctext%7BBarrier%20Type%7D%20&amp;=%20%5Ctext%7BUp%20and%20Out%7D%0A%5Cend%7Balign*%7D%0A"></p>
<div id="0594cefb" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Barrier 5</span></span>
<span id="cb8-2">notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2_000_000</span></span>
<span id="cb8-3">barrier <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span></span>
<span id="cb8-4">barrier_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.Barrier.UpOut</span>
<span id="cb8-5">exercise <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ql.EuropeanExercise(expiration_date)</span>
<span id="cb8-6"></span>
<span id="cb8-7">barrier_25d_105b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> put_barrier_factory.get_option(d25_put_strike, exercise, barrier, barrier_type)</span>
<span id="cb8-8">barrier_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarrierPosition(barrier_25d_105b, notional, NullInstrument())</span></code></pre></div></div>
</div>
<div id="758ac5d5" class="cell" data-execution_count="14">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">book: List[BarrierPosition] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [barrier_1, barrier_2, barrier_3, barrier_4, barrier_5]</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ins <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book:</span>
<span id="cb9-4">    FxBarrierOptionFactory.assign_analyitcal_price_engine(ins.instrument, process)</span></code></pre></div></div>
</div>
<p>Thus the premium of the options will be the following</p>
<div id="f6f5d071" class="cell" data-execution_count="15">
<div class="cell-output cell-output-display" data-execution_count="15">
<pre><code>[309.09996820797926,
 36387.852679406504,
 18342.987983359482,
 2064.118402600054,
 25897.462236607375]</code></pre>
</div>
</div>
<p>Since the <code>AnalyticBarrierEngine</code> in QuantLib doesn’t provide greek calculation functionalities, the simplest method that we can use to calculate the greeks numerically is by using the finite difference method. Let’s the define the basic central differences formulas below, and then specialize them using a partial function application to get the greeks of interest.</p>
<p>First order Finite Difference:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af'(x)%20=%20%5Cfrac%7Bf(x%20+%20h)%20-%20f(x%20-%20h)%7D%7B2%20%5Ccdot%20h%7D%0A"></p>
<p>Second order Finite Difference:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af''(x)%20=%20%5Cfrac%7Bf(x%20+%20h)%20-%202f(x)%20+%20f(x%20-%20h)%7D%7Bh%5E2%7D%0A"></p>
<p>Cross partial derivative:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af_%7Bxy%7D(x,%20y)%20=%20%5Cfrac%7Bf(x%20+%20h,%20y%20+%20k)%20-%20f(x%20+h,%20y%20-%20k)%20-%20f(x%20-%20h,%20y%20+%20k)%20+%20f(x%20-%20h,%20y%20-%20k)%7D%7B4hk%7D%0A"></p>
<div id="b64528da" class="cell" data-execution_count="16">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> central_diff(instrument: ql.Instrument, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, quote: ql.SimpleQuote) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb11-2">    u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote.value()</span>
<span id="cb11-3">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb11-4">    P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-5">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb11-6">    P_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-7"></span>
<span id="cb11-8">    quote.setValue(u0)</span>
<span id="cb11-9">    </span>
<span id="cb11-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h)</span>
<span id="cb11-11"></span>
<span id="cb11-12"></span>
<span id="cb11-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> central_diff_2nd(instrument: ql.Instrument, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, quote: ql.SimpleQuote) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb11-14">    u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote.value()</span>
<span id="cb11-15">    P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-16">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb11-17">    P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-18">    quote.setValue(u0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb11-19">    P_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-20"></span>
<span id="cb11-21">    quote.setValue(u0)</span>
<span id="cb11-22">    </span>
<span id="cb11-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h)</span>
<span id="cb11-24"></span>
<span id="cb11-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> cross_central_diff(instrument: ql.Instrument, h: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, k: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, quote_1: ql.SimpleQuote, quote_2: ql.SimpleQuote) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb11-26">    u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote_1.value()</span>
<span id="cb11-27">    u2_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote_2.value()</span>
<span id="cb11-28">    </span>
<span id="cb11-29">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb11-30">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k)</span>
<span id="cb11-31">    P_Plus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-32"></span>
<span id="cb11-33">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> h)</span>
<span id="cb11-34">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k)</span>
<span id="cb11-35">    P_Plus_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-36"></span>
<span id="cb11-37">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb11-38">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> k)</span>
<span id="cb11-39">    P_Minus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-40"></span>
<span id="cb11-41">    quote_1.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> h)</span>
<span id="cb11-42">    quote_2.setValue(u1_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k)</span>
<span id="cb11-43">    P_Minus_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> instrument.NPV()</span>
<span id="cb11-44"></span>
<span id="cb11-45">    quote_1.setValue(u1_0)</span>
<span id="cb11-46">    quote_2.setValue(u2_0)</span>
<span id="cb11-47">    </span>
<span id="cb11-48">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (P_Plus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Plus_Minus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P_Minus_Plus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> P_Minus_Minus) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k)</span></code></pre></div></div>
</div>
<p>From that we can easily define the greeks functions:</p>
<div id="5caadf3a" class="cell" data-execution_count="17">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">delta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote)</span>
<span id="cb12-2">vega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_quote)</span>
<span id="cb12-3">gamma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff_2nd, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote)</span>
<span id="cb12-4">volga <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(central_diff_2nd, quote<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_quote)</span>
<span id="cb12-5">vanna <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> partial(cross_central_diff, quote_1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_quote, quote_2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_quote)</span></code></pre></div></div>
</div>
</section>
<section id="hedging-the-book" class="level2">
<h2 class="anchored" data-anchor-id="hedging-the-book">Hedging the book</h2>
<p>Now that we have set up the trader portfolio, we need to find the optimal hedge for that portfolio that is going to minimize the sensitivities that we want not be exposed to. Usually a trader sitting in a exotic desk will not just keep low exposure to the classical greeks like <img src="https://latex.codecogs.com/png.latex?%5CDelta">, <img src="https://latex.codecogs.com/png.latex?%5CGamma"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BV%7D">, but also in some higher order Greeks live DVegaDvol (a.k.a. <img src="https://latex.codecogs.com/png.latex?Volga">) and the DVegaDSpot (a.k.a. <img src="https://latex.codecogs.com/png.latex?Vanna">).</p>
<p>This need comes from the fact that in case of exotics (especially for barrier and binaries) the “barrier” risks needs to be hedged: as spot moves closer to the barrier, vega can spike, that’s vanna risk.</p>
<p>The most common way to hedge those greeks in the FX option market is by using simple structure that can be obtained by <img src="https://latex.codecogs.com/png.latex?25%20%5CDelta"> calls and puts and ATM calls and puts. Here’s the basic structures that will compose our hedging portfolio:</p>
<ul>
<li>Spot: to oviously to hedge the <img src="https://latex.codecogs.com/png.latex?%5CDelta"></li>
<li>ATM straddles: involves buying an ATM vanilla call and ATM vanilla put, this structure provides positive gamma and positive vega</li>
<li>RR (Risks reversals): involves buying an <img src="https://latex.codecogs.com/png.latex?25%20%5CDelta"> vanilla call and sell a <img src="https://latex.codecogs.com/png.latex?25%20%5CDelta"> vanilla put, provides a strong vanna exposure (becuase calls and puts respond differently as spot moves)</li>
<li>Butterflies: involves buying an <img src="https://latex.codecogs.com/png.latex?25%20%5CDelta"> vanilla call and sell a <img src="https://latex.codecogs.com/png.latex?25%20%5CDelta"> vanilla put and selling twice a straddle, provides a strong volga exposure.</li>
</ul>
<section id="hedge-methodology" class="level3">
<h3 class="anchored" data-anchor-id="hedge-methodology">Hedge methodology</h3>
<p>The way we are going to find the optimal weights of our hedging portfolio is by using the classical <em>parameter hedging</em> methodology. Which mathematically means to set up a linear system where:</p>
<p>The exposure vector is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BE%7D%20=%20%5Cbegin%7Bbmatrix%7D%20%5CDelta%20%5C%5C%20%5CGamma%20%5C%5C%20%5Ctext%7BVega%7D%20%5C%5C%20%5Ctext%7BVanna%7D%20%5C%5C%20%5Ctext%7BVolga%7D%20%5Cend%7Bbmatrix%7D.%0A"></p>
<p>which represent the total exposure of our portfolio (the weighted sum of each instrument greek in our exotic portfolio). For each hedge instrument <img src="https://latex.codecogs.com/png.latex?j"> (spot, ATM straddle, RR, BF), compute its greek vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bh%7D_j">. Form the hedge matrix:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20=%20%5Cbegin%7Bbmatrix%7D%20%5Cmathbf%7Bh%7D_1%20&amp;%20%5Cmathbf%7Bh%7D_2%20&amp;%20%5Cdots%20&amp;%20%5Cmathbf%7Bh%7D_n%20%5Cend%7Bbmatrix%7D.%0A"></p>
<p>Then solve for weights <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bx%7D"> (position sizes):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20%5Cmathbf%7Bx%7D%20-%20E%20%20%5Capprox%200.%0A"></p>
<p>In our case the exposure vector is bigger than the hedging vector <img src="https://latex.codecogs.com/png.latex?x">, since we only 4 instruments and 5 greeks to hedge. We end up with a linear system with 4 variables and 5 equations, thus there is not an exact solution, but we can find a solution that minimized the sum of residual squares.</p>
<p>When we want to calculate the greeks of our portfolio, we want to do it in meaningful way, thus this requires to us to set the shift operator in a way that makes sense to measure the specific risk of our porfolio. The two main ways that industry professional do that is by using 1% of the spot change and 1% vol change.</p>
<div id="59dc8705" class="cell" data-execution_count="19">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">spot_change <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb13-2">vol_change <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb13-3"></span>
<span id="cb13-4">delta_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(delta, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb13-5">vega_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(vega, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb13-6">volga_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(volga, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb13-7">gamma_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(gamma, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb13-8">vanna_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(vanna, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span></code></pre></div></div>
</div>
<p>The raw Greek exposition for our book is then the following:</p>
<div id="252a9ff7" class="cell" data-execution_count="20">
<div class="cell-output cell-output-stdout">
<pre><code>Book Delta exposure: 371644.134787602
Book Gamma exposure: 13330632.102691187
Book Vega exposure: 1183872.580309415
Book Volga exposure: 380633.56067277736
Book Vanna exposure: 190025.47439962075</code></pre>
</div>
</div>
<p>In FX, Greeks can be confusing, because they depend on the quotation of the currency pair as well as the currency in which they are calculated. Furthermore, premium can be included or excluded, smile-effect can be included or not included, and numerical approximations may further add to the confusion.</p>
<p>What traders use in real life are a “modified version” of the plain greeks:</p>
<ul>
<li><p>The plain <img src="https://latex.codecogs.com/png.latex?%5CDelta"> of a forex option gives the amount of Foreing currency a trader would have to buy in the spot-market to delta-hedge a sold-option. To get the amount of dollars to buy those EURs (in our case) we need to multiply the delta by the current spot value.</p></li>
<li><p>Traders’ <img src="https://latex.codecogs.com/png.latex?%5CGamma">: where the spot in the finite diff method is varied by 0.5% and then multiplied by 1%, since traders consider the change of delta as spot changes relatively by 1%.</p></li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CGamma%5E%7Btr%7D_t%20=%20%5CGamma_t%20%5Cfrac%7BS_t%7D%7B100%7D%0A"></p>
<ul>
<li>Traders’ <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BV%7D">: A trader will typically consider vega as the change of the USD or EUR value of a derivative contract (or a book of derivatives) assuming a 1% absolute/constant change in volatility.</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7BV(%5Csigma%20+%200.5%5C%25)%20-%20V(%5Csigma%20-%200.5%5C%25)%7D%7B1%5C%25%7D%20%5Ccdot%201%5C%25%0A"></p>
<div id="e26f2cbc" class="cell" data-execution_count="21">
<div class="cell-output cell-output-stdout">
<pre><code>Book Delta exposure: $438540.0790493703
Book Gamma exposure: $157301.458811756
Book Vega exposure: $11838.72580309415
Book Volga exposure: $3806.3356067277737
Book Vanna exposure: $2242.300597915525</code></pre>
</div>
</div>
<p>As you might guess the exposition is negative since we are on the sell-side of the trade, thus we need to buy the hedging structures to hedge ourselves from those greeks.</p>
<p>Our exposure vector <img src="https://latex.codecogs.com/png.latex?E%20=%20%5B%20%5CDelta,%20%5CGamma,%20%5Cmathcal%7BV%7D,%20Volga,%20Vanna%20%5D"> is:</p>
<div id="ab5775f6" class="cell" data-execution_count="22">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Greek exposure vector</span></span>
<span id="cb16-2">E <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([delta_book, gamma_book, vega_book, volga_book, vanna_book])</span>
<span id="cb16-3">E</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="22">
<pre><code>array([  371644.1347876 , 13330632.10269119,  1183872.58030942,
         380633.56067278,   190025.47439962])</code></pre>
</div>
</div>
<p>For each of the base instrument (Spot, ATM call, ATM put, <img src="https://latex.codecogs.com/png.latex?25%5CDelta"> call and <img src="https://latex.codecogs.com/png.latex?25%5CDelta"> put) let’s calculate the basic greeks.</p>
<div id="fa03820a" class="cell" data-execution_count="23">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hedge_matrix</span></span>
<span id="cb18-2">base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.float64(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span>)</span>
<span id="cb18-3">spot_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb18-4">atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb18-5">    delta(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-6">    gamma(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-7">    vega(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-8">    volga(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-9">    vanna(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-10">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb18-11">atm_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb18-12">    delta(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-13">    gamma(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-14">    vega(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-15">    volga(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-16">    vanna(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-17">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb18-18">delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb18-19">    delta(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-20">    gamma(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-21">    vega(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-22">    volga(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-23">    vanna(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-24">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb18-25">delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb18-26">    delta(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-27">    gamma(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb18-28">    vega(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-29">    volga(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-30">    vanna(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb18-31">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
</div>
<p>From there let’s calculate the greek exposure for the strcutures that we are going to use in out hedging portfolio, and then create our Hedge Matrix:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20=%20%5Cleft%5B%0A%5Cbegin%7Barray%7D%7Bc%7Ccccc%7D%0A%20%20%20%20&amp;%20%5Ctext%7BSpot%7D%20&amp;%20%5Ctext%7BStraddle%7D%20&amp;%20%5Ctext%7BRR%7D%20&amp;%20%5Ctext%7BButterfly%7D%20%5C%5C%0A%20%20%20%20%5Chline%0A%20%20%20%20%5CDelta%20%20%20&amp;%20h_%7B11%7D%20&amp;%20h_%7B12%7D%20&amp;%20h_%7B13%7D%20&amp;%20h_%7B14%7D%20%5C%5C%0A%20%20%20%20%5CGamma%20%20%20&amp;%20h_%7B21%7D%20&amp;%20h_%7B22%7D%20&amp;%20h_%7B23%7D%20&amp;%20h_%7B24%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BVega%7D%20%20%20&amp;%20h_%7B31%7D%20&amp;%20h_%7B32%7D%20&amp;%20h_%7B33%7D%20&amp;%20h_%7B34%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BVolga%7D%20%20&amp;%20h_%7B41%7D%20&amp;%20h_%7B42%7D%20&amp;%20h_%7B43%7D%20&amp;%20h_%7B44%7D%20%5C%5C%0A%20%20%20%20%5Ctext%7BVanna%7D%20%20&amp;%20h_%7B51%7D%20&amp;%20h_%7B52%7D%20&amp;%20h_%7B53%7D%20&amp;%20h_%7B54%7D%20%5C%5C%0A%5Cend%7Barray%7D%0A%5Cright%5D%0A"></p>
<div id="c88abce5" class="cell" data-execution_count="24">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's create the straddle, RR, Butterfly from the base vanilla options</span></span>
<span id="cb19-2">atm_straddle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put_greeks</span>
<span id="cb19-3">rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put_greeks</span>
<span id="cb19-4">butterfly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> atm_straddle</span>
<span id="cb19-5"></span>
<span id="cb19-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hedge Matrix</span></span>
<span id="cb19-7">H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.stack([spot_greeks, atm_straddle, rr, butterfly]).T</span></code></pre></div></div>
</div>
<p>Since our system have more equations than variables, we must use a least square method (provided by <code>np.linalg.lstsq</code>) to solve it. Least square method minimizes the Euclidian 2-norm $ || E - Hx || $.</p>
<div id="decf7a68" class="cell" data-execution_count="25">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">x, _, _, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.lstsq(H, E)</span></code></pre></div></div>
</div>
<p>Now that we have a solution let’s see what’s our remaining exposure to each of the choosen hedged greeks. We do that by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH%20x%20-%20E%0A"></p>
<div id="d9d73e2f" class="cell" data-execution_count="26">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">residual_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E).tolist()</span>
<span id="cb21-2"></span>
<span id="cb21-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Delta exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Gamma exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Vega exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Volga exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Vanna exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>residual_greeks[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Residual Delta exposure: $-2.6100315153598783e-09
Residual Gamma exposure: $-13.422826034608855
Residual Vega exposure: $126.72764540341916
Residual Volga exposure: $0.016476123058237136
Residual Vanna exposure: $-0.09291220850032404</code></pre>
</div>
</div>
<p>Another crucial thing when hedging a book is, of course, to measure the P&amp;L from one rebalancing period to another. Let’s then calculate the value of the various components of our hedging portfolio. We have the the P&amp;L from time <img src="https://latex.codecogs.com/png.latex?t"> is calculated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP%5C&amp;L_%7Bt%20%5Crightarrow%20t%20+%201%7D%20=%20(V_%7Bt%20+%201%7D%5E%7Bbook%7D%20+%20V_%7Bt%20+%201%7D%5E%7Bhedge%7D%20+%20C_%7Bt%20+%201%7D)%20-%20(V_%7Bt%7D%5E%7Bbook%7D%20+%20V_%7Bt%7D%5E%7Bhedge%7D%20+%20C_%7Bt%7D)%0A"></p>
<p>where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?V_%7Bt%7D%5E%7Bbook%7D"> is the value of our book at time <img src="https://latex.codecogs.com/png.latex?t">, which is the sum of the premiums that we’ve been collected</li>
<li><img src="https://latex.codecogs.com/png.latex?V_%7Bt%7D%5E%7Bhedge%7D"> is the value of the hedging portfolio, the sum of the premium of the structures that we have bought to hedge our from the various sensistivies</li>
<li><img src="https://latex.codecogs.com/png.latex?C_t"> the cash/bank account of our portfolio which includes financing of cash and trade cashflows</li>
</ul>
<p>A shorter way to express the P&amp;L, is just by considering the variation of the value of the book + the variation of the value of the hedges + cost of borrowing + interest accrued:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP%5C&amp;L_%7Bt%20%5Crightarrow%20t%20+%201%7D%20=%20%5Cdelta%20V_%7Bt,%20t%20+%201%7D%5E%7Bbook%7D%20+%20%5Cdelta%20V_%7Bt,%20t%20+%201%7D%5E%7Bhedge%7D%20+%20%5Ctext%7Bcost%20of%20borrowing%7D%20+%20%5Ctext%7Binterest%20accrued%7D%0A"></p>
<p>since the rebalancing of the hedges of the bank account happend at the end of trading day.</p>
<div id="e6d102d2" class="cell" data-execution_count="27">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">spot_premium_eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb23-2">spot_premium_usd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb23-3">atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb23-4">rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb23-5">butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV())) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
</div>
<p>The usd borrowed (which is the sum of the premiums of or hedging instrument) is crucial for use to calculate the interest accrued for borrowing the usd dollars to buy the hedging structures.</p>
<div id="2eaa7f70" class="cell" data-execution_count="28">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> spot_premium_usd</span>
<span id="cb24-2">hedging_str_premium</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="28">
<pre><code>np.float64(4182631.052146415)</code></pre>
</div>
</div>
<p>Thus the value of the cash at time <img src="https://latex.codecogs.com/png.latex?t%20=%200"> is the equal to all the cash that we’ve borrowed to build the hedging structures plus the premiums that we’ve been collected from selling the exotics options</p>
<div id="2cf87165" class="cell" data-execution_count="29">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1">bank_account_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(premiums)</span>
<span id="cb26-2">bank_account_value</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="29">
<pre><code>np.float64(-4099629.5308762337)</code></pre>
</div>
</div>
<div id="590656fa" class="cell" data-execution_count="30">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">port_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bank_account_value]</span>
<span id="cb28-2"></span>
<span id="cb28-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The portfolio value at time t = 0 is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>port_value[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>The portfolio value at time t = 0 is 0.0</code></pre>
</div>
</div>
</section>
</section>
<section id="changes-of-the-hedging-portfolio-with-the-changes-of-the-market-conditions" class="level2">
<h2 class="anchored" data-anchor-id="changes-of-the-hedging-portfolio-with-the-changes-of-the-market-conditions">Changes of the hedging portfolio with the changes of the market conditions</h2>
<p>Now that we have setup our hedging portfolio see verify how its will change as the spot and vol change. Let’s assume that in a week period the spot went go down by 300 pips and the vol went up by <img src="https://latex.codecogs.com/png.latex?1%5C%25">.</p>
<div id="62eda722" class="cell" data-execution_count="31">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1">spot_quote.setValue(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.15</span>)</span>
<span id="cb30-2">vol_quote.setValue(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.14</span>)</span>
<span id="cb30-3">ql.Settings.instance().evaluationDate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, ql.Days)</span></code></pre></div></div>
</div>
<div id="ee56248c" class="cell" data-execution_count="32">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1">new_premiums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [pos.NPV() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book]</span></code></pre></div></div>
</div>
<p>With the new spot and vol let’s recalculate the book exposure</p>
<div id="b1fde571" class="cell" data-execution_count="33">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1">delta_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(delta, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb32-2">vega_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(vega, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb32-3">volga_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(volga, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb32-4">gamma_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(gamma, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb32-5">vanna_book <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>([pos.sensitivity(vanna, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> book])</span>
<span id="cb32-6"></span>
<span id="cb32-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># new Exposure vector</span></span>
<span id="cb32-8">E <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([delta_book, gamma_book, vega_book, volga_book, vanna_book])</span></code></pre></div></div>
</div>
<div id="4e20ab91" class="cell" data-execution_count="34">
<div class="cell-output cell-output-stdout">
<pre><code>Book Delta exposure: $16549.207988699432
Book Gamma exposure: $138778.80052508457
Book Vega exposure: $11096.72161278602
Book Volga exposure: $560.5276947207037
Book Vanna exposure: $2416.5207043533637</code></pre>
</div>
</div>
<p>As before let’s recalculate the greeks for the basic trading instruments, and then combine to obtain the greeks for the structures used in the hedging portfolio.</p>
<div id="ee751248" class="cell" data-execution_count="35">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hedge_matrix</span></span>
<span id="cb34-2">base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.float64(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000_000</span>)</span>
<span id="cb34-3">spot_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb34-4">atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb34-5">    delta(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-6">    gamma(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-7">    vega(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-8">    volga(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-9">    vanna(atm_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-10">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb34-11">atm_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb34-12">    delta(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-13">    gamma(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-14">    vega(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-15">    volga(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-16">    vanna(atm_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-17">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb34-18">delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb34-19">    delta(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-20">    gamma(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-21">    vega(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-22">    volga(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-23">    vanna(delta25_call, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-24">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb34-25">delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([</span>
<span id="cb34-26">    delta(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-27">    gamma(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change),</span>
<span id="cb34-28">    vega(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-29">    volga(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-30">    vanna(delta25_put, h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>spot_change, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vol_change),</span>
<span id="cb34-31">]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span></code></pre></div></div>
</div>
<div id="46e467dc" class="cell" data-execution_count="36">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's create the straddle, RR, Butterfly from the base vanilla options</span></span>
<span id="cb35-2">atm_straddle <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put_greeks</span>
<span id="cb35-3">rr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put_greeks</span>
<span id="cb35-4">butterfly <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put_greeks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> atm_straddle</span>
<span id="cb35-5"></span>
<span id="cb35-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hedge Matrix</span></span>
<span id="cb35-7">H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.stack([spot_greeks, atm_straddle, rr, butterfly]).T</span></code></pre></div></div>
</div>
<p>The esposure of our hedging portfolio is now is:</p>
<div id="14151e72" class="cell" data-execution_count="37">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># New exposure of the hedging portfolio</span></span>
<span id="cb36-2">exposure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (H <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> E).tolist()</span>
<span id="cb36-3"></span>
<span id="cb36-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Delta exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb36-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Gamma exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb36-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Vega exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb36-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Volga exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb36-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Residual Vanna exposure: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>exposure[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>value()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Residual Delta exposure: $-348789.7260608019
Residual Gamma exposure: $73758.42210270112
Residual Vega exposure: $5879.026689021433
Residual Volga exposure: $-54024.14332019365
Residual Vanna exposure: $117.87655605534354</code></pre>
</div>
</div>
<p>To calculate the P&amp;L from time t to t+1 we need to calculate the change of the value of the hedging portfolio as well</p>
<div id="69f656f5" class="cell" data-execution_count="38">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">spot_premium_usd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb38-2">atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb38-3">rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb38-4">butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (delta25_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> delta25_put.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (atm_call.NPV() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> atm_put.NPV())) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> base_notional</span>
<span id="cb38-5">new_hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> atm_straddle_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rr_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> butterfly_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> spot_premium_usd</span></code></pre></div></div>
</div>
<div id="40ea160f" class="cell" data-execution_count="39">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">dt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dc.yearFraction(today, today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ql.Period(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, ql.Days))</span>
<span id="cb39-2">usd_interest_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt</span>
<span id="cb39-3">eur_interest_accr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> spot_premium_eur <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_f <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> dt</span>
<span id="cb39-4">new_total_portfolio_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> new_hedging_str_premium <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(new_premiums) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bank_account_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> usd_interest_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> eur_interest_accr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> spot_quote.value()</span>
<span id="cb39-5"></span>
<span id="cb39-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Portfolio value at time t = 1 is: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>new_total_portfolio_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Portfolio value at time t = 1 is: $-8014.640804257949</code></pre>
</div>
</div>
<div id="a612426b" class="cell" data-execution_count="41">
<div class="cell-output cell-output-stdout">
<pre><code>The P&amp;L after a week is: $-8014.640804257949</code></pre>
</div>
</div>
<p>If we want to reduce our exposure and bring it back to a lower one, we need to recalculate the optiomal hedging weight for our hedging portfolio as before:</p>
<div id="e49576b6" class="cell" data-execution_count="42">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb42-1">x_new, _, _, _ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linalg.lstsq(H, E)</span></code></pre></div></div>
</div>
<p>This how our hedging portfolio has to change:</p>
<div id="095f0634" class="cell" data-execution_count="43">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1">x_new <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="43">
<pre><code>array([-0.33252725,  3.27195222,  0.99407244,  3.52132318])</code></pre>
</div>
</div>
<p>The residual greek exposure after the rebalancing is:</p>
<div id="18e5c5c8" class="cell" data-execution_count="44">
<div class="cell-output cell-output-stdout">
<pre><code>Residual Delta exposure: $-4.2255123844370243e-10
Residual Gamma exposure: $1.7120357189066706
Residual Vega exposure: $-16.21605768425856
Residual Volga exposure: $-0.0021280367333383764
Residual Vanna exposure: $0.014062687186218682</code></pre>
</div>
</div>


</section>

 ]]></description>
  <category>Quantitative finance</category>
  <category>Hedging</category>
  <category>Exotic options</category>
  <guid>https://your-website-url.example.com/posts/post-with-code/exotic-book.html</guid>
  <pubDate>Sat, 27 Sep 2025 00:00:00 GMT</pubDate>
  <media:content url="https://your-website-url.example.com/posts/post-with-code/barrier_price_surface.png" medium="image" type="image/png" height="142" width="144"/>
</item>
</channel>
</rss>
