Results 1 to 3 of 3
 0 Attachment(s)    

Thread: Zero Divide

  1. #1
    Junior Member Client's Avatar
    Join Date
    Jan 2011
    Posts
    1

    Lightbulb Zero Divide

    Hi,

    I have a proble when during a Strategy Testing i always get an error "zero divide".

    The only division I have are these lines -

    Proportion1=(Close0-Open0)/(Close0-Open1);
    Proportion2=(Close2-Open2)/(Open1-Close1);
    Proportion3=(Close0-Open0)/(Close2-Open2);

    Proportion4=(Close2-Open2)/(Close0-Open0);

    Proportion5=(Open0-Close0)/(Open1-Close0);
    Proportion6=(Open2-Close2)/(Close1-Open1);
    Proportion7=(Open0-Close0)/(Open2-Close2);

    Proportion8=(Open2-Close2)/(Open0-Close0);

    I think these lines can't be divided by zero because of the different prices each time...

    What should I add or change to eliminate this error?

  2. #2
    Junior Member Sloth's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    7

    Default

    Probably the open is equal to close.
    Excuse me for my bad English, but I'm from Italy.

  3. #3

    Default

    To prevent the zero divides from happening, you need to make it a condition that the calculation only runs if the open and close aren't equal. Like this:

    PHP Code:

    if(Close0 != Open1)  Proportion1=(Close0-Open0)/(Close0-Open1);
    if(
    Close1 != Open1)  Proportion2=(Close2-Open2)/(Open1-Close1);
    if(
    Close2 != Open2)  Proportion3=(Close0-Open0)/(Close2-Open2);

    // and so on... 
    If Close0 does equal Open1, etc, the values for Proportion1, Proportion2, etc will be their last stored value. Depending upon how the rest of your code works, you may need to assign them a different value in a new line before the condition. Like this:

    PHP Code:

    Proportion1 
    1// or whatever value you need it to be
    if(Close0 != Open1)  Proportion1=(Close0-Open0) / (Close0-Open1);

    // and so on... 
    One other tip. If you leave a space before and after the division slash (/) as I have done above, you will find it easier to search for divisions within your code because you won't be finding any comments.

Tags for this Thread

add, change, strategy, time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •