Hi QC community… I’m floundering over getting a custom consolidator class working, for the purpose of defining custom start and end times for daily data, so that my algorithm is fed values at my custom time rather than 00:00. The reason I’m trying to do this:
A day on the Forex markets is from 17:00 to 16:59 the next day (New York time). Since QC's included daily Forex data appears to be coded from 00:00 to 23:59, I've encountered some difficulties with daily consolidators not aligning with the true market close for the day. For example, subscribing to minute data and feeding it into a daily consolidator gives me price values sampled at 00:00 every day (which happens to be part way through the Asian session and not what I want). I have seen one person raise this on the forum and the suggested solution was to craft a custom consolidator (with an example given in C#). I’ve tried to recreate this in Python but I’m not having much luck, it seems like my custom class is being called but the logic inside it is being ignored. Below is the code I’ve come up with. Any advice is very much appreciated!
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin)
self.SetStartDate(datetime(2018,8,1))
self.SetEndDate(datetime.now())
self.SetCash(100000)
self.currency_pair_dict = {
"EURGBP": [], "EURAUD": [], "EURNZD": [], "EURUSD": [],
"EURCAD": [], "EURCHF": [], "EURJPY": [],
"GBPAUD": [], "GBPNZD": [], "GBPUSD": [], "GBPCAD": [],
"GBPCHF": [], "GBPJPY": [],
"AUDNZD": [], "AUDUSD": [], "AUDCAD": [], "AUDCHF": [],
"AUDJPY": [],
"NZDUSD": [], "NZDCAD": [], "NZDCHF": [], "NZDJPY": [],
"USDCAD": [], "USDCHF": [], "USDJPY": [],
"CADCHF": [], "CADJPY": [],
"CHFJPY": []
self.consolidator_dict = {}
for pair in self.currency_pair_dict:
self.AddForex(pair, Resolution.Minute, Market.Oanda, 100)
thisConsolidator = CustomConsolidator(timedelta(days=1))
self.consolidator_dict[pair] = thisConsolidator
self.consolidator_dict[pair].DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator(pair, self.consolidator_dict[pair])
def OnDataConsolidated(self, sender, bar):
if str(bar.Symbol) == "EURUSD":
self.Log("Bar date/time: " + bar.Time.ctime())
def OnData(self, data):
pass
class CustomConsolidator(QuoteBarConsolidator):
def __init__(self, timespan):
self.consolidatedBar = QuoteBar()
def Update(self, data):
if (data.Time.hour == 17 and data.Time.minute == 00):
OnDataConsolidated(self.consolidatedBar)
self.consolidatedBar = QuoteBar(data.Time)
else:
self.consolidatedBar.Update(data.Close, data.Bid, data.Ask)
Jing Wu
Hi Joel, the custom consolidator hasn't been implemented in LEAN. Using "CustomConsolidator(QuoteBarConsolidator)" will only implement the base class QuoteBarConsolidator instead of CustomConsolidator. We are going to add this feature shortly. Please subscribe to this Github issue so you'll get a notification once custom consolidator is available.
Joel Eames
Thanks for your reply Jing Wu! I will do that.
Joel Eames
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!