# Appreciate some help to understand this Trail Stop Code

Hi there, I have recently found this Trail stop Code but can't get my handle to understand it so any help is appreciated. Thanks in advance...

``````Buy=Buy1; // Buy1 is the buy condition that trigger the buy action
Short=Sell1; // Same usage as Buy1

Sell=0;
Cover=0;

SL=Param("trailing stop %", 0.4, 0.1, 10, 0.1);
StopLevel1 = 1 - SL/100;
StopLevel2 = 1 + SL/100;

trailARRAY =trailARRAYs= Null; // why need trail array here where it can't just use the trailstop array?
trailstop =tstop= 0; // why 2 different trailstop array for the same function
for( i = 1; i < BarCount; i++ )
{

if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * stoplevel1;
}
else Buy[ i ] = 0; // remove excess buy signals // how this remove excess signal?

if( trailstop > 0 AND (Low[ i ] < trailstop OR Sell1[i]) ) // what does this line mean?
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}

if( trailstop > 0 )
{
trailstop = Max( High[ i ] * stoplevel1, trailstop );
trailARRAY[ i ] = trailstop;
}
if( tstop == 0 AND Short[ i ])  // Is this triggering of 2nd level of Trail stop?
{
tstop = Low[ i ]*stoplevel2;
}
else Short[ i ] = 0; // remove excess buy signals // how this remove excess signal?

if( tstop > 0 AND (High[i]>tstop OR Buy1[i]) )
{
Cover[ i ] = 1;
CoverPrice[ i ] = tstop;
tstop= 0;
}

if( tstop> 0 )
{
tstop= Min( Low[ i ]*stoplevel2, tstop);
trailARRAYs[ i ] = tstop;
}
}type or paste code here
``````

Again, thanks in advance for all the help

When you post some random code, always cite its reference unless you own or wrote the code yourself.
Also ask the source these questions so you will get a clear answer.

why need trail array here where it can't just use the trailstop array?

There are two Arrays, each one for StopLevel1 and StopLevel2.

why 2 different trailstop array for the same function

These are not arrays, they are scalar/numeric variables that hold the recent SL levels. It is also being used to determine if the at the current bar, whether it is IN-TRADE or not.

remove excess buy signals // how this remove excess signal?

If In-TRADE checked by trailstop == 0, then Buy[i], means current bar is set to 0 so any buy signal if present is set to 0. This means a previous Buy exists and if any new or successive buy signal should be removed by setting to 0.
Same story for SELL Array.

Hi Travick,
Thanks for taking the time to answer the question.
The code was from about 7 - 8 yrs ago and I can't recall where it's from so can get the sources to offer any help. If was what I thought an effective stop but I don't know how the mechanism work ...
Your answer straight away end years of agony that I suffer from trying to understand how this works... (hours of staring at it, pulling out hair.. u know the drill ...same goes for array stuff.. re-read the manual about 8 times.. still can't get it...) though I still don't fully comprehend the logic, you answer show me a totally different light on this.. still need more hour of staring and going thru again...
Again, thanks so much for this