Hi All-
I've built a strategy upon minutely universe resolution. Now that it's mostly stable I want to speed things up a bit, but second data is too computationally intensive. Something like a few seconds (5 or 10 seconds) for the OnData rate seems to be more appropriate for the strategy.
I couldn't find anything in the Documentation or Forum covering this, but I do know that OnData is set by the universe resolution.
Attached is the time and data related code I have currently. Basically just a rollingwindow that consolidates the minutely data into daily bars. I removed the fluff for the sake of readability.
So, is a custom resolution possible to feed into OnData??
PS: This algo only trades Equities (for now)
def Initialize(self):
self.SetStartDate(2022, 8, 1)
self.SetCash(1000000)
self.rebalanceTime = datetime.min
self.AddUniverse(self.CoarseSelectionFilter, self.FineSelectionFilter)
self.UniverseSettings.Resolution = Resolution.Minute
self.AddEquity("SPY")
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 10), self.ExitPositions)
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9, 31), self.ScanTargets)
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.Midnight, self.ClearFills)
self.Data = {}
self.AddRiskManagement(TrailingStopRiskManagementModel(0.05))
def OnData(self, data):
for symbol in self.Targets.keys():
symbolData = self.Data[symbol]
if not symbolData.IsReady:
continue
open_orders = self.Transactions.GetOpenOrders(symbol)
if not self.Portfolio[symbol].Invested and len(open_orders) == 0 and symbol not in self.filled_today.keys():
# Strategy logic.....
def CoarseSelectionFilter(self, coarse):
# filter logic....
def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
symbol = security.Symbol
if symbol not in self.Data:
self.Data[symbol] = SymbolData(self, symbol)
for security in changes.RemovedSecurities:
symbol = security.Symbol
if symbol in self.Data:
symbolData = self.Data.pop(symbol, None)
self.SubscriptionManager.RemoveConsolidator(symbol, symbolData.consolidator)
class SymbolData:
def __init__(self, algorithm, symbol):
self.algorithm = algorithm
self.symbol = symbol
self.Bars = RollingWindow[TradeBar](6)
self.consolidator = TradeBarConsolidator(timedelta(days=1))
self.consolidator.DataConsolidated += self.OnDataConsolidated
algorithm.SubscriptionManager.AddConsolidator(symbol, self.consolidator)
def OnDataConsolidated(self, sender, bar):
self.Bars.Add(bar)
@property
def IsReady(self):
return self.Bars.IsReady
Nico Xenox
Hey Justin E,
I wouldnt recommend trying to adjust the rate of OnData because for that you would have to adjust the resolution. Otherwise it probably wont work.
example:
For that you would have to change the resolution of your stocks to second. This will make your algo work slowly…
A better way would be to make a scheduled Events. For this you would not need to change the resolution of your stocks and it would fire each time.
Hope it helps ;)
Justin E
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!