Divide by zero protections

Forums ProRealTime English forum ProRealTime platform support Divide by zero protections

Viewing 13 posts - 1 through 13 (of 13 total)
  • #216028

    Hi,

    When I was testing my code, then it suddenly stops and the following message pops up:

    ‘The trading system has stopped due to a divide by zero while checking the last candlestick. You can add protections to your code to prevent division by zero. Test this addition by backtesting the system first’

    What are these ‘protections’?

     

    #216030

    There is no ‘Protections’, we have to make sure there is never a zero or negative value due to a division.

    A common reason is due to an absence of a bar due to nil price change since last bar … this happens a lot of short duration Timeframes.  I’ve had the problem loads! 🙁

    If you want to post your code then maybe one of us may spot where you may get a divide by zero error.

    Below may help where you have a divison in your code …

    /min(1,xyz) //in place of /xyz

    #216031

    Hi,

    It is about this piece of code:

    (Close-Open)/(Open[1]-Close[1])>1

    The first sum or the second sum can be zero

    #216033

    Might this work in some way, or at least spark further ideas for you?

     

     

    1 user thanked author for this post.
    avatar JS
    #216034
    JS

    Hi,

    In addition to GraHal’s solution…

    It’s a division by zero so it’s about the second term (Open[1]-Close[1])…

    One solution may be… (don’t know if it’s the most elegant solution)

    X / Y > 1

    X = (Close – Open)

    Y = (Open[1] – Close[1])

    If Y = 0 then

    Y = 1

    EndIf

    Instead of dividing by zero, you now divide by 1 what does the least harm…

    1 user thanked author for this post.
    #216168

    Hi Grahal,

    I tested

    1
    If (Close <> Open AND Close[1] <> Open[1]) AND (CloseOpen)/(Open[1]Close[1])>1 Then

    but it is not working

     

    #216169

    Does ‘not working’ mean …

    1. not trading
      OR
    2. still getting divide by zero error?
    #216170

    Hi JS,

    I tested this solution:

    X / Y > 1

    X = (Close – Open)

    Y = (Open[1] – Close[1])

    If Y = 0 then

    Y = 1

    EndIf

    but my code is part of a big ‘if’ statement so I assume I cannot use ‘endif’ in the middle of the big ‘if’ statement. Or am I wrong?

     

    #216171

    Hi Grahal,

    I mean the I still get the ‘dividend by zero’ error message.

    #216172

    Try …

     

    #216173

    Ok, I will test…

    #216175
    JS

    Hi,

    Isn’t a problem…

    #216203

    Change your formula with:

    to make sure you divide by a value at least equal to ticksize (which is not zero).

Viewing 13 posts - 1 through 13 (of 13 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login