Hi guys,
I've read about how consolidation should work but seems I can't get my head around it and seem to find stuff that does not match any understanding I have with it.
I've tried consolidating Forex tick data in 30 minute bars and then tried to make sense of the logic behind it.
Here goes:
1. first algo, just outputs the bid/ask values:
def Initialize(self):
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.SetStartDate(2018, 10, 1) # Set Start Date
self.SetEndDate(2018, 10, 1) # Set Start Date
self.SetCash(1000) # Set Strategy Cash
self.AddForex("EURUSD", Resolution.Tick, Market.Oanda)
def OnData(self, data):
self.Log("Bid " + str(self.Securities["EURUSD"].BidPrice))
self.Log("ASK " + str(self.Securities["EURUSD"].AskPrice))
2. second algorithm uses a consolidator to generate 30 minute bars:
def Initialize(self):
self.SetStartDate(2018, 10, 1) # Set Start Date
self.SetEndDate(2018, 10, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddForex("EURUSD", Resolution.Tick)
consolidator = TickQuoteBarConsolidator(timedelta(minutes=30))
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator("EURUSD", consolidator)
def OnDataConsolidated(self, sender, bar):
self.Debug(str(self.Time) + " > New Bar! " + str(bar.Open))
When I look at the first line in the logfile for the consolidator algo, I have:
2018-10-0100:30:00 2018-10-01 00:30:00.173000 > New Bar! 1.159645
If I look at the tick bid/ask values for the begining of that minute, I have:
01/10/201800:29:57Bid1.1593701/10/201800:29:57ASK1.1595201/10/201800:30:02Bid1.1593701/10/201800:30:02ASK1.1595101/10/201800:30:02Bid1.1593601/10/201800:30:02ASK1.1595001/10/201800:30:03Bid1.1593701/10/201800:30:03ASK1.15951I don't understand where the bar.Open value (1.159645) comes from, looking at the Bid/Ask values for around that time.
When backtesting an algo using minute resolution, I noticed that the price the market orders were filled was usually the last bid/ask of the previous minute so when I was looking at open/close data to generate a trendline (for ex), the market orders were pretty far away from the open/close values.
Hope my question makes sense.
Dan
Dan Aldea
I think I've figured it out, I was looking at the time the wrong way: the 00:30:00 bar I guess consolidates 00:00:00 - 00:29:59 ticks, is that correct? In this case the bid/ask average matches the first tick of 00:00:00.
For the next bar (00:01:00) the difference is pretty small (1.15941 open value vs 1.15944 average bid/ask). Still not exactly the same, but a lot closer than my previous atempt.
Douglas Stridsberg
Hey Dan - you're right in saying that OnDataConsolidated() gets called when a bar has finished consolidating. That bar will be passed into the function, meaning the close of that bar should be closest to the live price. The open will be from the start of the bar, i.e. 30 minutes before.
Please see this documentation page for more information on how time is managed in QC.
Dan Aldea
Thanks Douglas, somehow missed that document, makes perfect sense now.
Dan Aldea
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!