【问题标题】:Pinescript: How to check if a specific condition occured in last 14 days for every candle?Pine Script:如何检查每支蜡烛在过去 14 天内是否发生了特定情况?
【发布时间】:2021-08-11 11:51:55
【问题描述】:

我有一个 pinescript 指标,我想在最后 14 根蜡烛满足指标条件并且满足特定 tsi 和随机 rsi 条件时绘制线条。

指标条件

if buy_goodf or buy_greatf or buy_incrediblef
    line.new(bar_index[0], low-lineheightbottom*LineLength, bar_index[0], high+lineheighttop*LineLength, color=color.new(color.lime, 50), width=linewidth)


if sell_goodf or sell_greatf or sell_incrediblef
    line.new(bar_index[0], low-lineheightbottom*LineLength, bar_index[0], high+lineheighttop*LineLength, color=color.new(color.red, 50), width=linewidth)

所以这部分效果很好,我测试了线条是否正确显示。

但现在我想在每根蜡烛上画一条蓝线:-

  1. 在最近 14 根蜡烛中,有一条买入线(即 buy_goodf or buy_greatf or buy_incrediblef 已满足。

  2. 有 tsi 和 stochrsi 是某种方式。

我遇到的唯一问题是第一点 - 如何检查每根蜡烛是否在最后 13 根蜡烛中存在买入条件。

我尝试使用这样的 for 循环对其进行编码,但这并没有给我信号。

for i = 0 to 13
    if (buy_goodf or buy_greatf or buy_incrediblef) and tsi_value>tsi_ema and tsi_value[1]<tsi_ema[1] and k[1]<d[1] and k>d
        line.new(bar_index[0], low-lineheightbottom*LineLength, bar_index[0], high+lineheighttop*LineLength, color=color.new(color.blue, 50), width=linewidth)

这是正确的做法吗?

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    我不确定这是否是您要查找的内容。但是此代码检查过去 14 天内是否有 EMA 交叉,如果有,它将在蜡烛图上绘制为真。您可以查看下面的图片。

    plot(ema(close, 20))
    plot(ema(close, 50), color=color.white)
    
    EMACrossover = crossover(ema(close, 20), ema(close, 50))
    
    plotshape(EMACrossover, text="EMA Crossover", location=location.belowbar)
    
    conditionOccurredDuringLookbackPeriod(condition, lookbackPeriod) =>
        bool conditionOccurred = false
        for i = 1 to lookbackPeriod
            if condition[i]
                conditionOccurred := true
        conditionOccurred
    
    EMACrossoverOccurredDuringLast14Candles = conditionOccurredDuringLookbackPeriod(EMACrossover, 14)
    
    plotshape(EMACrossoverOccurredDuringLast14Candles, text="True")
    

    See this image

    【讨论】:

    • 非常感谢您的回答,原来我正在寻找的是名为barsince() 的函数。它解决了我的问题。 :)
    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 2022-10-23
    • 2020-09-19
    • 2022-10-01
    • 2022-11-17
    • 2021-11-02
    • 2022-07-22
    • 2022-07-21
    相关资源
    最近更新 更多