NexusFi: Find Your Edge


Home Menu

 





Divide By Zero Error even though I am checking first


Discussion in EasyLanguage Programming

Updated
    1. trending_up 2,777 views
    2. thumb_up 2 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 1 attachments




 
Search this Thread
  #1 (permalink)
gurji
Miami, Florida
 
Posts: 35 since Apr 2022
Thanks Given: 1
Thanks Received: 10

Hi all,

I'm still new to easylanguage, but I can't figure this out.

I keep getting a divide by zero error even though the very first thing I am doing is to test if any of the denominators is 0.

 
Code
if (absvalue(open[1] - close[1]) > 0) Then
	if (curDirection = -1) Then
		olp = ((high - close[1]) / (open[1] - close[1]));
		if (curDirection = 1) Then
			if (low > close[1]) Then
				olp = (((low - close[1])*-1) / (close[1] - open[1])) Else
				olp = (absvalue(low - close[1]) / (close[1] - open[1])) Else
				olp = 0;
Any help is much appreciated!


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Requesting replay data for...
NinjaTrader
GFIs1 DAX System
Traders Hideout
Using multiple Versions of the same DLL in MC.Net
MultiCharts
Bloodhound news indicator
NinjaTrader
Sierra chart Trading system
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Kraken in Talks to Acquire NinjaTrader
42 thanks
Machine Learning Journal
21 thanks
NexusFi site changelog and issues/problem reporting
5 thanks
Demark Indicator
5 thanks
Just another trading journal: PA, Wyckoff & Trends
2 thanks
  #2 (permalink)
 
Silver Dragon's Avatar
 Silver Dragon 
Cincinnati Ohio
Legendary Master Data Manipulator
 
Experience: Intermediate
Platform: TastyWorks / NT
Broker: TastyWorks /NT
Trading: FX, Stocks, Options
Posts: 2,107 since Feb 2011
Thanks Given: 6,422
Thanks Received: 5,238


gurji View Post
Hi all,

I'm still new to easylanguage, but I can't figure this out.

I keep getting a divide by zero error even though the very first thing I am doing is to test if any of the denominators is 0.

 
Code
if (absvalue(open[1] - close[1]) > 0) Then
	if (curDirection = -1) Then
		olp = ((high - close[1]) / (open[1] - close[1]));
		if (curDirection = 1) Then
			if (low > close[1]) Then
				olp = (((low - close[1])*-1) / (close[1] - open[1])) Else
				olp = (absvalue(low - close[1]) / (close[1] - open[1])) Else
				olp = 0;
Any help is much appreciated!

Hi @gurji,

I dont have access to easylanguage, however I put your formulas into Excel. The only scenario I came up where div by zero error came up is when Close1 is zero. Perhaps try putting a check around the Open1 and Close1 to make sure they are both > than zero or absvalue(open[1] - close[1]) > 0 does not equal open[1] .

example:
if (absvalue(open[1] - close[1]) > 0 and absvalue(open[1] - close[1]) <> open[1])

Robert


nosce te ipsum

You make your own opportunities in life.
Visit my NexusFi Trade Journal Reply With Quote
  #3 (permalink)
gurji
Miami, Florida
 
Posts: 35 since Apr 2022
Thanks Given: 1
Thanks Received: 10


Hi, thanks for the reply, but thats not it.

I just confirmed a that when a number divided by a number with a decimal WHEN USING a VARIABLE, causes the error.

So for example, right now, I have this:


 
Code
qqq = 0.53234;
yyy = absvalue(open[1] - close[1]);  // (yyy equals 0.08 exactly.  No rounding)
zzz = 4545/0.08;

This works.
But this fails:


qqq = 0.53234;
yyy = absvalue(open[1] - close[1]);  
zzz = 4545/yyy;


Plot1 (zzz);


Reply With Quote
Thanked by:
  #4 (permalink)
gurji
Miami, Florida
 
Posts: 35 since Apr 2022
Thanks Given: 1
Thanks Received: 10

Ok, I solved the issue. Hopefully this might help someone else.
Basically, I wrote the IF statements to test for zero in a different way using <> 0 instead of > 0, and also I put the test around each direction, instead of the whole thing.
Not sure why all that matters, but it worked.

 
Code
if ((curDirection = -1) and ((open[1] - close[1]) <> 0)) Then
	olp = ((high - close[1]) / (open[1] - close[1]));
	if ((curDirection = 1) and ((close[1] - open[1]) <> 0)) Then
		if (low > close[1]) Then
			olp = (((low - close[1])*-1) / (close[1] - open[1])) Else
			olp = (absvalue(low - close[1]) / (close[1] - open[1])) Else
			olp = 0;


Reply With Quote
Thanked by:
  #5 (permalink)
erdocdpb
phoenix arizona
 
Posts: 6 since Apr 2015
Thanks Given: 0
Thanks Received: 0

Tradestation wants you to code this, this way:

nameofyourchoosing = open[1] -close[1]
if ((curDirection = -1) and ((open[1] - close[1]) <> 0)) Then
olp = iff(nameofyourchoosing <> 0, ((high - close[1]) / (nameofyourchoosing)), olp[1]);
if ((curDirection = 1) and ((nameofyourchoosing) <> 0)) Then
if (low > close[1]) Then
olp = iff(nameofyourchoosing <> 0, (((low - close[1])*-1) / (nameofourchoosing))olp[1]), Else
olp = iff(nameofyourchoosing <> 0,(absvalue(low - close[1]) / (nameofyourchoosing)), olp[1]), Else
olp = 0;


...in order to avoid the divide by zero error.


Reply With Quote




Last Updated on September 18, 2022


© 2025 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts