Help with a screener for the market watch window in mt5

 

Hi, i'm trying to code a screener for the market watch window for the technical indicator macd in Mt5 and it's imposible to me.

This code works correctly in mt4 but i don't know how to solve it in mt5. 

This is a simple code that read the macd of the first bar of all the symbols in the watch window.

I would really  apreciate your help with the indicator because i'm very stuck at this point.


the code in mt4 after OnCalculate is :


  {

//---

 for(int i=0;i<SymbolsTotal(selected);i++){

 

  string name=SymbolName(i,selected);   

   MqlRates rates[];

   ArraySetAsSeries(rates,true);

   int copied=CopyRates(name,0,0,100,rates);

  

 double macd=iMACD(name,0,fastEMA,slowEMA,signalSMA,PRICE_CLOSE,MODE_MAIN,1); 

        

  Print(name+ " " + macd);

   }

  }



Now in mt5 i think the code should be something like this:





for(int i=0;i<SymbolsTotal(selected);i++){

  

   name=SymbolName(i,selected);

   

   MqlRates rates[];

   ArraySetAsSeries(rates,true);

   int copied=CopyRates(name,0,0,100,rates);

  

  Print(i + name);

    double myPriceArray[];

    double  MacdDefinition=iMACD(name,0,fastEMA,slowEMA,signalSMA,PRICE_CLOSE); 

             ArraySetAsSeries(myPriceArray,true);

             CopyBuffer(MacdDefinition,0,1,100,myPriceArray);

    

    double MacdValue=(myPriceArray[1]);                

   Print("MACD ",MacdValue);

    }

    }

but it just take the parameters of the opened graphic but in mt4 works perfectly.

anyone knows the solution? thanks.

 
maybe your install java and adobe flash player for window series 
 
CAHAYANUR 99:
maybe your install java and adobe flash player for window series 
 In mt4 i don't need anything and for mt5 i need java or adobe flash player?  Do you know how to use java or flashplayer for mt5? Thanks
 
jlvazquez :

You do not read the help: in MQL5 you need to create an indicator handle ONCE! You cannot create an indicator handle on EVERY TICK!

Please study the help and reference examples of MQL5.

 
Vladimir Karputov:

You do not read the help: in MQL5 you need to create an indicator handle ONCE! You cannot create an indicator handle on EVERY TICK!

Please study the help and reference examples of MQL5.

Yes i know how to do it at one handle but i didnt put it here to not confuse and concrete the problem. But it has nothing to do with the main problem that is to read the macd of all symbols. Thanks to answer.
 
jlvazquez :
Yes i know how to do it at one handle but i didnt put it here to not confuse and concrete the problem. But it has nothing to do with the main problem that is to read the macd of all symbols. Thanks to answer.

There is no problem - the main problem is your crooked code. Read again: the indicator handle is created ONE time. Until you learn this, you will constantly make the same mistake. Please read the help. See how to correctly create an indicator handle (once in OnInit).

 
Vladimir Karputov:

There is no problem - the main problem is your crooked code. Read again: the indicator handle is created ONE time. Until you learn this, you will constantly make the same mistake. Please read the help. See how to correctly create an indicator handle (once in OnInit).

I use a function called IsNewbar for every bar for mt4 and mt5 and the code works only for mt4, but works. The difference between coding with that function is that de the code reads the macd for every bar or every tick but should write every tick of every symbol without it and doesn't do that. Thats why i dont undertand why you think it s so important for the correct working of the program. What would change? Thanks 
 
jlvazquez :
I use a function called IsNewbar for every bar for mt4 and mt5 and the code works only for mt4, but works. The difference between coding with that function is that de the code reads the macd for every bar or every tick but should write every tick of every symbol without it and doesn't do that. Thats why i dont undertand why you think it s so important for the correct working of the program. What would change? Thanks 

You will understand everything after you start reading the help. Read the help. Then come.

 
Vladimir Karputov:

You will understand everything after you start reading the help. Read the help. Then come.

Hi Vladimir, this is the complete code with de handle events created only once as you said,  but it doesn't work either

because read the macd of the first bar of the opened graphic but doesn't read the macd of all symbols.

what do you think i have to modify? thanks.


#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property indicator_chart_window



extern int    fastEMA = 12;

extern int    slowEMA = 26;

extern int    signalSMA=9;



bool selected=true;

string name;

double MacdDefinition,copybuffer;

 double myPriceArray[];

 MqlRates rates[];

 

 

int OnInit()

  {

       ArraySetAsSeries(rates,true);

       ArraySetAsSeries(myPriceArray,true);

       MacdDefinition=iMACD(name,0,fastEMA,slowEMA,signalSMA,PRICE_CLOSE);  

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

     if (IsNewbar()==true){

 

 for(int i=0;i<SymbolsTotal(selected);i++){

 

   name=SymbolName(i,selected);

   SymbolsTotal(selected);

   int copied= CopyRates(name,0,0,100,rates);

   

  Print(i + name);

 

   if(copied>0){

      

  copybuffer= CopyBuffer(MacdDefinition,0,1,500,myPriceArray);

     

   double MacdValue=(myPriceArray[1]);

                 

   Print("MACD ",MacdValue);

   

   }

   

      else Print("Fallo al recibir datos históricos para el símbolo ",name);

}

}

   return(rates_total);

  }





//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+ 

  

    bool IsNewbar()

{



      datetime Time[];

      int count=1000;   // number of elements to copy

      ArraySetAsSeries(Time,true);

      CopyTime(_Symbol,_Period,0,count,Time);

      static datetime RegBarTime=0;

      datetime ThisBarTime=Time[0];

 

      if (ThisBarTime==RegBarTime)

      

      {

    

         return(false);

         

       }

       

       else

       {

    

         RegBarTime=ThisBarTime;

         

         return(true);

         

         

         }

         

   }

 
jlvazquez:
...

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


 
Sergey Golubev:

ok, thanks.