How to get highest/lowest time & write it into csv file?

 

Hi,

I want to collect data for daily chart high/low occurring time

Say, Last day high occurred at 15:15 in 15TF chart. So i want to open a csv file, where it will write the high, low, high time, low time in different column.

I coded this one taking help from forum. But it shows error.

//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property version   "1.00"


int start()
{
int File;
double lowest;
double highest;
datetime lowtime;
datetime hightime;

	lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];
	highest = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,1,0)];

	lowtime = Time[lowest];
	hightime = Time[highest];

	File=FileOpen ("dcoll.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
 	if(File>0)
  	 {
 		 FileSeek(File, 0, SEEK_END);
   		 FileWrite(File, Symbol(),lowest,highest,lowtime,hightime);
    		 FileClose(File);
  	 }
     }

Its showing error "highest", "lowest" integer expected.

When i changed it from double to integer, then its creating another problem. Its does creating csv file, but say for eurusd high/low always 1 (as integar) & all the other parameter in the file shows in same column.

Help please?

Thank you

 
        lowtime = Time[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];
        hightime = Time[iHighest(NULL,PERIOD_D1,MODE_HIGH,1,0)];
 
qjol:
lowtime = Time[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)]; // Same as Time[0]
cashcube: But it shows error.
  1. Shows error 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.
    1. iLowest returns the index of the lowest bar. What is the lowest bar number (on the D1 chart or any chart) from zero for 1 bar(s?) Answer is always zero.
    2. You take the D1 index (zero) and apply it to the current chart (Low[]) - bogus unless you're on the D1.
            lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];
            highest = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,1,0)];
    
            lowtime = Time[lowest];
            hightime = Time[highest];
    
    
    Don't mix D1 indexes with chart indexes. Lowest is a price, you can't use that in Time[]
    There are several ways - see How to get a time of High/Low of previous day? - MQL4 forum
    datetime  bod = iTime(NULL,PERIOD_D1, 0);
    int      iBod = iBarShift(NULL,0, bod), // Chart index
             iLL  =  iLowest(NULL,0, MODE_LOW,  iBod+1, 0),
             iHH  = iHighest(NULL,0, MODE_HIGH, iBod+1, 0);
    
            lowest  =  Low[iLL];  lowtime = Time[iLL];
            highest = High[iHH]; hightime = Time[iHH];
    

 

WHRoeder:

qjol:
lowtime = Time[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)]; // Same as Time[0]


so ? this is what he wants

see his code above:

lowest = Low[iLowest(NULL,PERIOD_D1,MODE_LOW,1,0)];
highest = High[iHighest(NULL,PERIOD_D1,MODE_HIGH,1,0)];
 
qjol: so ? this is what he wants
Time[0] is not what he wanted:
cashcube: I want to collect data for daily chart high/low occurring time
 
WHRoeder:
  1. Shows error 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.
    1. iLowest returns the index of the lowest bar. What is the lowest bar number (on the D1 chart or any chart) from zero for 1 bar(s?) Answer is always zero.
    2. You take the D1 index (zero) and apply it to the current chart (Low[]) - bogus unless you're on the D1.
    Don't mix D1 indexes with chart indexes. Lowest is a price, you can't use that in Time[]
    There are several ways - see How to get a time of High/Low of previous day? - MQL4 forum


Using your coding i re coded again. Here is the result,

int start()
{
int File;
double lowest;
double highest;
datetime lowtime;
datetime hightime;

datetime  bod = iTime(NULL,PERIOD_D1, 0);
int      iBod = iBarShift(NULL,0, bod), // Chart index
         iLL  =  iLowest(NULL,0, MODE_LOW,  iBod+1, 0),
         iHH  = iHighest(NULL,0, MODE_HIGH, iBod+1, 0);

        lowest  =  Low[iLL];  lowtime = Time[iLL];
        highest = High[iHH]; hightime = Time[iHH];

File=FileOpen ("dcoll.csv", FILE_CSV|FILE_WRITE|FILE_READ, ';');
 if(File>0)
   {
  FileSeek(File, 0, SEEK_END);
    FileWrite(File, Symbol(),lowest,highest,lowtime,hightime);
    FileClose(File);
   }
   
}

It does creates csv file. But all data in same column (which i stated earlier)

It does remove the error & shows decimal points.

In excel colmun its shows like this :

SILVER;19.58;19.58;1398038400;1398038400

How lowest & highest can be same? Why time shows in that way?

Thanks

 
cashcube:



How lowest & highest can be same? Why time shows in that way?

Thanks

Because it is the first tick of the day?

Use TimeToStr to display the date

 

if you want them in separate columns, you could change the ";" to ","

File=FileOpen ("dcoll.csv", FILE_CSV|FILE_WRITE|FILE_READ, ',');
 
GumRai:

if you want them in separate columns, you could change the ";" to ","


GumRai:

Because it is the first tick of the day?

Use TimeToStr to display the date


That's really a great help!

I changed it, now its showing in different columns & proper time format.

Now about same high/low i changed it, for 15M time there are 96 bars in a day, so changed the no of bars, to get highest & lowest price & time.

But problem is, there are many same data of high/low constantly printing on csv. May be its checking bar by bar or every tick secuence, so printing so many for a single day. Is there any way to limit it? Like printing only once?

int start()
{
int File;
double lowest;
double highest;
datetime lowtime;
datetime hightime;
int iLL;
int iHH;
string T1;
string T2;

         iLL  =  iLowest(NULL,0, MODE_LOW,  96, 0);
         iHH  = iHighest(NULL,0, MODE_HIGH, 96, 0);
         lowest  =  Low[iLL];  lowtime = Time[iLL];
        highest = High[iHH]; hightime = Time[iHH];
        T1 = TimeToStr(lowtime,TIME_DATE|TIME_MINUTES);
        T2 = TimeToStr(hightime,TIME_DATE|TIME_MINUTES);


File=FileOpen ("dcoll.csv", FILE_CSV|FILE_WRITE|FILE_READ, ',');
 if(File>0)
   {
  FileSeek(File, 0, SEEK_END);
    FileWrite(File, Symbol(),lowest,highest,T1,T2);
    FileClose(File);
   }
     }
 
cashcube:


That's really a great help!

I changed it, now its showing in different columns & proper time format.

Now about same high/low i changed it, for 15M time there are 96 bars in a day, so changed the no of bars, to get highest & lowest price & time.

But problem is, there are many same data of high/low constantly printing on csv. May be its checking bar by bar or every tick secuence, so printing so many for a single day. Is there any way to limit it? Like printing only once?


As start is called every tick, you will get the same data repeating and as you are simply using shift 96, you will just be getting the result from the last 96 bars, but I think that you are looking for the highest and lowest bars and time in a 1 day period?

Never assume that there will be 96 bars in a day.

You can do this once per day at the same time and check the previous days data

You could read the date of the last entry in the file, and compare to the previous day's date, if different, then write the new data to the file.

You may also like to save the date as a Globalscope variable and while yesterday's date is the same, don't access the file until you need to.

 
GumRai:


As start is called every tick, you will get the same data repeating and as you are simply using shift 96, you will just be getting the result from the last 96 bars, but I think that you are looking for the highest and lowest bars and time in a 1 day period?

Never assume that there will be 96 bars in a day.

You can do this once per day at the same time and check the previous days data

You could read the date of the last entry in the file, and compare to the previous day's date, if different, then write the new data to the file.

You may also like to save the date as a Globalscope variable and while yesterday's date is the same, don't access the file until you need to.


Yes i am looking for the highest & lowest price & time in one day period.

Adding 96 bars sometimes do show daily high/low correctly, but sometimes its not. So i updated the code to get exact high/low during the daily span & it works at some point. Sometimes it shows only 1.27 like digit for high/low, i don't know why.

datetime dt = TimeCurrent();
      
      datetime TT =StrToTime(TimeToStr(dt, TIME_DATE));    
      int En=iBarShift(NULL, 0, TT);   
   

         iLL  =  iLowest(NULL,0, MODE_LOW,96,En );
         iHH  = iHighest(NULL,0, MODE_HIGH,96,En );
         lowest  =  Low[iLL];  lowtime = Time[iLL];
        highest = High[iHH]; hightime = Time[iHH];
        T1 = TimeToStr(lowtime,TIME_DATE|TIME_MINUTES);
        T2 = TimeToStr(hightime,TIME_DATE|TIME_MINUTES);
     

About reading last entry in the file & comparing, can you please share the codes ?

And for the duplicate values, i use excel to remove duplicate values.

So, at this point, problem solved somehow. ( Though Tester takes longer time & generate big sized files. )

Thank you