(Please help) How to record Open Order information and export to csv?

 
Hi, everyone. I am new to MQL and just wrote my first EA as shown below(just a tester).
Since I want to record all my open trades information( Open Trades ONLY), I tried using FileWrite Function to record it and export it to CSV.
The problem I am having is that, when I run it through Strategy test, it infinitely writes the same OpenOrder and it results as shown in the below table.
I want my EA to write Open order information only once, and move on to the next open order and write its information and so on..
Can any of you please teach me how to do it?

tons of thanks in advance,
Jay 

(sample image of the csv export file)
OrderOpen PriceOpen

Time

SymbolLots
01.057522017.03.01 00:00:00EURUSD0.1
01.057522017.03.01 00:00:00EURUSD0.1
01.057522017.03.01 00:00:00EURUSD0.1
(My tester EA) #property

copyright "Copyright 2017, MetaQuotes Software Corp." #property link      "https://www.mql5.com" #property version   "1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function                                   | //+------------------------------------------------------------------+ input string   FileName="Optimization.csv"; int OnInit()   {    int handle=FileOpen(FileName,FILE_CSV|FILE_READ|FILE_WRITE,",");    FileWrite(handle,"Order","Open Price","Open Time","Symbol","Lots");    FileClose(handle);    Print("File Header Written Correctly");    return(INIT_SUCCEEDED);   }    void OnDeinit(const int reason)   {   } int start()   {        if( OrdersTotal() == 0 )          {          if( Signal()==2) Open_Order( OP_BUY,  0.1 );          if( Signal()==1) Open_Order( OP_SELL, 0.1 );            }               for(int i=0;i<OrdersTotal();i++)         {//int e=0;          if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==TRUE)               {                int handle=FileOpen(FileName,FILE_CSV|FILE_READ|FILE_WRITE,",");              FileSeek(handle,0,SEEK_END);                                FileWrite(handle,OrderType(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots());              FileClose(handle);             }           }       return(0);   } int Open_Order( int tip, double llots )   {   int ticket = -1;    double max_lot; max_lot = MarketInfo(Symbol(), MODE_MAXLOT);       if( llots > max_lot ) llots = NormalizeDouble( (max_lot-1), Digits);   if( OrdersTotal() == 0 )     {      if( tip == 0 )         {          while( ticket == -1)            {            ticket  = OrderSend(Symbol(),OP_BUY, llots,Ask,3,Ask-300*Point,Ask+300*Point, "Long 1",1111,0, PaleGreen );            double lot = llots;            if( ticket > -1 ) break;            }           }            if( tip == 1 )      {      ticket = -1;       while( ticket == -1)          {          ticket  =OrderSend(Symbol(),OP_SELL,llots,Bid,3,Bid+300*Point,Bid-300*Point,"Short 1",2222,0, Red);          double lot = llots;          if( ticket > -1 ) break;          }       }   } //----------- return(0); }   int Signal()    {    int mode;       double Stochastic = iStochastic(NULL,NULL,3,5,5,0,0,0,0);       if(Stochastic <50)       //Exampl Long Rule       {       mode=2;       //Long opportunity       }       else if( Stochastic >=50)       //Short Rule       {       mode=1;       //Short opportunity       }       else       {       mode=0;       //Nothing       }       return(mode);    }
 
kb985504:
(My tester EA)

#property 
  1. Don't add text inside quoted or SRC blocks, put it outside. MQL4 Forum editor problem - MQL4 forum
  2. I want my EA to write Open order information only once
    Remember the last ticket number written and ignore that and earlier.


 
whroeder1:
  1. Don't add text inside quoted or SRC blocks, put it outside. MQL4 Forum editor problem - MQL4 forum
  2. Remember the last ticket number written and ignore that and earlier.


Dear whroeder1,


 Thanks for your prompt comment and your advice on not adding text inside SRC blocks. 

 I need a little more elaboration on what you mean by "Remember the last ticket number written and ignore that and earlier."

 The below coding is what I came up after reading your comment but it produces the same result as before. Which part am I getting wrong? 

       for(int i=0;i<OrdersTotal();i++)
        {int e=0;
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==TRUE)   
            {if(OrderTicket()!=e) 
             {int handle=FileOpen(FileName,FILE_CSV|FILE_READ|FILE_WRITE,",");
              FileSeek(handle,0,SEEK_END);                   
              FileWrite(handle,OrderType(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots());         
              FileClose(handle);
              e= OrderTicket();  
             }
            }   
        }
 
kb985504: . Which part am I getting wrong? 
  1. You loose the value of e every loop.
  2. You only don''t reject earlier tickets only e.
 
whroeder1:
  1. You loose the value of e every loop.
  2. You only don''t reject earlier tickets only e.

I got it now! 
Thank you very much for your advice! 

 

Hello! I am trying to make the same thing as you where it outputs all live data to a CSV file with my open trades without seeking file end. I have edited your code to just this but it only exports the last position and not all of the open positions. How would I loop for  each open position? Thanks for the help :)


I know you said the way around it is to reject earlier tickets with e and then ignore the previous tickets but I cant figure it out, I just want to export data into the csv live of all my open positions...


string FileName="Optimization.csv";
int start()
  { 
       for(int i=0;i<OrdersTotal();i++)
        {int e=0;
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==TRUE)   
            {if(OrderTicket()!=e) 
             {int handle=FileOpen(FileName,FILE_CSV|FILE_READ|FILE_WRITE,",");                  
              FileWrite(handle,OrderType(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots(),OrderProfit());         
              FileClose(handle);
              e= OrderTicket();  
             }
            }   
        }    
  return(0);
  } 

 
You are overwriting the first line of the file. Append to the file.

          Write to File Syntax - MQL4 programming forum (2019)

Reason: