Hello,
I would like to get an estimate of Forex volume data and have read various posts on the forum explaining how to do that using FxcmVolume and a downloader, but that require to run a local version of Lean which I would rather avoid.
Instead, I was thinking of using tick data to get a rough estimate. Ideally, I would like to do it by counting the number of ticks that have come in during a given period, as initially suggested by Douglas Stridsberg. Would it be a way to do that efficiently?
Alternatively, I was thinking of adding a TickQuoteBarConsolidator to my algorithm that would aggregate a given number of ticks (e.g. 1000) and then look at the difference between the ending time and the starting time of the consolidated bar and use the time it takes to consolidated 1000 ticks as a proxy for volume (the lower the time, the higher the volume).
The idea would then be to send this info into a custom indicator that could compare the current value or short term average to a longer term average.
I have tried to go with the latest option but got stuck very quickly as I could not even manage to make the TickQuoteBarConsolidator work and retrieve the start and end time (see backtest attached, the code related to that is commented to avoid the error and be able to share the algo).
Please note that I would like the rest of the algo to work with custom QuoteBarConsolidator based on Hour resolution as I am afraid performance will really deteriorate if I move everything to tick. Trying to have both running in parallel is probably part of the issue I have.
I would greatly appreciate some help. Thanks!
Rahul Chowdhury
Hey Pi..R,
To use a TickQuoteBarConsolidator, you must supply it with tick data. Right now in your algorithm, you are subscribed to hourly resolution data.
// In UserVar.cs Resolution _Res = Resolution.Hour; // Should Be Resolution _Res = Resolution.Tick;
You can't use both a QuoteBarConsolidator and a TickQuoteBarConsolidator simultaneously for the same symbol. This is because QuoteBarConsolidators require quotebar data (i.e. second, minute, hour, daily data), while TickQuoteBarConsolidators only take tick data.Also you are using a very long warm up period which is causing your algorithm to take a long time to warm up. Instead you can use
// In UserVar.cs private int _WarmUpPeriod = 10;
Pi..R
Hi again Rahul,
Since it is not possible to use a QuoteBarConsolidator and a TickQuoteBarConsolidator simultaneously, it seems like my only option it to run the entire algorithm with tick data.
However, since I want the algorithm to run with custom hourly TickQuoteBarConsolidator (i.e. using TimeSpan.FromHours(X)), the idea of using the difference in time between the beginning and the end of the consolidated bar does not work anymore (it will be always the same).
Is there a way to count the number of ticks within each consolidated bar instead?
Rahul Chowdhury
Hi Pi..R,
You can first create a TickQuoteBarConsolidator with a fixed period, for example one hour.
public static TimeSpan tickBarLength = TimeSpan.FromHours(1); TickConsolidator = new TickQuoteBarConsolidator (tickBarLength);
Also let's define a new field within SymbolData called number_of_ticks.
public int number_of_ticks = 0;
Now instead of subscribing our consolidator to the algorithm and automatically updating with ticks, we can manually update the consolidator with tick data, similar to how an indicator can be manually updated with data.In our OnData, let's access the tick data and count the number of ticks emitted in that time stamp. Then let's update our consolidator with each tick.
var ticks = data.Ticks[symbolData.Symbol]; var current_number_of_ticks = ticks.Count; symbolData.number_of_ticks += current_number_of_ticks; foreach(var tick in ticks){ symbolData.TickConsolidator.Update(tick); }
Finally we can reset the number of ticks after every QuoteBar is consolidated in the DataConsolidated event handler.
TickConsolidator.DataConsolidated += (sender, baseData) => { var _tickBar = (IBaseDataBar)baseData; TickBarsWin.Add(_tickBar); ConsolidatorFlag = true; algorithm.Debug($"TickQuoteBar emitted at {algorithm.Time} with {number_of_ticks} ticks"); number_of_ticks = 0; };
Now we have QuoteBars consolidated from tick data every hour and we also know the number of ticks in every bar.
Pi..R
Thanks Rahul, this is amazing! Last thing and then I'll stop, you have been doing a lot already!
What if I want to store the number of ticks after every QuoteBar is consolidated into a Moving Average Indicator?
I have tried the following code, but I get an error "Argument 2: cannot convert from 'int' to 'QuantConnect.Indicators.IIndicator'":
NumTicksEMAfast = new ExponentialMovingAverage(7).Of(number_of_ticks);
Also I am not sure what would be the code to update the indicator in that situation.
Rahul Chowdhury
Hi Pi..R,
You should create an ExponentialMovingAverage indicator and manually update it with the number of ticks.
// In SymbolData ExponentialMovingAverage tickema = ExponentialMovingAverage(7) // In consolidated event handler tickema.Update(number_of_ticks) number_of_ticks = 0
Best
Rahul
Pi..R
Hi Rhaul,
Thanks for the help, I just tried that but I still get the error message Argument 1: cannot convert from 'int' to 'QuantConnect.Data.IBaseData'
See backtest attached, if you uncomment line 53/54.
Rahul Chowdhury
Hey Pi..R,
Sorry, you have to also add the time as an argument when you update the indicator.
numTicksEMAfast.Update(algorithm.Time, number_of_ticks); numTicksEMAslow.Update(algorithm.Time, number_of_ticks);
Pi..R
Great, super helpful and I now know how to update IndicatorDataPoint with all sort of decimal value! Thanks a lot Rahul :)
Pi..R
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!