【问题标题】:How to transform Momentum strategy scripts to alert in pinescript?如何将 Momentum 策略脚本转换为 pinescript 中的警报?
【发布时间】:2018-09-29 10:59:37
【问题描述】:

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

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

【问题讨论】:

  • 看看alertcondition()函数。
  • 我写了一篇关于the alertcondition() function的深入文章。它可能有助于更好地理解如何编写警报条件。

标签: finance pine-script


【解决方案1】:

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

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

  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() 函数不是所谓的“输出函数”。但是每个指标都需要这样的功能(例如,用于绘图、着色或创建形状)。否则你会得到'script must have at least one output function call' error

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

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 2021-09-08
    • 1970-01-01
    • 2022-11-22
    • 2022-09-24
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多