Divide by zero error

Questions about MultiCharts and user contributed studies.
arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Divide by zero error

Postby arjfca » 13 May 2011

Hello

Just can't figure where is the error

Code: Select all

if volume > 0 and range > 0 then begin
ClimaxUp_Val = Highest(upticks*Range,PeriodBack);
If (upticks*Range)= ClimaxUp_Val then ClimaxUp = True;

ClimaxDown_Val = Highest(Downticks*Range,PeriodBack);
If (Downticks*Range)= ClimaxDown_Val then ClimaxDown = True;

// This section caused the error
HighVol_Churn_val = Highest((Volume / Range),PeriodBack);
If (Volume / Range) = HighVol_Churn_val then HighChurn_Vol = True;

end;
Periods with no volume or range = 0 should be excluded by the first condition

Martin

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Divide by zero error

Postby TJ » 13 May 2011

Hello

Just can't figure where is the error

Code: Select all

if volume > 0 and range > 0 then begin
ClimaxUp_Val = Highest(upticks*Range,PeriodBack);
If (upticks*Range)= ClimaxUp_Val then ClimaxUp = True;

ClimaxDown_Val = Highest(Downticks*Range,PeriodBack);
If (Downticks*Range)= ClimaxDown_Val then ClimaxDown = True;

// This section caused the error
HighVol_Churn_val = Highest((Volume / Range),PeriodBack);
If (Volume / Range) = HighVol_Churn_val then HighChurn_Vol = True;

end;
Periods with no volume or range = 0 should be excluded by the first condition

Martin
but this statement looks outside of your conditional bar...

Code: Select all

Highest((Volume / Range),PeriodBack)

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Divide by zero error

Postby arjfca » 13 May 2011

Hello TJ

No it is not outside the main routine. To my understanding, there is only one "END;" that close the main conditionnal loop. The others "If" conditions are only one line code and does not necessitate to be terminated with "end;"

I'm not home at the moment, I will try to move the problematic section.

Have a great weekend
Martin

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Divide by zero error

Postby TJ » 14 May 2011

...

but this statement looks outside of your conditional bar...

Code: Select all

Highest((Volume / Range),PeriodBack)
if anybody who needs further explanation of the above answer please raise their hands.



ps. This is a good debugging exercise. All EL students should give this a try.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: Divide by zero error

Postby arjfca » 14 May 2011

Thanks TJ for your implication and the debugging interrogation.

OK, since you define it as a debug test, i decided to play the game . My answer may look sarcastic, but don't take it to the first degree. In fact, i love these kind of questionning. Here is a step by step debugging and questioning operation. A speaking to myself process.... This is a long post. It is ended by my conclusion

The situation:
A divison by zero is detected by MC in the code.
A condition filter like << If Range >0 then ... >> as been added, Still the error persist

I read back my code and I still say that the logic is OK. "The If Volume = 0 and Range = 0 then begin " should filter these value and prevent to enter in the loop. Division / zero still there

- Since some value was not filtered, opted to see where is the faulty bar. I added a line to print the date and time of a bar when Range = 0. This line is placed before the faulty section, so I should catch it before the divide by zero period

Code: Select all

if volume > 0 and range > 0 then begin
ClimaxUp_Val = Highest(upticks*Range,PeriodBack);
If (upticks*Range)= ClimaxUp_Val then ClimaxUp = True;

ClimaxDown_Val = Highest(Downticks*Range,PeriodBack);
If (Downticks*Range)= ClimaxDown_Val then ClimaxDown = True;

// Test code to print the info of any bar with range = 0
// The code is situeted before the dicision, so logiquely it should capture a faulty
// Range = 0 before the division

If range = 0 then print ("Range = 0: ",date:0:0, " ", time:4:0);

HighVol_Churn_val = Highest((Volume / Range),PeriodBack);
If (Volume / Range) = HighVol_Churn_val then HighChurn_Vol = True;
end;
Same error, naturally since nothing as been done to modify the loop. To my surprise, nothing came out front the code on the Output window. No bar had a Range = 0. Logistically, it was supposed to be filtered before the loop. But, still the error divide by 0 persist

OK, Since the code does not detect a 0 , I will add a line to look, instead for range > 0 , I will look for Range > 1

Code: Select all

if range > 1 then begin
HighVol_Churn_val = Highest((Volume / Range),PeriodBack);
If (Volume / Range) = HighVol_Churn_val then HighChurn_Vol = True;
end;
end;
It resolved the problem. No error and result was as expected.

Fine, the problem is resolved, by I'm not satisfied with the result so I decided to see what is the lowest value for the Range. The answer should be between 0 and 1. 0 is causing error and 1 did not.

Result of this test,

If Range > .001 then Begin .... will fail
If Range > .01 then Begin ..... will pass

Hummm

Philosophical question.... What is a zero for MC. What is the lowest value. I coded some variables test by dividing any value lower .0001 than I certified that MC id doing all calculation properly. Any value divided by a variable slightly greater than 0 will not give an error.

Questioning persist. My code does not work, but the logic work.

Code: Select all


HighVol_Churn_val = Highest((Volume / Range),PeriodBack);
If (Volume / Range) = HighVol_Churn_val then HighChurn_Vol = True;
Maybe the problem is not with the line
<< If (Volume / Range) = HighVol_Churn_val then HighChurn_Vol = True; >>, Where if range = 0 an error will occur,

but whit the preceding one,
<< HighVol_Churn_val = Highest((Volume / Range),PeriodBack);

I removed the line and no error detected.

Yes it was...If range = 0 condition did not cause any error.

Since, on the last line, the division is made inside the function, I decided to replace the division by a variable

Original code line is
HighVol_Churn_val = Highest((Volume / Range),PeriodBack);

as been replace by
Value1 = Volume / Range;
HighVol_Churn_val = Highest(Value1,PeriodBack);


All OK. If Range = 0 did not caused any problem. All bars was ok.

My conclusion: MC function should be use with a define value ( a pre calculated value) instead to be call using a value to be calculated within the calling line

HighVol_Churn_val = Highest((Volume / Range),PeriodBack); ... Not OK

Value1 = Volume / Range;
HighVol_Churn_val = Highest(Value1,PeriodBack);........................... OK

Problem and quest.....resolve

Martin :)

User avatar
TJ
Posts: 7743
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: Divide by zero error

Postby TJ » 14 May 2011

Thanks TJ for your implication and the debugging interrogation.
...

Value1 = Volume / Range;
HighVol_Churn_val = Highest(Value1,PeriodBack);........................... OK

Problem and quest.....resolve

Martin :)
very nice interrogation work.


to prevent divide by zero, try this:

Code: Select all

if range <> 0 then
Value1 = Volume / Range
else
Value1 = 0;


Return to “MultiCharts”