# zero divide in my EA for charts that are not yet opened in MT4

120
2014.05.25 09:29

Hi,

I'm trying to compare two candles. How much percentage is one candle compared to the next.

for that I am doing something Like:

open1 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_OPEN, 0);
close1 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
open2 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_OPEN, 1);
close2 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_CLOSE, 1);

comp1 = open1 - close1;

comp2 = open2 - close2;

percent = comp1 / comp2;

Lets say the symbol is SGDJPY. I get the following error when I run the EA.

zero divide in 'compare_bars.mq4'

But when I open the chart (at-least once) in MT4, then it works fine.

The same thing repeats when I use a new symbol say AUDHKD. Looks like market data is not downloaded for the new symbols.

Is it possible to programattically download market data if that is indeed the problem ?

2497
2014.05.25 10:04

There is no data. Simplest thing to do is to add a test:

```open1 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_OPEN, 0);
close1 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
open2 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_OPEN, 1);
close2 = iMA(symbol, PERIOD_H1, 1, 0, MODE_SMA, PRICE_CLOSE, 1);

comp1 = open1 - close1;

comp2 = open2 - close2;  // this equals 0 when there is no data or in case where open2 == close2

if(comp2 != 0.0)
percent = comp1 / comp2;
else
percent = 0.0;  // or some other value meaningful for your EA
```
120
2014.05.25 11:57
drazen64:

There is no data. Simplest thing to do is to add a test:

Thank you for the reply drazen64.

We're on the same page. I agree that that there is no data, hence the error.

The key here is the problem does not happen if I open the chart, so adding a check/test won't help as I will get the error for one symbol or the other.

My question was how do I retrieve the market info programmatically, for each symbol and time-frame of my choice. This way

I don't get the error and I don't have to manually open the charts.

2497
2014.05.25 13:01

I see. I thought it happens when you open chart for the first time, but you are checking other currency pairs that are not in the chart you are opening.

I don't know if that could be done, but check these articles: Organizing Data Access and CopyRates()

Regarding test, I think you should put it there since open and close can be equal and it will crash your EA.

10183
2014.05.25 14:30

i guest just show all symbol in  market watch will  solve your problem.

http://puu.sh/8QTat

/