Help with the next code to export data

Back to topics list To post a new topic, please log in or register
avatar
29
castann86 2015.06.15 21:39
 

Hi all,

 I have doubts whether this code is correct. 

 The code is to export data in real time. I had still not successful because I think that the code is not correct. But still I could not fix.

Can anyone explain why the code is not correct?

 

#property copyright 
#property link      

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_width1 2
#property indicator_color1 Tomato
double ExtMap[];
;

extern int       EURUSD = 200;


int init()
{
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMap);
   return(0);
}

int start()
{
   static int rea = 0;
   static int old_bars = 0; 
   static int rea1 = 0;
   
   
   if (old_bars != iBars("EURUSD",PERIOD_D1))
   {
      delete_result();
      clear_graf();
      write_data(); // 
   }
   
   int handle_read = FileOpen("EURUSD"+"_D1"+"_result.csv",FILE_CSV|FILE_READ,';');
   if(handle_read >= 0)
   { 
      Comment("EURUSD"+"_D1" );
      read_n_draw(handle_read);
      delete_result();
      FileClose(handle_read);
      rea=0;
   }
   else 
   {
      rea++;
      Comment("descargar: "+"EURUSD_D1"+"#"+GetLastError()+"#rea"+rea);
      //FileClose(handle_read);
   }
   
   old_bars = iBars("EURUSD",PERIOD_D1);  // 
   
   
   //////////////////////////////////
  
//+------------------------------------------------------------------+
void read_n_draw(int handle_read)
{
   int i=0;
   while ( !FileIsEnding(handle_read) )
   {
      ExtMap[i] = FileReadNumber(handle_read);
      i++;     
      Comment(ExtMap[0]); 
   }
   ExtMap[i-1] = EMPTY_VALUE;
   
}


/////////////////////////////////////////////////////


void clear_graf()
{
   ArrayInitialize( ExtMap, EMPTY_VALUE);
   return(0);
}


///////////////////////////////////////////////////////7

void delete_result()
{
   
   string filename = "EURUSD"+ "_D1" + "_result.csv";
   FileDelete(filename);
   return(0);
} 
 

 /////////////////////////////////////////////////////////////////////
 
 
void write_data()
{
  int handle;
  string PAIR = "EURUSD";
  string _MN1;
  string filename =  PAIR + "_D1"+"_"+EURUSD  + ".txt"; // 
  handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';');
  if(handle >= 0)
  {
    Print(" #", GetLastError());
    
    FileClose(handle);
  }
  
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // 
  int i;
  for (i=EURUSD-1; i>=0; i--)
  {
  
  double O,H,L,C, T, V;
  
         O = iOpen (PAIR, PERIOD_D1, i);
         H = iHigh (PAIR, PERIOD_D1, i);      
         L = iLow  (PAIR, PERIOD_D1, i);      
         C = iClose(PAIR, PERIOD_D1, i); 
         T = iTime (PAIR, PERIOD_D1, i); 
         V = iVolume (PAIR, PERIOD_D1, i);
                     
        
         
       
         
    FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS),
                      H, L, C, O, V);
  }
  FileClose(handle);
  return(0);
}



////////////////////////////////////////////////////////////////////

 He had a few days ago said the following:

handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';');
  if(handle >= 0)
  {
    Print(" #", GetLastError());
    return(0);

" You try open the file. If it is successful, you return, loosing the handle, keeping the file open, and nothing works after that."

 

 But I'm still learning and I could not do the correct code.

Thank you very much in advance! 

avatar
29
castann86 2015.06.16 14:58 #
 

If handle >=0 (succesful ) then 

fileWrite(handle, "DATE"......

 else

fileclose(handle)

??

 

Si handle is succesful, then we write in file, but  if handle is not correct, we close the file

 

It is correct?

 

Thank yoy very much  for your patience

 

avatar
12983
WHRoeder 2015.06.16 20:41 #
 
What does FileOpen return when successful and unsuccessful? what do you do?

Answer the question.

If it is successful the function returns the file handle, and if it is unsuccessfull returns invalid handle:

Answer the question. What is a good file handle? what is an invalid handle. What do you do?
avatar
29
castann86 2015.06.16 20:56 #
 
WHRoeder:
What does FileOpen return when successful and unsuccessful? what do you do?
Answer the question.

If it is successful the function returns the file handle, and if it is unsuccessfull returns invalid handle:

 

void write_data()
{
  int handle;
  string PAIR = "EURUSD";
  string _MN1;
  string filename =  PAIR + "_D1"+"_"+EURUSD  + ".txt"; // 
  handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';');
  if(handle != INVALID_HANDLE)
  {
   
  
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // 
  int i;
  for (i=EURUSD-1; i>=0; i--)
  {
  
  double O,H,L,C, T, V;
  
         O = iOpen (PAIR, PERIOD_D1, i);
         H = iHigh (PAIR, PERIOD_D1, i);      
         L = iLow  (PAIR, PERIOD_D1, i);      
         C = iClose(PAIR, PERIOD_D1, i); 
         T = iTime (PAIR, PERIOD_D1, i); 
         V = iVolume (PAIR, PERIOD_D1, i);
                     
        
         
       
         
    FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS),
                      H, L, C, O, V);
  }
  FileClose(handle);
  }
  return(0);
}

 Is it correct?

 

Thank you very much 

avatar
12983
WHRoeder 2015.06.17 02:54 #
 
castann86:

If it is successful the function returns the file handle, and if it is unsuccessfull returns invalid handle:

 Is it correct?

  1. Now you've change the code again, you never answered the question with the original code.
    What does FileOpen return when successful and unsuccessful? what do you do?

    You still don't understand what was wrong with the original code.


  2. If it returns an invalid handle, how can you close it (after the if?)
  3. Check your return codes (FileWrite and FileClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
avatar
29
castann86 2015.06.20 12:00 #
 

Hi,

 I'm trying to get export data, but building the code correctly.


If FileOpen is succesfull, then FileWrite.

If FileOpen is unsuccesfull, then "error"


I read about functions "return" but still do not understand.

In theory, "return" to "avoid" can be omitted.


The following code is on which I am working, although it is not correct because it does not write correctly the values in the file:

#property copyright "Copyright © 2007, dkoloskov"
#property link     "dkoloskov@rambler.ru" 

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_width1 2
#property indicator_color1 Tomato
double ExtMap[];
;

extern int       EURUSD = 200;


int init()
{
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMap);
   return(0);
}

int start()
{
   static int rea = 0;
   static int old_bars = 0; 
   static int rea1 = 0;
   
   
   if (old_bars != iBars("EURUSD",PERIOD_D1))
   {
      delete_result();
      clear_graf();
      write_data(); // 
   }
   
   int handle_read = FileOpen("EURUSD"+"_D1"+"_result.csv",FILE_CSV|FILE_READ,';');
   if(handle_read >= 0)
   { 
      Comment("EURUSD"+"_D1" );
      read_n_draw(handle_read);
      delete_result();
      FileClose(handle_read);
      rea=0;
   }
   else 
   {
      rea++;
      Comment("descargar: "+"EURUSD_D1"+"#"+GetLastError()+"#rea"+rea);
      //FileClose(handle_read);
   }
   
   old_bars = iBars("EURUSD",PERIOD_D1);  // 
   
    return(0);
   
}
   
   //////////////////////////////////
  
//+------------------------------------------------------------------+
void read_n_draw(int handle_read)
{
   int i=0;
   while ( !FileIsEnding(handle_read) )
   {
      ExtMap[i] = FileReadNumber(handle_read);
      i++;     
      Comment(ExtMap[0]); 
   }
   ExtMap[i-1] = EMPTY_VALUE;
   
}


/////////////////////////////////////////////////////


void clear_graf()
{
   ArrayInitialize( ExtMap, EMPTY_VALUE);
   
}


///////////////////////////////////////////////////////7

void delete_result()
{
   
   string filename = "EURUSD"+ "_D1" + "_result.csv";
   FileDelete(filename);
   
} 
 

 /////////////////////////////////////////////////////////////////////
 
 
void write_data()
{
  int handle;
  string PAIR = "EURUSD";
  
  string filename =  PAIR + "_D1" +"_"+EURUSD  + ".txt"; // 
  handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';');
  if(handle != INVALID_HANDLE)
  {
  
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // 
  int i;
  for (i=EURUSD-1; i>=0; i--)
     {
  
         double O,H,L,C, T, V;
  
         O = iOpen (PAIR, PERIOD_D1, i);
         H = iHigh (PAIR, PERIOD_D1, i);      
         L = iLow  (PAIR, PERIOD_D1, i);      
         C = iClose(PAIR, PERIOD_D1, i); 
         T = iTime (PAIR, PERIOD_D1, i); 
         V = iVolume (PAIR, PERIOD_D1, i);
                     
        
         
       
         
    FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS),
                      H, L, C, O, V);
                      
    FileClose(handle);
   
                      
     }
  
  }
  
  else
  {
   Print(" error", GetLastError());
   
  }
  

}

 Thank you very much

avatar
29
castann86 2015.06.21 10:59 #
 

Any comment?

Thank you very much 

avatar
12983
WHRoeder 2015.06.21 19:21 #
 

castann86:

I'm trying to get export data, but building the code correctly.

The following code is on which I am working, although it is not correct because it does not write correctly the values in the file:

If FileOpen is succesfull, then FileWrite.

Any comment?

  1. I didn't ask you what you were trying to do, I asked over seven (7) different questions, which you have refused to answer.
  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here. You never replied what is does and why it's wrong.
  3. One of the question I asked was "what does it return when successful" You've refused to answer, at least three times. If you don't know what it returns you can't code "successful, then write." you still don't know why your original code was wrong.
  4. You don't want to answer questions, you don't want to learn. Either learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem, but since you don't want to try to learn, I'm done with you.
avatar
29
castann86 2015.06.22 18:30 #
 

I think you are not reading it right or I'm not expressing clearly enough.

 When I say that the successful return = writing on file. I think it is clear enough what I want.


I'm learning to encode, while I'm learning many other things, so you can not question what I'm willing to learn or not, just because you know a little more than others does not mean you're better than others in other ways.

I do not need your help, much less. Whenever there is a solution, I'll find her. To learn how to "codify" must see code. That's what I intend.


thank you very much

Back to topics list  

To add comments, please log in or register