marcq
marcq

Reputation: 483

Issue with color backgrounding executed and closed trades

I have two issues with color backgrounding executed and closed trades :

I would like to highlight with a background color win and loss trades :

longExists          =  strategy.position_size > 1
// --- Check if trade is won or lost
isProfitable = strategy.netprofit > 0
// --- Background colors according to outcome
profitColor = color.new(color.green, 5)
lossColor   = color.new(color.red, 5)

bgcolor(longExists ? isProfitable ? profitColor : lossColor : na)

Background is set, but : 1a. one trade which should be backgrounded green (won) is backgrounded red and the trade is indeed won. Won trade which should be backgrounded as won (green) and not lost 1b. one trade which should be backgrounded red(lost) is backgrounded green and the trade is indeed lost. lost trade which should be backgrounded as lost (red) and not won Would appreciate your expertise 2. Transparency isn't working obviously

Would appreciate your expertise. Thanks.

Upvotes: 0

Views: 61

Answers (1)

e2e4
e2e4

Reputation: 3818

Your script checks strategy.netprofit, which represents the total profit of the strategy and is updated at the close of each trade.

If you want to background color based on the PnL of currently opened trades only, you can check strategy.openprofit instead:

bool longExists =  strategy.position_size > 0
bool isProfitable = strategy.openprofit > 0

profitColor = color.new(#4caf4f, 5)
lossColor   = color.new(color.red, 5)

bgcolor(longExists ? isProfitable ? profitColor : lossColor : na)

The background will change dynamically, indicating on each bar of an open trade whether it was at a loss or a profit, until it is closed.

enter image description here

Upvotes: 1

Related Questions