Hi,
I'm having a bit of a problem with a zero divide error.
I'm working on a simple EA just for practice really,
It compiled with no errors, but when I tried it in the strategy tester I kept getting zero divide error in the journal.
I added Getlasterror with instructions to print where an error occurs showing the line in the code
As you can see from the attachment
The very last line before the Start return I put if(GetLastError()>0) Print("Line 137");
And I ended up at the 1st line after Start
if(GetLastError()>0) Print("Line 53"); and this is where the error has occured.
That means that there was no error when it hit return, but immediately after Start, there was an error.
In other words outside of my code! How can this happen?
Is it a problem with the strategy tester or have I done something stupid.
Thanks in advance
I'm having a bit of a problem with a zero divide error.
I'm working on a simple EA just for practice really,
It compiled with no errors, but when I tried it in the strategy tester I kept getting zero divide error in the journal.
I added Getlasterror with instructions to print where an error occurs showing the line in the code
As you can see from the attachment
The very last line before the Start return I put if(GetLastError()>0) Print("Line 137");
And I ended up at the 1st line after Start
if(GetLastError()>0) Print("Line 53"); and this is where the error has occured.
That means that there was no error when it hit return, but immediately after Start, there was an error.
In other words outside of my code! How can this happen?
Is it a problem with the strategy tester or have I done something stupid.
Thanks in advance
Inserted Code
//+------------------------------------------------------------------+
//| HigherHi LowerLo.mq4 |
//| Keith |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Keith"
#property link "http://www.metaquotes.net"
//--- input parameters
extern int MagicNumber=1;
extern int StopLoss = 50;
extern int TakeProfit = 100;
extern int DecimalPip=4;
extern int MaxSlippage=5;
extern double RiskPerTrade = 1;
double Pip;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
// Calculate Pip decimal
int decimal = 4;
Pip= 1;
for (int a = 1; a<= DecimalPip; a++)
{
Pip = Pip*0.1;
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if(GetLastError()>0) Print("Line 53");
if(High[1]>High[2] && Low[1]>Low[2]) // Buy Signal
string Signal = "Buy" ;
if(GetLastError()>0) Print("Line 57");
if(High[1]<High[2] && Low[1]<Low[2]) // Sell Signal
Signal = "Sell" ;
if(GetLastError()>0) Print("Line 61");
//----------------------------------Check if a trade is already open with current pair
int STicket =0;
int BTicket =0;
for(int x=OrdersTotal()-1;x>=0;x--)
{
OrderSelect(x, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderMagicNumber()==MagicNumber) STicket = OrderTicket() ;
if(OrderType()<=OP_BUY && OrderMagicNumber()==MagicNumber) BTicket = OrderTicket() ;
}
//-----If STicket >0,Sell trade already open, If BTicket >0 Buy trade open, If both 0, No current trade
//--------------------------------Place new Trade?
if(GetLastError()>0) Print("Line 76");
if(STicket==0 && BTicket==0) //No trades open
{
if (Signal == "Buy")
{
double ask =MarketInfo(Symbol(),MODE_ASK); //Get current ask price
double StopLoss = Ask - StopLoss*Pip;
double TakeProfit = Ask +TakeProfit*Pip;
if(GetLastError()>0) Print("Line 86");
//----------------Calculate LotSize and check that is more than minimum allowed by broker
double Step = MarketInfo(Symbol(), MODE_LOTSTEP);
double LossLot = MathAbs( Ask- StopLoss)/ MarketInfo(Symbol(), MODE_TICKSIZE)*MarketInfo(Symbol(), MODE_TICKVALUE) ;
int Lot = AccountEquity( )*RiskPerTrade/100 / LossLot/ Step ;
double LotSize = Lot * Step;
if(LotSize < MarketInfo(Symbol(), MODE_MINLOT))
Alert ("Lotsize is too small to trade") ;
else
OrderSend(Symbol(),OP_BUY,LotSize,Ask,MaxSlippage*Pip/Point,StopLoss,TakeProfit *Pip,NULL,MagicNumber,0,0);
}
if (Signal == "Sell")
{
double bid =MarketInfo(Symbol(),MODE_BID); //Get current bid price
StopLoss = Bid + StopLoss*Pip;
TakeProfit = Bid -TakeProfit*Pip;
if(GetLastError()>0) Print("Line 106");
//----------------Calculate LotSize and check that is more than minimum allowed by broker
if(GetLastError()>0) Print("Line 108");
Step = MarketInfo(Symbol(), MODE_LOTSTEP);
LossLot = MathAbs( Bid- StopLoss)/ MarketInfo(Symbol(), MODE_TICKSIZE)*MarketInfo(Symbol(), MODE_TICKVALUE) ;
Lot = AccountEquity( )*RiskPerTrade/100 / LossLot/ Step ;
LotSize = Lot * Step;
if(LotSize < MarketInfo(Symbol(), MODE_MINLOT))
Alert ("Lotsize is too small to trade") ;
else
OrderSend(Symbol(),OP_SELL,LotSize,Bid,MaxSlippage*Pip/Point,StopLoss,TakeProfit *Pip,NULL,MagicNumber,0,0);
}
}
if(GetLastError()>0) Print("Line 121");
//---------------------Check if need to close trade
if (Signal == "Buy" && STicket>0) //open sell trade needs closing
{
OrderSelect(STicket,SELECT_BY_TICKET);
OrderClose(STicket,OrderLots( ) ,MarketInfo(OrderSymbol( ),MODE_ASK) ,50,Violet);
}
if (Signal == "Sell" && BTicket>0) //open buy trade needs closing
{
OrderSelect(BTicket,SELECT_BY_TICKET);
OrderClose(BTicket,OrderLots( ) ,MarketInfo(OrderSymbol( ),MODE_BID) ,50,Violet);
}
if(GetLastError()>0) Print("Line 137");
//----
return(0);
}
//+------------------------------------------------------------------+ Attached File(s)
Please Do Not PM Me With Coding Enquiries