pine-script - 在 pine 脚本中获取先前的交易利润百分比
问题描述
我试图在开始新交易之前获得之前的交易利润百分比,如策略测试器上的交易子选项卡列表中所示。
我试过了...
profit = strategy.netprofit
profitChange = roc(profit, 50)
但我得到了奇怪的结果......它必须是 7,66
有小费吗?
解决方案
这没有经过彻底测试,很可能仅适用于简单的进入/退出场景:
//@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)
推荐阅读
- swift - 使用 ARKit 将画廊中的图像放在墙上
- html - IE11 中的 SVG 蒙版比例和缩放
- python-2.7 - 打开要追加的文件
- c++ - 这个 declval 的定义基于什么 C++ 规则?
- google-chrome - yum 如何从不存在的基本 URL 下载软件包?
- javascript - Knockout.JS 点击绑定将 null 传递给处理函数
- c# - 使用linq c#检查列表中的条件,除了最后一个条目
- aws-lambda - Alexa 没有回应
- javascript - 未处理的拒绝 (TypeError):无法读取未定义 Stripe Javascript 的属性“id”
- c# - 如何在 C# 中以毫秒为单位获得两个日期之间的时间差?