Hi
I've set up two consolidators, one for days starting/ending at New York 17:00 and one for weeks starting/ending at at New York Friday 17:00, each with their own event handler. Both will be used for several Forex and CFD symbols. My question is what order are the event handlers called? For each symbol I'd like the weekly event handler to execute before the daily, but I don't know to ensure that.
Cheers
Tony
Here's the relevant initialisation code:
// Set up instrument data.
foreach (string inst in _fxPairs)
{
// Security fxPair = AddForex(inst, _resolution, Market.Oanda, false); // To match TradingView data.
Security fxPair = AddForex(inst, _resolution, Market.FXCM, false); // To match Research environment.
JPDPSetup instrumentData = new JPDPSetup(fxPair.Symbol, // QC symbol
_dataLookback, // day rolling window size
_smaLen, // SMA length
_smaSlopeLen, // Length for SMA slope
_atrLen // ATR length
);
_setupByInstrument.Add(inst, instrumentData);
}
foreach (string inst in _cfds)
{
// Instrument Data
Security cfd = AddCfd(inst, _resolution, Market.Oanda, false);
JPDPSetup instrumentData = new JPDPSetup(cfd.Symbol, // QC symbol
_dataLookback, // day rolling window size
_smaLen, // SMA length
_smaSlopeLen, // Length for SMA slope
_atrLen // ATR length
);
_setupByInstrument.Add(inst, instrumentData);
}
foreach (var kvp in _setupByInstrument)
{
// This is required because we're using closures below, for more information see:
// http://stackoverflow.com/questions/14907987/access-to-foreach-variable-in-closure-warning
var setupData = kvp.Value;
// Daily consolidation
var nyCloseConsolidator = new QuoteBarConsolidator(NYDaily);
nyCloseConsolidator.DataConsolidated += (sender, baseData) =>
{
// The consolidated bar.
var bar = (IBaseDataBar)baseData; // the consolidated bar
setupData.DailyBarWindow.Add(bar);
// Update indicators and rolling windows.
setupData.Sma.Update(bar.Time, bar.Close);
setupData.SmaWindow.Add(setupData.Sma);
setupData.Atr.Update(bar);
// Strategy actions.
OnNYClose(sender, baseData);
};
SubscriptionManager.AddConsolidator(setupData.Symbol, nyCloseConsolidator);
// Weekly consolidation
var nyWeekConsolidator = new QuoteBarConsolidator(NYWeekly);
nyWeekConsolidator.DataConsolidated += OnNYWeekClose;
SubscriptionManager.AddConsolidator(setupData.Symbol, nyWeekConsolidator);
// Order management
_entries = new List<OrderGroup>(); // *** Think this through.
}
Tony Shacklock
Further to this question, my simple tests show that it is the last consolidator to be initialised that runs first and OnData() runs after them both, but is this guaranteed, or unpredictable?
For example, this code runs OnNYWeekClose() before OnNYClose().
// Daily Consolidation
var nyCloseConsolidator = new QuoteBarConsolidator(NYClose);
nyCloseConsolidator.DataConsolidated += OnNYClose;
SubscriptionManager.AddConsolidator(_sym, nyCloseConsolidator);
// Weekly Consolidation
var nyWeekConsolidator = new QuoteBarConsolidator(NYWeekly);
nyWeekConsolidator.DataConsolidated += OnNYWeekClose;
SubscriptionManager.AddConsolidator(_sym, nyWeekConsolidator);
Whereas, this code runs OnNYClose() before OnNYWeekClose().
// Weekly Consolidation
var nyWeekConsolidator = new QuoteBarConsolidator(NYWeekly);
nyWeekConsolidator.DataConsolidated += OnNYWeekClose;
SubscriptionManager.AddConsolidator(_sym, nyWeekConsolidator);
// Daily Consolidation
var nyCloseConsolidator = new QuoteBarConsolidator(NYClose);
nyCloseConsolidator.DataConsolidated += OnNYClose;
SubscriptionManager.AddConsolidator(_sym, nyCloseConsolidator);
Cheers, Tony
Alexandre Catarino
Hi Tony Shacklock ,
The SubscriptionDataConfig.Consolidators object that stores the consolidators is a ConcurrentSet<IDataConsolidator> object that wraps a ConcurrentDictionary, it means that its order is not guaranteed to be deterministic.
Tony Shacklock
Hi Alexandre
Thank you for that clarification. I will have to write my code to be independent of execution order.
Cheers, Tony
Tony Shacklock
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!