[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 512

 
Roman.:

It's an obsession with wanting to pass parameters by reference. :-)



How else? Same thing to calculate 100 times?
 
hoz:


Yes, I was wrong. In general. If it is clear here, the pending order is set only by price.

Here's the code:

The question is essentially uncomplicated. I passed formal parameters priceForBuy and priceForSell to the Trade(int signal, double& priceForBuy, double& priceForSell) function. The parameters are passed by the links. Isn't it done this way? The functions are local, not global.

An error has occurred during the compilation:

Where is the error coming from? I have already defined these parameters in theGetPriceToInput() function.

The priceForBuy and priceForSell variables were not found - the compiler says. You need to figure out how to pass the values of the variables by reference.
 
hoz:

How else could it be? Do you have to calculate the same thing over and over again 100 times?

Make the signal part of the expert, as in the textbook, and you'll be happy!

I gave you all the links in the thread earlier...


 
hoz:

How else could it be? Do you have to calculate the same thing 100 times over?
The idea is RIGHT, the implementation is CROSS! :)))
 

Well there are no concrete examples here:

https://docs.mql4.com/ru/basis/variables/formal

It is written at the top where the function parameters are like mine, i.e. & after the data type.

 
hoz:

Well there are no concrete examples here:

https://docs.mql4.com/ru/basis/variables/formal

It's written at the top where the function parameters are like mine, i.e. & after the data type.

Maybe it will compile that way:

//+-------------------------------------------------------------------------------------+
//| Получение цены входа в покупку или в продажу                                        |
//+-------------------------------------------------------------------------------------+
bool GetPriceToInput (int signal)
{
  firstBarClosed = iClose(Symbol(),1440,2);
  secondBarClosed = iClose(Symbol(),1440,1);

    double deltaForSell = (firstBarClosed - secondBarClosed)/2;
    double priceForSell = secondBarClosed - deltaForSell;


    double deltaForBuy = (secondBarClosed - firstBarClosed)/2;
    double priceForBuy = secondBarClosed + deltaForBuy;  
    return (Trade (signal, priceForBuy, priceForSell));
}
//+-------------------------------------------------------------------------------------+
//| Функция Start                                                                       |
//+-------------------------------------------------------------------------------------+
int start()
{
  int signal = GetSignal();
    
    if (signal != SIGNAL_NO)
      if(!GetPriceToInput (signal))
      return(0);

  return(0);
}

although the code structure is kind of "left-handed"...

 
TarasBY:

Maybe it will compile that way:

although the code structure is kind of "left-handed"...


Well... I was the one who wrote it. Sing the first scoop. :)
 

I rewrote a little bit of the expert. This is what came out:

//+------------------------------------------------------------------+
//|                                                       2 Days.mq4 |
//|                                                              hoz |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "hoz"
#property link      ""

extern string    A1 = "Объем сделки. Если i_lots = 0, то считается в процентах";
extern double    i_lots = 0.1;
extern string    A2 = "Управление рисками";
extern double    i_sl = 15,
                 i_tp = 10;
extern int  slippage,
            price_b,
            price_a;

extern string    Z1 = "=== Прочие настройки ===";
extern string    i_openOrderSound = "ok.wav";
extern int       i_magicNumber = 400021;

double firstBarClosed,
       secondBarClosed;
 
// Идентификаторы типов сигналов
#define SIGNAL_BUY              1                     // Сигнал на покупку
#define SIGNAL_SELL            -1                     // Сигнал на продажу
#define SIGNAL_NO               0                     // Нет сигнала

#include <stderror.mqh>

//+-------------------------------------------------------------------------------------+
//| Функция деиницилизации эксперта                                                     |
//+-------------------------------------------------------------------------------------+
int init()
{
   
   return(0);
}
//+-------------------------------------------------------------------------------------+
//| Функция деиницилизации эксперта                                                     |
//+-------------------------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+-------------------------------------------------------------------------------------+
//| Открытие позиций                                                                    |
//+-------------------------------------------------------------------------------------+
bool Trade(int signal, double& priceForBuy, double& priceForSell)
{
 // FindOrders();
  priceForBuy = NormalizeDouble(priceForBuy,Digits);
  priceForSell = NormalizeDouble(priceForSell,Digits);
  
  if (signal == SIGNAL_BUY)
     if (!OrderSend(Symbol(),OP_BUYLIMIT, i_lots,priceForBuy,slippage,i_sl,i_tp,"",i_magicNumber,3))
     return(false);
     
  if (signal == SIGNAL_SELL)
     if (!OrderSend(Symbol(),OP_SELLLIMIT,i_lots,priceForSell,slippage,i_sl,i_tp,"",i_magicNumber,3))
     return(false);
     
  return(true);
}
//+-------------------------------------------------------------------------------------+
//| Получение цены входа в покупку или в продажу                                        |
//+-------------------------------------------------------------------------------------+
double GetPriceToInput(int signal)
{
  firstBarClosed = iClose(Symbol(),1440,2);
  secondBarClosed = iClose(Symbol(),1440,1);
    
  double deltaForSell = (firstBarClosed - secondBarClosed)/2;
  double priceForSell = secondBarClosed - deltaForSell;
    
//  return(priceForSell);

  
  double deltaForBuy = (secondBarClosed - firstBarClosed)/2;
  double priceForBuy = secondBarClosed + deltaForBuy;  
  return(Trade(signal, priceForBuy, priceForSell));
}
//+-------------------------------------------------------------------------------------+
//| Генерация сигнала закрытия, покупки или продажи                                     |
//+-------------------------------------------------------------------------------------+
int GetSignal()
{
  if(firstBarClosed > secondBarClosed)
  return(SIGNAL_BUY);

  if(firstBarClosed < secondBarClosed)
  return(SIGNAL_NO);
}
//+-------------------------------------------------------------------------------------+
//| Функция Start                                                                       |
//+-------------------------------------------------------------------------------------+
int start()
{
  int signal = GetSignal();
    
    if (signal != SIGNAL_NO)
      if(!Trade(signal, priceForBuy, priceForSell))
      return(0);

  return(0);
}

 

By the way, the error:

'priceForBuy' - variable not defined    E:\Insall'd soft's\Forex\Admiral Markets\experts\2 Days.mq4 (103, 25)
'priceForSell' - variable not defined   E:\Insall'd soft's\Forex\Admiral Markets\experts\2 Days.mq4 (103, 38)

occurs exactly in thestart function these variables are not defined, not somewhere else. So something needs to be added to start?

 
hoz:

By the way, the error:

occurs exactly in thestart function these variables are not defined, not somewhere else. So something needs to be added to start?

They must be defined in start() and passed to the called function by reference.
 
Reshetov:
They must be defined in start() and passed by reference to the function we call.


This is the simplest way. But I am watching articles written by one programmer and there is nothing defined in the start() function at all.

Here's a link to his advisor:

http://www.forextrade.ru/media/Image/MQLabs/181_ag/ChannelByMACross_Expert.mq4

In particular, I'm very interested in how he writes code intelligently. Therefore, I want to learn how to write everything in separate functions, and add only the essentials tostart without declaring variables there.

If we pass a variable by reference into a function, we are working with a buffer of the original variable, not with its separate double. This means it doesn't matter which function we previously declared it in. Isn't it?

Reason: