Division by zero ERROR

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #135220 quote
    snucke
    Participant
    Veteran

    Hey!

    as ive understod this error occurs when something is trying to divide itself with 0, but i cant find it….

    anyone thats a bit more skilled that would mind helping me?

    DONDON-5M-STOC.itf
    #135225 quote
    GraHal
    Participant
    Master

    Hope you don’t mind … code is below so that the wizards can see if they can spot anything to produce the divide by zero error.

    Is below similar to / a copy off here or your own code?

    How often are you getting the error and when?

    Is it when you try to start the System on Pro-Order or at random times?

    Has the System taken any trades at all on ProOrder / Auto-Trading??

    // ALLEMAGNE 30
    // H1
    DEFPARAM CumulateOrders = False
     
    // TAILLE DES POSITIONS
    N = 1
    TIMEFRAME (60 MINUTES, UPDATEONCLOSE)
    c7 = Average[50] > Average[50][1]
    //c10 = Average[50] < Average[50][1]
    TIMEFRAME (10 MINUTES, UPDATEONCLOSE)
    indicator11 = Stochastic[14,3](close) //Stabilare men mindre returns
    c14 = (indicator11 > 95)
    TIMEFRAME (Default)
     
    // Donchian
    // Pour le DAX :V = 7
    V = ValueX
    //ONCE Profitablecloses = 80
    //ONCE Profx = 0
     
    //DonchianSupV = highest[V](high)
    DonchianInfV = lowest[V](low)
    indicator1, ignored, ignored, ignored, ignored, ignored, ignored = CALL "VWAP BAND SOM FUNKAR"
    
     
    // ACHAT
    
    c2 = close crosses under DonchianInfV[1]
    c1 = (close > indicator1)
    //c5 = close[3] < indicator1
    //c6 = RSI[10] > 85 // mer returns men mer risk
    //indicator3, ignored, ignored, ignored, ignored, ignored, ignored, ignored = CALL "M-Oscillator"
    //c6 = (indicator3 >= 12)
    
    IF c1 and c2 and c7 THEN
    buy N shares at market
    //Profx = 0
    ENDIF
    
    set stop $loss ValueY
    
    //IF LONGONMARKET and close[0] >= TRADEPRICE then
    //profx = profx + 1
    //ENDIF
    //
    //IF profx >= profitablecloses THEN
    //SELL AT MARKET
    //ENDIF
    
    
    IF longonmarket and C14 THEN
    sell at market
    endif
    
     
    //// VENTE
    //c3 = close crosses under DonchianSupV[1]
    //c11 = (close < indicator1)
    //c12 = RSI[14] < 30
    //
    //
    //IF c10 and c11 and c3 THEN
    //sellshort N shares at market
    //ENDIF
    //
    //if shortonmarket and c12 then
    //exitshort at market
    //endif
    
    //period1=7
    //period2=14
     
    HeuristicsCycleLimit = 2
    once HeuristicsCycle = 0
    once HeuristicsAlgo1 = 1
    once HeuristicsAlgo2 = 0
    If HeuristicsCycle >= HeuristicsCycleLimit  Then
    If HeuristicsAlgo1 = 1 Then
    HeuristicsAlgo2 = 1
    HeuristicsAlgo1 = 0
    ElsIf HeuristicsAlgo2 = 1 Then
    HeuristicsAlgo1 = 1
    HeuristicsAlgo2 = 0
    EndIf
    HeuristicsCycle = 0
    EndIf
    //
    //If HeuristicsAlgo1 = 1 Then
     
    //Heuristics Algorithm 1 Start
     
    If (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) Then
    optimize = optimize + 1
    EndIf
     
    StartingValue = 22 //5
    ResetPeriod = 3 //Specify no of months after which to reset optimization
    Increment = 1
    MaxIncrement = 6 //3 //Limit of no of increments either up or down
    Reps = 3 //Number of trades to use for analysis
     
    MinValue = 1 //Minimum allowed value
    MaxValue = 30 //Maximum allowed value
     
    If monthinit = 1 or monthinit = 3 or monthinit = 5 or monthinit = 7 or monthinit = 8 or monthinit = 10 or monthinit = 12 Then
    MonthDays = 31
    ElsIf monthinit = 4 or monthinit = 6 or monthinit = 9 or monthinit = 11 Then
    MonthDays = 30
    ElsIf monthinit = 2 Then
    If (yearinit/4 = round(yearinit/4)) or (yearinit/400 = round(yearinit/400)) Then //haha not sure how exactly to do this
    MonthDays = 29 //leap year
    Else
    MonthDays = 28
    EndIf
    EndIf
     
    If (month = monthinit and day = dayinit + ResetPeriod) or (month = monthinit + 1 and (day + (MonthDays - dayinit)) >= ResetPeriod) Then
    ValueX = StartingValue
    WinCountB = 0
    StratAvgB = 0
    BestA = 0
    BestB = 0
    dayinit = day
    monthinit = month
    yearinit = year
    EndIf
     
    once ValueX = StartingValue
    once PIncPos = 1 //Positive Increment Position
    once NIncPos = 1 //Neative Increment Position
    once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
    once Mode = 1 //Switches between negative and positive increments
    //once WinCountB = 3 //Initialize Best Win Count
    //GRAPH WinCountB coloured (0,0,0) AS "WinCountB"
    //once StratAvgB = 4353 //Initialize Best Avg Strategy Profit
    //GRAPH StratAvgB coloured (0,0,0) AS "StratAvgB"
     
    If Optimize = Reps Then
    WinCountA = 0 //Initialize current Win Count
    StratAvgA = 0 //Initialize current Avg Strategy Profit
    HeuristicsCycle = HeuristicsCycle + 1
     
    For i = 1 to Reps Do
    If positionperf(i) > 0 Then
    WinCountA = WinCountA + 1 //Increment Current WinCount
    EndIf
    StratAvgA = StratAvgA + (((PositionPerf(i)*countofposition[i]*100000)*-1)*-1)
    Next
    StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
    //Graph (PositionPerf(1)*countofposition[1]*100000)*-1 as "PosPerf1"
    //Graph (PositionPerf(2)*countofposition[2]*100000)*-1 as "PosPerf2"
    //Graph StratAvgA*-1 as "StratAvgA"
    //once BestA = 300
    //GRAPH BestA coloured (0,0,0) AS "BestA"
    If StratAvgA >= StratAvgB Then
    StratAvgB = StratAvgA //Update Best Strategy Profit
    BestA = ValueX
    EndIf
    //once BestB = 300
    //GRAPH BestB coloured (0,0,0) AS "BestB"
    If WinCountA >= WinCountB Then
    WinCountB = WinCountA  //Update Best Win Count
    BestB = ValueX
    EndIf
     
    If WinCountA > WinCountB and StratAvgA > StratAvgB Then
    Mode = 0
    ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
    ValueX = ValueX - (Increment*NIncPos)
    NIncPos = NIncPos + 1
    Mode = 2
    ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
    ValueX = ValueX + (Increment*PIncPos)
    PIncPos = PIncPos + 1
    Mode = 1
    ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
    ValueX = ValueX + (Increment*PIncPos)
    PIncPos = PIncPos + 1
    Mode = 1
    ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 2 Then
    ValueX = ValueX - (Increment*NIncPos)
    NIncPos = NIncPos + 1
    Mode = 2
    EndIf
     
    If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
    If BestA = BestB Then
    ValueX = BestA
    Else
    If reps >= 10 Then
    WeightedScore = 10
    Else
    WeightedScore = round((reps/100)*100)
    EndIf
    ValueX = round(((BestA*(20-WeightedScore)) + (BestB*WeightedScore))/20) //Lower Reps = Less weight assigned to Win%
    EndIf
    NIncPos = 1
    PIncPos = 1
    ElsIf ValueX > MaxValue Then
    ValueX = MaxValue
    ElsIf ValueX < MinValue Then
    ValueX = MinValue
    EndIF
     
    Optimize = 0
    EndIf
     
    // Heuristics Algorithm 1 End
     
    //ElsIf HeuristicsAlgo2 = 1 Then
     
    //Heuristics Algorithm 2 Start
     
    If (onmarket[1] = 1 and onmarket = 0) or (longonmarket[1] = 1 and longonmarket and countoflongshares < countoflongshares[1]) or (longonmarket[1] = 1 and longonmarket and countoflongshares > countoflongshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares < countofshortshares[1]) or (shortonmarket[1] = 1 and shortonmarket and countofshortshares > countofshortshares[1]) or (longonmarket[1] and shortonmarket) or (shortonmarket[1] and longonmarket) Then
    optimize2 = optimize2 + 1
    EndIf
     
    StartingValue2 = 60
    ResetPeriod2 = 3 //Specify no of months after which to reset optimization
    Increment2 = 1
    MaxIncrement2 = 44 //Limit of no of increments either up or down
    Reps2 = 3 //Number of trades to use for analysis
     
    MinValue2 = 84 //Minimum allowed value
    MaxValue2 = 78 //Maximum allowed value
     
    If monthinit2 = 1 or monthinit2 = 3 or monthinit2 = 5 or monthinit2 = 7 or monthinit2 = 8 or monthinit2 = 10 or monthinit2 = 12 Then
    MonthDays2 = 31
    ElsIf monthinit2 = 4 or monthinit2 = 6 or monthinit2 = 9 or monthinit2 = 11 Then
    MonthDays2 = 30
    ElsIf monthinit2 = 2 Then
    If (yearinit2/4 = round(yearinit2/4)) or (yearinit2/400 = round(yearinit2/400)) Then //haha not sure how exactly to do this
    MonthDays2 = 29 //leap year
    Else
    MonthDays2 = 28
    EndIf
    EndIf
     
    If (month = monthinit2 and day = dayinit2 + ResetPeriod2) or (month = monthinit2 + 1 and (day + (MonthDays2 - dayinit2)) >= ResetPeriod2) Then
    ValueY = StartingValue2
    WinCountB2 = 0
    StratAvgB2 = 0
    BestA2 = 0
    BestB2 = 0
    dayinit2 = day
    monthinit2 = month
    yearinit2 = year
    EndIf
     
    once ValueY = StartingValue2
    once PIncPos2 = 1 //Positive Increment Position
    once NIncPos2 = 1 //Neative Increment Position
    once Optimize2 = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
    once Mode2 = 1 //Switches between negative and positive increments
    //once WinCountB2 = 3 //Initialize Best Win Count
    //GRAPH WinCountB2 coloured (0,0,0) AS "WinCountB2"
    //once StratAvgB2 = 4353 //Initialize Best Avg Strategy Profit
    //GRAPH StratAvgB2 coloured (0,0,0) AS "StratAvgB2"
     
    If Optimize2 = Reps2 Then
    WinCountA2 = 0 //Initialize current Win Count
    StratAvgA2 = 0 //Initialize current Avg Strategy Profit
    HeuristicsCycle = HeuristicsCycle + 1
     
    For i2 = 1 to Reps2 Do
    If positionperf(i) > 0 Then
    WinCountA2 = WinCountA2 + 1 //Increment Current WinCount
    EndIf
    StratAvgA2 = StratAvgA2 + (((PositionPerf(i)*countofposition[i]*100000)*-1)*-1)
    Next
    StratAvgA2 = StratAvgA2/Reps2 //Calculate Current Avg Strategy Profit
    //Graph (PositionPerf(1)*countofposition[1]*100000)*-1 as "PosPerf1-2"
    //Graph (PositionPerf(2)*countofposition[2]*100000)*-1 as "PosPerf2-2"
    //Graph StratAvgA2*-1 as "StratAvgA2"
    //once BestA2 = 300
    //GRAPH BestA2 coloured (0,0,0) AS "BestA2"
    If StratAvgA2 >= StratAvgB2 Then
    StratAvgB2 = StratAvgA2 //Update Best Strategy Profit
    BestA2 = ValueY
    EndIf
    //once BestB2 = 300
    //GRAPH BestB2 coloured (0,0,0) AS "BestB2"
    If WinCountA2 >= WinCountB2 Then
    WinCountB2 = WinCountA2  //Update Best Win Count
    BestB2 = ValueY
    EndIf
     
    If WinCountA2 > WinCountB2 and StratAvgA2 > StratAvgB2 Then
    Mode = 0
    ElsIf WinCountA2 < WinCountB2 and StratAvgA2 < StratAvgB2 and Mode2 = 1 Then
    ValueY = ValueY - (Increment2*NIncPos2)
    NIncPos2 = NIncPos2 + 1
    Mode2 = 2
    ElsIf WinCountA2 >= WinCountB2 or StratAvgA2 >= StratAvgB2 and Mode2 = 1 Then
    ValueY = ValueY + (Increment2*PIncPos2)
    PIncPos2 = PIncPos2 + 1
    Mode = 1
    ElsIf WinCountA2 < WinCountB2 and StratAvgA2 < StratAvgB2 and Mode2 = 2 Then
    ValueY = ValueY + (Increment2*PIncPos2)
    PIncPos2 = PIncPos2 + 1
    Mode2 = 1
    ElsIf WinCountA2 >= WinCountB2 or StratAvgA2 >= StratAvgB2 and Mode2 = 2 Then
    ValueY = ValueY - (Increment2*NIncPos2)
    NIncPos2 = NIncPos2 + 1
    Mode2 = 2
    EndIf
     
    If NIncPos2 > MaxIncrement2 or PIncPos2 > MaxIncrement2 Then
    If BestA2 = BestB2 Then
    ValueY = BestA
    Else
    If reps2 >= 10 Then
    WeightedScore2 = 10
    Else
    WeightedScore2 = round((reps2/100)*100)
    EndIf
    ValueY = round(((BestA2*(20-WeightedScore2)) + (BestB2*WeightedScore2))/20) //Lower Reps = Less weight assigned to Win%
    EndIf
    NIncPos2 = 1
    PIncPos2 = 1
    ElsIf ValueY > MaxValue2 Then
    ValueY = MaxValue2
    ElsIf ValueY < MinValue2 Then
    ValueY = MinValue2
    EndIF
     
    Optimize2 = 0
    EndIf
     
    // Heuristics Algorithm 2 End
    
    #135227 quote
    GraHal
    Participant
    Master

    The code below looks like it may be a possible source of divide by zero??

    (yearinit/4 = round(yearinit/4)) or (yearinit/400 = round(yearinit/400))
    robertogozzi thanked this post
    #135228 quote
    robertogozzi
    Moderator
    Master

    Try also changing line 17 to:

    V = max(1,ValueX)

    Try moving line 25 BEFORE the expression highlighted by GraHal, this might cause assign a correct value to yearinit before it’s evaluated.

    #135229 quote
    GraHal
    Participant
    Master

    You may want to do some research / reading back over Topics from which you got the ML parts of your code because below has been discussed and it is flawed re the i2 (see my comment below).

     

    For i2 = 1 to Reps2 Do
    If positionperf(i) > 0 Then
    WinCountA2 = WinCountA2 + 1 //Increment Current WinCount
    EndIf
    StratAvgA2 = StratAvgA2 + (((PositionPerf(i)*countofposition[i]*100000)*-1)*-1)

    should be as below  (3 instances of i that should be i2)

    For i2 = 1 to Reps2 Do
    If positionperf(i2) > 0 Then
    WinCountA2 = WinCountA2 + 1 //Increment Current WinCount
    EndIf
    StratAvgA2 = StratAvgA2 + (((PositionPerf(i2)*countofposition[i2]*100000)*-1)*-1)
    #135231 quote
    snucke
    Participant
    Veteran

    i dont believe the ML code is wrong, ive used it in multiple strategies with no errors

    #135233 quote
    snucke
    Participant
    Veteran

    The ML code is copied from this forum

    i get the error very quick from the point i put it in the proorder auto and activate it

    #135236 quote
    GraHal
    Participant
    Master

    Yeah I recall same issue when I was involved with the ML code … best to read over any suggestions already made for a fix?

    But that i2 is flawed anyway as the code states

    For i2 = 1 to Reps2 Do

    but the code doesn’t have an i2 (it has i but not i2).

    The result is crosstalk / cross referring of values between the ValueX code and the ValueY code.

    #135237 quote
    snucke
    Participant
    Veteran

    I have cleaned up the code now, and now it gives me a new error; “The trading system was stopped because it encountered an error. please send us a report to enable us to analyze the error….

    this is the code wich gave the error

    // ALLEMAGNE 30
    DEFPARAM CumulateOrders = False
    
    TIMEFRAME (60 MINUTES, UPDATEONCLOSE)
    c7 = Average[50] > Average[50][1]
    TIMEFRAME (10 MINUTES, UPDATEONCLOSE)
    indicator11 = Stochastic[14,3](close)
    c14 = (indicator11 > 95)
    TIMEFRAME (Default)
    
    // Donchian
    V = 23
    
    DonchianInfV = lowest[V](low)
    indicator1, ignored, ignored, ignored, ignored, ignored, ignored = CALL "VWAP BAND SOM FUNKAR"
    
    c2 = close crosses under DonchianInfV[1]
    c1 = (close > indicator1)
    
    IF c1 and c2 and c7 THEN
    buy 1 shares at market
    ENDIF
    
    set stop $loss 90
    
    IF longonmarket and C14 THEN
    sell at market
    endif
    #135238 quote
    GraHal
    Participant
    Master

    Here you are … a good one to start with  … JuanJ is the Author of the ML code!

    https://www.prorealcode.com/topic/division-by-zero-error-solved/

    and another

    https://www.prorealcode.com/topic/division-by-zero-error/

    #135240 quote
    GraHal
    Participant
    Master

    it gives me a new error

    When … Is it when you try to start the System on Pro-Order or at random times?

    Also did you see the suggestion made higher up by Roberto … I only just noticed it.

    #135241 quote
    snucke
    Participant
    Veteran

    It takes about 1 min after i start it in proorder until i get an error, as u can see the code now has nothing from the ML in it as i posted above

    #135242 quote
    snucke
    Participant
    Veteran

    Might have found the problem, was a while ago i started it again in proorder now since i configured my VWAP indicator wich i think and hope was the problem.

    thanks GraHal and Robert for the help!

    #135247 quote
    Vonasi
    Moderator
    Master

    @snucke. Always use the ‘Insert PRT Code’ button when putting code in your posts. Please read the forum rules before posting anything else in the forums.

    I have edited your post.

    #135258 quote
    Nicolas
    Keymaster
    Master

    Try to preloadbars when the strategy crash almost instantly, it is a sign that some calculation are made wrong and need more data history to be made.

    GraHal and snucke thanked this post
Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.

Division by zero ERROR


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
snucke @snucke Participant
Summary

This topic contains 19 replies,
has 6 voices, and was last updated by GraHal
5 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/08/2020
Status: Active
Attachments: 1 files
Logo Logo
Loading...