HighestSince Stop query

I have a trailing stop based on the Highestsince close price minus an ATR value since the Buy. This seems to work fine except there are occasions when it seems to deviate to a lower value and I'm unsure why. My code is below and I'll attach an example of what I mean.

// ATR Breakout System
//---- Neil Donaldson 22/03/2019

// The system has a index filter override where no trades will be generated, a setup based on a slow ROC indicator and a
// trigger for trade initiation 

SetOption("InitialEquity",100000); // Set portfolio size
//index=Foreign("XAO","Close",True); // All Ordinaries Index (not used due to worse outcome)
//IndexMA=MA(index,100); //Moving average of the index
//IndexF = index>IndexMA;

// Volatility position sizing
posrisk=1; // Portfolio Risk
sharestop=6*ATR(21); //Stop $ amount
Pctsize= posrisk*BuyPrice/sharestop; // Calculate % portfolio position
SetPositionSize(Pctsize,2); // Calculate share quantity

SROC = EMA(ROC(C,150),75)>30; // Close Rate of change filter
DVAL=MA(DOLV,7)>500000; // Dollar turnover filter

// Trigger band lower stop band
TATR = EMA(Avg,21)+(3*ATR(21)); // Top ATR band
BATR = C-Sharestop; // Bottom ATR band

Setup=SROC AND DVAl; // Initial trade setup condition
Trig= Setup AND Cross(C,TATR); //Buy when price closes above top band and setup true

Buy = Trig;
Tstop=HighestSince(Buy,BATR,1); // Highest level of lower ATR band
Buy=ExRem(Buy,Sell);  //Remove excess buy signals
Sell=ExRem(Sell,Buy);  // Remove excess sell signals
Intrade = Flip(Buy,Sell);
Stopline = IIf(Intrade,Tstop,Null);

Plot(TATR, "Top ATR", colorGreen,styleLine,0,0,0,0,2);
Plot(BATR, "Bottom ATR",colorRed,styleDashed);
Plot(Stopline, "Trailing Stop", colorRed,styleLine,0,0,0,0,2);

//====== Change Log --------

// 15/04/2019    Change to liquidity filter MA(V x C), was MA(V) x C
// 23/04/2019    Locked short cover variable to zero




Just browsing through the code, I can't spot anything wrong but you should consider the way ATR responds.

When you are in periods of high volatility, large gaps and wide range bars, you are definitely going to have such wide/large stops.

Hi Travick,
That was my suspicion, but surely the HighestSince would retain the last value in its array and compare it to the previous value and know if it was higher or lower. Maybe doing a manual calculation will shed some light on the issue. It would be good to know the internals of the HighestSince function.

I have used a spreadsheet just to see what should be happening bar by bar and at the moment the only think I can conclude is the HighestSince function is not latching in the high value of the previous day. The increased volatility and gaps should not cause it to give a false reading as an ATR value is still being calculated and it matches my manual calculations. I have also noticed other places where the value falls instead of holding the higher value, not to the same extent as in the example but this would affect backtest results.

If you want to analyze this, use the AA window. It will be far better than the spreadsheet and you can add columns for each step.
Ofc, you can use the spreadsheet to match the output.

Can you circle the data by what exactly you mean when you say not latching the Prev Day H.
Just add a few columns and run it on the period in the chart.

Hi Travick,
In the graphic at the beginning of the thread you will see a dotted line, that is the line generated by the C - (ATR(21)*6), it hangs under the close. The HighestSince is then meant to follow this line each time it rises then hold at the highest point. I will have to check how to use the use the AA window to analyse to formula.


Here is the exported data:

Ticker	Date/Time	Bottom ATR	Stop Line	
AVH	        19-Mar-19	      0.13	            0.13
AVH	        20-Mar-19	      0.13	            0.13
AVH	        21-Mar-19	      0.15	            0.15
AVH	        22-Mar-19	      0.18	            0.18
AVH	        25-Mar-19	      0.18	            0.18
AVH	        26-Mar-19	      0.18	            0.18
AVH	        27-Mar-19	      0.18	            0.18
AVH	        28-Mar-19	      0.19	            0.19
AVH	        29-Mar-19	      0.19	            0.19
AVH	        01-Apr-19	      0.21	            0.21
AVH	        02-Apr-19	      0.21	            0.21
AVH	        03-Apr-19	      0.22	            0.22
AVH	        04-Apr-19	      0.23	            0.23
AVH	        05-Apr-19	      0.23	            0.23
AVH	        08-Apr-19	      0.30	            0.30
AVH	        09-Apr-19	      0.33	            0.33
AVH	        10-Apr-19	      0.15	            0.33
AVH	        11-Apr-19	      0.21	            0.21
AVH	        12-Apr-19	      0.16	            0.21
AVH	        15-Apr-19	      0.15	            0.21
AVH	        16-Apr-19	      0.17	            0.21
AVH	        17-Apr-19	      0.16	            0.21
AVH	        18-Apr-19	      0.16	            0.21
AVH	        23-Apr-19	      0.20	            0.21
AVH	        24-Apr-19	      0.19	            0.21

As you can see, it behaves itself until 1 day after the bottom ATR band falls then instead of holding at 0.33 it falls to 0.21.


Hi Neil,

With your code the Buy signals used to create Tstop continue though the trade and are resetting your stop level.

When doing trailing stops we have to generate a state signal and the associated stop value before we can determine the Sell, however the Sell needs to be defined before the state signal to turn it false at the end of each trade.

I resort to looping code to resolve this. I can throw together a sample but give it a go yourself first if you think its the way to go.

Once again:

for proper stops you either need to use ApplyStop or for custom stops you have to write looping code. Anything else is wrong (which includes code of post #1).

HighestSince has similar effect as using ValueWhen in this thread

But apparently some people still don't understand.

Hi All,
I'm pretty new to using Amibroker and have not tried looping code due to not understanding the working and how to construct it. I know there are plenty of programmers out there that would see this as a minor issue but it can be a struggle for people not brought up to do coding. Any help would be appreciated. Most of the time the trailing stop works without an issue it is just in this case that it does something strange.


Read knowledge base

And everything else in knowledge base

1 Like

Thanks all for your help.
I am brushing up my knowledge of loops and will be better in a while. After stepping from Metastock to Amibroker it takes a bit of time to re-orientate to what is a far more flexible and powerful program.