What am I doing wrong

 
//+------------------------------------------------------------------+
//|                                             Another one..lol.mq5 |
//|                                              Chioma J. Obunadike |
//|                                     https://wa.me/+2349124641304 |
//+------------------------------------------------------------------+
#property copyright "Chioma J. Obunadike"
#property link      "https://wa.me/+2349124641304"
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int EMA_10_h=0; // Asssign variable to 10EMA
  double ema_10[];
  
int OnInit()
  {EMA_10_h=iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE);
  ArraySetAsSeries(ema_10,true);
//--- indicator buffers mapping
   
//---
   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[])
  {CopyBuffer(EMA_10_h,0,1,5,ema_10);
 //---code from Rajesh Kumar Nait----to loop for all symbols 
   for  (int i=0; i<SymbolsTotal(1); i++) 
    { 
    MqlRates PriceInformation [];        // defines closePrice. Source: https://www.youtube.com/watch?v=H10QFIZoUYA
       ArraySetAsSeries(PriceInformation,true);
          int Data=CopyRates(_Symbol,NULL,0,Bars(_Symbol,NULL), PriceInformation);
             double closePrice = PriceInformation[0].close;
      if ((ema_10[0])>=((closePrice)-0.10) && (ema_10[0])<= ((closePrice)+0.10))
     Print("SYMBOL: ",SymbolName(i,1)," Found At: ",     +closePrice );
      else Print ("Not here", "SYMBOL: ",SymbolName(i,1),   +closePrice); 
      }
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Hello World. I just created an indicator that can tell me when price is within +/- 0.10 of the closeprice. I have been able to loop it around all the symbols in my market watch thanks to help I got in this forum. However, the closePrice does not update when looping around the symbols and only calculates the close price of the first symbol. Can you please tell me what I'm doing wrong? 

 
Chioma Obunadike:

Hello World. I just created an indicator that can tell me when price is within +/- 0.10 of the closeprice. I have been able to loop it around all the symbols in my market watch thanks to help I got in this forum. However, the closePrice does not update when looping around the symbols and only calculates the close price of the first symbol. Can you please tell me what I'm doing wrong? 

I have not run your code but it would be easier to debug if you compute the values used in the if condition and then inspect them during runtime.

For example:

             double closePrice  = PriceInformation[0].close;
             double closePriceLess  -= 0.10;
             double closePriceMore  += 0.10;
      if ( (ema_10[0]>=closePriceLess) && (ema_10[0]<= closePriceMore) )

Also inspect ema_10[0] to check the logic is working as expected - your brackets also look like they could be wrong

Give that a try as a first step

 
EMA_10_h=iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE);
int Data=CopyRates(_Symbol,NULL,0,Bars(_Symbol,NULL), PriceInformation);

You are only getting the EMA and Close price of the chart symbol.

 
Chioma Obunadike:

This is the third topic that you have started which is about the progression of the same code.

https://www.mql5.com/en/forum/433834

https://www.mql5.com/en/forum/433914

There is no need to start a new topic every time so continue with this one from now on.

Please help me
Please help me
  • 2022.10.02
  • www.mql5.com
This is my first EA. I'm trying to make an EA that will notify me when price touches the 10 EMA on the daily timeframe...
 
Keith Watford #:

This is the third topic that you have started which is about the progression of the same code.

https://www.mql5.com/en/forum/433834

https://www.mql5.com/en/forum/433914

There is no need to start a new topic every time so continue with this one from now on.

Can I use this one instead please? Whenever, I add  new comment, people stop giving me help which is why I created a new one

//+------------------------------------------------------------------+
//|                                             Another one..lol.mq5 |
//|                                              Chioma J. Obunadike |
//|                                     https://wa.me/+2349124641304 |
//+------------------------------------------------------------------+
#property copyright "Chioma J. Obunadike"
#property link      "https://wa.me/+2349124641304"
#property version   "1.00"
#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int EMA_10_h=0; // Asssign variable to 10EMA
  double ema_10[];
  
int OnInit()
  {EMA_10_h=iMA(NULL,_Period,10,0,MODE_EMA,PRICE_CLOSE);
  ArraySetAsSeries(ema_10,true);
//--- indicator buffers mapping
   
//---
   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[])
  {CopyBuffer(EMA_10_h,0,1,5,ema_10);
   for  (int i=0; i<SymbolsTotal(1); i++) 
    { 
    MqlRates PriceInformation [];        // defines closePrice. Source: https://www.youtube.com/watch?v=H10QFIZoUYA
       ArraySetAsSeries(PriceInformation,true);
          int Data=CopyRates(NULL,NULL,0,Bars(NULL,NULL), PriceInformation);
             double closePrice = PriceInformation[0].close;
      if ((ema_10[0])>=((closePrice)-0.10) && (ema_10[0])<= ((closePrice)+0.10))
     Print("SYMBOL: ",SymbolName(i,1)," Found At: ",     +closePrice );
      else Print ("Not here", "SYMBOL: ",SymbolName(i,1),   +closePrice); 
      }
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

I have replaced all "_Symbol" with "NULL" yet my results remain unchanged. Any tips please?

 
R4tna C #:

I have not run your code but it would be easier to debug if you compute the values used in the if condition and then inspect them during runtime.

For example:

Also inspect ema_10[0] to check the logic is working as expected - your brackets also look like they could be wrong

Give that a try as a first step

Hi @R4tna C thanks for the advice!

I did what you recommended but there has been no change. My code compiles and runs on all symbols but the only problem I have is that it isn't updating the closePrice for every symbol its loops on

 
Chioma Obunadike #:

Can I use this one instead please? Whenever, I add  new comment, people stop giving me help which is why I created a new one

I have replaced all "_Symbol" with "NULL" yet my results remain unchanged. Any tips please?

Why? What do you expect to happen by using NULL instead of the symbol?

With respect you should not be trying to work on code such as this as it is way above your level.

Start with something simple and make sure that you completely understand what the code does.

 
Keith Watford #:

Why? What do you expect to happen by using NULL instead of the symbol?

With respect you should not be trying to work on code such as this as it is way above your level.

Start with something simple and make sure that you completely understand what the code does.

What then is my level 😔? I think this is a simple indicator but I’m just not smart enough to figure out the last part of my own. I thought adding the “NULL” function would make the program run on all symbols 🤷‍♀️
 
Chioma Obunadike #:
 I thought adding the “NULL” function would make the program run on all symbols 🤷‍♀️

Documentation for iMA

iMA

symbol

[in] The symbol name of the security, the data of which should be used to calculate the indicator. The NULL value means the current symbol



NULL means the current chart symbol.

Documentation for CopyRates

CopyRates

symbol_name

[in]  Symbol name.



Nowhere does it say that you can use NULL.

 
Keith Watford #:

Documentation for iMA

NULL means the current chart symbol.

Documentation for CopyRates

Nowhere does it say that you can use NULL.

Yes as Keith has pointed out, you need to specify the symbol.

I don't usually use this approach, but I think as you are using a SymbolsTotal() loop, it looks like you need to get the symbol for each iteration of the loop and use that.

Here is an (incomplete) example to give you an idea. 

   string symbol; 
   for(int i = 0; i < SymbolsTotal(true); i++)
     {
      symbol = SymbolName(i, true);
      MqlRates PriceInformation [];        // defines closePrice. Source: https://www.youtube.com/watch?v=H10QFIZoUYA
      ArraySetAsSeries(PriceInformation,true);
      int Data = CopyRates(symbol,NULL,0,Bars(NULL,NULL), PriceInformation);
      double closePrice = PriceInformation[0].close;
 

Why is no one pointing out you don't need to copy the total rates, if your intention is to compare the last value only?

  int Data = CopyRates(symbol, 0, 0, 1,PriceInformation);
  //--- Check the number of values copied
  if(Data!=1)
   continue;
   double closePrice = PriceInformation[0].close;   
  
Looking at your code, it is important to note that the value 0.1 is different from pair to pair, for Fiber (EurUsd) that is about a 1000 pips while for gold,silver, JPY pairs you get the point.