hysteresis

 

Hi,

if you want to apply the principle of a thermostat, (switch on the heat if the temperature drops below a low level, switch off if temperature goes above a high level)

-it's called hysteresis- to avoid switching on and off multiple times in a row if the indicator is close to the threshold, how would you do it? Take a look at this example

with the ADX indicator and 22 as the high level and 18 as the low level: but here I want it to show 1 (for trend) and 0 (for no trend).

so:

adx - indi (should have this value)

17 - 0

18 - 0

19 - 0

20 - 0

21 - 0

22 - 1

23 - 1

22 - 1

21 - 1

20 - 1

19 - 1

18 - 1

17 - 0


// now in mql4 (iteration function of a custom indicator):

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

int limit;

double middle, upper, lower;

int counted_bars=IndicatorCounted();

//---- check for possible errors

if(counted_bars<0) return(-1);

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(int i=0; i<limit; i++)

{

double iADX(NULL,0,14,PRICE_HIGH,MODE_MAIN,0) = ADX;

double indi;

//and then..

if (ADX[i]>22)

{

indi=1

}else

{

indi=0

}

}

//----

return(0);

}

 

Hi

Hysteresis requires two levels and a memory but your code only has one level and no memory of the previous value.


indi should be a global variable so it retains its value between ticks and the code should contain two test


if(adx > 22 ) indi=1;

if(adx< 18 ) indi=0;

Reason: