如何将 Momentum 策略脚本转换为在 pinescript 中发出警报?

How to transform Momentum strategy scripts to alert in pinescript?

谁能帮我把mom strategy pine-script代码改成alert? 这是代码:

//@version=3
strategy("Momentum Strategy", overlay=true)
length = input(12) 
price = close

momentum(seria, length) =>
    mom = seria - seria[length]
    mom

mom0 = momentum(price, length)
mom1 = momentum(mom0, 1)

if (mom0 > 0 and mom1 > 0)
    stop_price = high+syminfo.mintick
    strategy.entry("MomLE", strategy.long, stop=stop_price, comment="MomLE", qty=2)
else
    strategy.cancel("MomLE")

if (mom0 < 0 and mom1 < 0)
    stop_price = low - syminfo.mintick
    strategy.entry("MomSE", strategy.short, stop=stop_price, comment="MomSE", qty=2)
else
    strategy.cancel("MomSE")

Can anyone help me to transform the mom strategy pine-script codes to the alert?

要将策略代码转化为可以生成警报的指标,需要做四件事:

  1. strategy()函数替换为study()
  2. 删除策略特定代码。在本例中是 strategy.entry()strategy.exit() 函数。
  3. 然后添加 the alertcondition() function 对警报条件进行编码。为此,您可以使用与所用策略相同的逻辑。
  4. 在您的代码中添加某种输出函数*。

这是它的样子:

//@version=3
study("Momentum Alert", overlay=true)
length = input(12) 
price = close

momentum(seria, length) =>
    mom = seria - seria[length]
    mom

mom0 = momentum(price, length)
mom1 = momentum(mom0, 1)

// Create alert conditions
alertcondition(condition=mom0 > 0 and mom1 > 0,
     message="Momentum increased")

alertcondition(condition=mom < 0 and mom1 < 0,
     message="Momentum decreased")

// Output something
plot(series=mom0)

*:TradingView 的 alertcondition() 函数不是所谓的 'output function'。但每个指标都需要这样的功能(例如,用于绘图、着色或创建形状)。否则你会得到 'script must have at least one output function call' error.

这就是为什么我在上面的示例代码中添加了 plot() 函数,即使严格来说它不一定是您的问题。