Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 149

 
gInitTrue = false;   // В глобальных
-------------------------------------
// Сама функция нашего ИНИТА
bool myInit()
{
   // Инициализируем..
   // неоходимые..
   // нам..
   // переменные.
   return(gInitTrue =true);
}
-------------------------------------
//Вызов из старта так:
if (gInitTrue = false)
   myInit();
 

hoz:

gInitTrue = false;   // В глобальных
-------------------------------------
// Сама функция нашего ИНИТА
bool myInit()
{
   // Инициализируем..
   // неоходимые..
   // нам..
   // переменные.
   return(gInitTrue =true);
}
-------------------------------------
//Вызов из старта так:
if (gInitTrue = false)
   myInit();

It's the same as:

gInitTrue = false;   // В глобальных
-------------------------------------

bool init()
{
   if
   {
   // Инициализируем..
   // неоходимые..
   // нам..
   // переменные..
   }
   return(gInitTrue =true);
}
-------------------------------------
void start()
 {//Вызов из старта так:
  if (gInitTrue = false) init();
 }
 
001:

I can't figure out how to implement the logic with the least amount of effort.

If(...) set a stop order;

If(the order's lifetime>time) withdraw the order, and if(...) set a new order;

The difficulty is that there may be several positions which are already open, and how do we fight them all? What is the easiest way?

Thank you!

int MagicNumber=555;
//---
if (OrdersTotal()>0)
{  for (int i=OrdersTotal()-1; i>=0; i--)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  //--- фильтр по символу
         if (OrderSymbol()!=Symbol()) continue;
         //--- фильтр по магик номеру (если такая проверка предусмотрена)
         if (OrderMagicNumber()!=MagicNumber) continue;
         //--- срабатывает условие удаления отложенного ордера
         if (OrderOpenPrice()>vremya)
         {  
            if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP)
            {  
               if (OrderDelete(OrderTicket())==true)
               {  Sleep(5*1000); //после удачного удаления усыпляем советник на 5 секунд
                  if (/*установить стоповый ордер*/)
                  {   OrderSend(...)
                  }
               }
            }
            else return(0);
         }
}  }  }
 
Zhunko:
This is passing the parameter by reference.
No one is stopping you from calling init() where you want it, according to your condition.


Then again we must solve the issue of control of this moment. After all, in order to know "where it should be", you need to know it. And you can find it out through a flag, for example. It turns out, for example, that flag will be in global:

gInitTrue = false;

In init(), assign a value at the very end of the function:

gInitTrue = true;

And we will control the start at the very beginning of the function:

if (gInitTrue != true)
    init();
Am I understanding this correctly? Or is there something else to consider?
 
hoz:


But more to the point? It's been discussed that INIT can fail in case of, for example, disconnects or other similar situations. Because it does not re-initialise the data itself afterwards, in case of contingencies, and it only runs once! So, this is not quite a correct way of doing it.
There is no time limit for init(), theoretically calculations may run for several seconds/minutes. It doesn't make sense to break a connection here. I.e., owl has been launched on a chart -> a tick has come -> calculations in init() have started (at this time there may be many ticks or a connection failure) -> the Expert Advisor for the first operation start() and so it is waiting for a new tick.
 
how should the code be correctly written so that if the specified stop loss or take profit is less than the minimum stop, then make them equal and use the stops as the minimum stop. ?
 
webip:
how should the code be correctly written so that if the specified stop loss or take profit is less than the minimum stop, then make them equal and use the stops as the minimum stop. ?
MathMax().
 
Can you tell me a little code? Opened file.... What functions can be used to write a record, save it, and then close it...
 
Zolotai:
Can you tell me a little code? Opened file.... Which function can be used to write a recording, save it and then close it...

https://docs.mql4.com/ru/files
 
Zolotai:
Can you tell me a little code? Opened file.... What functions can be used to write a record, save it, and then close it...


double Balance,Equity,Free;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
    Balance = AccountBalance(); 
    Equity  = AccountEquity(); 
    Free    =AccountFreeMargin();                 // Свободн средства
   
//----
 if(NevBar())  Средства();
//----
   return(0);
  }
//+------------------------------------------------------------------+

//====================================================================
//-----------------------------------------------------------------------------+
// Функция контроля нового бара                                                |
//-----------------------------------------------------------------------------+
bool NevBar(){
   static int PrevTime=0;
   if (PrevTime==Time[0]) return(false);
   PrevTime=Time[0];
   return(true);} 
//====================================================================   
void Средства()
{
  int handle;
  string filename = "Средства.csv"; // Формируем имя файла
  handle = FileOpen(filename,FILE_CSV|FILE_READ | FILE_WRITE,';');
  if(handle < 1)
  {
    Print("Не удалось создать файл. Ошибка #", GetLastError());
    return(0);
    //FileClose(handle);
  }
  
  FileWrite(handle, "Время",
                    "Баланс",
                    "Средства",
                    "Свободная маржа"); //пишем заголовок
                    
  FileSeek(handle, 0, SEEK_END);        //следущая строка
  
  FileWrite(handle,TimeToStr(Time[0]),  //пишем что-то
                   Balance,
                   Equity, 
                   Free);
 
  FileClose(handle);
  return(0);
}
Reason: