Skip to content

Commit 6869a68

Browse files
committed
Improve regression algos
1 parent f2b4371 commit 6869a68

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

Algorithm.CSharp/OptionChainedUniverseSelectionModelRegressionAlgorithm.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ namespace QuantConnect.Algorithm.CSharp
3030
/// </summary>
3131
public class OptionChainedUniverseSelectionModelRegressionAlgorithm: QCAlgorithm, IRegressionAlgorithmDefinition
3232
{
33-
Symbol _aapl;
3433
public override void Initialize()
3534
{
3635
UniverseSettings.Resolution = Resolution.Minute;
3736
SetStartDate(2014, 6, 6);
3837
SetEndDate(2014, 6, 6);
3938
SetCash(100000);
40-
_aapl = QuantConnect.Symbol.Create("AAPL", SecurityType.Equity, Market.USA);
41-
var config = new SubscriptionDataConfig(typeof(TradeBar), _aapl, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, false, false, true);
42-
var universe = AddUniverse(new ManualUniverse(config, UniverseSettings, new[] { _aapl }));
39+
40+
var universe = AddUniverse("my-minute-universe-name", time => new List<string> { "AAPL", "TWX" });
4341

4442
AddUniverseSelection(new OptionChainedUniverseSelectionModel(universe, u => u.Strikes(-2, +2)
4543
// Expiration method accepts TimeSpan objects or integer for days.
@@ -49,10 +47,11 @@ public override void Initialize()
4947

5048
public override void OnData(Slice slice)
5149
{
52-
if (!Portfolio.Invested && IsMarketOpen(_aapl))
50+
if (!Portfolio.Invested && IsMarketOpen("AAPL") && IsMarketOpen("TWX"))
5351
{
54-
OptionChain chain;
55-
if (slice.OptionChains.TryGetValue("?AAPL", out chain))
52+
var values = slice.OptionChains.Where(x => (x.Key == "?AAPL" || x.Key == "?TWX")).Select(x => x.Value);
53+
54+
foreach (var chain in values)
5655
{
5756
// we find at the money (ATM) put contract with farthest expiration
5857
var atmContract = chain
@@ -84,7 +83,7 @@ public override void OnData(Slice slice)
8483
/// <summary>
8584
/// Data Points count of all timeslices of algorithm
8685
/// </summary>
87-
public long DataPoints => 936646;
86+
public long DataPoints => 1033404;
8887

8988
/// <summary>
9089
/// Data Points count of the algorithm history
@@ -96,7 +95,7 @@ public override void OnData(Slice slice)
9695
/// </summary>
9796
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
9897
{
99-
{"Total Trades", "2"},
98+
{"Total Trades", "4"},
10099
{"Average Win", "0%"},
101100
{"Average Loss", "0%"},
102101
{"Compounding Annual Return", "0%"},
@@ -116,11 +115,11 @@ public override void OnData(Slice slice)
116115
{"Information Ratio", "0"},
117116
{"Tracking Error", "0"},
118117
{"Treynor Ratio", "0"},
119-
{"Total Fees", "$2.00"},
120-
{"Estimated Strategy Capacity", "$100000.00"},
118+
{"Total Fees", "$4.00"},
119+
{"Estimated Strategy Capacity", "$110000.00"},
121120
{"Lowest Capacity Asset", "AAPL 2ZTXYLO9EQPZA|AAPL R735QTJ8XC9X"},
122-
{"Portfolio Turnover", "8.01%"},
123-
{"OrderListHash", "3c4bef29d95bf4c3a566bca7531b1df0"}
121+
{"Portfolio Turnover", "8.85%"},
122+
{"OrderListHash", "6a8e72c22752967e0d5d4e344e794dc4"}
124123
};
125124
}
126125
}

Algorithm.Python/OptionChainedUniverseSelectionModelRegressionAlgorithm.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ def Initialize(self):
2323
self.SetEndDate(2014, 6, 6)
2424
self.SetCash(100000)
2525

26-
self.aapl = Symbol.Create("AAPL", SecurityType.Equity, Market.USA)
27-
tradebar = TradeBar()
28-
config = SubscriptionDataConfig(type(tradebar), self.aapl, Resolution.Minute, TimeZones.NewYork, TimeZones.NewYork, False, False, True)
29-
universe = self.AddUniverse(ManualUniverse(config, self.UniverseSettings, [ self.aapl ]))
26+
universe = self.AddUniverse("my-minute-universe-name", lambda time: [ "AAPL", "TWX" ])
3027
self.AddUniverseSelection(
3128
OptionChainedUniverseSelectionModel(
3229
universe,
@@ -38,20 +35,18 @@ def Initialize(self):
3835
)
3936

4037
def OnData(self, slice):
41-
if self.Portfolio.Invested or not self.IsMarketOpen(self.aapl): return
42-
chain = slice.OptionChains.GetValue("?AAPL")
43-
if chain is None:
44-
return
45-
46-
# we sort the contracts to find at the money (ATM) contract with farthest expiration
47-
contracts = sorted(sorted(sorted(chain, \
48-
key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
49-
key = lambda x: x.Expiry, reverse=True), \
50-
key = lambda x: x.Right, reverse=True)
38+
if self.Portfolio.Invested or not (self.IsMarketOpen("AAPL") and self.IsMarketOpen("AAPL")): return
39+
values = list(map(lambda x: x.Value, filter(lambda x: x.Key == "?AAPL" or x.Key == "?TWX", slice.OptionChains)))
40+
for chain in values:
41+
# we sort the contracts to find at the money (ATM) contract with farthest expiration
42+
contracts = sorted(sorted(sorted(chain, \
43+
key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
44+
key = lambda x: x.Expiry, reverse=True), \
45+
key = lambda x: x.Right, reverse=True)
5146

52-
# if found, trade it
53-
if len(contracts) == 0: return
54-
symbol = contracts[0].Symbol
55-
self.MarketOrder(symbol, 1)
56-
self.MarketOnCloseOrder(symbol, -1)
47+
# if found, trade it
48+
if len(contracts) == 0: return
49+
symbol = contracts[0].Symbol
50+
self.MarketOrder(symbol, 1)
51+
self.MarketOnCloseOrder(symbol, -1)
5752

0 commit comments

Comments
 (0)