Navigation

    Quantiacs Community

    • Register
    • Login
    • Search
    • Categories
    • News
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Bollinger Bands

    Strategy help
    2
    3
    299
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      anthony_m last edited by

      Hi, I would like to code a mean reverting system based on bands similar to Bolliger bands. Do you have an example for coding Bollinger bands?

      A 1 Reply Last reply Reply Quote 0
      • A
        antinomy @anthony_m last edited by

        @anthony_m
        Bollinger Bands are actually quite easy to calculate.
        The middle band is just the simple moving average, the default period is 20.
        For the other bands you need the standard deviation for the same period.
        The upper band is middle + multiplier * std
        The lower band is middle - multiplier * std
        Where the default for the multiplier is 2.

        There's an article on the formula for Bollinger Bands on Investopedia - they use the 'typical price' (high + low + close) / 3 but I think most people just use the close price.

        For the code it depends if you only need the latest values or the history.
        Using pandas the code for the first alternative could be:

        def strategy(data):
            close = data.sel(field='close').copy().to_pandas().ffill().bfill().fillna(0) 
        
            # let's just use the default 20 period:
            period = 20
            sma = close.iloc[-period:].mean()
            std = close.iloc[-period:].std()
        
            # and the default multiplier of 2:
            multiplier = 2
            upper = sma + multiplier * std
            lower = sma - multiplier * std
        

        If you need more than the last values you can use pandas.rolling:

        def strategy(data):
            close = data.sel(field='close').copy().to_pandas().ffill().bfill().fillna(0)
        
            # let's just use the default 20 period:
            period = 20
            sma = close.rolling(period).mean()
            std = close.rolling(period).std()
        
            # and the default multiplier of 2:
            multiplier = 2
            upper = sma + multiplier * std
            lower = sma - multiplier * std
        
        A 1 Reply Last reply Reply Quote 2
        • A
          anthony_m @antinomy last edited by

          @antinomy wow, thank you so much, this is awesome!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB | Contributors
          • Documentation
          • About
          • Career
          • My account
          • Privacy policy
          • Terms and Conditions
          • Cookies policy
          Home
          Copyright © 2014 - 2021 Quantiacs LLC.
          Powered by NodeBB | Contributors