Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1478

 
Valeriy Yastremskiy #:

and what do you mean by initialise))))) it's kind of unclear to the person)))))

Good evening, Valery! We open the article proposed to the person and find there first a brief description:

Далее идет функция инициализации советника. Это функция вызывается первой после запуска советника или смены графика и вызывается только один раз.

Этот раздел - лучшее место для проведения проверок, чтобы убедиться в правильности работы нашего советника.

Например, можно проверить, достаточно ли баров на графике для работы нашего советника и т.п.

Также это лучшее место для получения хэндлов технических индикаторов, которые будут использоваться (в нашем случае это индикаторы ADX и Moving Average).

and then a more detailed one:

int OnInit()
  {
//--- Получить хэндл индикатора ADX
   adxHandle=iADX(NULL,0,ADX_Period);
//--- Получить хэндл индикатора Moving Average
   maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
//--- Нужно проверить, не были ли возвращены значения Invalid Handle
   if(adxHandle<0 || maHandle<0)
     {
      Alert("Ошибка при создании индикаторов - номер ошибки: ",GetLastError(),"!!");
     }

In general, he should at least read this article first. ))

Regards, Vladimir.

 

Good day everyone!

I am continuing my self-study and again encountered a confusion. Here is the script code:

//+------------------------------------------------------------------+
//| Input variables                                                  |
//+------------------------------------------------------------------+
input uchar candles = 60;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   MqlRates price_array[];
   double price_low[];
   ArraySetAsSeries(price_low, true);
   ArraySetAsSeries(price_array, true);
   int copy_low = CopyLow(_Symbol, _Period, 0, candles, price_low);
   if(copy_low > 0)
     {
      int candle_low = ArrayMinimum(price_low, 0, candles);
      int Data = CopyRates(_Symbol, _Period, 0, candles, price_array);
      ObjectCreate(0, "UpwardTrendline", OBJ_TREND, 0, price_array[candle_low].time, price_array[candle_low].low,
                   price_array[0].time, price_array[0].low,0);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_COLOR, Blue);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_WIDTH, 3);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_RAY_RIGHT, true);
     }
  }
//+------------------------------------------------------------------+

The script was supposed to draw a trend line as a segment. To do this, I set the initial and the second point to draw this segment. The documentation also says that you need two anchor points to create a trend line. I think I have done everything as it should be done. In my understanding, the second point is also an extreme point, but for some reason it is drawn not a segment, but a ray.


Could you please tell me where I am mistaken?

Regards, Vladimir.

 

MrBrooklin #:
     

      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_COLOR, Blue);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_STYLE, STYLE_SOLID);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_WIDTH, 3);
      ObjectSetInteger(0, "UpwardTrendline", OBJPROP_RAY_RIGHT, true);
ObjectSetInteger(0, "UpwardTrendline", OBJPROP_RAY_RIGHT, false);
 
Artyom Trishkin #:

Thank you, Artyom! Man, I didn't have enough intelligence to understand such a simple and obvious thing. God bless you!

Regards, Vladimir.

 

Good morning and good mood, everyone!

That's it! "I'm coming, your roof!" That's me. I run the script:

//+------------------------------------------------------------------+
//|                                              Count_Pos (v.2).mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Calculate positions Buy and Sell                                 |
//+------------------------------------------------------------------+
void Calc_Pos(uint pos_buy,uint pos_sell)
  {
   for(int i=0; i<PositionsTotal(); i++)
     {
      PositionGetSymbol(i);
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
         pos_buy++;
      if(
         PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
         pos_sell++;
     }
   Print("pos_buy ",pos_buy);
   Print("pos_sell ",pos_sell);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   uint count_buy=0, count_sell=0;
   Calc_Pos(count_buy,count_sell);
   Print("По символу ",_Symbol," открыто: ",count_buy," длинных позиций");
   Print("По символу ",_Symbol," открыто: ",count_sell," коротких позиций");
  }
//+------------------------------------------------------------------+

and I get this:

2023.11.22 07:59:29.375 Count_Pos (v.2) (EURUSDrfd,M1)  pos_buy 0
2023.11.22 07:59:29.375 Count_Pos (v.2) (EURUSDrfd,M1)  pos_sell 2
2023.11.22 07:59:29.375 Count_Pos (v.2) (EURUSDrfd,M1)  По символу EURUSDrfd открыто: 0 длинных позиций
2023.11.22 07:59:29.375 Count_Pos (v.2) (EURUSDrfd,M1)  По символу EURUSDrfd открыто: 0 коротких позиций

and there should be two short positions. Could you please tell me where I have made a mistake?

Regards, Vladimir.

 
MrBrooklin #:

Good morning and good mood, everyone!

That's it! "I'm coming, your roof!" That's me. I'm running the script:

and I get this:

and there should be two short positions. Could you please tell me where I've made a mistake?

Regards, Vladimir.

The last two prints

are outputting the value of variables

uint count_buy=0, count_sell=0;

which are initialised, but nothing was assigned to them further.
I.e. everything works as written - no miracles.

 
Alexander Sevastyanov #:

The last two prints

outputs the value of variables

which are initialised, but nothing was assigned to them further.
I.e. everything works as written - no miracles.

Hello, Alexander. That's where I was stumped. What should be assigned? I think the Calc_Pos(count_buy, countsell) function should work, or do I misunderstand?

Regards, Vladimir.

 
MrBrooklin #:

Hello, Alexander. That's where I got stumped. What should be assigned? I think the function Calc_Pos(count_buy, countsell) should work, or do I misunderstand?

Regards, Vladimir.

1. You declare and initialise two variables.

uint count_buy=0, count_sell=0;

2. You pass their values to the function.

Calc_Pos(count_buy,count_sell);

It is not quite clear why this is done.
Then you output the values of these variables. But they have not been changed anywhere,
respectively, and zeros are printed. Everything works as written.

P.S. Trying to figure out your plan: you can declare these two variables global.
For example, like this:

//+------------------------------------------------------------------+
//|                                              Count_Pos (v.2).mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

   uint pos_buy=0, pos_sell=0;  // это глобальные переменные

//+------------------------------------------------------------------+
//| Calculate positions Buy and Sell                                 |
//+------------------------------------------------------------------+
void Calc_Pos()
  {
   for(int i=0; i<PositionsTotal(); i++)
     {
      PositionGetSymbol(i);
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
         pos_buy++;
      if(
         PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
         pos_sell++;
     }
   Print("pos_buy ",pos_buy);
   Print("pos_sell ",pos_sell);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Calc_Pos();
   Print("По символу ",_Symbol," открыто: ",pos_buy," длинных позиций");
   Print("По символу ",_Symbol," открыто: ",pos_sell," коротких позиций");
  }
//+------------------------------------------------------------------+
 
Alexander Sevastyanov #:

1. You declare and initialise two variables.

2. You pass their values to the function.

It is not quite clear why this is done.
Then output the values of these variables. But they have not been changed anywhere,
respectively and are printed with zeros. Everything works as written.

P.S. Trying to figure out your plan: you can declare these two variables global.
For example, like this:

Thank you very much, Alexander. Your correction made the code work. To be honest, I realised that I was confused in three pines myself. I need to study passing arguments to a function again. Apparently, I have a complete misunderstanding here.

Regards, Vladimir.

 
MrBrooklin #:

Thank you very much, Alexander. Your correction made the code work. To be honest, I realised that I was confused in three pine trees myself. I need to study passing arguments to a function again. Apparently, I have a complete misunderstanding here.

Regards, Vladimir.

Outputting variables to the global area is not passing arguments to a function...

//+------------------------------------------------------------------+
//|                                              Count_Pos (v.2).mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Calculate positions Buy and Sell                                 |
//+------------------------------------------------------------------+
void Calc_Pos(uint & pos_buy,uint & pos_sell)
  {
   for(int i=0; i<PositionsTotal(); i++)
     {
      PositionGetSymbol(i);
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
         pos_buy++;
      if(
         PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
         pos_sell++;
     }
   Print("pos_buy ",pos_buy);
   Print("pos_sell ",pos_sell);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   uint count_buy=0, count_sell=0;
   Calc_Pos(count_buy,count_sell);
   Print("По символу ",_Symbol," открыто: ",count_buy," длинных позиций");
   Print("По символу ",_Symbol," открыто: ",count_sell," коротких позиций");
  }
//+------------------------------------------------------------------+

Put & before variable names at the input to the function and all the problems will be solved.

Reason: