Need help not repeating code

 

Hi everyone!

I am new to coding in MQL and was looking to use iMACD and iMA conditions for my indicator. I want to compare the iMACD and iMA of current chart's symbol with the iMACD and iMA two different symbols.

Below is the simple condition for the current chart symbol:

double BC1 = iMACD(NULL, PERIOD_CURRENT, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i) > 0;
double BC2 = iMA(NULL, PERIOD_CURRENT, 26, 0, MODE_SMA, PRICE_CLOSE, i) > iMA(NULL, PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE, i);
double BC3 = //... more conditions here
double BC4 = //... more conditions here
//.. more variables here
bool Buy_Trend_Current_Symbol = BC1 && BC2 && BC3 && BC4;

//I want to repeat above for two other symbols for the same timeframe and cast it into variables for comparison later


What I want to be able to do is compare current chart(Buy_Trend_Current_Symbol) with the BC1, BC2, BC3 & BC4 of EURUSD and GBPJPY without repeating the code(above) for each different currency pair(if possible). Once compared it would result in a signal.

If i hard code for EURUSD as an example then i would have to create variables similar to BC1 - BC4 but instead of NULL replace with "EURUSD". Similarly, for GBPUSD too, and then i would compare as follows:

if (Buy_Trend_Current_Symbol && Buy_Trend_EU && Buy_Trend_GJ) {
// ... do something
}


I hope i haven't confused the matter, I just want to reduce repetition where, there could be upto 10 variables that are all checked for the one currency and later each currency is compared with each other, and once all are true(as per the above if statement) then it will do something. 

Many thanks in advance for any guidance on this.

 

Hello , consult the following code , the comments will guide you through it .

#property copyright "Copyright 2033, Salted Caramel Almonds Inc."
#property version   "1.00"
#property strict
  /*
  From what i understand you have 4 buy conditions for each symbol (and 4 sell conditions i assume)(on this iteration) 
  and if they are valid the symbol is valid.
  The symbols that are in your collection must all be valid toward a direction for a trade to fire on the symbol your 
  ea is on.
  Trading aside the process is simple .
  We start at the foundation . What do you have ? 
     -symbols with buy and sell triggers
     -list of symbols
     and if we like we can extend it further to "symbol alignment"-where you mix buy and sell triggers for a buy for instance , 
     but that will confuse things, so we'll put it aside for now.
  The most basic thing we can do at first , although there's more handy ways , is to
  create our own variable that will have :
     The symbol 
     It's buy triggers
     It's sell triggers
     If its an overall buy
     If its an overall sell
  Then run through the list of such variables , and check the triggers one by one ,without writting up 
     the entire list of symbols.
  So , a custom variable :
  */
  struct aSymbolSignal{
  string symbol;//the symbol you'll be checking
  bool   isSell,isBuy;//is it an overall sell and an overall buy ?
  bool   buy_checks[];//list of passed/failed buy checks : the size of the list will be assigned on creation
  bool   sell_checks[];//list of passed/failed sell checks : same 
         /*
         You have the variable now what ? Now you need to set the default state of the variable 
         when it's created , you can ignore this step though 
         */
         aSymbolSignal(void){reset();}//so when the user (you) type in a new aSymbolSignal variable what happens is a reset
         //and here is the reset function
    void reset(){
         //so no symbol 
           symbol=NULL;
         //no overall triggers 
           isSell=false;isBuy=false;
         //remove the lists - we'will add them later - we could have added them upon the variable being created but that would confuse things so we skip it for now 
           ArrayFree(buy_checks);//list = array 
           ArrayFree(sell_checks);
         //so by default your variable has nothing what will you need next ?
         }
         /*
           for this you have to consider how your system works .
           You have symbols , 4 buy triggers and 4 sell triggers so 
           you will need to tell this "variable" that it has to know 
           which symbol the triggers belong to 
           and how many they are
           so , you need a setup function 
         */
    void setup(string _symbol,//the symbol
               int    _buy_checks_total,//total # of buy checks
               int    _sell_checks_total){//total # of sell checks
         //pass the symbol in 
           symbol=_symbol;
         //and resize the checks lists according to the totals received 
           //so if the buy checks needed are more than zero , resize the list
             if(_buy_checks_total>0){
             ArrayResize(buy_checks,_buy_checks_total,0);//simple eh?
             }  
           //similarly for the sell checks 
             if(_sell_checks_total>0){
             ArrayResize(sell_checks,_sell_checks_total,0);
             }
         //now your variable is ready to play ? nope you need one more thing 
         }
         /*
         You consult your signal process again 
         You have symbol variables with triggers and you will be checking them at intervals 
         and the trigger states of each side will be deciding whether or not your symbol
         is a buy overall or a sell overall , so what you need is a decision function that you will
         call after all the trigger checks.
         so...
         */
    void decide(){
         //and what does decide do ?
           /*
           You have a list of buy check results and sell check results and you also 
           are in the advantageous position to know how many of each you have . 
           So can you loop inside your lists and if all the triggers are valid 
           then the overall signal of the symbol is decided ? 
           Yes 
           But there is an even better way , what if you say at first both 
           overall sell and buy are valid like so : 
           */
           isBuy=true;
           isSell=true;
           /*
           looks crazy but , you then go into your lists and if you find even one check that 
           was not valid you kill the overall signal for that side for this symbol .
           So to get the size of the list we call ArraySize(Our_list)
           Let's check the buys 
           */
           for(int i=0;i<ArraySize(buy_checks);i++){
           //so you are asking the loop to go into all the buy check outcomes
             if(buy_checks[i]==false){
             //and if it finds a check was not valid 
               isBuy=false;//it will kill the overall signal for that side 
               break;//and it will stop checking the buys (this exists the loop and puts you on X>)
             }
           }
           //X>
           //and similarly for the sells - you can fill that up 
             //replace the line below with the checks for the sell side
               isSell=false;
          
         }
         
  };
  /*
  You have your variable type as you need it now what do you need ?
  You need many of those . Can you type them all one by one ? yes 
  Is there a better way ? always 
  For the sake of simpllicity though let's just go the writing it out way 
  and then we can advance further
  So , declare a list of your symbol variables ,no size 
  */
  aSymbolSignal MySymbols[];
  
int OnInit()
  {
//--- create timer
   //EventSetTimer(60);
   //What do you need to do on the start ? 
     //a.empty the list of your symbol variables
       ArrayFree(MySymbols);
     //b.decide how many you need and resize the list 
       //let's say we need 3 
         ArrayResize(MySymbols,3,0);
         //and set up your symbols 
           MySymbols[0].setup("EURUSD",4,4);//so you just added EURUSD with 4 buy checks and 4 sell checks
           MySymbols[1].setup("GBPUSD",4,4);
           MySymbols[2].setup("USDJPY",4,4);
         //ready to play
         //continue on tick 
//---
   return(INIT_SUCCEEDED);
  }
void OnTick()
  {
  /*
  we won't get at all into whether or not symbols are ready 
  and indicators returned data and if errors occured , or how often we access the checks 
  these are important but for later.
  For now , how does your check of all the symbols looks like ?
  Well , you first need to do what ? go into the list of your symbol variables 
  so : 
  */
  for(int s=0;s<ArraySize(MySymbols);s++)
   {
   /*you are now in the position to know which symbol must be checked by calling MySymbols[s].symbol
     so since we chose the easy way we will write out the checks as before but 
     swap the NULL with the current symbol checked
     so buy check 1 , you do the check as before but for this specific symbol 
     and you store the result into this "your symbol variable" :P
     you are calling the check for bar i , so here is a mock integer 
     be sure to adress it if you loop into bars 
     */ 
     int i=1;
     //so first buy check goes into where ? buy check slot 0 which is the first one 
     MySymbols[s].buy_checks[0] = iMACD(MySymbols[s].symbol, PERIOD_CURRENT, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i) > 0;
     MySymbols[s].buy_checks[1] = iMA(MySymbols[s].symbol, PERIOD_CURRENT, 26, 0, MODE_SMA, PRICE_CLOSE, i) > iMA(NULL, PERIOD_CURRENT, 50, 0, MODE_EMA, PRICE_CLOSE, i);
     /* you can add more checks here similarly 
        Make sure you don't go over the # of checks you have created your variables with 
        so in this case , you have 4 buy checks so the first one will be buy_checks[0] and the last one buy_checks[3]
     */
     /*
     cool , now what ?
     after all the checks for THIS symbol you tell the symbol to decide if its a sell or a buy 
     */
     MySymbols[s].decide();
   }
   //and by this point you have checked all your symbols (again assuming no data errors etc)
   //Now what do you need to do ? 
   /*
   If all your symbols agree do that trade , right ? so 
   you declare a signal buy and signal sell bool variable per the decision to be made 
   And again , what you'll do is go through all the symbols (instead of the checks) and light up 
       your signal if all the symbols agree on one side .
   But since you are smart you will light up the signals first and then shut them off if you find 
   a disagreeing member 
   Fun fact : You can also do a "parliament" style signal where you tally each signal as a vote 
              and if you have a % majority the signal is taken 
   But for now , let's go with 100% majority so : 
   your signal switches : 
   */
   bool signalBuy=true,signalSell=true;
   //go into your symbols list 
     for(int s=0;s<ArraySize(MySymbols);s++)
     {
     //cool 
       if(MySymbols[i].isBuy==false){
       //and if you find a symbol that is not a buy 
         signalBuy=false;
         //you shut off the buy signal , but , unlike the checks loops earlier you don't exit the loop yet 
         //because you must also check the sell signal !
       }
       if(MySymbols[i].isSell==false){
       //so if you find a symbol that is not a sell 
         signalSell=false;
         //you shut off the sell signal 
       }
       //and finally , if both signals are off you can exit the loop as you no longer 
       //need to check
         if(signalSell==false&&signalBuy==false){break;}
     }
   //and the rest is up to you 
     //if you get a buy signal 
       if(signalBuy==true){
       //do stuff 
       }
     //or if you get a sell signal 
       else if(signalSell==true){
       //do stuff 
       }
  //that is it . don't forget to fill up the sell checks in the variable structure 
  //enjoy      
  }  
 
Lorentzos Roussos #:

Hello , consult the following code , the comments will guide you through it .

Thanks a lot for taking time out for my post. That code looks very sophisticated. I will study it for sure.

 
Zabar Ullah #:

Thanks a lot for taking time out for my post. That code looks very sophisticated. I will study it for sure.

Awesome 


 
is it possible to write an indicator that scan through the currency pairs in the watchlist and then highlight any of the pairs if a predefined trading setup is found ??
Reason: