Bollinger Bands not matching chart values in my algo

Asked by Sejpalsinh Jadeja · SOLVED
1 reply 528 views Jump to solution Reply
Sejpalsinh Jadeja Original Post
AlgoDelta Team

I’m trying to use Bollinger Bands (BB) in my algo using my 1-minute candle data.

I implemented the formula using SMA and standard deviation, but when I compare it with chart values, it doesn’t match exactly.

Candles look correct, still bands are slightly off.

Is there something specific I need to take care of while calculating BB?

Posted Apr 25
Reply

    1 Reply

    Support AlgoDelta
    Support AlgoDelta AlgoDelta Support ·

    Yes, Bollinger Bands are simple, but small mistakes in data or calculation make them mismatch.

    Let break it properly:

    So if you are using Bb on less candles data then also this happnes, usally you should use the 20 period by default if anything else, values won’t match.

    May be the another reason is standard deviation – if even small variation in how std dev is calculated → visible difference in bands.

    If you start with less data, then also this happens so if you are testing this in morning at market starts then it also gives inaccurate values, so you also need to feed some historical data + live candle data (aftermarket start).

    Below is the code you can refer:

    import math
    
    def calculate_bollinger_bands(candles, period=20):
        # Use only closed candles (skip last running candle)
        candles = candles[:-1]
    
        if len(candles) < period:
            return None
    
        closes = [c["close"] for c in candles]
        recent = closes[-period:]
    
        sma = sum(recent) / period
    
        std_dev = math.sqrt(
            sum((price - sma) ** 2 for price in recent) / period
        )
    
        upper = sma + (2 * std_dev)
        lower = sma - (2 * std_dev)
    
        return {
            "upper": round(upper, 2),
            "middle": round(sma, 2),
            "lower": round(lower, 2),
        }
    
    candles = [
        {"close": 100}, {"close": 101}, {"close": 102}, {"close": 103}, {"close": 104},
        {"close": 105}, {"close": 106}, {"close": 107}, {"close": 108}, {"close": 109},
        {"close": 110}, {"close": 111}, {"close": 112}, {"close": 113}, {"close": 114},
        {"close": 115}, {"close": 116}, {"close": 117}, {"close": 118}, {"close": 119},
        {"close": 120}  # last candle (running → will be ignored)
    ]
    
    bb = calculate_bollinger_bands(candles)
    print(bb)
    Verified Solution
    • Thanks for the guidance. Values are matching exactly. – Sejpalsinh Jadeja Team Jun 14
    Markdown supported · ```json for code blocks · paste or drop screenshots

    Be specific and kind. Never share API keys, passwords or personal account details.

    Welcome to the community

    Log in or create your account in under a minute.

    1. 1Mobile
    2. 2E-mail
    3. 3Password
    +91

    Have a password?

    By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.