Download MetaTrader 5

check the highest and lowest price live

To add comments, please log in or register
m_shafiei2006
147
m_shafiei2006  

hi


i am new in mql4 an i need to make an EA that get the Max and Min price in a period of time and as it goes. for example now it is 12:00 o'clock, and i need to check the highest price between 14:00 and 16:00 and if we would be in 15:00 , comment the max price in every tick.


i write this, but it works only if the the period time is passed. please help. thanks.

extern            ENUM_TIMEFRAMES TimeFrame = PERIOD_M1;
extern datetime   inTime = D'01.01.2016 00:00:00',
                  outTime = D'01.01.2016 00:00:00';


int     inBar  = iBarShift(NULL,TimeFrame,inTime),
        outBar = iBarShift(NULL,TimeFrame,outTime-1),
        BarRange  = inBar - outBar + 1,
        MaxShift = iHighest(NULL,TimeFrame,MODE_HIGH,BarRange,outBar),
        MinShift = iLowest(NULL,TimeFrame, MODE_LOW,BarRange,outBar);



double  
         MaxPrice = iHigh(NULL,TimeFrame,MaxShift),
         MinPrice = iLow(NULL,TimeFrame,MinShift);   


void OnTick()
{

Comment(
"\n MaxPrice = ",DoubleToString(NormalizeDouble(MaxPrice,5),5),
"\n MinPrice = ",DoubleToString(NormalizeDouble(MinPrice,5),5),
)


}


I also make it as a function but it doesn't work.


extern            ENUM_TIMEFRAMES TimeFrame = PERIOD_M1;
extern datetime   inTime = D'01.01.2016 00:00:00',
                  outTime = D'01.01.2016 00:00:00';


double MaxPrice(int frame,datetime in,datetime out)
{
   int ShiftRange  = iBarShift(NULL,frame,in) - iBarShift(NULL,frame,out-1) + 1,
       ShiftMax = iHighest(NULL,frame,MODE_HIGH,BarRange,iBarShift(NULL,frame,out-1));
double Max = iHigh(NULL,frame,ShiftMax);
return (Max);
}

double MinPrice(int frame,datetime in,datetime out)
{         
int    ShiftRange  = iBarShift(NULL,frame,in) - iBarShift(NULL,frame,out-1) + 1,
       ShiftMin = iLowest(NULL,frame, MODE_LOW,BarRange,iBarShift(NULL,frame,out-1));
double Min = iLow(NULL,frame,ShiftMin);
return (Min);
}  

void OnTick()
{

Comment(
"\n MaxPrice = ",DoubleToString(NormalizeDouble(MaxPrice(TimeFrame,inTime,outTime),5),5),
"\n MinPrice = ",DoubleToString(NormalizeDouble(MinPrice(TimeFrame,inTime,outTime),5),5),
)


}
Keith Watford
Moderator
10569
Keith Watford  

In your first block of code

int     inBar  = iBarShift(NULL,TimeFrame,inTime),
        outBar = iBarShift(NULL,TimeFrame,outTime-1),
        BarRange  = inBar - outBar + 1,
        MaxShift = iHighest(NULL,TimeFrame,MODE_HIGH,BarRange,outBar),
        MinShift = iLowest(NULL,TimeFrame, MODE_LOW,BarRange,outBar);



double  
         MaxPrice = iHigh(NULL,TimeFrame,MaxShift),
         MinPrice = iLow(NULL,TimeFrame,MinShift);   

should be in OnTick()

m_shafiei2006
147
m_shafiei2006  

many thanks,


yes. my outTime, would be the next candle and start of next action. in fact min and max checking time is like: intime <= checktime < outtime

am I doing wrong ??

Fernando Carreiro
2202
Fernando Carreiro  
  1. Before submitting code here, make sure that it compiles correctly (or report errors if you are unable to fix them). There were several unbalanced parenthesis and braces.
  2. Your "start" and "stop" dates are exactly the same, so obviously, it would only work over a single bar, returning the High and Low of that bar.
  3. Since, there were several other points, I decided to submit my own code for you to analyse and hopefully be able to understand:

EDIT: Removed first code sample, in light of the more recent version (see next post)!

Fernando Carreiro
2202
Fernando Carreiro  
m_shafiei2006:

many thanks,

yes. my outTime, would be the next candle and start of next action. in fact min and max checking time is like: intime <= checktime < outtime

am I doing wrong ??

OK! Now that I understand the need for your "one second less" reason, here is my code again corrected for that need:

#property strict

input ENUM_TIMEFRAMES
   enumTimeFrame = PERIOD_M1;

input datetime
   dtBeginTime = D'2016.04.12 08:00:00',
   dtEndTime   = D'2016.04.13 17:30:00';

double
   dblHighPrice   = EMPTY,
   dblLowPrice    = EMPTY;

void GetRangeExtremes( datetime dtStartTime, datetime dtStopTime )
{
   dblHighPrice = EMPTY;
   dblLowPrice  = EMPTY;

   datetime
      dtFirstTime = fmin( dtStartTime, dtStopTime ),
      dtLastTime  = fmax( dtStartTime, dtStopTime ) - 1;
      
   int
      intStartShift = iBarShift( NULL, enumTimeFrame, dtFirstTime ),
      intStopShift  = iBarShift( NULL, enumTimeFrame, dtLastTime  );
      
   if( ( intStartShift >= intStopShift ) && ( intStopShift >= 0 ) )
   {
      int
         intRange = intStartShift - intStopShift + 1,
         intShiftHighest
            = iHighest( NULL, enumTimeFrame, MODE_HIGH, intRange, intStopShift ),  
         intShiftLowest
            = iLowest(  NULL, enumTimeFrame, MODE_LOW,  intRange, intStopShift );
            
      if( intShiftHighest >= 0 )
         dblHighPrice = iHigh( NULL, enumTimeFrame, intShiftHighest );
     
      if( intShiftLowest  >= 0 )
         dblLowPrice  = iLow(  NULL, enumTimeFrame, intShiftLowest  );
   }
}

void OnTick()
{
   static bool boolOnceOnly = true;
   
   if( boolOnceOnly )
   {
      GetRangeExtremes( dtBeginTime, dtEndTime );
      
      Comment(
         "Highest: ",  DoubleToString( dblHighPrice, _Digits ),
         "; Lowest: ", DoubleToString( dblLowPrice,  _Digits ) );
         
      boolOnceOnly = false;
   }
}
m_shafiei2006
147
m_shafiei2006  

unbalanced parenthesis and braces was because of cut and paste and editing here, the original code compiles correctly in my meta editor.

because i always change input times, I set them the same.

BUT... with your code I disappoint myself. it is very nice.

thanks

Fernando Carreiro
2202
Fernando Carreiro  
m_shafiei2006:

unbalanced parenthesis and braces was because of cut and paste and editing here, the original code compiles correctly in my meta editor.

because i always change input times, I set them the same.

BUT... with your code I disappoint myself. it is very nice.

thanks

Thank you!

Just made I minor bug correction to my code, so consider the latest version now instead of the previous. I previously had "if( intShiftHighest > 0 )" when it should have been "if( intShiftHighest >= 0 )" and the same was done for intShiftLowest.

Keith Watford
Moderator
10569
Keith Watford  
m_shafiei2006:

many thanks,


yes. my outTime, would be the next candle and start of next action. in fact min and max checking time is like: intime <= checktime < outtime

am I doing wrong ??

Sorry, I wasn't thinking that you don't want to include the bar with the out time
m_shafiei2006
147
m_shafiei2006  

thank you GumRai and FMIC for your help,

it solved my problem.

m_shafiei2006
147
m_shafiei2006  

Hi


today I tested the EA in two ways;

FMIC code does not work as I expected and just read offline times.

Gumrai solution make it work and change the price along with min and max changes in a separate EA that has just the code related to this part, but I need that variables in the top to use in many functions. what can I do ?

Fernando Carreiro
2202
Fernando Carreiro  
m_shafiei2006:

Hi

today I tested the EA in two ways;

FMIC code does not work as I expected and just read offline times.

Gumrai solution make it work and change the price along with min and max changes in a separate EA that has just the code related to this part, but I need that variables in the top to use in many functions. what can I do ?

My code was written specifically for the problem you described with your original example code - That is, the times are given as static input parameters, and thus only gets calculated once.

If you wanted a solution for dynamic dates that change during the course of the EA's workings, then you should have specified that or given a more realistic example in your original post. We cannot guess or anticipate what your final goal is for the code.

In essence, you should always consider code given here on the forum as skeleton code or example code that you then have to adapt to your requirements. In other words, you should read the documentations with regards to each function and then apply that knowledge to your own code.

Here is sample code for just the main function, adapted for dynamic use:

void GetRangeExtremes(
   string strSymbol, ENUM_TIMEFRAMES enumTimeframe,
   datetime dtStartTime, datetime dtStopTime,
   double &dblHighPriceVar, double &dblLowPriceVar )
{
   dblHighPriceVar = EMPTY;
   dblLowPriceVar  = EMPTY;

   datetime
      dtFirstTime = fmin( dtStartTime, dtStopTime ),
      dtLastTime  = fmax( dtStartTime, dtStopTime ) - 1;
      
   int
      intStartShift = iBarShift( strSymbol, enumTimeframe, dtFirstTime ),
      intStopShift  = iBarShift( strSymbol, enumTimeframe, dtLastTime  );
      
   if( ( intStartShift >= intStopShift ) && ( intStopShift >= 0 ) )
   {
      int
         intRange = intStartShift - intStopShift + 1,
         intShiftHighest
            = iHighest( strSymbol, enumTimeframe, MODE_HIGH, intRange, intStopShift ),  
         intShiftLowest
            = iLowest(  strSymbol, enumTimeframe, MODE_LOW,  intRange, intStopShift );
            
      if( intShiftHighest > 0 )
         dblHighPriceVar = iHigh( strSymbol, enumTimeframe, intShiftHighest );
     
      if( intShiftLowest  > 0 )
         dblLowPriceVar  = iLow(  strSymbol, enumTimeframe, intShiftLowest  );
   }
}
12
To add comments, please log in or register