Plot TP, SL and EP levels on the chart

Hi all,
I used applystop function to assign TP and SL to my strategy. I'm wondering if it is possible to show EP, TP and SL levels on the chart?
Something like:
Capture

What about exit level if it exit (Sell or Cover) before hit SL or TP. Can I plot that level also ?
Thanks.

@Oscar a similar question was asked before.
Please, see this thread and above all study the linked KB article.

Before opening a new thread, to search the forum, the KB and/or Google is always a recommended practice.

By the way, you posted in the wrong section.

In general, this kind of issue should be posted in the "AFL Programming" one, but since it seems that you are not a properly registered user, you should post in the "Presale/ Trial" section until you verify your license.
See this thread to learn how to do it

2 Likes

@Oscar, nice to see that you followed the License Verified badge procedure. :smile:

Now you are free to post your questions in any category, but be sure always to select the most appropriate one to get more chances to get more help and suggestions.

@beppe
Thanks for your reply.
That thread plot trailing stop which is an array. I want to plot a single value as a line for SL and TP.
With below code, I can plot a circle at SL level.

slline = IIf( Buy, SL, Null );
PlotShapes(shapeCircle,colorRed,layer = 0, yposition = slline, offset = 0, XShift = 0 );

I guess I need to turn the SL in (a two-element) array and then plot it as a line.
How can I do such a plot with an arbitrary length?

Thanks.

PS: I hold a valid LIcense and a proper registered user.

SL and TP are arrays too!


No, instead of guessing read second example here
https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart

EP = ValueWhen( Buy, BuyPrice );

StopLevel = 2; // points ($)
Stopline = IIf( InTrade, EP - StopLevel, Null );
//Plot(...

TgtLevel = 3;// points ($)
TgtLine  = IIf( InTrade, EP + TgtLevel, Null );
//Plot(...

And then plot the two array variables using Plot() function.

1 Like

Hello again.
This is what I need. The key is "ValueWhen" function to get the EP at the entry.
Now I can plot SL and TP as I expected (plot from the entry candle to the exit candle). However, EP line plotted all the way along with the chart to the end. Two questions here:

  1. How can I limit the EP line length (same as SL and TP) to the InTrade?

  2. How can I modify the code that works for Long and Short? It now works only for Long positions.
    I tried many (stupid!) combinations but didn't manage. for example:

InTrade =  IIf( Buy, Flip( Buy, Sell ),  Flip( Short, Cover ));
epline 	= ValueWhen( Buy OR Short, BuyPrice );

Appreciate your support.

@oscar for your separation of trades (Long and Short) instead of using InTrade you could create an InLong and an InShort, example below.

Buy = Cover = Cross( MACD(), Signal() );
Short = Sell = Cross( Signal(), MACD() );

InLong   = Flip( Buy, Sell );
InShort  = Flip( Short, Cover );

How to limit your EP line length? You are missing the key here, the NULL

TgtLine = IIf( InTrade, EP + TgtLevel, Null );

1 Like

Simply do as I have shown to you for Tgt and Stpline.
Actually I left it out on purpose. But apparently you didn't get it.

EP = ValueWhen( Buy, BuyPrice );

EP_line = Iif( InTrade, EP, Null);

Do it simple instead of over-complicating it.

InTrade_Long =  Flip( Buy, Sell );

InTrade_Short = Flip( Short, Cover );

EP_Long = ValueWhen( Buy, BuyPrice );

StopLevel_Long = 2; // points ($)
Stopline_Long = IIf( InTrade_Long, EP_Long - StopLevel_Long, Null );
//Plot(...

TgtLevel_Long = 3;// points ($)
TgtLine_Long  = IIf( InTrade_Long, EP_Long + TgtLevel_Long, Null );
//Plot(...

// Similarly for Short

// EP_Short = ....;

// etc.
2 Likes

Hi and thanks again. Now everything works as expected. Sorry I didn't get the point. I'm quite new in the programming :frowning:

One final question:
Is it possible to extend theses lines by an arbitrary number of candles?
Thanks again.

@oscar this previous example is similar to what you are requesting. You can adapt it to suit your needs.

1 Like

Too much code for such simple task.

All that is needed is just one additional Plot line with x-shift parameter.

InTrade = Flip( Buy, Sell );

EP = ValueWhen( Buy, BuyPrice );

Plot( C, "Price", colorDefault, styleBar );

StopLevel = 2; // points ($)
Stopline = IIf( InTrade, EP - StopLevel, Null );
Plot( Stopline, "Stopline", colorRed );
Plot( Stopline, "", colorRed, styleLine, Null, Null, bars = 5 );// extend line

TgtLevel = 3; // points ($)
TgtLine  = IIf( InTrade, EP + TgtLevel, Null );
Plot( TgtLine, "Tgtline", colorGreen );
Plot( TgtLine, "", colorGreen, styleLine, Null, Null, bars );// extend line

4

5 Likes

Hi!
Great and easy solution.

If we control the exist only by ApplyStop and assign both:

Sell = Cover = 0;

Then when a trades stoped out either on TP or SL, then we don't have Sell/cover to flip into here:

InTrade_Long =  Flip( Buy, Sell );
InTrade_Short = Flip( Short, Cover );

Here is an example:
A short trade gets triggered on 2000/01/25 and existed on 2000/02/07.
On 2000/01/25, the "Short" array turns to true.
Would you be kind and explain if there is no "Sell" and "Cover" set in the code, what Flip between Buy and Sell or between Short and Cover do? Sell = Cover = 0;
What CoverPrice and SellPrice return here although they set to zero!
How you can get the exit date in explore mode in this case?

 AddColumn( Buy, "Buy" ); 
 AddColumn( BuyPrice, "BuyPrice" ); 
 
 AddColumn( Sell, "Sell" ); 
 AddColumn( SellPrice, "SellPrice" ); 
 
 AddColumn( Short, "Short" ); 
 AddColumn( ShortPrice, "ShortPrice" ); 
 
 AddColumn( Cover, "Cover" ); 
 AddColumn( CoverPrice, "CoverPrice" );  

Untitled

Thanks in advance.

Flip() function in your case is used to plot the line (only if being "in trade" -> so only plot ValueWhen() prices in between Buy and Sell or in between Short and Cover)!

ApplyStop function calls lead to Sell and Cover returning values being greater zero too.
Please carefully read Applystop() documentation from top to bottom.

possible return values by Sell/Cover variables:

1 - regular exit
2 - max. loss
3 - profit target
4 - trailing
5 - n-bar stop
6 - ruin stop 

Sell = Cover = 0; before Applystop just says that there is no regular Sell/Cover applied but just Sell/Cover triggered by ApplyStop() function. And ApplyStop is backtester function. So you need backtester to return results of ApplyStop calls.

Trade prices just store price arrays. But which one is chosen is dependent on where Buy/Sell/Short/Cover are triggered in backtest. Your Sell/Cover columns in your exploration output are zero all the way because you did not use Equity() function. To get stop triggers and prices of ApplyStop calls in non-backtester environment such as chart/exploration you have to call that (single security) backtester function after ApplyStop line(s).

Example output
9

So there in upper picture Sell == 3 tells you that there was exit at profit target and with exit price.
So if you use Equity() for exploration output then you will get Cover value of 3 too then and with price of 42.91 on 2000-02-07.

2 Likes