Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1243

 

I originally relied on this article:https://www.mql5.com/ru/articles/43

But the code there is only up to the moment of checking and getting indicator data into buffer arrays, further there is no example how to work with received indicator data. I looked through the reference book, there are mainly code examples for indicators, I picked up setindexbuffer from it. I want to know which way to "dig". I got tired of it yesterday, I've spent half a day here.

Способы вызова индикаторов в MQL5
Способы вызова индикаторов в MQL5
  • www.mql5.com
В MQL5 существует несколько вариантов вызова индикаторов, и осуществляются они в основном при помощи функций IndicatorCreate() и iCustom(). Причем эти функции лишь возвращают хендл индикатора, и дальнейшая работа с индикаторами ведется именно через него. Так что же такое хендл? Как работать с функциями IndicatorCreate() и iCustom()? И как...
 
Sayberix:

Tried it already. But in the wizard it generates there with classes. I would at least like to understand the code in a simple way - the code I understand at least a little bit. Could you please advise how to correct the code in order to get indicator values in the EA?

Example: How to get iEnvelopes indicator values from EA

//+------------------------------------------------------------------+
//|                         Example iEnvelopes values on a chart.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"
//--- input parameters
input int      Input1=9;
//---
int      handle_iEnvelopes;                     // variable for storing the handle of the iEnvelopes indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iEnvelopes
   handle_iEnvelopes=iEnvelopes(Symbol(),Period(),3,0,MODE_LWMA,PRICE_OPEN,0.03);
//--- if the handle is not created
   if(handle_iEnvelopes==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iEnvelopes indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   string text="";
   double upper[],lower[];
   ArraySetAsSeries(upper,true);
   ArraySetAsSeries(lower,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iEnvelopes,UPPER_LINE,start_pos,count,upper) ||
      !iGetArray(handle_iEnvelopes,LOWER_LINE,start_pos,count,lower))
     {
      return;
     }
   string text_upper="",text_lower="";
   for(int i=count-1; i>=0; i--)
     {
      text_upper  = text_upper   + "Upper"+"["+IntegerToString(i)+"]"+" "+DoubleToString(upper[i],Digits()+1)  +" | ";
      text_lower  = text_lower   + "Lower"+"["+IntegerToString(i)+"]"+" "+DoubleToString(lower[i],Digits()+1)  +" | ";
     }
   Comment(text_upper+"\n"+text_lower);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+


Result:


 
Vladimir Karputov:

Example: how to get iEnvelopes indicator values from an EA

Thank you very much! I will look into it!

 
Vladimir Karputov:


Vladimir, please tell me: where does it get UPPER_LINE and LOWER_LINE from? If we only got the indicator handle, did we get it straight from the handle?

 
Sayberix:

Vladimir, please tell me: where does it get UPPER_LINE and LOWER_LINE from? If we only got the indicator handle, did we get the handle at once too?

I do not understand your question. It is a set of letters without meaning. Please rephrase your question - do not hurry to type, think over your question and type calmly.

 
Vladimir Karputov:

I don't understand your question. A set of letters with no meaning. Please rephrase your question - don't rush to type, think about your question and type calmly.

In the argument of the function:

iGetArray(handle_iEnvelopes,UPPER_LINE,start_pos,count,upper)

you used UPPER_LINE.

Where does this parameter value come from, from the handle you got earlier:

handle_iEnvelopes=iEnvelopes(Symbol(),Period(),3,0,MODE_LWMA,PRICE_OPEN,0.03);

?

 
Sayberix:

In the function argument:

you used UPPER_LINE.

Where does this parameter value come from, from the previously received handshake:

?

No. This value is taken from the iEnvelopes help

Документация по MQL5: Технические индикаторы / iEnvelopes
Документация по MQL5: Технические индикаторы / iEnvelopes
  • www.mql5.com
//|                                              Demo_iEnvelopes.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //| Перечисление способов создания хэндла                            |                   ma_period=14;              ...
 

Afternoon. When running the EA on GBPUSD currency, I set the indicator in the code for EURUSD, the EA crashes on startup. However, if I run the Expert Advisor on EURUSD and specify GBPUSD as the indicator, everything works. What can it be related to?

   CSignalAC *filter0=new CSignalAC;

   if(filter0==NULL)

     {

      //--- failed

      printf(__FUNCTION__+": error creating filter0");

      ExtExpert.Deinit();

      return(INIT_FAILED);

     }

   signal.AddFilter(filter0);

//--- Set filter parameters

   filter0.Symbol("EURUSD");

   filter0.Weight(Signal_AC_Weight);
 
Nikita Bushuev:

Afternoon. When running an EA on GBPUSD currency, I set an indicator in the code for EURUSD, the EA crashes on startup. However, if I run the EA on EURUSD and specify GBPUSD as the indicator, everything works. What can this be due to?

1. Generate an EA using Wizard MQL5 - for example, specify 'GBPUSD' for the EA and 'EURUSD' for the module of signals. You will get a RIGHT sample code. Study the code. In the future, just avoid manual editing, and generate the code using the MQL5 Wizard.

2. If you specified 'GBPUSD' for an Expert Advisor, you should run this EA ONLY on 'GBPUSD'.

Files:
1.mq5  13 kb
 
Vladimir Karputov:

1. Generate an EA using Wizard MQL5 - specify for example 'GBPUSD' for the EA and 'EURUSD' for the signal module. You will get a RIGHT sample code. Study the code. In the future, just avoid manual editing, and generate the code using the MQL5 Wizard.

2. If you specified 'GBPUSD' for an Expert Advisor, then you should run this EA ONLY on 'GBPUSD'.

Vladimir, I generated the code using MQL5 Wizard. I tried your EA, it also does not work for me when I run it on GBPUSD currency.

It says the following in the log:

2020.08.16 13:01:20.793 Core 1  EURUSD: symbol to be synchronized
2020.08.16 13:01:20.793 Core 1  EURUSD: symbol synchronized already, 18 bytes received
2020.08.16 13:01:20.793 Core 1  2018.01.01 00:00:00   cannot load indicator 'Accelerator Oscillator' [4804]
2020.08.16 13:01:20.793 Core 1  2018.01.01 00:00:00   CSignalAC::InitAC: error initializing object
2020.08.16 13:01:20.793 Core 1  2018.01.01 00:00:00   CExpert::InitIndicators: error initialization indicators of signal object
2020.08.16 13:01:20.793 Core 1  2018.01.01 00:00:00   OnInit: error initializing indicators
2020.08.16 13:01:20.793 Core 1  tester stopped because OnInit returns non-zero code 1
Reason: