pine-script - Pine Script 中的交易/赢/输计数和盈利百分比?
问题描述
pine 脚本中有一些功能,例如:
closed_trades = strategy.closedtrades
win_trades = strategy.wintrades
loss_trades = strategy.losstrades
percent_profitable = (strategy.wintrades/strategy.closedtrades)*100
但是如果不使用这些函数,我如何在 pine 脚本中测量这些参数?我试图对其进行编码,但我无法管理。请你帮助我好吗?
此致
//@version=4
study("My Script",overlay=true)
ema1=ema(close,9)
ema2=ema(close,21)
buy=crossover(ema1,ema2)
sell=crossunder(ema1,ema2)
buy_price=valuewhen(buy,close,1)
sell_price=valuewhen(sell,close,1)
plot(ema1,color=color.green)
plot(ema2,color=color.red)
buy_cnt=cum(buy?1:0)
sell_cnt=cum(sell?1:0)
total_cnt = buy_cnt + sell_cnt
win_cnt = cum(buy_price>sell_price?1:0)
loss_cnt = cum(buy_price<sell_price?1:0)
f_draw_label(x,y,textline)=>
var label Label = na
label.delete(Label)
Label := label.new(x, y, textline, color=color.blue, textcolor=color.white,textalign=text.align_left, style=label.style_labeldown, yloc=yloc.price, xloc=xloc.bar_time)
x = timenow
y = highest(close,50)
format_text(str) =>
str + "\n"
txt1 = format_text(tostring(total_cnt))
txt2 = format_text(tostring(buy_cnt))
txt3 = format_text(tostring(sell_cnt))
txt4 = format_text(tostring(win_cnt + loss_cnt))
txt5 = format_text(tostring(win_cnt))
txt6 = format_text(tostring(loss_cnt))
all_txt=txt1 + txt2 + txt3 + txt4 + txt5 + txt6
f_draw_label(x,y,all_txt)
解决方案
您需要进行自己的交易管理并使用 vars 来跟踪您何时进入,何时进行交易。进入时保存入场级别,然后在退出时测量增量并相应地更新交易计数。有关完整的交易管理代码,请参阅回测和交易引擎。
推荐阅读
- r - 基于R中函数中的变量过滤数据和命名
- javascript - 如何检查订阅功能是否在角度工作?
- google-sheets - 谷歌电子表格 - 比较同一行上的单元格并获取匹配数?
- php - 简单的mysql插入错误,查询未执行且数据未插入表
- r - 如何计算 R 中 df 中满足特定条件的值的数量
- javascript - React 无法动态呈现正确的组件
- dart - 如何转换 FutureBuilder
到 BoxDecoraiton Image - rest - REST API:基于角色设计路由
- google-apps-script - 网页上显示的 Google Script 短标签
- java - 如何使用双缓冲消除闪烁(java swing)