MQL4 Help required - up/down stoploss indicator

 

I'm trying to code an indicator which is often refered to as an UP/DOWN STOPLOSS indicator within equities trading and has he following traits:

a) has 1 extern variable (let's call it slvar)

b) can move up/down upon close of each bar

c) could retain the value from the previous bar

In essence late's say the currency we're tracking is sitting at a close of 100, and my slvar is set at 10, then the indicator would have a value of 90

If the currency were to drop, the indicator would remain as 90 until the currency went as low as 80, after which if price continued to drop, the indicator would go down as well, but staying 10 pips (slvar) away from price, until such time that price rose and breaks through, but would again stay at 90 until the new high of 100 was reached.

The process would simply continue

I've looked and scoured and attempted to code various versions, but have just come to a dead end. Using the various documents & examples available online, I'm gaining experience coding basic indicators when each bar invokes a new calaculation/value, but just can't get my head around potentially retaining the indicator from the [i+1] bar.

Any help or advice or examples would be greatly appreciated.

Paul.

Some sample code I created used iFractal - see below - but not sure if this is the best method or whether I have misisng logic.

#property indicator_chart_window

#property indicator_buffers 1

//---- BUFFER

double SLBuffer[];

//---- EXTERNAL INPUT(S)

extern int SLVar = 50;

//---- INTERNAL VARIABLE(S)

double CLV[];

double CPV[];

double frachigh;

double fraclow;

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

//+ INIT |

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

int init()

{

//---- ERROR TRAPS

//---- INDICATOR STYLE

SetIndexBuffer(0, SLBuffer);

SetIndexStyle( 0, DRAW_LINE, STYLE_SOLID, 1, Red);

//----

return(0);

}

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

//+ DEINIT |

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

int deinit() { return(0); }

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

//+ START |

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

int start()

{

//---- Find MaxRecords

int counted_bars=IndicatorCounted();

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

if(counted_bars>0) { counted_bars--; }

int MaxRecords = Bars-counted_bars;

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

{

CPV[i] = Close[i] + SLVar*Point;

CLV[i] = Close[i] - SLVar*Point;

Frachigh = ifractal( );

Fraclow = ifractal( );

if(frachigh !>0 && fraclow !>0) SLBuffer[i] = SLBuffer[i+1];

if(frachigh >0) SLBuffer[i] = MathMax( SLBuffer[i+1], CLV[i] ):

if(fraclow >0) SLBuffer[i] = MathMax( SLBuffer[i+1], CPV[i] ):

}

//----

return(0);

}

 

Problem solved.

Tried various modes of attack, including tweaking Parabolic SAR, but in the end managed to identify what I was looking for was indeed a tweaked version of the old VT ATR Stop & Reverse system.

The big lesson learnt for me was that whilst I was on the right track in the 1st place, it was my very poor usage of if-then-else clustering in my code causing the error.

So at least I've learnt something new as well.

Paul