计算已平仓交易的收益百分比,如果收益高于阈值,则在多头交易入场柱下方/空头交易入场柱上方绘制标签

问题描述 投票:0回答:1

我的 if 可以工作(参见下面的代码),但它会在条件为 true 的每个策略输入栏上方绘制大量标签,而不是只绘制一个标签。有人可以帮忙吗?

谢谢!

    for tradeNo = 0 to strategy.closedtrades - 1
        ProfitPct_high = 0.0
        ProfitPct_low  = 0.0
        entryP = strategy.closedtrades.entry_price(tradeNo)
        exitP = strategy.closedtrades.exit_price(tradeNo)
        profit_this_trade = exitP / entryP - 1
        ProfitPct_high := profit_this_trade > ProfitPct_high ? profit_this_trade : ProfitPct_high
        ProfitPct_low := profit_this_trade > ProfitPct_low ? profit_this_trade : ProfitPct_low
        if profit_this_trade > 0.005 and strategy.closedtrades.size(tradeNo) > 0 //profit_this_trade > (ProfitPct_high * percentile) //
            label.new(strategy.closedtrades.entry_bar_index(tradeNo), low, str.tostring(profit_this_trade*100, "#.##") + "%", color=#5252ffb0, style=label.style_label_up)
        if profit_this_trade > 0.005 and strategy.closedtrades.size(tradeNo) < 0 //profit_this_trade < (ProfitPct_high * percentile) and strategy.closedtrades.size(tradeNo) < 0
            label.new(strategy.closedtrades.entry_bar_index(tradeNo), high, str.tostring(profit_this_trade*100, "#.##") + "%", color=#eb34a5b0, style=label.style_label_down)

我期望看到一个标签绘制在蜡烛上方或下方,其中条件为真,但它每次都会绘制大量标签。多到我都数不过来了

label pine-script
1个回答
0
投票

下面是正确的代码。也许有更简单的方法,但这可行。

    for tradeNo = 0 to strategy.closedtrades - 1
    ProfitPct_high = 0.0
    ProfitPct_low  = 0.0
    entryP = strategy.closedtrades.entry_price(tradeNo)
    exitP = strategy.closedtrades.exit_price(tradeNo)
    profit_this_trade_long = exitP / entryP - 1
    profit_this_trade_short = (entryP-exitP)/entryP
    ProfitPct_high := strategy.closedtrades.size(tradeNo) > 0 ? profit_this_trade_long > ProfitPct_high ? profit_this_trade_long : ProfitPct_high : na
    ProfitPct_low := strategy.closedtrades.size(tradeNo) < 0 ? profit_this_trade_short > ProfitPct_low ? profit_this_trade_short : ProfitPct_low : na
    if strategy.closedtrades.size(tradeNo) > 0 and profit_this_trade_long > 0.05 //profit_this_trade_long > (ProfitPct_high * percentile) //
        lbl = label.new(strategy.closedtrades.entry_bar_index(tradeNo), na)
        label.set_text( lbl, str.tostring(profit_this_trade_long*100, "#.##") + "%")
        label.set_color(lbl, #9696fcd7)
        label.set_yloc( lbl, yloc.belowbar)
        label.set_size(lbl, size.tiny)
        label.set_style(lbl, label.style_label_up)
    if strategy.closedtrades.size(tradeNo) < 0 and profit_this_trade_short > 0.03 //profit_this_trade_short > (ProfitPct_high * percentile) //
        lbl = label.new(strategy.closedtrades.entry_bar_index(tradeNo), na)
        label.set_text( lbl, str.tostring(profit_this_trade_short*100, "#.##") + "%")
        label.set_color(lbl, #faa1e7d8)
        label.set_yloc( lbl, yloc.abovebar)
        label.set_size(lbl, size.tiny)
        label.set_style(lbl, label.style_label_down)
© www.soinside.com 2019 - 2024. All rights reserved.