3

In MetaTrader4 Terminal I write a simple alert EA

I would like to run my EA on all FOREX Majors and FOREX Minors, I've got my code all done, but I want it to automatically run on all symbols in my [ Market Watch ] panel.

Here is my code, it is very simple, can someone please help in showing me how I can make this run automatically on all Symbols or Symbols in my market watch?

   double RSI            = iRSI(        NULL,15, 7,PRICE_CLOSE,0 );
   double Stoc           = iStochastic( NULL,15, 5,3,3,MODE_SMA,STO_LOWHIGH,0,0);
   double BoliUpper      = iBands(      NULL,15,20,2,0,PRICE_MEDIAN,1,1 );
   double BoliLower      = iBands(      NULL,15,20,2,0,PRICE_MEDIAN,2,1 );
   double CandlePrevHigh = iOpen(       NULL,15, 1 );
   double CandlePrevLow  = iClose(      NULL,15, 1 );

   if ((RSI > 75) && (Stoc > 80)&& (BoliUpper < CandlePrevHigh) )
   {
       Alert(Symbol()+" is over bought");
   }
   else if ((RSI < 25) && (Stoc < 20)&&(BoliLower>CandlePrevLow))
   {
       Alert(Symbol()+" is over sold");
   }
user3666197
  • 1
  • 6
  • 50
  • 92
Patrick Aipoh
  • 31
  • 1
  • 2
  • Did you get this to work? (You need to iterate over all symbols (`_Symbol`) in the *Watch List*, and replace `NULL` with that symbol in your functions. – not2qubit Jun 29 '18 at 07:47

2 Answers2

1

Just iterate over the symbols like this:

string syms[] = {"EURUSD","USDJPY"}; // Currency pair symbol array

for(int i=0; i < ArraySize(syms); i++) {
 double RSI = iRSI( syms[i], 15, 7,PRICE_CLOSE,0 );
 // ...
}

So just replace all the 'NULL' with syms[i].

To get the Market Watch symbols, you need to add some code, but here and here you can find some free code/library to help you do that.

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
not2qubit
  • 14,531
  • 8
  • 95
  • 135
-1

how I can make this run automatically on all Symbols ?

Two things:

1) design the code not as an EA, but as a Script ( so as to get your code execution free from local-graph flow of events

2) "wrap" the logic into a for-loop, best smart-short-cut, so as to scan all trading instruments of your choice:

...
   for ( int anInstrumentNAME_INDEX =  0;
             anInstrumentNAME_INDEX < ArraySize( anInstrumentNAME_ARRAY );
             anInstrumentNAME_INDEX++
             )
   {  string anInstrumentNAME = anInstrumentNAME_ARRAY[anInstrumentNAME_INDEX] );

      double aFetchedLastPRICE =  iClose( anInstrumentNAME, PERIOD_CURRENT, 0 );
      if (   aFetchedLastPRICE == 0 )
      {   // 0: signals that the requested piece of data is not in Terminal's local datastore
          // get Error# + analyse + remedy
             continue;
      }
      if (   anInstrumentPRICE[anInstrument_NAME_INDEX] == aFetchedLastPRICE ) continue; // .WAS the same, loop
      else   anInstrumentPRICE[anInstrument_NAME_INDEX] =  aFetchedLastPRICE;            // .STO

      runAsync_MonitorOnThisOne( anInstrumentNAME );

   }

void runAsync_MonitorOnThisOne( string aSymbolNAME )
{    double RSI         =        iRSI( aSymbolNAME, 15,  7, PRICE_CLOSE, 0 );
     double Stoc        = iStochastic( aSymbolNAME, 15,  5, 3, 3, MODE_SMA, STO_LOWHIGH, 0, 0 );
     double BoliUpper        = iBands( aSymbolNAME, 15, 20, 2, 0, PRICE_MEDIAN, 1, 1 );
     double BoliLower        = iBands( aSymbolNAME, 15, 20, 2, 0, PRICE_MEDIAN, 2, 1 );   
     double CandlePrevHigh   = iOpen(  aSymbolNAME, 15,  1 );
     double CandlePrevLow    = iClose( aSymbolNAME, 15,  1 );

     if (  ( RSI       >  75 )
        && ( Stoc      >  80 )
        && ( BoliUpper <  CandlePrevHigh )
           )
    {
           Alert( aSymbolNAME + " is over bought [BEWARE, THIS BLOCKS...] " );
    }
    else if (  ( RSI       <  25 )
            && ( Stoc      <  20 )
            && ( BoliLower >  CandlePrevLow )
               )
         {
               Alert( aSymbolNAME + " is over sold [BEWARE, THIS BLOCKS... ]" );
         }
}
user3666197
  • 1
  • 6
  • 50
  • 92
  • wow thaks alot buddy, you are really very kind, is this the final code that I can directly use ? Im not very proficient in coding, I understand the code language but is this the actual code that I can use? what should I add or remove to work it ? Many many thanks bro – Patrick Aipoh May 12 '18 at 18:50
  • @user3666197 I don't agree to use script in this case. Can you try to explain what you mean with: "`get your code execution free from local-graph flow of events`"? Also, where are you putting the main code? In *OnTick()* or elsewhere? – not2qubit Jun 04 '18 at 05:49
  • *( **Prologue :** English is my ~ 4th language, so kindly consider any piece of the text further to be principally handicaped by this fact )* StackOverflow practice is to use an **Evidence-based** argumentation ( best using MCVE formulated problem + solution ). Given you have expressed above that you do not agree with some advice, that was well supported in the answer, would you mind to present also the pieces of evidence, that may support your disagreement to use MQL4-Script, that's fair, isn't it? Otherwise an evidence-free "*naked*" disagreement has no value for technical debate, does it? – user3666197 Jun 04 '18 at 07:09
  • Most likely because the OP specifically ask for an EA and not a script. This is more likely, because he probably intend to auto-trade on the results from his EA. That is not scriptable. In addition, regardless the language, your one-sentence explanation(in 1) is clearly too technical for most users, and also do not provide any evidence that is understandable (for most.) – not2qubit Jun 04 '18 at 08:03
  • **[a]** MQL4/5 Script does **not** prevent one from trading / auto-trading XTO operations -- so this not a blocker ( sure, if none other *New*-MQL4.56789 language-creep has stepped in, as silently as it had so many times so far in the past :o) ) **[b]** EA will never allow for achieving the posted wish, as the "local"-event-trigger will never permit to see updates in any, the less all, other, non-"local"-event flows. This is elementary flaw in the wish, for which it was suggested to rather use Script, which is not locked-into any event-flow blocking-loop. *(After 10+y in MQL4 I know a bit...)* – user3666197 Jun 04 '18 at 09:00
  • @user3666197 (a) No doubt you know a lot, but can you explain it? :) (b) I just tried the script as you described it, and it doesn't work. It gives `array out of range` and other errors. Also `anInstrumentPRICE` is not defined. (c) can you link to where we can learn about this *"code execution free from local-graph flow of events"*? – not2qubit Jun 04 '18 at 09:24