首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将动量策略脚本转换为文字记录中的警告?

如何将动量策略脚本转换为文字记录中的警告?
EN

Stack Overflow用户
提问于 2018-09-29 10:59:37
回答 1查看 2.4K关注 0票数 1

有人能帮我把mom策略松脚本代码转换成警报吗?以下是代码:

代码语言:javascript
复制
//@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")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-03 12:14:16

有人能帮我把mom策略松脚本代码转换成警报吗?

要将策略代码转换为可以生成警报的指示符,有四项工作要做:

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

如下所示:

代码语言:javascript
复制
//@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()函数,尽管严格地说,它并不一定适用于您的问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52567698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档