dorien
dorien

Reputation: 5387

Plotting profit per trade in a strategy on the plot

I have a strategy pine script. I'd like to be able to better identify loosing trades on my plot, so I'm trying to make a bar plot like this:

enter image description here

Ideally, this only displays the bar when the trade is finalized (so I'm only interested in the final profit % of each trade (not its volatility). Even better would be if I can plot it at the time when the trade opens (not closes), so I can try to identify which trades not to open.

One option I see is to keep track of the profit and somehow create a series (not too sure about the syntax). Or, very possibly, this information is saved in strategy right?

Update: after trying the code from Andrey-D it shows up like this (even without overlay = true):

enter image description here

Upvotes: 1

Views: 1957

Answers (1)

Andrey D
Andrey D

Reputation: 1694

Here you are:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=4
strategy("My Strategy", overlay=true, margin_long=100, margin_short=100)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

float lastTradeProfit = na
if strategy.position_size != strategy.position_size[1]
    lastTradeProfit := strategy.netprofit - strategy.netprofit[1]
plot(lastTradeProfit, style = plot.style_columns, color = lastTradeProfit > 0 ? color.green : lastTradeProfit == 0 ? color.gray : color.red, title = "Trade profit/loss")

Upvotes: 4

Related Questions