Divide by zero protections

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #216028 quote
    RichardVeen
    Participant
    Junior

    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 quote
    GraHal
    Participant
    Master

    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 quote
    RichardVeen
    Participant
    Junior

    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 quote
    GraHal
    Participant
    Master

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

    If (Close <> Open AND Close[1] <> Open[1]) AND (Close-Open)/(Open[1]-Close[1])>1 Then
    JS thanked this post
    #216034 quote
    JS
    Participant
    Senior

    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…

    GraHal thanked this post
    #216168 quote
    RichardVeen
    Participant
    Junior
    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 quote
    GraHal
    Participant
    Master
    Does ‘not working’ mean …
    1. not trading OR
    2. still getting divide by zero error?
    #216170 quote
    RichardVeen
    Participant
    Junior
    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 quote
    RichardVeen
    Participant
    Junior
    Hi Grahal, I mean the I still get the ‘dividend by zero’ error message.
    #216172 quote
    GraHal
    Participant
    Master
    Try …
    C1 = (Close–Open) > 1*(Open[1]–Close[1])
    
    
    #216173 quote
    RichardVeen
    Participant
    Junior
    Ok, I will test…
    #216175 quote
    JS
    Participant
    Senior

    Hi,

    Isn’t a problem…

    #216203 quote
    Nicolas
    Keymaster
    Master
    Change your formula with:
    (Close-Open )/ max(ticksize,(Open[1]-Close[1])) > 1
    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)
  • You must be logged in to reply to this topic.

Divide by zero protections


Platform Support: Charts, Data & Broker Setup

New Reply
Author
Summary

This topic contains 12 replies,
has 4 voices, and was last updated by Nicolas
2 years, 8 months ago.

Topic Details
Forum: Platform Support: Charts, Data & Broker Setup
Language: English
Started: 06/12/2023
Status: Active
Attachments: No files
Logo Logo
Loading...