Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 827

 

Hello.

Let's say I have 2 indicators, the first has 4 buffers, the second has 2. I put them on a chart and open data window (CTRL+D). The list contains 6 buffers from 0 to 5. How can I read the value of buffer 5, say, from this list in MQL? It is not convenient to use iCustom by the required indicator. It is more convenient to use this list. I know they do it, but how?

 

Studying the examples in the help, object creation is always done with a check:

//--- сбросим значение ошибки 
   ResetLastError(); 
//--- создадим кнопку 
   if(!ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0)) 
     { 
      Print(__FUNCTION__, 
            ": не удалось создать кнопку! Код ошибки = ",GetLastError()); 
      return(false); 
     } 


Why is this done for objects? What is the probability of an object failing to be created?


In general, I am interested in where I can read about rules of good manners in mql programming, to know in which bushes lie the rake, advise.

 
psyman:

Studying the examples in the help, object creation is always done with a check:


Why is this done for objects? What is the probability of an object failing to be created?


In general, I am interested in where I can read about good manners in mql programming, to know where the pitfalls lie, advise me.

For example, an object with this name already exists.
The rules of good form - they are the same for any language. Receive data - check what you've got, create an object, check what you've created, and whether you've created it... And so on.
 
How to fill 4-dimensional array with values of the format int Mas[1][2][2][2], how to assign a value to each cell of the array, we have in mind such a notation for example a 2-dimensional array int Arr[1][2]={3,4};, how to fill 4-dimensional?
 
Artyom Trishkin:
For example, an object with this name already exists.
And the rules of etiquette - they are the same for any language. You receive data - check what you have received, you create an object, check what you have created and whether you have created it... And so on.


When I create a button inside a function, I get swearing at

 return(false);


'return' - 'void' function returns a value 112 16


What should I do in this case?

 
Seric29:
How to fill 4-dimensional array with values of the format int Mas[1][2][2][2], how to assign a value to each cell of the array, in this case, there is a 2-dimensional array int Arr[1][2]={3,4}; how to fill 4-dimensional?

Multidimensional arrays are usually filled in loops with the right values - that makes it harder to make a mistake, for a four-dimensional array it should be something like this, but I could be wrong:

int A[2,2,4,3] = {
                     {/*----------- 3-e измерение = 4 ---------------------------*/ 
/*2-e измерение=2 */ {  {0,1,2/*4-e измерение = 3 */}, {0,1,2}, {0,1,2}, {0,1,2} },
                        { {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2} }   
               
                     },
 // 1-е измерение = 2           
                     {  { {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2} },
                     {   {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2} }}
                  }; 

psyman:

When I create a button inside a function, I get swearing at

The 'return' - 'void' function returns a value 112 16

What should I do in this case?

start reading the helpat https://www.mql5.com/ru/docs/basis/function

If you need more help, you should read any C++ book. The first chapter should explain the functions

 
psyman:


When I create a button inside a function, I get a swearing at


'return' - 'void' function returns a value breakeven_fish.mq5 112 16


What to do in this case?

If you return a bool value from a function, you don't need to make the function type void, but guess what it is.
 

Comrades - look at the indicator for correctness. There is one formula Value = (Open[0]-SMA(P,n))/SMA(P,n))*100%.

I put it on the chart - it calculates and plots everything. I start checking on some bar on the calculator - its value does not coincide with the one drawn...

Here is an example - I calculate 1,0178 for Open and 1,0182 for MA(13) using the formula ((1,0178-1,0182)/1,0182) * 100% - the result on the calculator is -0.0393. And the MT4 chart shows -0.0929. Is it an error in the code or the calculator does not correctly calculate some values there? Please help. I am attaching the indicator.

#property indicator_separate_window 
#property indicator_buffers 1       // Количество буферов
#property indicator_color1  Blue    // Цвет линии 0 буфера

#property  indicator_level1  1
#property  indicator_level2  -1
//--------------------------------------------------------------- 2 --
//---- indicator parameters 

extern int History    =5000;      // Колич.баров в расчётной истории
extern int Period_SMA =13; 
 

double
   Line_0[];                        // Инд. массив  ravi 
    
//--------------------------------------------------------------- 4 --

int init()                          // Специальная функция init()
  {
   SetIndexBuffer(0,Line_0);        // Назначение массива буферу
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Стиль линии
   
   IndicatorShortName("KRI");
//--------------------------------------------------------------- 5 --
   
   return(0);                          // Выход из спец. функции init()
  }

//--------------------------------------------------------------- 8 --
int start()                         // Специальная функция start()
  {
//--------------------------------------------------------------- 9 --
  
   double
   SMA;                   //  МА для расчета значений индикатора 
                                 // формyла (PRICE-SMA/SMA)*100); 
                             
   int
   i,                            // Индекс бара
   n,                            // Формальн. параметр(индекс бара)
   Counted_bars;                 // Количество просчитанных баров 

//-------------------------------------------------------------- 10 --
   Counted_bars=IndicatorCounted(); // Количество просчитанных баров 
   i=Bars-Counted_bars-1;           // Индекс первого непосчитанного
   if (i>History-1)                 // Если много баров то ..
      i=History-1;                  // ..рассчитывать заданное колич.
//-------------------------------------------------------------- 11 --
   while(i>=0)                      // Цикл по непосчитанным барам
     {
      //-------------------------------------------------------- 12 --
        
      SMA=iMA(NULL,0,Period_SMA,0,MODE_SMA,PRICE_CLOSE,i); // Значение  SМА
     
      
      Line_0[i] =0; 
      if (SMA>0) Line_0[i] = ((Open[0]-SMA)/SMA)*100;    //  Индик. массив линии KPI
     
      //-------------------------------------------------------- 13 --
      i--;                          // Расчёт индекса следующего бара
      //-------------------------------------------------------- 14 --
     }
   return(0);                          // Выход из спец. ф-ии start()
  }
//-------------------------------------------------------------- 15 --
Files:
KRI.mq4  7 kb
 
Artyom Trishkin:
If you are doing return of bool-type value from function, then function type should not be void, but guess which one it is.

What if you want to return data from a function?


Is it possible to loop through the positions only for a given symbol without using PositionsTotal every time?


So far, the only thing I've come up with is to write the tickets into an array and work with them separately, but maybe there's a simpler option?

 
psyman:

What if you want to return data from a function?


Is it possible to loop through the positions only for a given symbol without using PositionsTotal every time?


So far, the only thing I've come up with is to write the tickets to an array and work with them separately, but maybe there's a simpler option?

It's not clear what's needed in the end - some disjointed thoughts.

Reason: