Pine Script strategy.entry order that follow a moving average

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

我正在尝试在一组条件适用时在移动平均线下限价单,效果很好。但我希望订单在每个柱线收盘时更新,以跟随平均上涨/下跌。 Pine Script 的新手,不知道这是否可行。

这是我的代码:

//SETUP
setupSqueeze = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid ? 1 : 0
setupSqueezeSum = math.sum(setupSqueeze, 8)
//-------------------------------------------------------------------------------------------------------------------------------------
//BREAKOUT
breakOut = BB_lower < KC_lower_low or BB_upper > KC_upper_low ? 1 : 0
breakOutSum = math.sum(breakOut, 2)
//-------------------------------------------------------------------------------------------------------------------------------------
//TRADING CONDITIONS
condition1 = setupSqueezeSum >= 2 and breakOutSum >= 2
//-------------------------------------------------------------------------------------------------------------------------------------

if (condition1 == true and close > ema4 and strategy.position_size == 0)
    strategy.entry("Long", strategy.long, limit = ema3)
    strategy.exit("Long Stop", from_entry = "Long", loss = 2000, profit = 4000)

if (condition1 == true and close < ema4 and strategy.position_size == 0)
    strategy.entry("Short", strategy.short, limit = ema3)
    strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
pine-script pine-script-v5
1个回答
0
投票

您可以使用 strategy.opentrade 来测试交易是否开启。
您可以使用 id_entry 来了解实际交易是做多还是做空。
然后根据您的意愿调整退出参数:

if strategy.opentrade > 0
    if strategy.opentrades.entry_id(strategy.opentrades-1)=="Long"
        strategy.exit("Long Stop", from_entry = "Long", loss = NewValueLoss, profit = NewValueProfit)
    else if strategy.opentrades.entry_id(strategy.opentrades-1)=="Short"
        strategy.exit("Short Stop", from_entry = "Short", loss = 2000, profit = 4000)
© www.soinside.com 2019 - 2024. All rights reserved.