I made an indicator to display on your chart any strategy performance for a better reading of how indicators can lead you to believe that your fresh new founded trading strategy is an holy grail ..
A PnL indicator that display gain and loss on your chart! Why?
Someone ask me recently why his strategy doesn’t perform so well than what he was seeing on chart. I answered him that he should look at price Close, not at the color of the indicator he took to make his trades entries and exits. That’s the reason why I decided to code this little indicator that could help understand that price is the only relevant information, even if an indicator tell you something else.
Include the trading strategy code into the indicator
First, you’ll have to define your strategy into the indicator code. As an example I made a simple moving average crossover:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//the strategy mm1 = average[10,1] mm2 = average[50,1] atr = averagetruerange[10] if mm1 crosses over mm2 then direction=1 lastclosebullish=close lastbarbullish=barindex elsif mm1 crosses under mm2 then direction=-1 lastclosebearish=close lastbarbearish=barindex else direction=0 endif |
If a new bullish or bearish crossover is detected, we store the price at where we took the decision to make a trade entry (lastclosebullish and lastclosebearish variables). We also store the barindex of the current chart, for future reference to draw our profit and loss segments (lines between a closure and a new entry).
At this point, if you need to change the strategy (and for sure you will), the useful variables are: lastclosebullish,lastbarbullish,lastclosebearish,lastbarbearish,direction. The “direction” variable is the one that tell us in which direction is the actual opened trade.
Draw segments between 2 trades with their profits and global PnL
Now, that we have the useful informations stored, we can use them each time a new trade is launched.
case of a bullish trade:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//(gain or loss?) //case Bullish trend if direction=1 and lastclosebearish>0 then if close<lastclosebearish then r=0 g=200 else r=200 g=0 endif //draw segment lines drawsegment(lastbarbearish,lastclosebearish,barindex,close) coloured(r,g,0,200) //calculate PnL and draw information text tradeprofit=round((lastclosebearish-close)/pointsize)*pointvalue pnl = pnl+tradeprofit if tradeprofit>=0 then drawtext("gain=#tradeprofit#$",barindex,high+4*pointsize+atr) coloured(0,200,0) else drawtext("loss=#tradeprofit#$",barindex,high+4*pointsize+atr) coloured(200,0,0) endif drawtext("PnL=#pnl#$",barindex,high+atr) endif |
If a new bullish trade is detected, the current Close is tested to know if the last trade entry (a bearish one), was above or below it. Assuming the Close is above the last bearish entry, so the color is define to red (we have a loss) and vice-versa.
Segment liens are drawn between 2 last entries. The trade profit is calculated between these 2 last prices and displayed on chart with the according color (gain or loss, green or red).
Below this trade profit text, the global profit and loss (PnL) of the strategy is also displayed, to get the whole picture.
Full code to display the PnL on your chart
This is the full indicator’s code including the bullish and bearish parts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
//the strategy mm1 = average[10,1] mm2 = average[50,1] atr = averagetruerange[10] if mm1 crosses over mm2 then direction=1 lastclosebullish=close lastbarbullish=barindex elsif mm1 crosses under mm2 then direction=-1 lastclosebearish=close lastbarbearish=barindex else direction=0 endif //(gain or loss?) //case Bullish trend if direction=1 and lastclosebearish>0 then if close<lastclosebearish then r=0 g=200 else r=200 g=0 endif //draw segment lines drawsegment(lastbarbearish,lastclosebearish,barindex,close) coloured(r,g,0,200) //calculate PnL and draw information text tradeprofit=round((lastclosebearish-close)/pointsize)*pointvalue pnl = pnl+tradeprofit if tradeprofit>=0 then drawtext("gain=#tradeprofit#$",barindex,high+4*pointsize+atr) coloured(0,200,0) else drawtext("loss=#tradeprofit#$",barindex,high+4*pointsize+atr) coloured(200,0,0) endif drawtext("PnL=#pnl#$",barindex,high+atr) endif //case Bearish trend if direction=-1 and lastclosebullish>0 then if close>lastclosebullish then r=0 g=200 else r=200 g=0 endif //draw segment lines drawsegment(lastbarbullish,lastclosebullish,barindex,close) coloured(r,g,0,200) //calculate PnL and draw information text tradeprofit=round((close-lastclosebullish)/pointsize)*pointvalue pnl = pnl+tradeprofit if tradeprofit>=0 then drawtext("gain=#tradeprofit#$",barindex,low-4*pointsize-atr) coloured(0,200,0) else drawtext("loss=#tradeprofit#$",barindex,low-4*pointsize-atr) coloured(200,0,0) endif drawtext("PnL=#pnl#$",barindex,low-atr) endif RETURN |
Improve the profit and loss indicator with any other useful information
This indicator could be improved with any information you’d like to be displayed on your chart (profit in points, indicators value, etc.).
Thank you.!Have been looking for this for some time.I get error: Syntax error: line 32, character 24.This might seem stupid remark but I’m still learning coding for this platform.Will it be possible to add an “Order ID” tet label on the graph where the deal starts/stops? So one can trace at which point which trade started and stoped. Especially if one have multiple strategies running on the same graph.Thank you
Thanks for letting me know this syntax error. I changed the code, so it’s fixed now.
This code is an indicator that simulate trades open/close of a strategy, it doesn’t deal with real manual trades or automated ones.
Thank you.
Merci Nicolas pour ton partage très utile
Philippe
Hello Nicolas,
great indicator, thanks a lot
KR
Peter
Great idea Nicolas, does anyone have an overlap of the PnL and Gain (Loss) figure though? Makes it hard to see: https://www.dropbox.com/s/rqoupg61zlka3a7/gainprofitlossoverlap.png?dl=0
Depends of the chart zoom.. coordinates are not in pixels but in price.
You can try to enlarge the spread between text lines with a multiplier of the ATR, for example:
drawtext(“gain=#tradeprofit#$”,barindex,high+4*pointsize+atr*2) coloured(0,200,0)
Bonjour, félicitation c’est une excellente idée !
Hi, is it possible to input my own buy and sell data? so I can do post analysis of the trades for example. From an Excel/Csv, or put it trade by trade quickly?
thanks
Gracias por el trabajo Nicolás, era lo que estaba buscando.