"Zero divide" when attempting File functions.

 

I am trying to load a text file I saved with FileWrite() that contains order open times and directions (buy or sell). My intention is to use these variables to replicate all the orders opened by one of my EAs (EA#1) within another EA (EA#2). That way I can see if EA#2 is better at closing EA#1's orders than EA#1 itself.

My text file looks like this:

S;2007.02.09 12:50
S;2007.07.27 15:05
S;2008.09.15 15:40
S;2008.09.15 16:15
S;2008.09.15 16:20
S;2008.09.15 16:25
S;2008.09.15 16:30
B;2008.09.22 14:30
S;2008.09.29 13:34
S;2008.09.29 14:00
S;2008.10.10 09:20
S;2008.10.10 09:35
S;2007.07.27 15:35
S;2008.10.10 09:45
S;2008.10.10 09:50   ...so on and so on

The first variable's value is "B" or "S" (which represents OP_BUY or OP_SELL), and the second variable is the OrderOpenTime().

The code I am having issues with is the read/execute portion; its written below. I found it on this website in this article and modified it from there:

int start()
{
...
 double price = 0, sl= 0, tp=0,lots=0.1;

      
  int LinesCNT=0,i,ticket,pos; double p; datetime t; string s_;
 
  int error = 0;
  int fh=FileOpen(FileNameForRead,FILE_CSV|FILE_READ,';'); // to test the file, it is necessary to pout it into tester\files\TL_DATA.txt

  if(fh<0) { Print("Error opening file \"" + FileNameForRead + "\"","ERROR", IDOK + MB_ICONERROR); return(1); }

  while(true)
  {
 
    string Operation=FileReadString(fh);
    
    if(FileIsEnding(fh))  break; // file ended? - exit

    // count the section coordinates

    string st1=FileReadString(fh);  
    datetime t1=StrToTime(st1);
    string MarkComent = "X";
    

 
    //**********************************************************************************
    // 2) block opening orders as soon as the beginning of the pattern section is passed.
    //**********************************************************************************

    bool OrderNotPresent=true; // a sign showing that we haven't opened such an order yet
    
  if (error>0){ Print("GetLastError() = ",error); }

    if(t1<=TimeCurrent()) // time to open - make sure that this order is not opened 
    { Print("t1 = ",t1," timecurrent = ",TimeCurrent());
      for(i=0;i<OrdersTotal();i++)
      { 
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) { Print ("Could not select"); continue; }
       
        if(OrderComment()==MarkComent) { OrderNotPresent=false; break; } // order already exists
      }
      for(i=0;i<OrdersHistoryTotal() && OrderNotPresent == true;i++ )
      {
        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false){ Print ("Could not select"); continue; }
        // order in history is added to the end, something like "[sl]" - it must be cut off!!
 
        
        if(StringFind(OrderComment(),MarkComent,0)!=-1) { OrderNotPresent=false; break; } // order already exists
      }
      if(OrderNotPresent) // no such order - open it
      { Print("OrderNotPresent");
        // open an order 
        if(Operation=="B") // Buy
        { 
          price = NormalizeDouble(Ask,Digits);
  
          sl = If(StopLoss > 0, NormalizeDouble(price - StopLoss*pt,Digits), 0);
          tp = If(TakeProfit > 0, NormalizeDouble(price + TakeProfit*pt,Digits), 0);
          lots = NormalizeDouble(Lots,LotDigits);
          ticket=OrderSend(Symbol(),OP_BUY,lots,price,3,sl,tp,MarkComent,111,0,Blue);
          if (ticket!=-1) { if (!OrderSelect(ticket,SELECT_BY_TICKET))Print("Could not select order"); } else { Print("No Order ticket"); }
        } 
       else if (Operation=="S") // Sell
        {
          price = NormalizeDouble(Bid,Digits);
   
          sl = If(StopLoss > 0, NormalizeDouble(price + StopLoss*pt,Digits), 0);
          tp = If(TakeProfit > 0, NormalizeDouble(price - TakeProfit*pt,Digits), 0);
          lots = NormalizeDouble(Lots,LotDigits);      
          ticket=OrderSend(Symbol(),OP_SELL,lots,price,3,sl,tp,MarkComent,111,0,Red);
          if (ticket!=-1) { if (!OrderSelect(ticket,SELECT_BY_TICKET))Print("Could not select order"); } else { Print("No Order ticket"); }

        }
      }
    }
   
  
  FileClose(fh);
...
}

When I run this I get this in my journal (print statements left in to make debugging easier)

2010.10.10 06:47:14     2010.10.08 23:04  EA2  GBPUSD,M5: zero divide
2010.10.10 06:47:14     2010.10.08 23:04  EA2  GBPUSD,M5: OUT
2010.10.10 06:47:14     2010.10.08 23:04  EA2  GBPUSD,M5: t1 = 1171025400 timecurrent = 1286579099
2010.10.10 06:47:14     2010.10.08 22:55  EA2  GBPUSD,M5: zero divide
2010.10.10 06:47:14     2010.10.08 22:55  EA2  GBPUSD,M5: OUT
2010.10.10 06:47:14     2010.10.08 22:55  EA2  GBPUSD,M5: t1 = 1171025400 timecurrent = 1286578500
2010.10.10 06:47:14     2010.10.08 22:50  EA2  GBPUSD,M5: zero divide
2010.10.10 06:47:14     2010.10.08 22:50  EA2  GBPUSD,M5: OUT
2010.10.10 06:47:14     2010.10.08 22:50  EA2  GBPUSD,M5: t1 = 1171025400 timecurrent = 1286578200
2010.10.10 06:47:14     2010.10.08 22:45  EA2  GBPUSD,M5: zero divide
2010.10.10 06:47:14     2010.10.08 22:45  EA2  GBPUSD,M5: OUT
2010.10.10 06:47:14     2010.10.08 22:45  EA2  GBPUSD,M5: t1 = 1171025400 timecurrent = 1286577900

I have been trying to pin down the source of the zero divide error but I just can't seem to get it right. I'd really appreciate a little help :)

Edit: Perhaps it has something to do with this statement?:

if(FileIsEnding(fh))  break; // file ended? - exit
 
http://code.google.com/p/mt4tradeduplicator/
 
There is no divide operator in the code you posted. There is no Print("OUT") either. The problem lies elsewhere.
 
kolier:
http://code.google.com/p/mt4tradeduplicator/

Thanks but this is not what I am trying to achieve. I need to copy trades in backtesting, not between terminals.
 
WHRoeder:
There is no divide operator in the code you posted. There is no Print("OUT") either. The problem lies elsewhere.
Print("OUT") was supposed to go after the FileClose(fh) statement at the very bottom. Is it possible to get a "zero divide" error without explicitly diving by zero? I'd post my whole code but I'm sure no one wants to weed through that.
 
You were right WHRoeder, the problem was in something completely unrelated. However now that its fixed I have another problem. Instead of reading a the next set of variables, my EA keeps re-reading the ifirst set. Is this because my FileOpen() statement is in my start function? I tried to switch it to the init function but for some reason it give me messages like this: "handle 1 does not exist in FileIsEnding." How can I read the next set?

Here is my updated (relevant) code:

  double price = 0, sl= 0, tp=0,lots=0.1;

      
  int LinesCNT=0,i,ticket,pos; double p; datetime t; string s_;
 
  int error = 0;
  int fh=FileOpen(FileNameForRead,FILE_CSV|FILE_READ,';'); // to test the file, it is necessary to pout it into tester\files\TL_DATA.txt

  if(fh<0) { Print("Error opening file \"" + FileNameForRead + "\"","ERROR", IDOK + MB_ICONERROR); return(1); }

  while(true)
  {
 
    string Operation=FileReadString(fh);
    
    if(FileIsEnding(fh))  break; // file ended? - exit

    // count the section coordinates

    string st1=FileReadString(fh);  
    datetime t1=StrToTime(st1);
    string MarkComent = "X";
  //  Print(st1," ",Operation);
//Print (MarkComent);
 
    //**********************************************************************************
    // 2) block opening orders as soon as the beginning of the pattern section is passed.
    //**********************************************************************************

    bool OrderNotPresent=true; // a sign showing that we haven't opened such an order yet
    
  if (error>0){ Print("GetLastError() = ",error); }

    if(t1<=TimeCurrent()) // time to open - make sure that this order is not opened 
    { Print("t1 = ",t1," timecurrent = ",TimeCurrent());
      for(i=0;i<OrdersTotal();i++)
      {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
        if(OrderComment()==MarkComent) { OrderNotPresent=false; break; } // åñòü òàêîé îðäåð
      }
      for(i=0;i<OrdersHistoryTotal() && OrderNotPresent;i++)
      {
        if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) continue;
        // â îðäåð â èñòîðèè äîïèñûâàåòñÿ â õâîñò íå÷òî âðîäå "[sl]" - åãî íóæíî îòðåçàòü!!
        pos = StringFind(OrderComment(),"[");
        string CurOrderComment = StringSubstr(OrderComment(),0,pos);
        if(CurOrderComment==MarkComent) { OrderNotPresent=false; break; } // åñòü òàêîé îðäåð
      }
      if(OrderNotPresent) // no such order - open it
      { 
        // open an order 
        if(Operation=="B") // Buy
        { 
          price = NormalizeDouble(Ask,Digits);
  
          sl = If(StopLoss > 0, NormalizeDouble(price - StopLoss*pt,Digits), 0);
          tp = If(TakeProfit > 0, NormalizeDouble(price + TakeProfit*pt,Digits), 0);
          lots = NormalizeDouble(Lots,LotDigits);
          ticket=OrderSend(Symbol(),OP_BUY,lots,price,3,sl,tp,MarkComent,111,0,Blue);
          OrderSelect(ticket,SELECT_BY_TICKET);
          
        } 
       else if (Operation=="S") // Sell
        {
          price = NormalizeDouble(Bid,Digits);
   
          sl = If(StopLoss > 0, NormalizeDouble(price + StopLoss*pt,Digits), 0);
          tp = If(TakeProfit > 0, NormalizeDouble(price - TakeProfit*pt,Digits), 0);
          lots = NormalizeDouble(Lots,LotDigits);      
          ticket=OrderSend(Symbol(),OP_SELL,lots,price,3,sl,tp,MarkComent,111,0,Red);
          OrderSelect(ticket,SELECT_BY_TICKET);
        }
      }
    }
   
  
  FileClose(fh);


Reason: