1

谁能帮我将妈妈策略松脚本代码转换为警报?这是代码:

//@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")
4

1 回答 1

2

谁能帮我将妈妈策略松脚本代码转换为警报?

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

  1. 用 替换strategy()函数study()
  2. 删除策略特定代码。在这种情况下,这是strategy.entry()strategy.exit()功能。
  3. 然后添加对警报条件进行编码alertcondition()函数。为此,您可以使用与所用策略相同的逻辑。
  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()功能不是所谓的“输出功能”。但是每个指标都需要这样的功能(例如,用于绘图、着色或创建形状)。否则,您会收到“脚本必须至少有一个输出函数调用”错误

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

于 2018-10-03T12:14:16.600 回答