Loop to find daily price range of last x day

To add comments, please log in or register
Andrea Bertini
72
Andrea Bertini  

Hi, how can I loop through days to find minvalue and max value, it would be interesting to write to file the following informations:

<Day number>,<session>,<MinValInRange>,<MaxValInRange>,<NumPipsInRange>,<VarPercent>

Variables:

string Fname = Symbol()+".csv";//filename with current symbol

The first 2 functions could be:

double MaxVal(datetime t1, datetime t2)
 {
   string   s           = Symbol();
   int      p           = PERIOD_H1;
   int iHiH1;
   double HighestV;
   int      t1_shift    = iBarShift(s,p,t1);
   int      t2_shift    = iBarShift(s,p,t2);
   int      bar_count   = t1_shift-t2_shift;
   iHiH1    = iHighest( s, p, MODE_HIGH,bar_count,t2_shift);
   HighestV = iHigh(s, p, iHiH1);
   return HighestV;
 }

and

double MinVal(datetime t1, datetime t2)
 {
   string   s           = Symbol();
   int      p           = PERIOD_H1;
   int iLoH1;
   double LowestV;
   int      t1_shift    = iBarShift(s,p,t1);
   int      t2_shift    = iBarShift(s,p,t2);
   int      bar_count   = t1_shift-t2_shift;
   iLoH1    = iLowest(s, p, MODE_LOW,bar_count, t2_shift);
   LowestV = iLow(s, p, iLoH1);
   return LowestV;
 }
Vitalii Ananev
15884
Vitalii Ananev  

Use loop. Nothing else comes to mind.

DateTime TMStart;
DateTime TMEnd;
int DayCount   = 5;
int StartIndex = 0;
int EndIndex = 1000;

while (StartIndex<EndIndex)
{
        TMStart = Time[StartIndex];
        TMEnd   = TMStart + DayCount*24*3600;
        MaxVal(TMStart,TMEnd);
        StartIndex+=DayCount;
}

Some way to loop.

Andrea Bertini
72
Andrea Bertini  
Vitalii Ananev:

Use loop. Nothing else comes to mind.

Some way to loop.

Time[0] is a (last) candle time, if loops starts from the beginning the first date  should be Time[TMEnd]?

Vitalii Ananev
15884
Vitalii Ananev  
andy60:

Time[0] is a (last) candle time, if loops starts from the beginning the first date should be Time[TMEnd]?

Time[0] - this is a current candle not yet closed. If you build calculations from the past to the future. Then you need to change the algorithm a little. In my example, the calculation goes from the current candle to the past.

DateTime TMStart;
DateTime TMEnd;
int DayCount   = 5;
int StartIndex = 1000;
int EndIndex = 0;

while (StartIndex>EndIndex)
{
        TMStart = Time[StartIndex];
        TMEnd   = TMStart + DayCount*24*3600;//Date forward to future
        MaxVal(TMStart,TMEnd);
        StartIndex-=DayCount;
}

Like this.

In the previous example I was a little wrong I should have written like this:

DateTime TMStart;
DateTime TMEnd;
int DayCount   = 5;
int StartIndex = 0;
int EndIndex = 1000;

while (StartIndex<EndIndex)
{
        TMStart = Time[StartIndex];
        TMEnd   = TMStart - DayCount*24*3600;//Date back to the past
        MaxVal(TMStart,TMEnd);
        StartIndex+=DayCount;
}

Andrea Bertini
72
Andrea Bertini  
Vitalii Ananev:

Time[0] - this is a current candle not yet closed. If you build calculations from the past to the future. Then you need to change the algorithm a little. In my example, the calculation goes from the current candle to the past.

Like this.

Thx ! I will try the first solution, i had read bad first expression, it seems perfect.


StartIndex<EndIndex
Vitalii Ananev
15884
Vitalii Ananev  
andy60:

Thx ! I will try the first solution, i had read bad first expression, it seems perfect.


Give it a try. Maybe someone will offer an even better option. In any case, you must use a loop to calculate MaxVal() and MinVal() each time passing a new date range in the parameters.

To add comments, please log in or register