OnTick() means it is not an indicator but an EA!
Dear all, I am trying to migrate to MQL5 and have some problems with getting indicator values. Whenever I am attaching this EA to a chart, the Alert function always returns 1.0 as the indicator value ----why?????
...
Please read the documentation of CopyBuffer(). There are at least 3 errors in your code.
Why do you want to listen an alert everytime fast MA is higher than slow MA???. You should look for MA crossover only, or set the alert to trigger once per bar only or anyother method (I have placed a flag for this). Otherwise, you will listen thousands of alerts at every tick, until fast MA falls below slow MA.
//| Tigran.mq5 |
//| Copyright ©2017, Robertomar Trader |
//| https://robertomartrader.blogspot.com |
//| robertomartrader@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright ©2017, Robertomar Trader"
#property link "https://robertomartrader.blogspot.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| External Variables |
//+------------------------------------------------------------------+
//***********************************************************************************************************************************
//***********************************************************************************************************************************
input string IndSettings = "========== INDICATORS SETTINGS ==========";
input string sMA1 = " ~~~ MA 1 Parameters ~~~ ";
input ENUM_TIMEFRAMES MA1_TimeFrame = PERIOD_M30;
input int MA1_Period = 6;
input ENUM_MA_METHOD MA1_Method = MODE_EMA;
input ENUM_APPLIED_PRICE MA1_Price = PRICE_CLOSE;
input int MA1_Shift = 0;
input string sMA2 = " ~~~ MA 2 Parameters ~~~ ";
input ENUM_TIMEFRAMES MA2_TimeFrame = PERIOD_M30;
input int MA2_Period = 15;
input ENUM_MA_METHOD MA2_Method = MODE_EMA;
input ENUM_APPLIED_PRICE MA2_Price = PRICE_CLOSE;
input int MA2_Shift = 0;
//***********************************************************************************************************************************
//***********************************************************************************************************************************
string sMADigits;
int MA_FAST, MA_SLOW;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ResetLastError();
MA_FAST = iMA(_Symbol, MA1_TimeFrame, MA1_Period, MA1_Shift, MA1_Method, MA1_Price);
if(MA_FAST == INVALID_HANDLE)
{
printf("Unable to create MA_FAST handle. Error code: %d", GetLastError());
return(INIT_FAILED);
}
MA_SLOW = iMA(_Symbol, MA2_TimeFrame, MA2_Period, MA2_Shift, MA2_Method, MA2_Price);
if(MA_SLOW == INVALID_HANDLE)
{
printf("Unable to create MA_SLOW handle. Error code: %d", GetLastError());
return(INIT_FAILED);
}
sMADigits = IntegerToString(_Digits + 1);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double MA_buffer_slow[1], MA_buffer_fast[1];
if(CopyBuffer(MA_FAST,0,0,1,MA_buffer_fast) < 1) return;
if(CopyBuffer(MA_SLOW,0,0,1,MA_buffer_slow) < 1) return;
static bool alert = true;
if(MA_buffer_fast[0] > MA_buffer_slow[0])
{
if(alert)
{
alert = false;
Alert(StringFormat("MA_FAST(%s, %d, %s, %s): %."+sMADigits+"f > MA_SLOW(%s, %d, %s, %s): %."+sMADigits+"f",
StringSubstr(EnumToString(MA1_TimeFrame), 7), MA1_Period, StringSubstr(EnumToString(MA1_Method), 5), StringSubstr(EnumToString(MA1_Price), 6), MA_buffer_fast[0],
StringSubstr(EnumToString(MA2_TimeFrame), 7), MA2_Period, StringSubstr(EnumToString(MA2_Method), 5), StringSubstr(EnumToString(MA2_Price), 6), MA_buffer_slow[0]));
}
}
else alert = true;
}
//+------------------------------------------------------------------+
Please read the documentation of CopyBuffer(). There are at least 3 errors in your code.
Dear Alain, thank you very much for your response. Actually, this EA is only used for testing purposes to learn getting data from an indicator. The documentation is a bit confusing and I am still experiencing difficulties understanding my mistakes. I would highly appreciate it if you could write my code in the right way. It would definitely help me to eventually understand the peculiarities of this language. Thank you once again for you time.
I will not write your code. Just say what is confusing and you will get help.
Jose even provided you a complete code,
Dear Alain, thank you very much for your response. Actually, this EA is only used for testing purposes to learn getting data from an indicator. The documentation is a bit confusing and I am still experiencing difficulties understanding my mistakes. I would highly appreciate it if you could write my code in the right way. It would definitely help me to eventually understand the peculiarities of this language. Thank you once again for you time.
I provided you a version of your code in the right way. Did you see my post above??.
I will not write your code. Just say what is confusing and you will get help.
Jose even provided you a complete code,
Looks like he didn't see my code or he didn't like it, hahaha. I don't usually write a complete code in the forum, and once I do, it's ignored.
Regards.
I provided you a version of your code in the right way. Did you see my post above??.
Looks like he didn't see my code or he didn't like it, hahaha. I don't usually write a complete code in the forum, and once I do, it's ignored.
Regards.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear all, I am trying to migrate to MQL5 and have some problems with getting indicator values. Whenever I am attaching this EA to a chart, the Alert function always returns 1.0 as the indicator value ----why?????
Please HELP me to write the correct code to get the indicator. Thanks in advance.
//+------------------------------------------------------------------+
//| test.mq5 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double Ema_buffer[2];
int EMA_LOW_FAST = iMA(_Symbol,30,6,0,MODE_EMA,PRICE_CLOSE);
int EMA_LOW_SLOW = iMA(_Symbol,30,15,0,MODE_EMA,PRICE_CLOSE);
double LOW_FAST_INDEX_0 = CopyBuffer(EMA_LOW_FAST,0,0,1,Ema_buffer);
double LOW_SLOW_INDEX_0 = CopyBuffer(EMA_LOW_SLOW,1,0,1,Ema_buffer);
if(LOW_FAST_INDEX_0>LOW_SLOW_INDEX_0){
Alert(LOW_FAST_INDEX_0);
};
}
//+------------------------------------------------------------------+