diff --git a/Algorithm.CSharp/OptionChainedUniverseSelectionModelRegressionAlgorithm.cs b/Algorithm.CSharp/OptionChainedUniverseSelectionModelRegressionAlgorithm.cs index cc65d09d7db0..15facf1bc4a7 100644 --- a/Algorithm.CSharp/OptionChainedUniverseSelectionModelRegressionAlgorithm.cs +++ b/Algorithm.CSharp/OptionChainedUniverseSelectionModelRegressionAlgorithm.cs @@ -30,16 +30,14 @@ namespace QuantConnect.Algorithm.CSharp /// public class OptionChainedUniverseSelectionModelRegressionAlgorithm: QCAlgorithm, IRegressionAlgorithmDefinition { - Symbol _aapl; public override void Initialize() { UniverseSettings.Resolution = Resolution.Minute; SetStartDate(2014, 6, 6); SetEndDate(2014, 6, 6); SetCash(100000); - _aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA); - var config = new SubscriptionDataConfig(typeof(TradeBar), _aapl, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, false, false, true); - var universe = AddUniverse(new ManualUniverse(config, UniverseSettings, new[] { _aapl })); + + var universe = AddUniverse("my-minute-universe-name", time => new List { "AAPL", "TWX" }); AddUniverseSelection(new OptionChainedUniverseSelectionModel(universe, u => u.Strikes(-2, +2) // Expiration method accepts TimeSpan objects or integer for days. @@ -49,10 +47,11 @@ public override void Initialize() public override void OnData(Slice slice) { - if (!Portfolio.Invested && IsMarketOpen(_aapl)) + if (!Portfolio.Invested && IsMarketOpen("AAPL") && IsMarketOpen("TWX")) { - OptionChain chain; - if (slice.OptionChains.TryGetValue("?AAPL", out chain)) + var values = slice.OptionChains.Where(x => (x.Key == "?AAPL" || x.Key == "?TWX")).Select(x => x.Value); + + foreach (var chain in values) { // we find at the money (ATM) put contract with farthest expiration var atmContract = chain @@ -84,7 +83,7 @@ public override void OnData(Slice slice) /// /// Data Points count of all timeslices of algorithm /// - public long DataPoints => 936646; + public long DataPoints => 1033404; /// /// Data Points count of the algorithm history @@ -96,7 +95,7 @@ public override void OnData(Slice slice) /// public Dictionary ExpectedStatistics => new Dictionary { - {"Total Trades", "2"}, + {"Total Trades", "4"}, {"Average Win", "0%"}, {"Average Loss", "0%"}, {"Compounding Annual Return", "0%"}, @@ -116,11 +115,11 @@ public override void OnData(Slice slice) {"Information Ratio", "0"}, {"Tracking Error", "0"}, {"Treynor Ratio", "0"}, - {"Total Fees", "$2.00"}, - {"Estimated Strategy Capacity", "$100000.00"}, + {"Total Fees", "$4.00"}, + {"Estimated Strategy Capacity", "$110000.00"}, {"Lowest Capacity Asset", "AAPL 2ZTXYLO9EQPZA|AAPL R735QTJ8XC9X"}, - {"Portfolio Turnover", "8.01%"}, - {"OrderListHash", "3c4bef29d95bf4c3a566bca7531b1df0"} + {"Portfolio Turnover", "8.85%"}, + {"OrderListHash", "6a8e72c22752967e0d5d4e344e794dc4"} }; } } diff --git a/Algorithm.Python/OptionChainedUniverseSelectionModelRegressionAlgorithm.py b/Algorithm.Python/OptionChainedUniverseSelectionModelRegressionAlgorithm.py index 155bcf732118..e88dc2237f90 100644 --- a/Algorithm.Python/OptionChainedUniverseSelectionModelRegressionAlgorithm.py +++ b/Algorithm.Python/OptionChainedUniverseSelectionModelRegressionAlgorithm.py @@ -23,10 +23,7 @@ def Initialize(self): self.SetEndDate(2014, 6, 6) self.SetCash(100000) - self.aapl = Symbol.Create("AAPL", SecurityType.Equity, Market.USA) - tradebar = TradeBar() - config = SubscriptionDataConfig(type(tradebar), self.aapl, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, False, False, True) - universe = self.AddUniverse(ManualUniverse(config, self.UniverseSettings, [ self.aapl ])) + universe = self.AddUniverse("my-minute-universe-name", lambda time: [ "AAPL", "TWX" ]) self.AddUniverseSelection( OptionChainedUniverseSelectionModel( universe, @@ -38,20 +35,18 @@ def Initialize(self): ) def OnData(self, slice): - if self.Portfolio.Invested or not self.IsMarketOpen(self.aapl): return - chain = slice.OptionChains.GetValue("?AAPL") - if chain is None: - return - - # we sort the contracts to find at the money (ATM) contract with farthest expiration - contracts = sorted(sorted(sorted(chain, \ - key = lambda x: abs(chain.Underlying.Price - x.Strike)), \ - key = lambda x: x.Expiry, reverse=True), \ - key = lambda x: x.Right, reverse=True) + if self.Portfolio.Invested or not (self.IsMarketOpen("AAPL") and self.IsMarketOpen("AAPL")): return + values = list(map(lambda x: x.Value, filter(lambda x: x.Key == "?AAPL" or x.Key == "?TWX", slice.OptionChains))) + for chain in values: + # we sort the contracts to find at the money (ATM) contract with farthest expiration + contracts = sorted(sorted(sorted(chain, \ + key = lambda x: abs(chain.Underlying.Price - x.Strike)), \ + key = lambda x: x.Expiry, reverse=True), \ + key = lambda x: x.Right, reverse=True) - # if found, trade it - if len(contracts) == 0: return - symbol = contracts[0].Symbol - self.MarketOrder(symbol, 1) - self.MarketOnCloseOrder(symbol, -1) + # if found, trade it + if len(contracts) == 0: return + symbol = contracts[0].Symbol + self.MarketOrder(symbol, 1) + self.MarketOnCloseOrder(symbol, -1)