Division by zero. Bollinger bands and B%

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #259080 quote
    chroonbanoon
    Participant
    New

    Hi everyone! This is my first post here. I’m not completely sure about how to insert my code correctly but I’ll give it a try.


    I’m working on a new strategy based on Bollinger% and Bollinger bands. It’s an only long strategy where I’m looking to buy at extreme lows or extreme deviation from the average. Specifically I want it to buy when price is below the Bollinger bands. For example when low is 2.8 standard deviations below the 20 average. And I also want the B% to be higher than the previous bar.


    My strategy works well in backtesting and it looks like it does exactly what I want. But as I’ve come to learn it won’t work live because when I calculate the Bollinger% it gives a negative number (below zero) when price moves below the lower band.


    Acording to the written information in Pro real time it says that Bollinger% is calculated like below;


    B% = (close – lower band) / (upper band – lower band)


    Two of my criteria are therefore calculated like this;


    Bpercent = (close - Average[20](close)-2.8*std[20](close)) / (Average[20](close)+2.8*std[20](close) - Average[20](close)-2.8*std[20](close))
    
    
    C3 = Bpercent > Bpercent[1]
    
    
    indicator2 = Average[20](close)-2.8*std[20](close)
    C4 = (low < indicator2)
    



    Is there a way to keep the strategy as it is and to get around the problem with dividing by zero?

    I’ll be very thankful for any help that will point me in the right direction since I can’t figure out how to make it work live.

    #259082 quote
    Iván González
    Moderator
    Master

    The issue is actually a parentheses problem in your formula. Check the denominator:

    Average[20](close)+2.8*std[20](close) - Average[20](close)-2.8*std[20](close)
    

    Is evaluated as: avg+2.8*std-avg-2.8*std = always zero.

    chroonbanoon thanked this post
    #259092 quote
    chroonbanoon
    Participant
    New

    Thanks for your reply. Can’t believe I didn’t think about that 🙂

    My first idea that comes to mind is to change the positive std to 2.81 or maybe a little more to end up with a number slightly above 0. That should have a minimal impact on the strategy.

    Is there maybe a better idea to solve this?

    #259093 quote
    chroonbanoon
    Participant
    New

    Never mind, bad idea. Even a small change to 2.81 makes the result far less good. Is this solvable just by changing the parentheses? I’m not quite sure how.

    #259094 quote
    JC_Bywan
    Moderator
    Master

    Hi, it’s not about avoiding zero by changing 2.8 to 2.81, it’s about a “double minus” that should have become “plus” if not writing parenthesis but was made a simple minus in your denominator.

    To correct it, either add parenthesis as Ivan mentionned: (Average[20](close)+2.8*std[20](close) – ( Average[20](close)-2.8*std[20](close) ) )

    Or don’t add them, but the last minus becomes a plus: (Average[20](close)+2.8*std[20](close) – Average[20](close) + 2.8*std[20](close))

    chroonbanoon thanked this post
    #259095 quote
    chroonbanoon
    Participant
    New

    I understand the problem now. Thank you very much!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Division by zero. Bollinger bands and B%


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by chroonbanoon
10 hours, 56 minutes ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/17/2026
Status: Active
Attachments: No files
Logo Logo
Loading...