Download MetaTrader 5

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

To add comments, please log in or register
kb985504
16
kb985504  
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);    }
whroeder1
15902
whroeder1  
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.


kb985504
16
kb985504  
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();  
             }
            }   
        }
whroeder1
15902
whroeder1  
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.
kb985504
16
kb985504  
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! 

To add comments, please log in or register