首页 > 解决方案 > 在 pine 脚本中获取先前的交易利润百分比

问题描述

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

img 交易

我试过了...

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

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

在此处输入图像描述

有小费吗?

标签: pine-script

解决方案


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

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

推荐阅读