Skip to content

Commit

Permalink
Update option strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisSzeto committed Jul 5, 2024
1 parent 3a7f251 commit ac0162a
Show file tree
Hide file tree
Showing 41 changed files with 152 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<div class="highlight">Warning: There is currently no <code>OptionStrategies</code> method for Iron Butterfly orders, so this tutorial manually orders the individual legs in the strategy. If you manually place multi-leg orders one at a time while there is no liquidity at a strike price, you can get stuck in an unhedged position.</div>

<p>
The <span class="new-term">Iron Butterfly</span> is an option strategy which involves four Option contracts. All the contracts have the same underlying stock and expiration, but the order of strike prices for the four contracts is $A&gt;B&gt;C$. The following table describes the strike price of each contract:</p>
The <span class="new-term">Long Iron Butterfly</span> is an option strategy which involves four Option contracts. All the contracts have the same underlying stock and expiration, but the order of strike prices for the four contracts is $A&gt;B&gt;C$. The following table describes the strike price of each contract:</p>
<table class="table qc-table">
<thead>
<tr>
Expand All @@ -17,19 +15,19 @@
</thead>
<tbody>
<tr>
<td>-1 OTM call</td>
<td>1 OTM call</td>
<td> $A$</td>
</tr>
<tr>
<td>1 ATM call</td>
<td>-1 ATM call</td>
<td> $B$</td>
</tr>
<tr>
<td>1 ATM put</td>
<td>-1 ATM put</td>
<td> $B$</td>
</tr>
<tr>
<td>-1 OTM put</td>
<td>1 OTM put</td>
<td> $C=B-(A-B)$</td>
</tr>
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@
</div>

<li>In the <code class="csharp">OnData</code><code class="python">on_data</code> method, select the contracts and place the orders.</li>
<!--

<p><b>Approach A:</b> Call the <code class="csharp">OptionStrategies.IronButterfly</code><code class="python">OptionStrategies.iron_butterfly</code> method with the details of each leg and then pass the result to the <code class="csharp">Buy</code><code class="python">buy</code> method.</p>
<div class="section-example-container">
<pre class="csharp">var ironButterfly = OptionStrategies.IronButterfly(_symbol, otmPutStrike, atmStrike, otmCallStrike, expiry);
Buy(ironButterfly, 2);</pre>
<pre class="python">iron_butterfly = OptionStrategies.iron_butterfly(self._symbol, otm_put_strike, atm_strike, otm_call_strike, expiry)
self.buy(iron_butterfly, 2)</pre>
</div>
-->

<p><b>Approach B:</b> Create a list of <code>Leg</code> objects and then call the <a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-market-orders"><span class='csharp'>Combo Market Order</span><span class='python'>combo_market_order</span></a>, <a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-limit-orders"><span class='csharp'>Combo Limit Order</span><span class='python'>combo_limit_order</span></a>, or <a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-leg-limit-orders"><span class='csharp'>Combo Leg Limit Order</span><span class='python'>combo_leg_limit_order</span></a> method.</p>

<div class="section-example-container">
Expand All @@ -94,10 +94,10 @@

var legs = new List&lt;Leg&gt;()
{
Leg.Create(atmCall.Symbol, 1),
Leg.Create(atmPut.Symbol, 1),
Leg.Create(otmCall.Symbol, -1),
Leg.Create(otmPut.Symbol, -1)
Leg.Create(atmCall.Symbol, -1),
Leg.Create(atmPut.Symbol, -1),
Leg.Create(otmCall.Symbol, 1),
Leg.Create(otmPut.Symbol, 1)
};
ComboMarketOrder(legs, 1);</pre>
<pre class="python"># Select the contracts
Expand All @@ -107,10 +107,10 @@
otm_put = [x for x in puts if x.strike == otm_put_strike][0]

legs = [
Leg.create(atm_call.symbol, 1),
Leg.create(atm_put.symbol, 1),
Leg.create(otm_call.symbol, -1),
Leg.create(otm_put.symbol, -1)
Leg.create(atm_call.symbol, -1),
Leg.create(atm_put.symbol, -1),
Leg.create(otm_call.symbol, 1),
Leg.create(otm_put.symbol, 1)
]
self.combo_market_order(legs, 1)</pre>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<p>The long iron butterfly is a limited-reward-limited-risk strategy. The payoff is</p>

<p>The long call butterfly is a limited-reward-limited-risk strategy. The payoff is</p>
$$
\begin{array}{rcll}
C^{OTM}_T &amp; = &amp; (S_T - K^C_{OTM})^{+}\\
C^{ATM}_T &amp; = &amp; (S_T - K^C_{ATM})^{+}\\
P^{OTM}_T &amp; = &amp; (K^P_{OTM} - S_T)^{+}\\
P^{ATM}_T &amp; = &amp; (K^P_{ATM} - S_T)^{+}\\
P_T &amp; = &amp; (C^{ATM}_T + P^{ATM}_T - C^{OTM}_T - P^{OTM}_T - C^{ATM}_0 - P^{ATM}_0 + C^{OTM}_0 + P^{OTM}_0)\times m - fee
P_T &amp; = &amp; (C^{OTM}_T + P^{OTM}_T - C^{ATM}_T - P^{ATM}_T - C^{OTM}_0 - P^{OTM}_0 + C^{ATM}_0 + P^{ATM}_0)\times m - fee
\end{array}
$$
$$
Expand All @@ -27,10 +26,10 @@
&amp; K^P_{OTM} &amp; = &amp; \textrm{OTM put strike price}\\
&amp; K^P_{ATM} &amp; = &amp; \textrm{ATM put strike price}\\
&amp; P_T &amp; = &amp; \textrm{Payout total at time T}\\
&amp; C^{OTM}_0 &amp; = &amp; \textrm{OTM call value at position opening (credit received)}\\
&amp; C^{ATM}_0 &amp; = &amp; \textrm{ATM call value at position opening (debit paid)}\\
&amp; P^{OTM}_0 &amp; = &amp; \textrm{OTM put value at position opening (credit received)}\\
&amp; P^{ATM}_0 &amp; = &amp; \textrm{ATM put value at position opening (debit paid)}\\
&amp; C^{OTM}_0 &amp; = &amp; \textrm{OTM call value at position opening (debit paid)}\\
&amp; C^{ATM}_0 &amp; = &amp; \textrm{ATM call value at position opening (credit received)}\\
&amp; P^{OTM}_0 &amp; = &amp; \textrm{OTM put value at position opening (debit paid)}\\
&amp; P^{ATM}_0 &amp; = &amp; \textrm{ATM put value at position opening (credit received)}\\
&amp; m &amp; = &amp; \textrm{Contract multiplier}\\
&amp; T &amp; = &amp; \textrm{Time of expiration}
\end{array}
Expand All @@ -40,6 +39,6 @@

<img class="docs-image" src="https://cdn.quantconnect.com/i/tu/long-iron-butterfly.png" alt="Strategy payoff decomposition and analysis of long iron butterfly">

<p>The maximum profit is $K^C_{OTM} - K^C_{ATM} - C^{ATM}_0 - P^{ATM}_0 + C^{OTM}_0 + P^{OTM}_0$. It occurs when the underlying price is below the OTM put strike price or above the OTM call strike price at expiration.</p>
<p>The maximum loss is the net debit paid, $C^{OTM}_0 + P^{OTM}_0 - C^{ATM}_0 - P^{ATM}_0$. It occurs when the underlying price stays the same as when you opened the trade.</p>
<p>If the Option is American Option, there is a risk of <a href='/docs/v2/writing-algorithms/reality-modeling/options-models/assignment'>early assignment</a> on the contracts you sell.</p>
<p>The maximum profit is the net credit received, $C^{ATM}_0 + P^{ATM}_0 - C^{OTM}_0 - P^{OTM}_0$. It occurs when the underlying price stays the same as when you opened the trade.</p>
<p>The maximum loss is $K^C_{OTM} - K^C_{ATM} - C^{ATM}_0 - P^{ATM}_0 + C^{OTM}_0 + P^{OTM}_0$. It occurs when the underlying price is below the OTM put strike price or above the OTM call strike price at expiration.</p>
<p>If the Option is American Option, there is a risk of <a href='/docs/v2/writing-algorithms/reality-modeling/options-models/assignment'>early assignment</a> on the contracts you sell.</p>
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
$$
<br>
<p>So, the strategy losses $624.</p>
<!--

<p>The following algorithm implements a short iron butterfly Option strategy:</p>

<div class="python">
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=.html"></iframe>
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_0a848d96fa7b63705c33d4d23aeff9d2.html"></iframe>
</div>
</div>
</div>
Expand All @@ -66,8 +66,7 @@
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache/.html"></iframe>
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_adea53e2c428bf852e252f1184ddafda.html"></iframe>
</div>
</div>
</div>
-->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?
$strategy = "Iron Butterfly";
$strategy = "Long Iron Butterfly";
$nContracts = "4";
$csharpMethod = "IronButterfly(30, 5)";
$pythonMethod = "iron_butterfly(30, 5)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"type": "metadata",
"values": {
"description": "The Iron Butterfly is an Option strategy that involves four Option contracts where the order of strike prices for the four contracts is A>B>C. ",
"description": "The Iron Butterfly is an Option strategy that involves four Option contracts where the order of strike prices for the four contracts is A>B=C>D. ",
"keywords": "multi-leg orders, iron butterfly option strategy, out-the-money call, profits from a decrease in price movement, profits from an increase in price movement, limited-reward-limited-risk strategy, risk of early assignment",
"og:description": "The Iron Butterfly is an Option strategy that involves four Option contracts where the order of strike prices for the four contracts is A>B>C. ",
"og:description": "The Iron Butterfly is an Option strategy that involves four Option contracts where the order of strike prices for the four contracts is A>B=C>D. ",
"og:title": "Iron Butterfly - Documentation QuantConnect.com",
"og:type": "website",
"og:site_name": "Iron Butterfly - QuantConnect.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<div class="highlight">Warning: There is currently no <code>OptionStrategies</code> method for Short Iron Butterfly orders, so this tutorial manually orders the individual legs in the strategy. If you manually place multi-leg orders one at a time while there is no liquidity at a strike price, you can get stuck in an unhedged position.</div>

<p>
The <span class="new-term">Short Iron Butterfly</span> is an option strategy which involves four Option contracts. All the contracts have the same underlying stock and expiration, but the order of strike prices for the four contracts is $A&gt;B&gt;C$. The following table describes the strike price of each contract:</p>
<table class="table qc-table">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@
</div>

<li>In the <code class="csharp">OnData</code><code class="python">on_data</code> method, select the contracts and place the orders.</li>
<!--

<p><b>Approach A:</b> Call the <code class="csharp">OptionStrategies.ShortIronButterfly</code><code class="python">OptionStrategies.short_iron_butterfly</code> method with the details of each leg and then pass the result to the <code class="csharp">Buy</code><code class="python">buy</code> method.</p>
<div class="section-example-container">
<pre class="csharp">var shortIronButterfly = OptionStrategies.ShortIronButterfly(_symbol, otmPutStrike, atmStrike, otmCallStrike, expiry);
Buy(shortIronButterfly, 2);</pre>
<pre class="python">short_iron_butterfly = OptionStrategies.short_iron_butterfly(self._symbol, otm_put_strike, atm_strike, otm_call_strike, expiry)
self.buy(short_iron_butterfly, 2)</pre>
</div>
-->

<p><b>Approach B:</b> Create a list of <code>Leg</code> objects and then call the <a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-market-orders"><span class='csharp'>Combo Market Order</span><span class='python'>combo_market_order</span></a>, <a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-limit-orders"><span class='csharp'>Combo Limit Order</span><span class='python'>combo_limit_order</span></a>, or <a href="/docs/v2/writing-algorithms/trading-and-orders/order-types/combo-leg-limit-orders"><span class='csharp'>Combo Leg Limit Order</span><span class='python'>combo_leg_limit_order</span></a> method.</p>

<div class="section-example-container">
Expand All @@ -94,10 +94,10 @@

var legs = new List&lt;Leg&gt;()
{
Leg.Create(atmCall.Symbol, -1),
Leg.Create(atmPut.Symbol, -1),
Leg.Create(otmCall.Symbol, 1),
Leg.Create(otmPut.Symbol, 1)
Leg.Create(atmCall.Symbol, 1),
Leg.Create(atmPut.Symbol, 1),
Leg.Create(otmCall.Symbol, -1),
Leg.Create(otmPut.Symbol, -1)
};
ComboMarketOrder(legs, 1);</pre>
<pre class="python"># Select the contracts
Expand All @@ -107,11 +107,11 @@
otm_put = [x for x in puts if x.strike == otm_put_strike][0]

legs = [
Leg.create(atm_call.symbol, -1),
Leg.create(atm_put.symbol, -1),
Leg.create(otm_call.symbol, 1),
Leg.create(otm_put.symbol, 1)
Leg.create(atm_call.symbol, 1),
Leg.create(atm_put.symbol, 1),
Leg.create(otm_call.symbol, -1),
Leg.create(otm_put.symbol, -1)
]
self.combo_market_order(legs, 1)</pre>
</div>
</ol>
</ol>
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

<p>The short call butterfly is a limited-reward-limited-risk strategy. The payoff is</p>
<p>The short iron butterfly is a limited-reward-limited-risk strategy. The payoff is</p>

$$
\begin{array}{rcll}
C^{OTM}_T &amp; = &amp; (S_T - K^C_{OTM})^{+}\\
C^{ATM}_T &amp; = &amp; (S_T - K^C_{ATM})^{+}\\
P^{OTM}_T &amp; = &amp; (K^P_{OTM} - S_T)^{+}\\
P^{ATM}_T &amp; = &amp; (K^P_{ATM} - S_T)^{+}\\
P_T &amp; = &amp; (C^{OTM}_T + P^{OTM}_T - C^{ATM}_T - P^{ATM}_T - C^{OTM}_0 - P^{OTM}_0 + C^{ATM}_0 + P^{ATM}_0)\times m - fee
P_T &amp; = &amp; (C^{ATM}_T + P^{ATM}_T - C^{OTM}_T - P^{OTM}_T - C^{ATM}_0 - P^{ATM}_0 + C^{OTM}_0 + P^{OTM}_0)\times m - fee
\end{array}
$$
$$
Expand All @@ -26,10 +27,10 @@
&amp; K^P_{OTM} &amp; = &amp; \textrm{OTM put strike price}\\
&amp; K^P_{ATM} &amp; = &amp; \textrm{ATM put strike price}\\
&amp; P_T &amp; = &amp; \textrm{Payout total at time T}\\
&amp; C^{OTM}_0 &amp; = &amp; \textrm{OTM call value at position opening (debit paid)}\\
&amp; C^{ATM}_0 &amp; = &amp; \textrm{ATM call value at position opening (credit received)}\\
&amp; P^{OTM}_0 &amp; = &amp; \textrm{OTM put value at position opening (debit paid)}\\
&amp; P^{ATM}_0 &amp; = &amp; \textrm{ATM put value at position opening (credit received)}\\
&amp; C^{OTM}_0 &amp; = &amp; \textrm{OTM call value at position opening (credit received)}\\
&amp; C^{ATM}_0 &amp; = &amp; \textrm{ATM call value at position opening (debit paid)}\\
&amp; P^{OTM}_0 &amp; = &amp; \textrm{OTM put value at position opening (credit received)}\\
&amp; P^{ATM}_0 &amp; = &amp; \textrm{ATM put value at position opening (debit paid)}\\
&amp; m &amp; = &amp; \textrm{Contract multiplier}\\
&amp; T &amp; = &amp; \textrm{Time of expiration}
\end{array}
Expand All @@ -39,6 +40,6 @@

<img class="docs-image" src="https://cdn.quantconnect.com/i/tu/short-iron-butterfly.png" alt="Strategy payoff decomposition and analysis of short iron butterfly">

<p>The maximum profit is the net credit received, $C^{ATM}_0 + P^{ATM}_0 - C^{OTM}_0 - P^{OTM}_0$. It occurs when the underlying price stays the same as when you opened the trade.</p>
<p>The maximum loss is $K^C_{OTM} - K^C_{ATM} - C^{ATM}_0 - P^{ATM}_0 + C^{OTM}_0 + P^{OTM}_0$. It occurs when the underlying price is below the OTM put strike price or above the OTM call strike price at expiration.</p>
<p>The maximum profit is $K^C_{OTM} - K^C_{ATM} - C^{ATM}_0 - P^{ATM}_0 + C^{OTM}_0 + P^{OTM}_0$. It occurs when the underlying price is below the OTM put strike price or above the OTM call strike price at expiration.</p>
<p>The maximum loss is the net debit paid, $C^{OTM}_0 + P^{OTM}_0 - C^{ATM}_0 - P^{ATM}_0$. It occurs when the underlying price stays the same as when you opened the trade.</p>
<p>If the Option is American Option, there is a risk of <a href='/docs/v2/writing-algorithms/reality-modeling/options-models/assignment'>early assignment</a> on the contracts you sell.</p>
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
$$
<br>
<p>So, the strategy losses $39.</p>
<!--

<p>The following algorithm implements a short iron butterfly Option strategy:</p>

<div class="python">
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=.html"></iframe>
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_5b2f6158c1c6ec4e5a3ae5e5866d9f52.html"></iframe>
</div>
</div>
</div>
Expand All @@ -66,8 +66,7 @@
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache/.html"></iframe>
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_3e04645219adfadf24d9fbbce596c8b1.html"></iframe>
</div>
</div>
</div>
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?
$strategy = "Short Iron Butterfly";
$nContracts = "4";
$csharpMethod = "IronButterfly(30, 5)";
$pythonMethod = "iron_butterfly(30, 5)"
include(DOCS_RESOURCES."/universes/option/option-strategy-filter-universe.php");
?>
Loading

0 comments on commit ac0162a

Please sign in to comment.