在 Pine 中,如何检索 strategy.netprofit 不同参数值的结果?

In Pine, how to retrieve strategy.netprofit resulting of different parameter values?

我想显示策略在不同情况下的净利润,即在退出订单的不同条件下:

//@version=5
strategy("test 2", overlay=true, max_labels_count=500, calc_on_every_tick=true, initial_capital=500, default_qty_value = 500, currency='USD', process_orders_on_close=true, commission_type='percent', commission_value=0.075, default_qty_type=strategy.cash, calc_on_order_fills=true)


// retrieve Heikin Ashi values
haHandle=ticker.heikinashi(syminfo.tickerid)
haOpen=request.security(haHandle,timeframe.period,open)
haClose=request.security(haHandle,timeframe.period,close)
haDeltaAbs=haClose-haClose[1]
haDeltaRel=haDeltaAbs/haClose[1]*100


// Trade Entry
haEntryCondition=haDeltaRel>0 and haDeltaRel[1]<0
strategy.entry("trade", strategy.long, when = haEntryCondition)


// Trade Exit, I perform one Exit order for three different values of a thresold condition (-1, -0.5, 0)
float haThreshold = -1
for i = 0 to 2
    haThreshold := haThreshold + 0.5
    haExitCondition=haDeltaRel<haThreshold
    strategy.close("trade"+str.tostring(i), when = haExitCondition)
    
// I would like to display the result of the strategy (strategy.netprofit) for each of the "type" of exit performed above
if (barstate.islast)
    label.new(bar_index, open, str.tostring(strategy.netprofit), yloc = yloc.price, style = label.style_none, textcolor = color.red, size = size.normal)

最后一行(label.new(...)只显示总净利润(全部entries/exits都算进去了),如何取回每个对应的净利润退出订单的“配置”?

谢谢

你可以这样做 运行 循环检查条目 ID 并按名称添加利润。

profitByName(name) =>
    result = 0.
    for i = 0 to strategy.closedtrades-1
        if strategy.closedtrades.entry_id(i) == name
            result += strategy.closedtrades.profit(i) 
    result

您还可以查找退出 ID:

profitByExitName(name) =>
    result = 0.
    for i = 0 to strategy.closedtrades-1
        if strategy.closedtrades.exit_id(i) == name
            result += strategy.closedtrades.profit(i) 
    result

这个名字 space 中还有许多其他方便的 built-ins 值得一看