File Write txt

 

Hi 

I have the following function in the Expert, which begins to write the values of some indicators, when I open an order, but the problem is that while the order is open write line by line until the order is closed,

I am trying to use the OrderOpenTime () = Time Current, with the intention of Write only the values of the indicators " juts at the moment the order is taken " , but same thing write while the order is open ,
so I would like you to help me, I am new using the mql4
Thank you.

if(OrdersTotal()>-1)

 {
   

    int handle=FileOpen("Line_by_Line_Test.txt",FILE_TXT |FILE_WRITE|FILE_READ,";");
    if(handle<0)
    {
      Alert("An error while opening the file.");
      

      }
     else

    FileSeek(handle, 0, SEEK_END);
    FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket(),HoraActual);
    FileClose(handle);

and this is with OrderOpentime 

 datetime HoraActual= TimeCurrent();
 long HoraOpenTrade;
   double MACD_1=iMACD(NULL,0,8,34,1,PRICE_CLOSE,MODE_MAIN,0);

   double MA1= iMA(NULL,0,MA_Fast,0,MODE_EMA,PRICE_CLOSE,0);

   if( OrderSelect (0,SELECT_BY_POS,MODE_TRADES))
    {
      HoraOpenTrade=OrderOpenTime();
      }

  if(OrdersTotal()>0)
  {
  HoraOpenTrade=HoraActual;
  }
    if(HoraActual)
    {

   {
  
    int handle=FileOpen("Line_by_Line_Test.txt",FILE_TXT |FILE_WRITE|FILE_READ,";");
    if(handle<0)
    {
      Alert("An error while opening the file.");    
      }
      else

       FileSeek(handle, 0, SEEK_END);
          FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket());
             FileClose(handle);

OK I don't know what is wrong , but I need your help 

Thanks 

 
Some one with any opinion?
 
I suggest to use the code styler to indent your code (Ctrl-,) and then balance out your braces { } and complete the code (or show more of it in case you removed some parts.)
 

Why don't you just write to the file after the order is opened?


if(OrdersTotal()>-1)

This means that the code will be executed when there are no orders open.


You really should tidy up your code as it is rather messy and can make it difficult to spot mistakes.

Using the styler makes it easier to read

   datetime HoraActual= TimeCurrent();
   long HoraOpenTrade;
   double MACD_1=iMACD(NULL,0,8,34,1,PRICE_CLOSE,MODE_MAIN,0);
   double MA1= iMA(NULL,0,MA_Fast,0,MODE_EMA,PRICE_CLOSE,0);

   if( OrderSelect (0,SELECT_BY_POS,MODE_TRADES))
      {
      HoraOpenTrade=OrderOpenTime();
      }
   if(OrdersTotal()>0)
      {
      HoraOpenTrade=HoraActual;
      }
   if(HoraActual)
      {
         {
         int handle=FileOpen("Line_by_Line_Test.txt",FILE_TXT |FILE_WRITE|FILE_READ,";");
         if(handle<0)
            {
            Alert("An error while opening the file.");
            }
         else
            FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket());
         FileClose(handle);
         }
      }

You can spot the unnecessary {}

and also see that you are missing some

         else
            FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket());
         FileClose(handle);

should be

         else
            {
            FileSeek(handle, 0, SEEK_END);
            FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket());
            FileClose(handle);
            }
 

Ok

Thanks for The Braces 

Also I fix the Orders Total the function write correctly but , the problem is I Just I want write one line At the moment tha the trade is open ,

the function write till the trade is closed , 3 or 4 line by seconds x 3 or minutes to many lines , I will attach a TXT file 

 How can I do to store only one line from the value on the TXT every time I open a Trade , I have another question , which is the different between using a newCandle  Function  plus Condition Entry  on the EA or just  OnTick  and Condition Entry 

ok Thanks 

Files:
 
if(OrdersTotal()>0)

 {
   

    int handle=FileOpen("Line_by_Line_Test.txt",FILE_TXT |FILE_WRITE|FILE_READ,";");
    if(handle<0)
    {
      Alert("An error while opening the file.");
      

      }
     else
    {
    FileSeek(handle, 0, SEEK_END);
    FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket(),HoraActual);
    FileClose(handle);
    }
  }
  }
 
You haven't answered my question.
Keith Watford:

Why don't you just write to the file after the order is opened?

 
Keith Watford:
You haven't answered my question.

Hi Keith 

I tried this 

 I think if OrdersTotal >0 There are a position open , because I don't use pending order 

But I tried this and Create the File but not write  nothing 

 
 int handle=FileOpen("Line_by_Line_Test.txt",FILE_TXT |FILE_WRITE|FILE_READ,";");
    if(handle<0)
    {
      Alert("An error while opening the file.");
      

      }

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

 OrderSelect (0,SELECT_BY_POS,MODE_TRADES);

 if(OrdersTotal()>0)

 {
     
    FileSeek(handle, 0, SEEK_END);
    FileWrite(handle, MACD_1,OrderType(),MA1,OrderTicket(),HoraActual);
    FileClose(handle);
    
  }
  }
 
Sanbach21:

You are still not tidying up your code.

Are you saying that the trades are being placed manually or by another EA ?

for(int i=OrdersHistoryTotal()-1; i>=0; i--)

 OrderSelect (0,SELECT_BY_POS,MODE_TRADES);

This does not make any sense!

 
Keith Watford:

You are still not tidying up your code.

Are you saying that the trades are being placed manually or by another EA ?

This does not make any sense!

    
for(int i=OrdersHistoryTotal()-1; i>=0; i--)

 OrderSelect (0,SELECT_BY_POS,MODE_TRADES);
 double MA1= iMA(NULL,0,MA_Fast,0,MODE_EMA,PRICE_CLOSE,0);
   
   
     int handle=FileOpen("Line_by_Line_Test.txt",FILE_TXT |FILE_WRITE|FILE_READ,"\t"); 
  if(handle<0) return; 
  // write header 
  FileWrite(handle,"#","MACD","open time","STOCHASTIC","MA"); 
  int total=OrdersTotal(); 
  // write open orders 
  for(int pos=0;pos<total;pos++) 
    { 
     if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
     FileSeek(handle, 0, SEEK_END);
     FileWrite(handle,OrderTicket(),MACD_1,OrderOpenTime(),K_Line,MA1); 
    } 
  FileClose(handle);
}

Ok, Now I tried this .  I leave the TXT file Attach !

The problem is that write too many line while the order is open,  trade by trade .

I need write only one line per each trade at the moment that the order is open.  

Files:
Reason: