Skip to content

Commit

Permalink
Improve regression algos
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Feb 5, 2024
1 parent f2b4371 commit 6869a68
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ namespace QuantConnect.Algorithm.CSharp
/// </summary>
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<string> { "AAPL", "TWX" });

AddUniverseSelection(new OptionChainedUniverseSelectionModel(universe, u => u.Strikes(-2, +2)
// Expiration method accepts TimeSpan objects or integer for days.
Expand All @@ -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
Expand Down Expand Up @@ -84,7 +83,7 @@ public override void OnData(Slice slice)
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 936646;
public long DataPoints => 1033404;

/// <summary>
/// Data Points count of the algorithm history
Expand All @@ -96,7 +95,7 @@ public override void OnData(Slice slice)
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Trades", "2"},
{"Total Trades", "4"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
Expand All @@ -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"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit 6869a68

Please sign in to comment.