Expressingx
Expressingx

Reputation: 1572

How to loop through all Symbols and check the current bar and the previous one's High?

The problem is that when the i in the for loop reaches 15, I get an Array out of range error.

What I want to achieve is on every tick to check every Symbol in the MarketWatch and in each Symbol to check the current bar ( PERIOD_M5 ) and the previous one if there is a gap.

void OnTick()
{
 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, 1, 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,  1, 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" );
  }
 }
}

Symbols[] is the array containing all the symbols. I loop through it and pass the current Symbol and get the index for the current bar and the previous one and then simply check if there is a gap.

The first problem is that passing every Symbol doesn't work. It gets only one and that's it. How can I achieve that and also, why I get an Array out of range error?

Upvotes: 2

Views: 5692

Answers (2)

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

about the error - when you receive it, it also can see the address of the code that generates it: number of line and symbol number in that line

about Symbol - as you may know, in MQL5 you can select all symbols from the market watch, in MQL4 such option is not available, as far as I remember, so I suspect your array of symbol names is empty or sth like that.

Could you show how you initialized that array? A correct way is the following one:

string Symbol[]={"EURUSD","AUDUSD","GBPUSD"};

also your code doesn't make any sense - when you select iHighest(*,*,*,i,j) - that means that EA-code checks i-elements, starting from j-th element ( 1, 0 ) means a current bar only, ( 1, 1 ) means to select just 1 bar, starting from bar 1, so still only 1 Bar is used.

For that it is much easier to have

double  cur_high = iHigh( Symbol[i], 5, 0 ),
       prev_high = iHigh( Symbol[i], 5, 1 );

Upvotes: 1

user3666197
user3666197

Reputation: 1

Let's start with the Array out of range error:

Given the MCVE was not present, let's assume, there is some fair declaration of the MQL4 code in the header or something like that.

So to diagnose this, add a trivial debug tool:

string SymbolsARRAY[] = { ...,  // be carefull, some Brokers
                          ...,  //              have quite strange names
                          ...
                          };

for ( int cellPTR  = 0;
          cellPTR <  ArraySize( SymbolsARRAY );
          cellPTR++
          )
          PrintFormat( "%s[%d] = %s",
                       "SymbolsARRAY",
                        cellPTR,
                        SymbolsARRAY[cellPTR]
                        );

and post here the output.


Next, the GAP detection:

  • if you would not mind, checking a pair of M5-Bars for a GAP on each and every price QUOTE message arrival, is quite devastating the MQL4 computing resources. The GAP may principally appear only upon a new Bar starts ( and yet may disappear, during the Bar duration ).

Let's detect the initial GAP-condition ( it is similar to engulfing detection ):

if (   iLow( SymbolsARRAY[cellPTR], PERIOD_M5, 1 ) > iHigh( SymbolsARRAY[cellPTR], PERIOD_M5, 0 )
   || iHigh( SymbolsARRAY[cellPTR], PERIOD_M5, 1 ) <  iLow( SymbolsARRAY[cellPTR], PERIOD_M5, 0 )
      )
      Print( "Initial GAP detected" );

Using this inside a detection of a new bar event may look like this:

void OnTick(){

 // -----------------------------------------------------------------
 // MINIMALISTIC CONTEXT-AWARE GAP DETECT/REMOVE
 // -----------------------------------------------------------------
    static bool      aCurrentGAP  = False;
    static datetime  aCurrentBAR  = Time[0];
    if (             aCurrentBAR != Time[0] ){
                     aCurrentBAR  = Time[0];
                  // testInitGAP .........................present?
                     aCurrentGAP  = testInitialGAP_onNewBarEVENT();
    }
    else {
                if ( aCurrentGAP ){
                  // testInitGAP .....................disappeared?
                     aCurrentGAP  = testInitialGAP_DISAPPEARED();
                }
    }
 // -----------------------------------------------------------------
 // MAIN COMPUTING & TRADING DUTIES OF EA-MQL4-CODE
 // -----------------------------------------------------------------
    ...
}

Upvotes: 1

Related Questions