From 1be43e3b0bf80b68b0604d90ad1c5c35da952b7e Mon Sep 17 00:00:00 2001 From: LouisSzeto Date: Mon, 3 Jun 2024 12:33:40 +0800 Subject: [PATCH] match nit style option strategies --- .../01 Bear Call Spread/02 Implementation.html | 6 ++++-- .../02 Bear Put Spread/02 Implementation.html | 6 ++++-- .../03 Bull Call Spread/02 Implementation.html | 6 ++++-- .../04 Bull Put Spread/02 Implementation.html | 6 ++++-- .../05 Long Call Butterfly/02 Implementation.html | 6 ++++-- .../06 Short Call Butterfly/02 Implementation.html | 6 ++++-- .../07 Long Put Butterfly/02 Implementation.html | 6 ++++-- .../08 Short Put Butterfly/02 Implementation.html | 6 ++++-- .../02 Implementation.html | 6 ++++-- .../02 Implementation.html | 6 ++++-- .../02 Implementation.html | 6 ++++-- .../02 Implementation.html | 6 ++++-- .../15 Iron Butterfly/02 Implementation.html | 6 ++++-- .../21 Protective Collar/02 Implementation.html | 12 +++++++----- 14 files changed, 59 insertions(+), 31 deletions(-) diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/01 Bear Call Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/01 Bear Call Spread/02 Implementation.html index d344afdf3b..784d0d1549 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/01 Bear Call Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/01 Bear Call Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/02 Bear Put Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/02 Bear Put Spread/02 Implementation.html index 2aaf14bbb8..68c46c04f8 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/02 Bear Put Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/02 Bear Put Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/03 Bull Call Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/03 Bull Call Spread/02 Implementation.html index bbb79a3f2f..7c8f498f3c 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/03 Bull Call Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/03 Bull Call Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/04 Bull Put Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/04 Bull Put Spread/02 Implementation.html index ca755357fe..67e4f2f730 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/04 Bull Put Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/04 Bull Put Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/05 Long Call Butterfly/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/05 Long Call Butterfly/02 Implementation.html index 10a763d85e..c89e6b6557 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/05 Long Call Butterfly/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/05 Long Call Butterfly/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/06 Short Call Butterfly/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/06 Short Call Butterfly/02 Implementation.html index 59ee856519..965ca846e9 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/06 Short Call Butterfly/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/06 Short Call Butterfly/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/07 Long Put Butterfly/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/07 Long Put Butterfly/02 Implementation.html index 4315925617..171bace3b7 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/07 Long Put Butterfly/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/07 Long Put Butterfly/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/08 Short Put Butterfly/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/08 Short Put Butterfly/02 Implementation.html index dcd0da36b2..e11713aed6 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/08 Short Put Butterfly/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/08 Short Put Butterfly/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select the call Option contracts with the furthest expiry var expiry = chain.OrderByDescending(x => x.Expiry).First().Expiry; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/09 Long Call Calendar Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/09 Long Call Calendar Spread/02 Implementation.html index c1d582f2e3..8da89c3c83 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/09 Long Call Calendar Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/09 Long Call Calendar Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain == null || chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Get the ATM strike var atmStrike = chain.OrderBy(x => Math.Abs(x.Strike - chain.Underlying.Price)).First().Strike; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/10 Short Call Calendar Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/10 Short Call Calendar Spread/02 Implementation.html index 8ffcf0697f..129e0ec2ba 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/10 Short Call Calendar Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/10 Short Call Calendar Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain == null || chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Get the ATM strike var atmStrike = chain.OrderBy(x => Math.Abs(x.Strike - chain.Underlying.Price)).First().Strike; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/11 Long Put Calendar Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/11 Long Put Calendar Spread/02 Implementation.html index 411a0a18b4..9b0853174a 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/11 Long Put Calendar Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/11 Long Put Calendar Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain == null || chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Get the ATM strike var atmStrike = chain.OrderBy(x => Math.Abs(x.Strike - chain.Underlying.Price)).First().Strike; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/12 Short Put Calendar Spread/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/12 Short Put Calendar Spread/02 Implementation.html index 5ba01bf1d7..373d736fdd 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/12 Short Put Calendar Spread/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/12 Short Put Calendar Spread/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain == null || chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Get the ATM strike var atmStrike = chain.OrderBy(x => Math.Abs(x.Strike - chain.Underlying.Price)).First().Strike; diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/15 Iron Butterfly/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/15 Iron Butterfly/02 Implementation.html index e9fb14cc4e..20a4651463 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/15 Iron Butterfly/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/15 Iron Butterfly/02 Implementation.html @@ -32,8 +32,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_symbol, null); - if (chain == null || chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Separate the call and put contracts var calls = chain.Where(x => x.Right == OptionRight.Call); diff --git a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/21 Protective Collar/02 Implementation.html b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/21 Protective Collar/02 Implementation.html index 9dfb5274c4..d37e47ce4a 100644 --- a/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/21 Protective Collar/02 Implementation.html +++ b/03 Writing Algorithms/22 Trading and Orders/07 Option Strategies/21 Protective Collar/02 Implementation.html @@ -3,7 +3,7 @@
  1. In the Initializeinitialize method, set the start date, set the end date, subscribe to the underlying Equity, and create an Option universe.
  2. -
    private Symbol _equitySymbol, _optionSymbol;
    +        
    private Symbol _equitySymbol, _symbol;
     
     public override void Initialize()
     {
    @@ -13,7 +13,7 @@
         UniverseSettings.Asynchronous = true;
         _equitySymbol = AddEquity("GOOG").Symbol;
         var option = AddOption("GOOG", Resolution.Minute);
    -    _optionSymbol = option.Symbol;
    +    _symbol = option.Symbol;
         option.SetFilter(-10, 10, 0, 30);
     }
    def Initialize(self) -> None:
    @@ -23,7 +23,7 @@
         self.UniverseSettings.Asynchronous = True
         self.equity_symbol = self.AddEquity("GOOG").Symbol
         option = self.AddOption("GOOG", Resolution.Minute)
    -    self.option_symbol = option.Symbol
    +    self._symbol = option.Symbol
         option.SetFilter(-10, 10, 0, 30)
    @@ -34,8 +34,10 @@ if (Portfolio.Invested) return; // Get the OptionChain - var chain = slice.OptionChains.get(_optionSymbol, null); - if (chain == null || chain.Count() == 0) return; + if (!slice.OptionChains.TryGetValue(_symbol, out var chain)) + { + return; + } // Select an expiry date var expiry = chain.OrderBy(x => x.Expiry).Last().Expiry;