【问题标题】:How to count number of days since moving averages stacked one above the other如何计算移动平均线堆叠后的天数
【发布时间】:2021-05-21 07:18:28
【问题描述】:

如何计算距离长度为 20、50、100 和 200 的移动平均线自上次移动平均线以来的天数。

请参考所附图片。 橙色是 20 DMA 深蓝色是 50 DMA 绿色是 100 DMA 红色是 200 DMA 从 11 月 25 日开始,20 DMA、50 DMA、100 DMA、200 DMA 叠加在一起 20DMA > 50DMA > 100 DMA > 200 DMA。 我想获取它们堆叠的日期和堆叠后的天数。

2021年2月21日Bajaco提供barssince函数后更新:

如果我在 barsince 中使用单个交叉函数,我可以计算天数。请参见下图,其中突出显示了代码区域和代码输出。

代码: bsince = tostring(barssince(crossover(sma(close, 20), sma(close, 50))), '#.##')

enter image description here

但是,当我多次使用交叉函数时,barssince 没有提供任何结果。不知道我在这里做错了什么。

代码: bsince = tostring(barssince(crossover(sma(close, 20), sma(close, 50)) 和 crossover(sma(close, 50), sma(close, 100) ) 和交叉(sma(close, 100), sma(close, 200))), '#.##')

enter image description here

非常感谢任何帮助。

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    barssince(a > b and b > c and c > d)

    至于日期,请查看文档中的时间和会话信息。除了在所有条件都为真时保存变量之外,我不知道还有什么办法。

    可能是这样的:

    var int y = na
    var int m = na
    var int d = na
    
    ma = sma(close,20)
    mb = sma(close,50)
    mc = sma(close,100)
    md = sma(close,200)
    cond = ma > mb and mb > mc and mc > md
    if cond
        y := year
        m := month
        d := dayofmonth
    

    但可能有更好的解决方案

    【讨论】:

    • 嗨,我是 pinescript 的初学者。您能否提供一个工作示例,以便我可以将其添加为指标。谢谢。
    • 作为 pinescript 的初学者,您应该能够使用提供的代码,它是有效的。
    • 我会尝试花更多的时间,看看我能不能让它发挥作用。感谢您提供指导。
    猜你喜欢
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    相关资源
    最近更新 更多