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

 
-Aleks-:

Can you show me on my function how to do this? I'm really struggling to understand it.


You have to decide which variables from the function you need (for the main program) and pass/retrieve them. If you need All, then declare these variables at the beginning of the program and they will be visible to all functions at once.
 
-Aleks-:

Don't think 'why', better to think 'how'.

I told you, it doesn't apply to this function...

Such functions are big, as a rule, that's why it's a pity to call them for execution several times in order to get different values, but I want to get all results at once.

Well, for example, I want to get at once the number of all lots and the number of all orders broken down by order type.

#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
double n_Lot_BUY;
double n_Lot_SELL;
int n_Order_BUY;
int n_Order_SELL;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
   F();
   Print("Лотов BUY:",n_Lot_BUY,"   Лотов SELL:",n_Lot_SELL,"   Позиций BUY:",n_Order_BUY,"   Позиций SELL:",n_Order_SELL);
  }
//+------------------------------------------------------------------+
void F()
  {
   n_Lot_BUY=0;
   n_Lot_SELL=0;
   n_Order_BUY=0;
   n_Order_SELL=0;
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
     {
      if(!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)){break;}
      if(OrderType()==0)
        {
         n_Order_BUY++;
         n_Lot_BUY=n_Lot_BUY+OrderLots();
        }
      if(OrderType()==1)
        {
         n_Order_SELL++;
         n_Lot_SELL=n_Lot_SELL+OrderLots();
        }
     }
  }
//+------------------------------------------------------------------+
 
Nikolay Gaylis:

I mean, the idea is to declare all variables beforehand, and then modify them through a function - got it, thanks for the option. But, it's not always convenient, if one and the same variable name is used for the same kind of variables... no other options?
 

the same types can be combined into a structure and declared as an array, as suggested above)

//структура из переменных
struct OrdStruct
   {
   double tp;
   double sl;
   double open;
   double close;
   double lot;
   datetime time_open; 
   datetime time_close;
   };

//массив структур
OrdStruct ORDERS[];


//обращение к переменным
ORDERS[i].sl=OrderStopLoss();
ORDERS[i].tp=OrderTakeProfit();
ORDERS[i].lot=OrderLots();

//или
ORDERS[i]=ORDERS[i+1];

ZeroMemory(ORDERS[i]);
 
Taras Slobodyanik:

The same types can be combined into a structure and declared as an array, as suggested above)

This is already difficult to understand... for example, I can't understand i - to what it's equal and, shouldn't the ORDERS[i]= array be given a size?

 
-Aleks-:

This is already difficult to understand... for example, I can't understand i - what is equal and, shouldn't the ORDERS[i]= array be sized?


This is just an example, of course i needs to be specified and the size of the array needs to be adjusted according to the number of orders...

i.e. the variables are sort of the same tp / sl / lot ... but the index of array i changes

 
-Aleks-:

It pains me to see your anguish... You are on a technical forum, use logic, please.

1 Describe the problem;

Describe the variables you are using;

Describe what you want to get as an output;

Learn to express your ideas in a technical language on a technical forum. Otherwise, it will take a long time to guess here.

 
Taras Slobodyanik:


This is for example, of course i need to specify and set the size of the array depending on the number of orders...

i.e. the variables are sort of the same tp / sl / lot ... but the index of the array i changes

Yes, I got it, thanks, I should try to use it in the future, somewhere.... I just do not understand, and work with this structure as an array? Sort and perform other standard operations?

 
Alexey Kozitsyn:

It pains me to see your anguish... You are on a technical forum, use logic, please.

1. describe the problem;

Describe the variables you are using;

Describe what you want to get as an output;

Learn to express your ideas in a technical language on a technical forum. Otherwise, it will take a long time to guess here.

Please write where there is no logic in my words?

Torment in knowing is pleasure in solving.

I didn't want to spill my problem, so I kept it simple - I find it difficult to guess what caused the difficulty in perceiving... Sorry if I made you feel uncomfortable with my "anguish".

 
-Aleks-:

I.e. the idea is to declare all variables in advance, and then change them through a function - got it, thanks for the variant. But, it's not always convenient if one and the same variable name is used for the same kind of variables... no other options?

Read carefully here and you'll see what's going on. This describes the first option, which was proposed, parameter passing by reference.

Simply put, when a function is called, the variable can be passed by parameter

double FirstMethod(int i,int j)

or by reference if the variable name is preceded by an & sign.

double SecondMethod(int &i,int &j)

In the second variant, the variables passed to the function by reference will be changed along with the variables that have even different names. In the special case these are variables i and j

Everything else is clear enough from the help text.

Передача параметров - Функции - Основы языка - Справочник MQL4
Передача параметров - Функции - Основы языка - Справочник MQL4
  • docs.mql4.com
Передача параметров - Функции - Основы языка - Справочник MQL4
Reason: