Problem with new EA

 

Hello,

I have a new EA. If the BID is above a 200 period moving average and the %B indicator is below 50% 1 period ago then buy at market.

The following code does compile but in MT4 under the Journal tab it says "divide by zero" --- which I interpret as an attempt to divide by zero -- but nowhere do I see that issue in the code. Please show me how to get this code to work.

#property copyright "Copyright © 2011, NMD"
#property link ""
#include <stdlib.mqh>

//External Parameters
extern int MagicNumber = 123;
extern int MAPeriod = 200;
extern int Bands_period = 20;
extern double Bands_deviation = 2;

//---- buffers
double PercentBB[];

//Global variables
int BuyTicket;
int CloseTicket;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()

{
int counted_bars = IndicatorCounted();
//----
double LB, UB;
int limit;

if(counted_bars > 0) counted_bars--;
limit = Bars - counted_bars - 1;

for(int i = 0; i < limit; i++){
LB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_LOWER, i);
UB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_UPPER, i);

PercentBB[i] = (iClose(NULL, 0, i) - LB)/(UB - LB) * 100;
}
//----

{
//Moving Averages
double SlowMA = iMA(NULL,0,MAPeriod,0,0,0,0);
// Buy Order
if(Bid>SlowMA && BuyTicket == 0 && PercentBB[1]<50)
// Open buy order
OrderSend(Symbol(),OP_BUY,.01,Ask,50,0,0,"Buy Order",MagicNumber,0,Green);
}
//Order Close
{
if(PercentBB[1]>80)
OrderSelect(CloseTicket,SELECT_BY_TICKET);
if(OrderCloseTime() == 0 && OrderType() == OP_BUY)
{
double CloseLots = OrderLots();
double ClosePrice = Bid;

bool Closed = OrderClose(CloseTicket,CloseLots,ClosePrice, 50,Red);
}
}

return(0);
}
//+------------------------------------------------------------------+

 

PercentBB[i] = (iClose(NULL, 0, i) - LB)/(UB - LB) * 100;

There for starters.

 
ubzen:

PercentBB[i] = (iClose(NULL, 0, i) - LB)/(UB - LB) * 100;

There for starters.


true, that is a division but i got that from a EA library and it works fine as an indicator without any problem --- why won't it owrk here as an EA?
 

I cannot see your library nor am I aware of it. double LB, UB; appears to be defined within this file. Try going over your allocation and see why it may be zero.

 

The following code:

 for(int i = 0; i < limit; i++){
 LB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_LOWER, i);
 UB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_UPPER, i);

    if(UB-LB==0){Alert("UB-LB=0 ",i);}
 PercentBB[i] = (iClose(NULL, 0, i) - LB)/(UB - LB) * 100;
 }

Generates the following:

23:29:45 2010.01.04 21:47  Helping EURUSD,M5: zero divide
23:29:45 2010.01.04 21:47  Helping EURUSD,M5: Alert: UB-LB=0 1256
23:29:45 2010.01.04 21:47  Helping EURUSD,M5: zero divide
23:29:45 2010.01.04 21:48  Helping EURUSD,M5: Alert: UB-LB=0 1256
23:29:45 2010.01.04 21:48  Helping EURUSD,M5: zero divide
23:29:45 2010.01.04 21:49  Helping EURUSD,M5: Alert: UB-LB=0 1256
23:29:45 2010.01.04 21:49  Helping EURUSD,M5: zero divide
23:29:45 2010.01.04 21:49  Helping EURUSD,M5: Alert: UB-LB=0 1256
23:29:45 2010.01.04 21:49  Helping EURUSD,M5: zero divide
23:29:45 2010.01.04 21:50  Helping EURUSD,M5: Alert: UB-LB=0 1257

i-say, check for 0-divide when using division.

example include code:

if(UB-LB==0){return(0);}

Please don't ask me why Ub-Lb equals 0. I simply don't know.

 
limit = Bars - counted_bars - 1;
for(int i = 0; i < limit; i++){
   LB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_LOWER, i);
   UB = iBands(NULL, 0, Bands_period, Bands_deviation, 0, PRICE_CLOSE, MODE_UPPER, i);
   PercentBB[i] = (iClose(NULL, 0, i) - LB)/(UB - LB) * 100;
To compute an SMA(Bands_period) you need Bands_period bars. So from Bars-1 through Bars-Bands_period, iBands is undefined.