Need help regarding a trailing stop loss..

Hello people,
I need some help for setting up a trailing sl for exiting the trade. I have the logic but cannot put it successfully into code.
Here is the logic:-
after a buy signal is generated, the stop needs to be placed below the "low" of the last red candle AND that red candle should be succeeded by two successive green candles.
Another way of understanding the logic is -
In terms of candles on the chart it should be RED,Green, Green (stop below the low of the RED ) and this stop should be trailed whenever a similar pattern arrives again till the position is open.
If the price drops below the low of the red candle we exit.

Note:- the stop would not trail if after a red candle there is only one green bar, it only trails if there are two green bars back to back next to a red candle.

I would be happy if anyone can help me code this.


Do you have some code you can share here that people can help you with?

w.r.t candles should be red, green, green, you can do this with the following:

  1. The Ref function will give you any previous/future candles (AFL Function Reference - REF)

  2. A Green candle is when the Close is greater than the Open and Red is reversed.

With these two pieces of info, you should be able to write a formula that will find the last candle, check it's Open and Close, and repeat for the last 3 candles.


1 Like

Thanks for reply my friend,
yes i have coded the red and green candle logic.. what i am failing at is how to put this in a trailing stop ... it would be great if you could help me with a short code as i am unable to do it.

Note:- the stop would not trail if after a red candle there is only one green bar, it only trails if there are two green bars back to back next to a red candle.

red3 = Ref(O - C > 0, -3);
green2 = Ref(C - O > 0, -2);
green1 = Ref(C - O > 0, -1);

Sell = red3 & green2 & green1;

// OR if you want it to be a marker
TrailingLevel = Ref ( L, -3 )
// so here, whenever the previous 3 candles at any bar forms the red, green, green pattern, the trailing level will hold the value of the low of that red candle which is 3 bars away.

PlotShapes(Sell * shapeDownTriangle, colorWhite);


Did you check Knowledge Base:

Hi Joe,
Thanks for the help,
One doubt though.. with -3,-2 and -1 it would be looking at only the last three candles . What i am looking for here is - that the pattern of red,green,green could also be 10 or 15 candles behind ( as the pattern might not be formed necessarily in the previous three bars) .

For example, if i buy on 10 November with a buy rule, the stop loss pattern of red, green, green could be there on nov1,nov2 and nov3. In that case i want my stop loss to be below those bars .

But if i apply only -3,-2,-1 then it would only look up the nov7,nov8 and nov9 only right ?

So what ammendments would be required in your given code for such scenario?
I am adding a chart on which i have plotted a manual example.

I would be glad if you can help on this .
sl example

No, this is not correct and it's a misunderstanding of how AmiBroker's array based programming works.
Run that code on your chart. See what it picks up. Hint: It picks up more than what you think.

You're thinking about it in terms of time as in -1 is going back in time. Ref of -1 is simply referring to an relative index in an array w.r.t some arbitrary point. This gets run on the whole array.
Read this: Understanding how AFL works

To understand what it's doing, go into step-by-step debug and look at those variables.

Everything you need is here, this is where I learned what Close is and how Buy or C > 1 or Ref ( C, i) actually works. You want to run the debugger ( Step 6 ) and understand what it's doing:

You need to figure out mathematical formula for above logic and you can use that using Sell condition.
TSL can be in
or any customized indicator that can be coded in afl.

I actually did put it on the chart before writing here. i am pasting the screen as well. What we can clearly see that the red arrow "sell" is coming in as soon as the red,green,green condition is met in the last 3 candles.
So basically what i want is that the exit should happen only if the close is below the low of the red candle.
Any suggestions?

Please have a look at Tomasz's article, give it a go and come back here if you're still unable to do it.

Also, please have a read up of the articles on how AFL works. What you want to know is literally in front of your face, we've given you the links, please put some effort into learning the basics.



This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.