首页 > 解决方案 > 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)

标签: pine-script

解决方案


您需要进行自己的交易管理并使用 vars 来跟踪您何时进入,何时进行交易。进入时保存入场级别,然后在退出时测量增量并相应地更新交易计数。有关完整的交易管理代码,请参阅回测和交易引擎


推荐阅读