MA200 is not getting calculated

 

Hi guys,

I have a problem as on my computer this is not working but on other friend's computer it is working, any idea?

Please help, as I need MA200 for one of my strategies but it doesnt work.

barsCalculated is always show -1 for any MA more than 100 .

I have tested this with different brokers, different time frames and different instruments but still the same thing.

//+------------------------------------------------------------------+
//|                                                   Test MA200.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
// Indicator handles
int maHandle;

int OnInit()
  {
   maHandle = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE);

//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
int a = BarsCalculated(maHandle);
   
    Print("Bars: " + a);
   
  }
//+------------------------------------------------------------------+
 

Please run this code and show the result.

//+------------------------------------------------------------------+
//|                                                   Test MA200.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.000"
//--- input parameters
input int                  Inp_MA_ma_period     = 200;         // MA: averaging period
input int                  Inp_MA_ma_shift      = 5;           // MA: horizontal shift
input ENUM_MA_METHOD       Inp_MA_ma_method     = MODE_SMA;    // MA: smoothing type
input ENUM_APPLIED_PRICE   Inp_MA_applied_price = PRICE_CLOSE; // MA: type of price
//---
int    handle_iMA;                           // variable for storing the handle of the iMA indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMA
   handle_iMA=iMA(Symbol(),Period(),Inp_MA_ma_period,Inp_MA_ma_shift,
                  Inp_MA_ma_method,Inp_MA_applied_price);
//--- if the handle is not created
   if(handle_iMA==INVALID_HANDLE)
     {
      int bars=Bars(Symbol(),Period());
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d, all bars %d, 'MA: averaging period' %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError(),
                  bars,
                  Inp_MA_ma_period);
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int a=BarsCalculated(handle_iMA);
   if(a<0)
     {
      int bars=Bars(Symbol(),Period());
      PrintFormat("BarsCalculated return '-1', all bars %d, 'MA: averaging period' %d",bars,Inp_MA_ma_period);
     }
   else
      Print("BarsCalculated: ",a);
  }
//+------------------------------------------------------------------+

Also show the chart parameters:


Files:
 
Vladimir Karputov:

Please run this code and show the result.

Also show the chart parameters:


This is what I have received from the logs, I have attached the log file but I had to delete some of it, but it was the same thing all the way to the end.

Thanks for your help.

Files:
 
Danial Bakhsheshi:

This is what I have received from the logs, I have attached the log file but I had to delete some of it, but it was the same thing all the way to the end.

Thanks for your help.

How do you want to calculate a MA200 when you have 102/103 bars on your chart ?
 
Alain Verleyen:
How do you want to calculate a MA200 when you have 102/103 bars on your chart ?

As I said I had to shorten the logs because it doesnt let me post the whole file, this is the EOF:

CK  0   09:04:16.067    Core 1  2018.09.10 16:03:40   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
OI  0   09:04:16.067    Core 1  2018.09.10 16:03:40   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
LH  0   09:04:16.067    Core 1  2018.09.10 16:03:41   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
HO  0   09:04:16.067    Core 1  2018.09.10 16:03:41   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
PM  2   09:04:19.677    Core 1  disconnected
GK  0   09:04:19.677    Core 1  connection closed
FM  3   09:04:19.677    Tester  stopped by user
CQ  0   10:18:21.480    Tester  Cloud servers switched off
 
Danial Bakhsheshi:

As I said I had to shorten the logs because it doesnt let me post the whole file, this is the EOF:

CK  0   09:04:16.067    Core 1  2018.09.10 16:03:40   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
OI  0   09:04:16.067    Core 1  2018.09.10 16:03:40   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
LH  0   09:04:16.067    Core 1  2018.09.10 16:03:41   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
HO  0   09:04:16.067    Core 1  2018.09.10 16:03:41   BarsCalculated return '-1', all bars 1602, 'MA: averaging period' 200
PM  2   09:04:19.677    Core 1  disconnected
GK  0   09:04:19.677    Core 1  connection closed
FM  3   09:04:19.677    Tester  stopped by user
CQ  0   10:18:21.480    Tester  Cloud servers switched off
Add the error number to the log.
 
Danial Bakhsheshi :

This is what I have received from the logs, I have attached the log file but I had to delete some of it, but it was the same thing all the way to the end.

Thanks for your help.

You have no bars.

Here is the line:

GR      0       09:03:11.518    Core 1  2017.09.20 01:00:00   BarsCalculated return '-1', all bars 102, 'MA: averaging period' 200

Increase the number of bars that are displayed on the chart.


 
The number of bars is important at the time of creating the indicator handle: at the time of creation of iMA (200) you have only 100 bars.
 
Vladimir Karputov:
The number of bars is important at the time of creating the indicator handle: at the time of creation of iMA (200) you have only 100 bars.
I did set it to what you have said but it is still showing like that
 
Alain Verleyen:
Add the error number to the log.
the error code is 4806
 
Danial Bakhsheshi :
I did set it to what you have said but it is still showing like that

Did you restart the terminal after changing the settings?