Division by zero error

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #58857 quote
    kem2709
    Participant
    Junior

    Hello,

    I’ve started my automated trading strategy and it works well in backtesting mode, however in real life it is on for a couple of minutes, then disables and an error message tells me there has been a division by zero.

    I’ve checked and rechecked the variables, doesn’t seem there is an error from me (might be mistaken since the code is over 4 pages and contains 40 entry conditions). Would you have a solution to resolve this issue? I’m looking for something like in excel, ignore if error phrase to be added to each of the 40 entry conditions.

    Thank you so much in advance,

    Kind regards.

    #58858 quote
    kem2709
    Participant
    Junior

    P.S. it usually happens when the market is not open physically (CFD only) and some candlesticks are very small or inexistent.

    Alfred thanked this post
    #58860 quote
    Vonasi
    Moderator
    Master

    At some point somewhere your code is trying to divide by a value of zero. You’ll need to go through the code and look at all the division sums and try to work out which variable or indicator return is causing the zero and then put some code in to ensure a value of zero is never returned. There is no other way really. Something is returning a zero and you just need to narrow down what it is with methodical working through of the code. Maybe sharing the code would make it easier in case someone else on here fancies having a look for you for anything obvious.

    kem2709 thanked this post
    #58872 quote
    Vonasi
    Moderator
    Master

    P.S. it usually happens when the market is not open physically (CFD only) and some candlesticks are very small or inexistent.

    You’ve answered your own question there.

    kem2709 thanked this post
    #58894 quote
    Nicolas
    Keymaster
    Master

    Only do division if the divisor variable is superior to 0, it usually avoid this kind of error.

    kem2709 thanked this post
    #58907 quote
    Leo
    Participant
    Veteran

    Have you check you variables in the fist candle. Try to define those with the function “once”

    kem2709 thanked this post
    #59163 quote
    kem2709
    Participant
    Junior

    Thank you all guys,

    I’ve added <> 0 condition to all of my variables, now it works!

    #59168 quote
    Vonasi
    Moderator
    Master

    Thank you all guys, I’ve added <> 0 condition to all of my variables, now it works!

    Well that is one way to do it! A bit untidy and slight overkill but sometimes whatever works is the way to do it!

    #59201 quote
    GraHal
    Participant
    Master

    Wow … 40 entry conditions … is it successful? Guess you will know now you sorted the divide by zero error?

    If you want to share the code I’m sure we’d all like to see? 🙂

    #59208 quote
    Vonasi
    Moderator
    Master

    Wow … 40 entry conditions … is it successful? Guess you will know now you sorted the divide by zero error? If you want to share the code I’m sure we’d all like to see? 🙂

    I hadn’t spotted that –  40 conditions! Wow – if I write a strategy with 5 conditions I find it opens one trade a year. How do you satisfy 40 conditions and still get trades to open – even if only half are for entry and half for exit!? I want to see that code now – my interest is piqued!

    #59216 quote
    kem2709
    Participant
    Junior

    Well, it is 20 conditions to go long and 20 conditions to go short. Since I’m using a mix of 5-6 indicators, it doesn’t seem too excessive to me 40 entry points 😉 but my hands hurt with all these corrections made manually 🙁

    I cannot share the code here, sorry. Maybe if it turns to be profitable in the mid term, I will open a hedge fund :DDD

    #59217 quote
    kem2709
    Participant
    Junior

    successful for a couple of days, however since the code is heavy, i think it will be pain in the ass re-adjusting it to market conditions…

    #59225 quote
    Vonasi
    Moderator
    Master

    Does it Walk Forward Test well? The quantity of indicators and conditions could be a recipe for curve fitting.

    #74871 quote
    Daniel_H
    Participant
    Senior

    Vonasi and Nicolas,

    this problem seems to be related to the problem I had with how the IF statements are handled by PRT. That is that PRT do everything at once at the end of a candle. I found this thread when I had the same zero-problem.

    This is how I planned on fixing the problem. First the error:

    bearcandlesize = open - close
    bearwicksize = close - low
    bearcandlevswick = bearcandlesize / bearwicksize

    Then the planned solution that doesn’t work (but I think should work – I used times 10 this time but could be 100 or 1000 or something else mathematically correct that’s needed):

    bearcandlesize = (open - close) * 10
    bearwicksize = (close - low) * 10
    bearcandlevswick = bearcandlesize / bearwicksize

    This time nothing should be divided by zero (and aren’t zero when looking with the GRAPH-function), but since PRT doesn’t do anything until the end of the candle, bearcandlesize and bearwicksize isn’t multiplied by 10 yet (which would increase as example 0.12 to 1.2).

    How I know that the multiplication doesn’t work? I have a system I’m testing and without the division I get 25 trades (11 wins and 14 losses), when I use the division I guess all zero-division trades are removed and the result is only 14 trades (5 wins and 9 losses).

    The way to get around this, but doesn’t help the trading system (because we need the calculation to be done at this candle), is this (which does the calculation of the previous candle instead):

    bearcandlesize = (open - close) * 10
    bearwicksize = (close - low) * 10
    bearcandlevswick = bearcandlesize[1] / bearwicksize[1]

    This time the result is once again 25 trades (11 wins and 14 losses), which means the division doesn’t cut all (or some of the) zero-trades anymore.

    Do you see a resolution to this that I doesn’t see?

    Sincerely,
    Daniel.

    #74873 quote
    Vonasi
    Moderator
    Master

    At the candle close the values for open close and low are fixed and the code is then read. The only way you can have a division by zero is if the close and low are the same value or open and close are the same value.

    Multiplying zero by ten still gives you zero so I do not understand why you are doing this.

    In your last piece of code you are looking at the candle values for the candle before the one that has just closed – I have no idea why you are doing this either as it could still have a zero value for close – low or open – close.

    You could do this to ensure no zero value is calculated:

    bearcandlesize = max((open - close), 0.000000001)
    bearwicksize = max((close - low), 0.000000001)
    bearcandlevswick = bearcandlesize / bearwicksize

     

    Note : It is normally preferred that you start a new topic with a new coding problem rather than add on to an old topic.

    Daniel_H thanked this post
Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.

Division by zero error


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
kem2709 @kem2709 Participant
Summary

This topic contains 18 replies,
has 6 voices, and was last updated by Daniel_H
7 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/10/2018
Status: Active
Attachments: No files
Logo Logo
Loading...