在pine脚本中获取以前的交易利润百分比

时间:2020-10-27 18:41:43

标签: pine-script tradingview-api

我试图在开通新交易之前获得先前的交易利润百分比,如策略测试器上的交易列表子选项卡所​​示。

img trades

我尝试过...

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

但是我得到的结果很奇怪...必须是7,66

enter image description here

任何提示?

1 个答案:

答案 0 :(得分:0)

这没有经过彻底的测试,很可能只适用于简单的进入/退出场景:

//@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)