Sekenenz
Sekenenz

Reputation: 13

Get previous trade profit percentage in pine script

I'm trying to obtain the previous trade profit percentage before opening a new trade, like showed in the list of trades sub-tab on the strategy tester.

img trades

I've tried...

profit = strategy.netprofit
profitChange = roc(profit, 50)

but I'm getting strange results... it had to be 7,66

enter image description here

any tips?

Upvotes: 0

Views: 1889

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

This was not thoroughly tested and will most probably work only for simple entry/exit scenarios:

//@version=4
strategy("BarUpDn Strategy", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, precision = 6)
// Issue long/short on bar up/dn and gap conditions.
enterLong  = close > open and open > close[1]
enterShort = close < open and open < close[1]
if enterLong
    // On the close of the bar, place order for long entry and 
    strategy.entry("Long executed", strategy.long)
    alert("Long order placed")
if enterShort
    strategy.entry("Short executed", strategy.short)
    alert("Short order placed")

changeInProfits = change(strategy.netprofit)
lastPercentProfit = changeInProfits ? 100 * (changeInProfits / strategy.position_size[1]) / strategy.position_avg_price[1] : na
plotchar(lastPercentProfit, "lastPercentProfit", "", location.top, size = size.tiny)

// For validation
plotchar(strategy.netprofit, "strategy.netprofit", "", location.top, size = size.tiny)
plotchar(strategy.position_size, "strategy.position_size", "", location.top, size = size.tiny)
plotchar(strategy.position_avg_price, "strategy.position_avg_price", "", location.top, size = size.tiny)

Upvotes: 1

Related Questions