Display profit and loss on price chart with a custom indicator

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:

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:

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.

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.).

Share this

  1. andre.vorster • 254 days ago #

    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

    • Nicolas • 254 days ago #

      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.

  2. andre.vorster • 254 days ago #

    Thank you.

  3. Pepsmile • 213 days ago #

    Merci Nicolas pour ton partage très utile

  4. pmeier • 26 days ago #

    Hello Nicolas,
    great indicator, thanks a lot

Register or