Self-learning the MQL5 language from scratch - page 80

 

There are errors, so the code will not work as the author intended. You have to figure it out. You have the code, you have the cards to hand.


From what you have shown, I see that either the candles are less than 60, or the handle of one of the indicators is INVALID, which means that the parameters are sent to it incorrectly. Or there is an error in another part of the code that I do not see.

Regards, Vladimir.

Oops, Alexey;)

 
MrBrooklin #:

I managed to find out where the problem lies myself, but the question was about something else: what can be affected by these errors and what should I change in the code to eliminate them?

Sincerely, Vladimir.

If an error occurs, the Expert Advisor will not be attached to the chart.

You do not have to do anything. What has already been done - in case of an error, the Expert Advisor is detached from the chart

and informs the user.

This error does not practically occur, except when the computer is in a mess.

==

However, if it always happens during the development process...

It means that indicator is not called correctly - fix indicator parameters.

 
Dmitry Fedoseev #:

If the error happens, the EA will not attach to the chart.

You do not need to do anything. What has already been done - in the event of an error, the Expert Advisor detaches from the chart

and informs the user.

This error does not practically occur, except when the computer is in a mess.

==

However, if it always happens during the development process...

It means that the indicator is called incorrectly - fix the indicator parameters.

Thank you, Dmitry for your comprehensive and understandable answer!

Regards, Vladimir.

 
SanAlex #:

here it works - maybe you're not setting the period correctly

Thanks for the tip!!! Indeed, the problem was that I couldn't set the periods for both indicators from "zero" and above in the tester settings. I should have set them at least from "one" and above.

Thank you all for your participation!

Regards, Vladimir.

 
MrBrooklin #:

P.S. You should remove the EA's code "spoils", because these "spoils" annoy a lot of people. I've already removed mine.

Those who are very annoying let them go woods periodically taking motherwort or tincture of fly agaric.

I don't like those shoelaces either, but I just don't watch them.

 
Not many people want to learn the language. Why would that be
 
Vladimir Baskakov #:
I've decided to learn the language but not many people have started learning it. What for?

Vladimir, it's very simple. I have learned what I needed to learn. Now I take different EAs and make them for myself. The most important thing is that now I no longer look at MQL5 as the Chinese characters, but as a normal piece of software. Many thanks to all professionals who helped me in my self-study!

Regards, Vladimir.

 
MrBrooklin #:


I'd rather have a spoiler than download a file and stick it in my sandbox. )

 

Good morning everyone and good mood!

Continuing self-study of MQL5 programming language. The time came when I had to deal with arrays. I have decided to calculate the lot size depending on the risk specified in the input parameters. I have no errors or warnings after compilation, but the function code does not work. Can you please tell me please, where is the error?

Sincerely, Vladimir.

input double   Risk0=1.0;           //Риск (% от баланса)
.
.
.
.
input double   Risk9=1.0;           //Риск (% от баланса)

//+------------------------------------------------------------------+
//| Функция Money_Management рассчитывает размер лота в зависимости  |
//|  от риска, заданного во входных параметрах советника.            |
//+------------------------------------------------------------------+
double Money_Management()
  {
   static int Risk[];
   int Number=0;
//----+ Объвляем переменную для хранения размеров массивов переменных
   static int Size_ = 0;
//----+ Изменяем размер массивов переменных
   if(Number + 1 > Size_)
     {
      uint size = Number + 1;
      //---- Предварительно обнуляем ячейки массива
      Risk[Number] = false;
     }
   if(Risk[0] != Risk[Number])
     {
      Risk[Number] = Risk[0];
     }
   double Lots=AccountInfoDouble(ACCOUNT_MARGIN_FREE)*Risk[Number]/100000*10;
   Lots=MathMin(5,MathMax(0.1,Lots));
   if(Lots<0.1)
      Lots=NormalizeDouble(Lots,2);
   else
     {
      if(Lots<1)
         Lots=NormalizeDouble(Lots,1);
      else
         Lots=NormalizeDouble(Lots,0);
     }
   return(Lots);
  }
//+------------------------------------------------------------------+
 
MrBrooklin of MQL5 programming language. The time came when I had to deal with arrays. I have decided to calculate the lot size depending on the risk specified in the input parameters. I have no errors or warnings after compilation, but the function code does not work. Can you please tell me please, where is the error?

Sincerely, Vladimir.

I very much hope that you did not write it...

Here's a working function

/********************************************************************\
|   Calculate optimal lot size     Расчет объема лота                |
/********************************************************************/
double contractSize(double Lot)
 {
  double volume = Lot > 0.0 ? Lot : AccountInfoDouble(ACCOUNT_MARGIN_FREE)/10000,
         v = volume,
         volumeStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP),
         minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN),
         maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
  v = round(volume/volumeStep)*volumeStep;
  return(fmin(maxLot, fmax(minLot, v)));
 }/******************************************************************/

All you need to do is to enter the amount of risk, so that it doesn't count from the full free margin...

Reason: