Condition Scanner – indicator MetaTrader 4

Condition Scanner (cs)

The idea behind the attached chunks of code is this:

You dream up a (set of) condition(s). You write code to test for your conditions. You embed your code within these chunks of code. You make a couple of easy mods.

You end up with a couple of indicators. One will point out historically on the current chart where your conditions arose. The other will show on a panel whether, for all of the instruments and timeframes in which you are interested, the conditions have currently arisen. This saves you from having to manually scan pairs and timeframes looking for your conditions.


To install:

Copy the contents of the four folders (Indicators, Include, Templates, Profiles) into the equivalent folders of your installation. There is nothing in there to hurt you – only indicators, etc.
Exit and re-enter the MetaQuotes program.

To demonstrate what the scanner can do:

This example is based on Power Pro (setup identifier = “PP”) which was being pushed around the traps by Russ Horn. It is a band break-out strategy where the upper band limit is an ema of highs and the lower limit is an ema of lows. There is absolutely no endorsement of these strategies by me – this is purely as example of what you can develop.

On your system, change to profile cspp. This could take several seconds for the first display but doesn’t use much computer resource thereafter.

You should see a screen similar to this:



The example above consists of:
an on-chart indicator with 4 plots,
a separate-window indicator showing EMA of RSI,
a separate-window indicator showing where conditions suggesting long or short trades have occurred historically,
and a panel indicator showing where the same conditions have just occurred or are currently occurring across a range of pairs and timeframes.

The code common to all scans:

Utils.mqh contains some utility functions which can be used in any indicator, expert adviser, etc.
cs_Chart.mqh contains almost all of the code which goes into the current-chart indicator.
cs_Panel.mqh contains almost all of the code which goes into the panel display indicator.

The code specific to the PP (Power Pro) strategy:

csPP_0.mq4 displays the relevant “chart-window” indicators.
csPP_1.mq4 displays the relevant “separate-window” indicators.
csPP_Chart.mq4 is the indicator which shows what happened historically on the current chart.
csPP_Panel.mq4 is the indicator which shows the current situation on all of the pairs and timeframes in which you are interested.
csPP_GetBarFlag.mqh is the code “included” in both the Chart and Panel indicators. It is the bit that decides whether there is a condition of interest at a particular bar.


I included another less complex example (identifier=MAX).

The code specific to the MAX (moving average crossover) strategy:

csMAX_0.mq4 is a custom indicator which displays two moving averages. It is completely self-contained.
There is no equivalent csMAX_1 because, unlike PP above, there is no need for a separate_window display.
csMAX_Chart.mq4, csMAX_Panel.mq4 and csMAX_GetBarFlag.mqh do the equivalent thing as for the PP strategy above.

The code whose ids begin with csXXXX:

These are templates which can be used when developing additional scans.
Alternative:  T3 TRIX (ROC of T6) - indicator MetaTrader 4

How I go about coding for a new condition:

I decide on a strategy identifier (eg PB for pinbars).

I set my window up as above under “To demonstrate what the scanner can do”, removing any existing indicators.

If my new scan relies on on-chart indicators, I build them using csXXXX_0.mq4 as a pattern. I remember to ‘save as…’ csPB_0.mq4. I apply my custom indicator to the left hand chart.

If I need a separate window indicator, I build it too. I remember to ‘save as…’ csPB_1.mq4. I apply it to the left hand chart.

I build custom indicators rather than using standard indicators so that I can refer to them elsewhere using the iCustom function. This way, if I change my mind about the basic parameters of my strategy (eg moving average method) I only have to change my custom indicators. If I used standard indicators, I would have to change them and then go digging into the bowels of my code to accommodate my mind change.

I grab csXXXX_Chart.mq4, replace all instances of ‘XXXX’ by my strategy identifier ‘PB’, and save the code as csPB_Chart.mq4. It wont compile yet because there is some “include” code still to be written.

Now to provide the “include” code.

If the conditions for which I want to test are dead easy, I just use the “include” file csXXXX_GetBarFlag.mqh, code up what I want, and ‘save as…’ csPB_GetBarFlag.mqh. But if the coding is tricky, I go back to the csPB_Chart.mq4 that I previously created, I comment out the line of code that now reads “#include <csPB_GetBarFlag.mqh>, and I build the code I want directly within the csPB_Chart.mq4 file. When I’ve got it clean, I move/paste the condition-finding code to the “include” file ‘csPB_GetBarFlag.mqh’ and un-comment the #include statement in the ‘csPB_Chart.mq4 file.

Now the ‘csPB_Chart.mq4’ file can be compiled and applied to the left hand chart.

At this point, I save the template for the left hand chart as csPB-CHART.tpl. It incorporates any on-chart custom indicators, separate-window custom indicators, and the plot of results as they apply to the current chart

I load the file ‘csXXXX_Panel.mq4’, replace all instances of “XXXX” by “PB”, then ‘save as…’ “csPB_Panel.mq4”. This last file is compiled and applied to the right hand chart. It can be a bit slow to run when first applied but doesn’t use much resource when running.

I store csPB_panel.tpl as a template for the right hand chart.

I will then usually store csPB as a profile.

NOTE: When building custom indicators and then referring to them with the iCustom function, I take care that the names are exactly right. You get no warning if iCustom refers to an indicator that doesn’t exist.

NOTE: There may be a bug such that when the panel indicator is first loaded, it produces results which are not consistent with those on the specific chart on display. I find that if I change the timescale on the panel window (thus forcing a re-calc), everything comes out right. I would be grateful if anyone can steer me right with this problem.

NOTE: In the …GetBarFlag.mqh code which you develop yourself, you can return any integer to the calling program to be displayed. As the code stands, zero means ‘no result’ and causes display of a gray wingding indicating which bar was tested. Returning a negative integer will result in a red display while a positive integer will result in a green display.

Alternative:  Rotate TEXT - script MetaTrader 4

Some aspects of the software:

The code was written as indicators so that, when a panel is on display and you want to look at a particular pair and timeframe, you just pick the pair from the Market Watch window, drag and drop it onto the current chart space and change the timeframe to suit. The template looks after the rest.

In an effort to reduce computer overload, I did two things.

Firstly, when dealing with time-series arrays, I mostly only go back 200 bars.

Secondly, the panel display software doesn’t test the user’s conditions at every tick. It uses a timer mechanism such that testing is applied to bar[0] only if bar[0] is 90% formed, otherwise testing is applied to bar[1]. If bar[1] has the focus (i.e. bar[0] is less than 90% formed), it is tested once and a changeover to bar[0] time is calculated. For every subsequent tick that comes along until the changeover time is reached, nothing happens. I am not 100% sure that I have got this coded up right yet but it seems to work pretty good.

Further to the timer mechanism mentioned above, the top line of the panel shows the date and time that the most recent condition-testing event occurred. Also, in the examples supplied here, the neutral wingdings will show a zero if bar[0] is in focus and a ‘1’ if bar[1] is in focus.

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks