仅平均红色蜡烛的收盘价

Averaging The Closes Of Red Candles Only

让我们直接进入。 所以我目前正在研究一个指标,因此,我希望能够平均计算红色蜡烛的收盘价。问题是,出于某种奇怪的原因,我得出的解决方案也是平均绿色蜡烛,我将不胜感激。

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

我还没有写一行 "pine-script" 但是看了你的代码我想问题出在这里

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

您总是检查最后一根柱线。

也许代码应该是这样的:

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

?

欢迎来到堆栈溢出。 这是一个带有注释的简洁代码。

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

享受