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

 
Tretyakov Rostyslav #:
Check

Good morning Rostislav!!!

Could you comment on yesterday's changes I can't understand their logic

//-------------------------------------------------------------------+
   Spread       = MarketInfo(Symbol(),MODE_SPREAD)*Point;
   MinLot       = MarketInfo(Symbol(),MODE_MINLOT);
   Balance      = AccountInfoDouble(ACCOUNT_BALANCE);
   FreeMargin   = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   avg_buy      = ObjectGetDouble(0,"AveragePriceLine0",OBJPROP_PRICE);
   avg_sell     = ObjectGetDouble(0,"AveragePriceLine1",OBJPROP_PRICE);
   Drawdown     = (Balance - FreeMargin)/Balance*100;
//-------------------------------------------------------------------+  Команда на удаление линий отображающую среднюю цену и текста
   if(CountTrade() == 0)
     {
      flag_close=0;
      ObjectsDeleteAll(0,"AveragePriceLine");
      if(ObjectFind(0,"signal4")==0&&CountTrade(0)<1)//для бай
        {
         ObjectDelete(0,"signal4");
        }
      if(ObjectFind(0,"signal3")==0&&CountTrade(1)<1)//для селл
        {
         ObjectDelete(0,"signal3");
        }
     }
 
EVGENII SHELIPOV #:

Good morning Rostislav!!!

Could you comment on yesterday's changes I can't understand their logic


If there are no open orders flag_close gets "0"

   if(CountTrade() == 0)
     {
      flag_close=0;

When flag_close=0; the request to ClosseAll() stops

 
Tretyakov Rostyslav #:

If there are no open orders, flag_close will receive "0".

When flag_close=0; the request for ClosseAll() stops

Yes, I got it.

You need it when deleting objects.

The interesting thing is that if the flag is locked in this place, the Expert Advisor gets weird

 
EVGENII SHELIPOV #:

Yes, I understand that.

Why it is needed when deleting objects.

The interesting thing is that if you block the flag in this place, the Expert Advisor starts to do weird stuff.

This has nothing to do with deleting objects

It is a determination of the absence of orders

   if(CountTrade() == 0)
     {
      flag_close=0;
      ObjectsDeleteAll(0,"AveragePriceLine");
      if(ObjectFind(0,"signal4")==0&&CountTrade(0)<1)//для бай
        {
         ObjectDelete(0,"signal4");
        }
      if(ObjectFind(0,"signal3")==0&&CountTrade(1)<1)//для селл
        {
         ObjectDelete(0,"signal3");
        }
     }

You can perform any internal operations that do not require the absence of orders

Besides, there is a lot of unnecessary things in this part of the code,

This variant will do the same thing as the previous one

   if(CountTrade() == 0)
     {
      flag_close=0;
      ObjectsDeleteAll(0,"AveragePriceLine");
      ObjectDelete(0,"signal4");
      ObjectDelete(0,"signal3");
     }
 
Tretyakov Rostyslav #:

What does removing objects have to do with

it's about determining if there are no orders

and inside you can perform any operation that requires no orders

Besides, there is a lot of unnecessary stuff in this part of the code,

This variant will do the same thing as the previous one

I understand it, Rostislav.

I do not understand why, if I block the flag in this function, the EA starts to open and close 100-200 orders at a time

 
Vitaly Muzichenko #:

It's locking, from the word lock.

Yes. But a lock opens with the same lot. And two differently directed orders are opened simultaneously. A hedging is when a deal went into a drawdown, and the trader opens a position with a bigger lot in the same direction (I mixed up with different directions) implying that the price will reverse to make a profit on a deal with a bigger lot and cover the loss on a deal with a smaller lot or average to 0. If I got something wrong, correct me.

 
EVGENII SHELIPOV #:

I get it, Rostislav

I do not understand why if I block the flag in this function, the Expert Advisor starts to open and close 100-200 orders at a time

Because if the flag is not equal to "0", it constantly calls the ClosseAll() function
 
EVGENII SHELIPOV #:

I do not understand why, if I block the flag in this function, the EA will open and close 100-200 orders at a time

Let me explain using your code:

Initially, the flag is set.

int flag_close=0;
//+------------------------------------------------------------------+

Then it opens orders

//-------------------------------------------------------------------+  Команда на открытие первых ордеров в сетке
   if((UseHour==1 && Hour() >= StartTime && Hour()<=StopTime)||UseHour==0)
     {

If a close order condition comes up (you have three of them)

//-------------------------------------------------------------------+  Команда на закрытие сетки ордеров при условии (Суммарный профит сетки >=0 и Просадка больше заданного уровня)
   if(CalculiteProfit() >= 0 && Drawdown > DrawdownClosingTakeprofitZero)
     {
      flag_close=1;
//-------------------------------------------------------------------+  Команда на закрытие сетки ордеров
   if((CountTrade(0) > 1 && CalculiteProfit() >= 0 && OrderGroupCloseSignal()==0)||(CountTrade(1) > 1 && CalculiteProfit() >= 0 && OrderGroupCloseSignal()==1))
     {
      flag_close=1;
//-------------------------------------------------------------------+  Команда на закрытие первых ордеров
   if((CountTrade(0) == 1 && CalculiteProfit() >= 0 &&  OrderCloseSignal()==0)||(CountTrade(1) == 1 && CalculiteProfit() >= 0 &&  OrderCloseSignal()==1))
     {
      flag_close=1;

This flag gets value "1" and starts to close the orders

//-------------------------------------------------------------------+  Команда на закрытие ордеров
      if(flag_close==1)ClosseAll();

And once all the orders are closed, the flag gets "0".

   if(CountTrade() == 0)
     {
      flag_close=0;
 
Tretyakov Rostyslav #:
Because if the flag is not "0", it constantly calls the ClosseAll() function

This may be the case provided the command to open an order is given. So there is a kind of "CHECKING" going on.

 
EVGENII SHELIPOV #:

This may be the case provided the command to open an order is given. So there's a kind of "CALCULATION" going on.

You got a credit card?

You go into a shop and buy a beer one bottle at a time and each time you pay with the card until you get "0" on the card,

then go and refill the card and go and get another beer.