Download MetaTrader 5

How to get the High/Low of the last 10 bars?

To add comments, please log in or register
Denisx
14
Denisx  

I need to create an EA that checks on every tick if there is a gap on every pair.

Im using for loop to loop 10 times and i'm using 

double current_high = iHigh(Symbols[j], PERIOD_M5, i);
double current_low = iLow(Symbols[j], PERIOD_M5, i);
double previous_high = iHigh(Symbols[j], PERIOD_M5, i + 1);
double previous_low = iLow(Symbols[j], PERIOD_M5, i + 1);

to get the current bar's High value and the previous one by shifting. There is simple if to check if current bar's High is lower than previous bar's Low and vice versa. It works but sometimes detects there is a gap without there is an actual gap. How can i accurately get the values? 

I'm using nested for loop. The first one gets the pair and the next one is looping 10 times to get the values and compare them if there is a gap. Symbols is the pairs array.

Carl Schreiber
7295
Carl Schreiber  

You have to use the functions iHighest and iLowest (which return an index!) and then High[] and Low[].

Read the example in the Reference (cursor in the function+F1).
whroeder1
15925
whroeder1  
Denisx: but sometimes detects there is a gap without there is an actual gap. How can i accurately get the values?
  1. Where is your comparison code? There are no mind readers here.
  2. i should be one only, not in a loop, not zero.
  3. You must make sure other pairs are loaded. Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
  4. I recommend
    Do not trade multiple currencies in one EA
Denisx
14
Denisx  
Carl Schreiber:

You have to use the functions iHighest and iLowest (which return an index!) and then High[] and Low[].

Read the example in the Reference (cursor in the function+F1).

Thanks, ill try it!
whroeder1:
  1. Where is your comparison code? There are no mind readers here.
  2. i should be one only, not in a loop, not zero.
  3. You must make sure other pairs are loaded. Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
  4. I recommend

if(current_low > (previous_high + 0.00002))

Pairs are loaded because im hardcode-ing them in a array.

Checking for every pair is because thats my task. Doing intern and i'm just following orders. 

whroeder1
15925
whroeder1  
Denisx: Pairs are loaded because im hardcode-ing them in a array.
if(current_low > (previous_high + 0.00002))
  1. Didn't say anything about the symbol names being in an array. You must make sure you have history for them downloaded in to the terminal if you want to look at them.
  2. That only checks if there is a gap higher. No check for lower gap. Don't hard code numbers. If you want two points put in 2*_Point. Code fails on JPY pairs.


Denisx
14
Denisx  
whroeder1:
  1. Didn't say anything about the symbol names being in an array. You must make sure you have history for them downloaded in to the terminal if you want to look at them.
  2. That only checks if there is a gap higher. No check for lower gap. Don't hard code numbers. If you want two points put in 2*_Point. Code fails on JPY pairs.



Oh thanks, well i don't really understand your code. What i'm supposed to change to get the history for PERIOD_M5 of all pairs so i can loop them and check for gap? 

#define HR2400 PERIOD_D1 * 60    // 86400 = 24 * 3600
int      TimeOfDay(datetime when=0)
{      
   if(when == 0)  when = TimeCurrent();
      return( when % HR2400 );            
}
datetime DateOfDay(datetime when=0)
{ 
   if(when == 0)  when = TimeCurrent();
      return( when - TimeOfDay(when) );   
}

#define SYMBOL string
#define THIS_SYMBOL ""

string Currencies[] = {"AED", "AUD", "BHD", "BRL", "CAD", "CHF", "CNY", "CYP", "CZK", "DKK", "DZD", "EEK", "EGP", "EUR", "GBP", "HKD", "HRK", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JOD", "JPY", "KRW", "KWD", "LBP", "LTL", "LVL", "LYD", "MAD", "MXN", "MYR", "NOK", "NZD", "OMR", "PHP", "PLN", "QAR", "RON", "RUB", "SAR", "SEK", "SGD", "SKK", "SYP", "THB", "TND", "TRY", "TWD", "USD", "VEB", "XAG", "XAU", "YER", "ZAR"};
string Symbols[58];
int SymbolCount;

int init()
{
   CreateSymbolList();
   
   return(0);
}

bool  download_history(ENUM_TIMEFRAMES period=PERIOD_M5)
{
   return download_history(_Symbol, period); 
}
bool  download_history(SYMBOL symbol=THIS_SYMBOL, ENUM_TIMEFRAMES period=PERIOD_CURRENT)
{
   if(symbol == THIS_SYMBOL)     
      symbol = _Symbol;
   if(period == PERIOD_CURRENT) 
      period = _Period;
       
   datetime today = DateOfDay();
   
   ResetLastError();
   
   datetime other = iTime(symbol, period, 0);
   
   if(_LastError == 0 && today == DateOfDay(other)) 
   {
      return true;   
   }
   if(_LastError != ERR_HISTORY_WILL_UPDATED && _LastError != ERR_NO_HISTORY_DATA)
      Print(StringFormat("iTime(%s,%i) Failed: %i", symbol, period,_LastError));
   return false;
}

string CreateSymbolList()
  {
   string allsyms;
   
   int CurrencyCount = ArrayRange(Currencies, 0);
   int Loop, SubLoop;
   string TempSymbol;
   for(Loop = 0; Loop < CurrencyCount; Loop++)
       for(SubLoop = 0; SubLoop < CurrencyCount; SubLoop++)
         {
           TempSymbol = Currencies[Loop] + Currencies[SubLoop];
           if(MarketInfo(TempSymbol, MODE_BID) > 0)
             {
               ArrayResize(Symbols, SymbolCount + 1);
               Symbols[SymbolCount] = TempSymbol;
               allsyms = allsyms + TempSymbol +"n";
               SymbolCount++;
             }
             
           TempSymbol = Currencies[Loop] + Currencies[SubLoop] +"m";
           if(MarketInfo(TempSymbol, MODE_BID) > 0)
             {
               ArrayResize(Symbols, SymbolCount + 1);
               Symbols[SymbolCount] = TempSymbol;
               allsyms = allsyms + TempSymbol +"n";
               SymbolCount++;
             }
         }
   return(allsyms);
  }

void OnTick()
{
   while(!download_history(PERIOD_M5) )
   { 
      Sleep(1000); RefreshRates(); 
   }
   
   int size = ArraySize(Symbols);
   
   for(int i = 0; i < size; i++)
   {
      int current_bar_index = iHighest(Symbols[i], PERIOD_M5, MODE_HIGH, 1, 0);
      int previous_bar_index = iHighest(Symbols[i], PERIOD_M5, MODE_HIGH, 2, 1);
      int current_bar_index_low = iLowest(Symbols[i], PERIOD_M5, MODE_LOW, 1, 0);
      int previous_bar_index_low = iLowest(Symbols[i], PERIOD_M5, MODE_LOW, 2, 1);
      
      double current_high = High[current_bar_index];
      double previous_high = High[previous_bar_index];
      double current_low = Low[current_bar_index_low];
      double previous_low = Low[previous_bar_index_low];
   
      if (current_low > (previous_high + 0.00002) || current_high < (previous_low - 0.00002))
      {
         Print("There is a gap");
         //SendMail("Gap!", "There is a gap at: " + Symbols[i]);
      }
   }
}

Everything i made so far.

whroeder1
15925
whroeder1  
  1. Denisx: i don't really understand your code. What i'm supposed to change to get the history for PERIOD_M5 of all pairs so i can loop them and check for gap? 
       while(!download_history(PERIOD_M5) )
       { 
          Sleep(1000); RefreshRates(); 
       }
    That just makes sure the current pair/M5 minute data is current. If M5 is the period you are running on, that code does nothing.
  2.       int current_bar_index = iHighest(Symbols[i], PERIOD_M5, MODE_HIGH, 1, 0);
    
    You want all symbols downloaded. Move the code inside the loop and call it using Symbols[i].
  3. Your iHighest has a length of one, starting at zero. What do you think that will return? Only zero.
  4. Make up your mind. Either you want to find the "High/Low of the last 10 bars" as your title says. Or you want to "loop them and check for gap"
  5. Just get the values and check.
    double current_high  = High[0];
    double previous_high = High[1];
    double current_low   =  Low[0];
    double previous_low  =  Low[1];



To add comments, please log in or register