Pine Script 策略输入:遵循移动平均线的订单

huangapple go评论43阅读模式
英文:

Pine Script strategy.entry order that follows a moving average

问题

我正在尝试在一组条件满足时以移动平均为基础下订单,这部分正常运作。但我希望在每个柱状图关闭时更新订单以跟随平均上下波动。对于Pine Script相对新手,不确定是否可行。

以下是您的代码,我将不翻译代码部分,只提供注释和说明:

// 设置条件
setupSqueeze = BB_lower >= KC_lower_mid or BB_upper <= KC_upper_mid ? 1 : 0
setupSqueezeSum = math.sum(setupSqueeze, 8)

// 突破条件
breakOut = BB_lower < KC_lower_low or BB_upper > KC_upper_low ? 1 : 0
breakOutSum = math.sum(breakOut, 2)

// 交易条件
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)

如果您有任何进一步的问题或需要帮助,请告诉我。

英文:

I'm trying to place a limit order at a moving average when a set of conditions are meant, which is working fine. But I would like the order to be updated at the close of every bar to follow the average up/down. Pretty new to Pine Script and don't know if this is possible.

Here is my code:

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

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

if (condition1 == true and close &lt; ema4 and strategy.position_size == 0)
    strategy.entry(&quot;Short&quot;, strategy.short, limit = ema3)
    strategy.exit(&quot;Short Stop&quot;, from_entry = &quot;Short&quot;, loss = 2000, profit = 4000)

答案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)
英文:

You can use strategy.opentrade to test if a trade is on.
You can use id_entry to know if the actual trade is a long or a short.
Then adjust the exit parameters to your wish :

if strategy.opentrade &gt; 0
    if strategy.opentrades.entry_id(strategy.opentrades-1)==&quot;Long&quot;
        strategy.exit(&quot;Long Stop&quot;, from_entry = &quot;Long&quot;, loss = NewValueLoss, profit = NewValueProfit)
    else if strategy.opentrades.entry_id(strategy.opentrades-1)==&quot;Short&quot;
        strategy.exit(&quot;Short Stop&quot;, from_entry = &quot;Short&quot;, loss = 2000, profit = 4000)

huangapple
  • 本文由 发表于 2023年5月7日 06:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定