Enivid, ..a request for EarnForex range bar "indicator"


Jan 5, 2018

First, you are a great MQL coder. I asked you write an addition to your market profile indicator and it was amazing. More importantly, you shared the code with the community. I use this indicator every day I trade.

But I wanted to ask you to write an indicator (actually I think it'd need to be an EA) to produce range bars as a custom symbol. This would be for MT5 only bc there are plenty of MT4 range bar indicators (EAs) yet there is really nothing good to make range bars as a custom symbol for MT5. I use range bars on NinjaTrader8 and I used to use them in MarketDelta and I love em, just like I love your MT5 market profile indicator.

The reasons why::

I like range bars to filter out price noise, which in turn filters out the noise of indicators when applied to range bars. But being able to apply any indicator that I want requires the range bar chart be created as a custom symbol.

How I think it should be approached::

....you may have a better way, just my thoughts.

In OnInit(), EA would first check whether the custom symbol exists...let's call the symbol EURUSD_Range_[Ticks]...where "Ticks" is the number size, in ticks, of the range bars. Not all instruments will have a tick size equal to Point, so you'll have to get tick size with SymbolInfoDouble(SYMBOL_TRADE_TICK_SIZE). Just don't assume it is always equal to Point. Then, if custom symbol doesn't exist, create it using CustomSymbolCreate().

Still running in OnInit(), if the code just created the symbol, set all the symbol info using the various CustomSymbolSet.... functions. See here: https://www.mql5.com/en/docs/customsymbols

If the custom symbol already exists and the EA hasn't run in a while, e.g., not since yesterday, then the EA will have to iterate over the history that has occurred since the last bar time that was already fed into the custom symbol, and then update the custom symbol and add MqlRates objects to the custom symbol to cover the intermittent time and make sure the custom symbol has a full history. I think the best way to do this is to determine the open time of the last bar in custom symbol and then use CustomRatesReplace().

When doing this, the EA would have to iterate over all ticks (or M1 bars if server does not have tick data) from the current_time to the open time of the last bar in the custom symbol. This should cover the time window between when the EA last ran and current_time. Again, some brokers will have tick by tick data (such as American futures brokers) and other brokers will have only M1 data. Maybe the EA should just use M1 to keep things simpler, iterate over the M1 bars (which are MqlRates objects) and "convert" that price data (which is already MqlRates objects) into an array of MqlRates representing the range bars, which will be fed into the custom symbol and become the range bars. An MqlRate object is basically just a bar object. Use CustomRatesReplace() to replace the last bar in the custom symbol with the MqlRates array generated after iterating over the M1 for the time window described above.

Now running in OnTick(), whether the code created the custom symbol fresh or updated to fill in interim history, the EA can begin collecting live ticks. My understanding is that live ticks must come in in the form of MqlTick type structs....not MqlRates) and feed those into the symbol using CustomRatesUpdate(). So the code will have to take an MqlTick as input, then output a MqlRate which represents the range bar object. Then this MqlRate object is fed into the custom symbol using CustomRatesUpdate().

So this logic will be different than converting M1 bars to range bars. In other words, one function will take MqlRates objects (M1 bars) as an input and one function will take MqlTick objects as input, and both will output an MqlRates object representing the most recent range bar. The logic will have to determine whether a new range bar has effectively printed or if the most recent range bar has simply changed. The length of time a range bar stays open is completely indeterminable; it is based on the size set for the range bar and the distance of price movement.

User inputs that I think are necessary::

The key user input is the size (in symbol ticks) of the range bars. So a value of 5 will mean a range of 5 ticks. For EURUSD, 5 ticks is 0.00005 or 5 Points. But for the S&P500 emini contract, 5 ticks is a range of 1.25. For many CFDs, 5 ticks is 0.5. Get symbol's tick size w SymbolInfoDouble(SYMBOL_TRADE_TICK_SIZE), do not use Point. Let's call this input range_bar_ticks for now.

Further, I think the EA should also have the option to have the range_bar_ticks be determined based on ATR values, which means it would need an ATR_Timeframe input and and ATR_Period input. Say that if range_bar_ticks == 0, then use the ATR value to determine range bar size. If range_bar_ticks > 0 then use that value.

The problem with including ATR is...should it constantly recalculate all the MqlRates already in the custom symbol to redraw the range bars based on the ever changing ATR value? I think it should. If this is not performant, then add a user input to limit the max number of range bars to display, or maybe a better solution?

If you don't re-update based on the ever changing ATR value and, instead, just use whatever the ATR value is when the EA is first attached, then the custom symbol will still have to be recalculated entirely at that time. Also, the custom symbol should be something like EURUSD_Range_ATR_[ATR_TF]_[ATR_Period] where ATR_TF is like H1 for example and ATR_Period is the number for the period. This would allow the EA to differentiate the various custom symbols stored.

Or maybe you have better ideas...? Alternatively, just leave out the whole ATR thing. It's okay if it lacks the ATR component if it means getting the basics actually built.

My biggest problem is getting the code to "convert" the MqlTick objects or MqlRates objects into into the MqlRates range bar objects. It is not simple.

General info on Nicolellis range bars::

Here are links discussing the rules for creating range bars. Range bars will always have exactly one wick, never two. Well, they can have zero wicks but that's pretty rare. Never two, tho.

Anyway, I know this is a lot. But this would be a huge tool that does not already exist out in the world. And making an open source version would really help a lot of people. And the same EA could also be converted to make Renko bars or maybe even point-n-figure bars (aka, reversal bars). Here's info on reversal bars: https://www.multicharts.com/trading-software/index.php/Reversal_Bar
Last edited: