[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 364

 
Alsu, the 'n' variable in your code is not clear. If it's the number of fields, how can you find it if it's not known beforehand?
 
sibemol >> :
Mr. alsu, I don't understand the 'n' variable in your code. If it's the number of fields, how can we find it if we don't know it beforehand?

But you are going to write something to the file, aren't you? Or do you mean that the CSV structure is not known beforehand? Then what's the sense to add in it if we know neither the number of fields, nor their type (string, integer, floating, time), nor the semantic load?

 
alsu >> :

But you are going to write something to the file, aren't you? Or do you mean that the CSV structure is not known beforehand? Then what is the point of adding to it, if we know neither the number of fields, nor their type (string, integer, floating, time), nor their meaning?

My task is simple - to write to csv file quotes of current instrument for transferring to other application. Everything is successfully written if data collection loop for(int i=Bars-1; i>=0; i--). But if we optimize the code using:

int counted_bars=IndicatorCounted(), limit;
if (counted_bars==0) limit=Bars-1;
if (counted_bars>0) limit=Bars-counted_bars-1;

with a loop for(int i=limit; i>=0; i--),

and instead of int f=FileOpen(Symbol()+""+Period()+".csv", FILE_CSV|FILE_WRITE, ",");

insert int f=FileOpen(Symbol()+""+Period()+".csv", FILE_CSV|FILE_READ|FILE_WRITE, ",")

then the zero bar is written at the beginning of the file. If you use FileSeek(), the lines are added with each tick, while we need the last file line to be overwritten at the current bar.

I hope everything is clear now. Just in case, here is full non-optimized working code:

//+------------------------------------------------------------------+
//|                                                  iHisoryBars.mq4 |
//|                                        Copyright © 2009, Sibemol |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Sibemol"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 White
//---- input parameters
double v1[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators 
   SetIndexStyle(0,DRAW_LINE);  
   SetIndexBuffer(0, v1);      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {  
  int counted_bars=IndicatorCounted(), limit;
  if ( counted_bars==0)  limit=Bars-1;
  if ( counted_bars>0)   limit=Bars- counted_bars-1;    
  int f=FileOpen(Symbol()+" "+Period()+".csv", FILE_CSV| FILE_WRITE, ",");
  //int f=FileOpen(Symbol()+" "+Period()+".csv", FILE_CSV|FILE_READ|FILE_WRITE, ",");    
  FileWrite( f,
  "<ticker>,<per>,<date>,<time>,<open>,<high>,<low>,<close>,<volume>,<oi>");
  for(int i=Bars-1; i>=0; i--)
    {
    v1[ i]=Close[ i];    
    int _Year=TimeYear(Time[ i]),
        _Month=TimeMonth(Time[ i]),
        _Day=TimeDay(Time[ i]),
        _Hour=TimeHour(Time[ i]),
        _Minute=TimeMinute(Time[ i]);
        
    string Y, M, D, H, Mi, Date, _Time;
        
    if(_Month<10) M="0"+DoubleToStr(_Month, 0); else M=DoubleToStr(_Month, 0);
    if(_Day<10) D="0"+DoubleToStr(_Day, 0); else D=DoubleToStr(_Day, 0);
    if(_Hour<10) H="0"+DoubleToStr(_Hour, 0); else H=DoubleToStr(_Hour, 0);
    if(_Minute<10) Mi="0"+DoubleToStr(_Minute, 0); else Mi=DoubleToStr(_Minute, 0);
    Date=DoubleToStr(_Year, 0)+ M+ D;
    _Time= H+ Mi;
        
    FileWrite( f,     
    Symbol(), Period(),
    Date, _Time,     
    Open[ i], High[ i], Low[ i], Close[ i], Volume[ i], 0);
    
    }
    
    FileClose( f);
    
  return(0);
  }
//+------------------------------------------------------------------+
 
sibemol >> :

My task is simple - to write quotes of the current instrument into a csv file for transmission to another application. Everything is successfully written with data collection loop for(int i=Bars-1; i>=0; i--). But if we optimize the code using:

int counted_bars=IndicatorCounted(), limit;
if (counted_bars==0) limit=Bars-1;
if (counted_bars>0) limit=Bars-counted_bars-1;

with loop for(int i=limit; i>=0; i--),

and instead of int f=FileOpen(Symbol()+""+Period()+".csv", FILE_CSV|FILE_WRITE, ",");

insert int f=FileOpen(Symbol()+""+Period()+".csv", FILE_CSV|FILE_READ|FILE_WRITE, ",")

then the zero bar is written at the beginning of the file. If you use FileSeek(), the lines are added with each tick, while we need the last file line to be overwritten at the current bar.

I hope everything is clear now. Just in case, full unoptimized running code:


or is it not easier to remember the current position in the file at the beginning.

Change with the bar change.

 
Sorento >> :

or is it not easier to remember the current position in the file at the beginning.

Change with bar change.


If you don't mind, how would it look in code?

 

Can you tell me if there is a feature to disable all the scripts that are on other charts from the current script? Or better yet, turn off the terminal altogether.

The reason for this is that if one of the enabled scripts "notices" that a disaster is coming - it would stop itself and all the others from working. How can this be done if the above cases are still impossible to implement?

 
sibemol >> :

My task is simple - to write into csv file quotations of current symbol for sending it to another application. Everything is successfully written with data collection loop for(int i=Bars-1; i>=0; i--). But if we optimize the code using:

int counted_bars=IndicatorCounted(), limit;
if (counted_bars==0) limit=Bars-1;
if (counted_bars>0) limit=Bars-counted_bars-1;

with loop for(int i=limit; i>=0; i--),

and instead of int f=FileOpen(Symbol()+""+Period()+".csv", FILE_CSV|FILE_WRITE, ",");

insert int f=FileOpen(Symbol()+""+Period()+".csv", FILE_CSV|FILE_READ|FILE_WRITE, ",")

then the zero bar is written at the beginning of the file. If you use FileSeek(), the lines are added with each tick, while we need to overwrite the last file line on the current bar.

I hope everything is clear now. Just in case, full unoptimized working code:


I don't understand what the problem is.

Here is an excerpt from your text, read the fields:

FileWrite( f,     
    Symbol(), Period(),
    Date, _Time,     
    Open[ i], High[ i], Low[ i], Close[ i], Volume[ i], 0);

Symbol, period, date, time, OHLCV - a total of 9 fields in CSV. In my above listing they are all read as string, but it doesn't matter as it's not their value, but their number in the string that matters to us.

Substitute 9 for n and use it, the subroutine is universal.

At the same time, as Sorento has rightly said, you can just write a variable, which will remember pointer position before every record. Then before next record you can, if needed, move cursor to the beginning of last record and overwrite it. Note that you should move file opening to init() and closing to deinit(). The variable f should be declared globally.

Well, that is as much detail as possible. Consider it as homework:)))))))

 
Mathers >> :

Can you tell me if there is a feature to disable all the scripts that are on other charts from the current script? Or better yet, turn off the terminal altogether.

The reason for this is that if one of the enabled scripts "notices" that a disaster is coming - it would stop itself and all the others from working. How can this be done if above mentioned cases are impossible to implement?

I would set a flag in the form of a global variable for the script which could be read by other scripts and wash up on time.

 

alsu писал(а) >>

Well, that's about as detailed as you can get. Think of it as homework:)))))))

Thank you for your reply, I'll try it out and let you know what happens.

 
Reworked the VSIindicator.Wanted to get the volumes in plus and minus.The result is staggering.Where did what come from? Unclear mathematics.
Files:
Reason: