仅平均关闭红色蜡烛

时间:2019-07-25 17:32:42

标签: pine-script

让我们深入其中。 因此,我目前正在研究指标,因此,我希望能够仅对红色蜡烛的收盘时间进行平均。问题是,出于某种奇怪的原因,我也提出了将绿色蜡烛平均化的解决方案,在此方面我会提供一些帮助。

averagePastRedCandles(amount) =>
    currentnum = 0.0
    currentreds = 0.0
    for i = 0 to 99999
        if currentreds == amount // end the loop if amount averaged is met
            break
        else
            if open > close // check if the candles is red
                currentreds := currentreds + 1 // basically the current
amount that's already averaged
                currentnum := currentnum + close[i] // the sum of the closes of the red candles only
            continue

    currentnum / amount

2 个答案:

答案 0 :(得分:0)

我还没有写过一行“ pine-script”,但是查看您的代码,我想问题是当您检查时

if open > close // check if the candles is red

您总是在最后一个栏上进行检查。

也许代码应该像这样:

if open[i] > close[i] // check if the candles is red

答案 1 :(得分:0)

欢迎堆栈溢出。 这是带有注释的整洁代码。

//@author=lucemanb
//@version=4
study("Red Candles Average")

averagePastRedCandles(amount) =>
    // number of counted candles
    candles = 0
    // current average
    sum = 0.0
    // check if the number of candles so far has exceeded the amount of bars on the chart
    if bar_index > amount
        // start counting with a limit of the current bars in chart
        for i=0 to bar_index - 1
            // confirm if the candle is red
            if open[i] > close[i]
                // add the average
                sum := sum + close[i]
                // add count of the candles we have counted
                candles := candles + 1
            // check if we have reached the amount of the candles that we want
            if candles == amount
                //close the loop
                break
    // return the average
    avarege = sum/amount

s = averagePastRedCandles(10)
plot(s)

享受?