Self-learning the MQL5 language from scratch - page 84

 
Georgiy Merts #:

That's what I'm saying!

And you say 'no one has seen'. No one is interested - so no one has seen it (clowns are not interested - they haven't seen it). Those who are interested - they have seen.

Sent a complaint to the moderators against the clown, and will do so until you get banned
 
Vladimir Baskakov #:
Sent a complaint to the moderators about the clown, and will do so until you get banned

Oh, scared the hedgehog with his bare ass...

 
Georgiy Merts #:

Oh, scared the hedgehog with his bare ass...

I'm in no hurry.
 
Georgiy Merts #:

I don't see the problem.

Right. A filled array is declared, and then - it is filled again, with other data. In my opinion, it's quite an acceptable error for a beginner.

And is it an error?

When I'm declaring variables and arrays, I MUST FILL them IMMEDIATELY. If at the moment of declaration the exact value is unknown, I fill it with knowingly invalid value. If a variable is declared without initialization and then used, it can contain random values, which will be used during work, and the result of this work will be incorrect and it will be very difficult to find the problem (I've found it more than once).

But if a variable is initialized with invalid value right at creation, in case of using uninitialized variable it will be immediately found out that it contains some rubbish, it will be easy to find where it came from and see that the variable was used without initialization.

So personally, I think that declaring an array already populated is good practice. But we should take WRONG_VALUE values there.

I'm not against initializing variables and even arrays at all. For example, like this

int ExtHandle[21] = {};

will also be an initialized array. But even this in this case is not necessary because the entire array is filled in OnInit(). So I do not see any need in it. And even Pinocchio with his apples does not work here, because before using (reading) values, they will already be initialized.

 
Alexey Viktorov #:

I'm not at all against initialising variables and even arrays. For example, like this

will also be an initialized array. But even this in this particular case is not necessary, because in OnInit() the entire array is filled. So I do not see any need in it. And even Pinocchio with his apples won't work here, because before using (reading) values, they will already be initialized.

Eh... And where will it be "initialized"? By code, such an array will be filled with rubbish.

In my opinion, it's good practice to initialize with non-invalid values at creation, as I've already said:

int ExtHandle[3] = {WRONG_VALUE;WRONG_VALUE;WRONG_VALUE};

Of course, if array is big - it's unreasonable to fill it by such method, in this case I use DEBUG-version, in which I fill even big array with non-invalid values, while in RELEASE-version there is no filling.

This practice has repeatedly saved me from using uninitialized variables.

 
SanAlex #:

Your name in the code - the code you set up I created under your name to know. But it didn't work.


Well, who's to blame for your bad hands?

On the image you can see that the calculated value is 9.08253 and after conversion 9.08 and do not tell me about 1 in 16 decimal places.

And also, if you get a value less than the minimum volume

I had to enter another variable to show...

 
Georgiy Merts #:

Erm... And where would it be "initialized"? By code, such an array would be full of rubbish.

In my opinion, it's good practice to initialize at creation with invalid values, as I've already said:

Of course, if array is big - it's unreasonable to fill it by such method, in this case I use DEBUG-version, in which I fill even big array with non-invalid values, while in RELEASE-version there is no filling.

This practice has repeatedly saved me from using uninitialized variables.

Right here, in the highlighted loop. And note the fact that I was only talking about one array.

Forum on trading, automated trading systems and strategy testing

Self-study in MQL5 from scratch

SanAlex, 2021.11.29 07:17

i can't understand what you're talking about. i did it with my own hands. what do you mean? the code is from the terminal, but the way itself, how to implement opening a few pairs in the tester, i personally achieved the result.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Show me where I took the code from you.

//+------------------------------------------------------------------+

//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
#include <Trade\Trade.mqh>
//---
CTrade ExtTrade;
//+------------------------------------------------------------------+
input double MaximumRisk        = 0.02; // Maximum Risk in percentage
//---
int    ExtHandle[]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
double LotsRisk[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
string symb_name[]= {"EURUSD","GBPUSD","USDCHF","USDJPY","USDCAD","AUDUSD","AUDNZD",
                     "AUDCAD","AUDCHF","AUDJPY","CHFJPY","EURGBP","EURAUD","EURCHF",
                     "EURJPY","EURNZD","EURCAD","GBPCHF","GBPJPY","CADCHF","GBPAUD"
                    };

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ExtTrade.SetExpertMagicNumber(0);
   ExtTrade.SetMarginMode();
//---
   for(int i=0; i<20; i++)
     {
      ExtTrade.SetTypeFillingBySymbol(symb_name[i]);
      //--- Moving Average indicator
      ExtHandle[i]=iMA(symb_name[i],_Period,12,6,MODE_SMA,PRICE_CLOSE);
      if(ExtHandle[i]==INVALID_HANDLE)
        {
         printf("Error creating MA indicator");
         return(INIT_FAILED);
        }
     }
//---
   return(INIT_SUCCEEDED);
  }


I've removed everything from the code that doesn't concern our dialogue. Well, almost everything...

 
Alexey Viktorov #:

Right there, in the highlighted loop. And note the fact that I was only talking about one array.

Yes, that's right. In my opinion, normal practice.

 
SanAlex #:

This one is almost ready - I just need to remove unnecessary non-functional functions. This multicurrency Expert Advisor is from this "MACD Sample.mq5"

I seem to have removed all unnecessary things for it to work. I have to polish it to the taste and then I will post it here.

Screenshot 2021-11-29 155519

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

This is what I have got so far - maybe someone will do it better

Files:
00001_Mult.mq5  15 kb
 
Alexey Viktorov #:

I really hope you didn't write this...

Here's a working function

All you have to do is enter the risk size to count not from the full free margin...

Good day and good mood everyone!

It took me a long time to understand and examine arrays, but I already have the result, because I wrote the code of function that calculates the risk for 12 currency pairs by myself.

Regards, Vladimir.

//+------------------------------------------------------------------+
//| Входные параметры советника                                      |
//+------------------------------------------------------------------+

input double   Risk0=1.0;           //Риск для валютной пары EURUSD (% от баланса)
.
.
.
.
input double   Risk11=1.0;           //Риск для валютной пары EURJPY (% от баланса)

//+------------------------------------------------------------------+
//| Функция Money_Management рассчитывает размер лота в зависимости  |
//| от риска, заданного во входных параметрах советника.             |
//+------------------------------------------------------------------+
double Money_Management()
  {
//--- объявляем динамический массив
   double risk[];
//--- устанавливаем размер массива
   ArrayResize(risk,12);
//--- заполняем массив значениями риска
   ArrayFill(risk,0,1,Risk0);
   ArrayFill(risk,1,1,Risk1);
   ArrayFill(risk,2,1,Risk2);
   ArrayFill(risk,3,1,Risk3);
   ArrayFill(risk,4,1,Risk4);
   ArrayFill(risk,5,1,Risk5);
   ArrayFill(risk,6,1,Risk6);
   ArrayFill(risk,7,1,Risk7);
   ArrayFill(risk,8,1,Risk8);
   ArrayFill(risk,9,1,Risk9);
   ArrayFill(risk,10,1,Risk10);
   ArrayFill(risk,11,1,Risk11);
//---
   double price=0.0;
   double margin=0.0;
   double MaximumRisk=risk[11]/100;
//---
   if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))
      return(0.0);
   if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))
      return(0.0);
   if(margin<=0.0)
      return(0.0);
//---
   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
//---
   double lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*MaximumRisk/margin,2);
//---
   if(lot<min_volume)
      lot=min_volume;
   if(lot>max_volume)
      lot=max_volume;
   return(lot);
  }
//+------------------------------------------------------------------+