Hey there,
I'm trying to consolidate 500ticks data using TradeBarConsolidator,
using the following reference :
https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/DataConsolidationAlgorithm.pyI tried to adapt it to futures:
class DynamicCalibratedContainmentField(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 3, 1) # Set Start Date
self.SetEndDate(2020,3,2) #Set End Date
self.SetCash(50000)
ticker = Futures.Indices.NASDAQ100EMini
self.AddFuture(ticker, Resolution.Tick )
#self.ESSMA = self.SMA(futureES.Symbol, 2)
CountConsolidator = TradeBarConsolidator(500)
CountConsolidator.DataConsolidated += self.BarHandler
self.SubscriptionManager.AddConsolidator(ticker, CountConsolidator)
def BarHandler(self, sender, bar):
# With hourly data the bar period is 3-hours
self.Debug(str(bar.EndTime - bar.Time) + " " + bar.ToString())
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
for contract in data.FutureChains:
contracts = list(filter(lambda x: x.Expiry > self.Time, contract.Value))
if len(contracts) == 0:
return
self.Debug(f"{self.Time} Last Price: {contracts[0].LastPrice}")
Is the following request supported?
Derek Melchin
Hi Ofir,
To consolidate futures tick data, we first need to select a contract and use the TickConsolidator. We can't consolidate ticks of the canonical symbol.
In Initialize, we write
ticker = Futures.Indices.NASDAQ100EMini future = self.AddFuture(ticker, Resolution.Tick) future.SetFilter(0, 90) self.consolidator_by_symbol = {}
Then, in OnData, we select the contract and setup the consolidator. We only setup a consolidator once for each contract we select.
def OnData(self, data): for chain in data.FutureChains: contracts = list(chain.Value) if len(contracts) == 0: continue sortedByOIContracts = sorted(contracts, key=lambda k : k.OpenInterest, reverse=True) contract = sortedByOIContracts[0] if contract.Symbol in self.consolidator_by_symbol: continue CountConsolidator = TickConsolidator(500) CountConsolidator.DataConsolidated += self.BarHandler self.SubscriptionManager.AddConsolidator(contract.Symbol, CountConsolidator) self.consolidator_by_symbol[contract.Symbol] = CountConsolidator
See the attached backtest for reference.
Best,
Derek Melchin
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.
Ofir Shmulevich
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!