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

 
EVGENII SHELIPOV #:

Good day Makar !!!

Following your advice, I have found a place in void OnTick() function where I can attach the TrailingGroupOrder() function to send a command to modify a trail of group orders, and oh my god the trail of group orders has started working. However, my joy didn't last long. Looking closer, I saw that only the order with a min ticket is trilled.

I can very likely assume that the problem is in the modification of only the first (minimum) order and not all at once.

Makar advise how to make all orders in the grid modify at once????

Show me where you put TrailingGroupOrder() in the void OnTick() function ?
 
MakarFX #:
Show me where you put TrailingGroupOrder() in the void OnTick() function ?


void OnTick()
{
     double JAW = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
     double TEETH = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
     double LIPS = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
     double DI_PLUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_PLUSDI,0);
     double DI_MINUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_MINUSDI,0);
     double MacdCurrent=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,0);
     double MacdPrevious=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,2 ); 
     double ATR = iATR(Symbol(), TimeframesVolatility, BarCount, 0);
     {
      if (CountTrade() == 0)
      {
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS>TEETH&& TEETH>JAW&&DI_PLUSCurrent>18&&DI_PLUSCurrent>DI_MINUSCurrent&&MacdCurrent>MacdPrevious)                              
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Ask + TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_BUY, FirstLots, Ask, slip, 0, tp, "1-ый ордер", Magic, 0, Blue); 
         }
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS<TEETH&& TEETH<JAW&&DI_MINUSCurrent>18&&DI_MINUSCurrent>DI_PLUSCurrent&&MacdCurrent<MacdPrevious)                            
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Bid - TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_SELL, FirstLots, Bid, slip, 0, tp, "1-ый ордер", Magic, 0, Red); 
         }
       }
      if (CountTrade() == 1) Trailing();
     }
      if (CountTrade() < MaxOrders)                                                           
       {
           int order_type = FindLastOrderType();
           if (order_type == OP_BUY)
           { 
              price = FindLastOrderPrice(OP_BUY);  
              if(Ask<= price - Step()*Point)
              {
                  lastlot = NormalizeDouble(GetMinLotOrder()*MathPow( MultiplierParameter, OrdersTotal()), 2);
                  ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, slip, 0, 0, "Групповой ордер", Magic, 0, Blue);
                  if (ticket < 1)
                      Print ("Ошибка ордера на покупку");
                            ModifyOrders(OP_BUY);
              }
           }
             if (order_type == OP_SELL)
           { 
              price = FindLastOrderPrice(OP_SELL);  
              if(Bid>= price + Step()*Point)
              {
                  lastlot = NormalizeDouble(GetMinLotOrder()*MathPow( MultiplierParameter, OrdersTotal()), 2);
                  ticket = OrderSend(Symbol(), OP_SELL, lastlot, Bid, slip, 0, 0, "Групповой ордер", Magic, 0, Red);
                  if (ticket < 1)
                      Print ("Ошибка ордера на продажу!");
                            ModifyOrders(OP_SELL);
              }
           }
         }
         if(CountTrade()>1)
          {
           TrailingGroupOrder();
          } 
         double op = CalculiteProfit(); 
         if (op > FindLastLots() && Drawdown > DrawdownClosingMinMaxOrders)
           {
           ClosseMinMaxOrders();
           } 
      Comment("Всего открыто ордеров : " + DoubleToStr(OrdersHistoryTotal(), 0)+ "\n" +
              "Максимальное количество : " + DoubleToStr(MaxOrders, 0)+ "\n" +
              "Количество открытых ордеров: " + DoubleToStr(OrdersTotal(), 0)+ "\n" +
              "Осталось ордеров: " + DoubleToStr((MaxOrders - OrdersTotal()), 0)+ "\n" +
              "Баланс средств счета: " + DoubleToStr(AccountBalance(), 0)+ "\n" +
              "Свободные средства: " + DoubleToStr(AccountFreeMargin(), 0)+ "\n" +
              "Относительная текущая просадка: " + DoubleToStr(((AccountBalance() - AccountFreeMargin())/AccountBalance())*100, 2)+ "\n" +
              "Относительная максимальная просадка: " + DoubleToStr(GetMaxDrawdown(), 2)+ "\n" +
              "Текущая прибыль/убыток: " + DoubleToStr(AccountProfit(), 2)+ "\n" +
              "Абсолютная максимальная прибыль: " + DoubleToStr(GetMaxProfit(), 2)+ "\n" +
              "Абсолютный максимальный убыток (просадка): " + DoubleToStr(GetMaxLoss(), 2)+ "\n" +
              "Плечо: " + DoubleToStr(AccountLeverage(), 0)+ "\n" + 
              "Своп: " + DoubleToStr(OrderSwap(), 2)+ "\n" + 
              "Коммссия: " + DoubleToStr(OrderCommission(), 2)+ "\n" + 
              "Шаг: "  + DoubleToStr(Step(), 0)+ "\n" + 
              "Минимальный тикет ордера: " + DoubleToStr(GetTicketMinOrder(), 0)+ "\n" + 
              "Максимальный тикет ордера: " + DoubleToStr(GetTicketMaxOrder(), 0)+ "\n" + 
              "Профит минимальный  ордера: " + DoubleToStr(GetProfitMinOrder(), 2)+ "\n" +  
              "Профит максимального  ордера: " + DoubleToStr(GetProfitMaxOrder(), 2)+ "\n" +
              "Профит суммарный: " + DoubleToStr(CalculiteProfit(),2));       
}
 
EVGENII SHELIPOV #:

Delete the red highlighted items

void OnTick()
{
     double JAW = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
     double TEETH = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORTEETH,0);
     double LIPS = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORLIPS,0);
     double DI_PLUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_PLUSDI,0);
     double DI_MINUSCurrent=iADX(Symbol(),TimeframesIndicators,14, PRICE_CLOSE,MODE_MINUSDI,0);
     double MacdCurrent=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,0);
     double MacdPrevious=iMACD(Symbol(),TimeframesIndicators,12,26,9, PRICE_CLOSE,MODE_MAIN,2 ); 
     double ATR = iATR(Symbol(), TimeframesVolatility, BarCount, 0);
     {
      if (CountTrade() == 0)
      {
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS>TEETH&& TEETH>JAW&&DI_PLUSCurrent>18&&DI_PLUSCurrent>DI_MINUSCurrent&&MacdCurrent>MacdPrevious)                              
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Ask + TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_BUY, FirstLots, Ask, slip, 0, tp, "1-ый ордер", Magic, 0, Blue); 
         }
        if((UseHour==1&&Hour()>=StartTime&&Hour()<=StopTime)||UseHour==0)
        if(LIPS<TEETH&& TEETH<JAW&&DI_MINUSCurrent>18&&DI_MINUSCurrent>DI_PLUSCurrent&&MacdCurrent<MacdPrevious)                            
         {
           FirstLots = Lots();
           tp = NormalizeDouble(Bid - TakeProfitFirstOrder*Point, Digits);
           ticket = OrderSend(Symbol(), OP_SELL, FirstLots, Bid, slip, 0, tp, "1-ый ордер", Magic, 0, Red); 
         }
       }
      if (CountTrade() == 1) Trailing();
     }
      if (CountTrade() < MaxOrders)                                                           
       {
           int order_type = FindLastOrderType();
           if (order_type == OP_BUY)
           { 
              price = FindLastOrderPrice(OP_BUY);  
              if(Ask<= price - Step()*Point)
              {
                  lastlot = NormalizeDouble(GetMinLotOrder()*MathPow( MultiplierParameter, OrdersTotal()), 2);
                  ticket = OrderSend(Symbol(), OP_BUY, lastlot, Ask, slip, 0, 0, "Групповой ордер", Magic, 0, Blue);
                  if (ticket < 1)
                      Print ("Ошибка ордера на покупку");
                            ModifyOrders(OP_BUY);
              }
           }
             if (order_type == OP_SELL)
           { 
              price = FindLastOrderPrice(OP_SELL);  
              if(Bid>= price + Step()*Point)
              {
                  lastlot = NormalizeDouble(GetMinLotOrder()*MathPow( MultiplierParameter, OrdersTotal()), 2);
                  ticket = OrderSend(Symbol(), OP_SELL, lastlot, Bid, slip, 0, 0, "Групповой ордер", Magic, 0, Red);
                  if (ticket < 1)
                      Print ("Ошибка ордера на продажу!");
                            ModifyOrders(OP_SELL);
              }
           }
         }
         if(CountTrade()>1)
          {
           TrailingGroupOrder();
          } 
         double op = CalculiteProfit(); 
         if (op > FindLastLots() && Drawdown > DrawdownClosingMinMaxOrders)
           {
           ClosseMinMaxOrders();
           } 
      Comment("Всего открыто ордеров : " + DoubleToStr(OrdersHistoryTotal(), 0)+ "\n" +
              "Максимальное количество : " + DoubleToStr(MaxOrders, 0)+ "\n" +
              "Количество открытых ордеров: " + DoubleToStr(OrdersTotal(), 0)+ "\n" +
              "Осталось ордеров: " + DoubleToStr((MaxOrders - OrdersTotal()), 0)+ "\n" +
              "Баланс средств счета: " + DoubleToStr(AccountBalance(), 0)+ "\n" +
              "Свободные средства: " + DoubleToStr(AccountFreeMargin(), 0)+ "\n" +
              "Относительная текущая просадка: " + DoubleToStr(((AccountBalance() - AccountFreeMargin())/AccountBalance())*100, 2)+ "\n" +
              "Относительная максимальная просадка: " + DoubleToStr(GetMaxDrawdown(), 2)+ "\n" +
              "Текущая прибыль/убыток: " + DoubleToStr(AccountProfit(), 2)+ "\n" +
              "Абсолютная максимальная прибыль: " + DoubleToStr(GetMaxProfit(), 2)+ "\n" +
              "Абсолютный максимальный убыток (просадка): " + DoubleToStr(GetMaxLoss(), 2)+ "\n" +
              "Плечо: " + DoubleToStr(AccountLeverage(), 0)+ "\n" + 
              "Своп: " + DoubleToStr(OrderSwap(), 2)+ "\n" + 
              "Коммссия: " + DoubleToStr(OrderCommission(), 2)+ "\n" + 
              "Шаг: "  + DoubleToStr(Step(), 0)+ "\n" + 
              "Минимальный тикет ордера: " + DoubleToStr(GetTicketMinOrder(), 0)+ "\n" + 
              "Максимальный тикет ордера: " + DoubleToStr(GetTicketMaxOrder(), 0)+ "\n" + 
              "Профит минимальный  ордера: " + DoubleToStr(GetProfitMinOrder(), 2)+ "\n" +  
              "Профит максимального  ордера: " + DoubleToStr(GetProfitMaxOrder(), 2)+ "\n" +
              "Профит суммарный: " + DoubleToStr(CalculiteProfit(),2));       
}

Highlighted in yellow bring it up to the top

void OnTick()
{
      if (CountTrade() == 1) Trailing();
      if (CountTrade() > 1 ) TrailingGroupOrder();

     double JAW = iAlligator(Symbol(),TimeframesIndicators,13,8,5,8,5,3,MODE_SMMA,PRICE_MEDIAN,MODE_GATORJAW,0);
 
MakarFX #:

Delete the red highlighted items

Highlighted in yellow bring it up

Makar is the variant you sent me yesterday - it would be very simple in that variant the orders are also closed incorrectly

 
EVGENII SHELIPOV #:

Makar this is the variant you sent me yesterday - it would have been very simple in that variant the orders are not closed correctly

My corrections do not affect the closing.

One more thing - what does "tr" in the TrailingGroupOrder() function equal?

if(!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid - TrailingStopGroupOrder*Point, Digits), tp, 0))
 
EVGENII SHELIPOV #:

Makar this is the variant you sent me yesterday - it would have been very simple in that variant the orders are also closed incorrectly

What do you mean they don't close correctly?
 
MakarFX #:
What do you mean by "not closing correctly"?

Not all orders are closed at once from the trawl, but only the order with the min ticket and so on one by one

 
EVGENII SHELIPOV #:

Not all orders are closed at once from the trawl, but only the order with the min ticket and so on one by one

I will check it tomorrow
 

Hello all, I am writing code for a trawl of group orders . The code logic is as follows:

After the second and following orders are opened, we get a command to modify orders ModifyOrders() where we calculate the average and then takeprofit calculation and modify all orders but takeprofit only.

Then we call the TrailingGroupOrder() function from the void OnTick() function where Stop Loss is modified into Trailing Stop.

The question is that only one order with a minimum ticket is modified; this is not correct; all orders in the grid should be modified and close at TP or SL. Please help me to understand this. Thank you in advance.

//+----------------------------------------------------------------------------+
//| Модификация групповых ордеров                                              |
//+----------------------------------------------------------------------------+
void ModifyOrders(int otype)
  {
   for(int i = OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
           {
            if(otype == OP_BUY)
               tp = NormalizeDouble(GetAveragePrice() + TakeProfitGroupOrder*Point, Digits);
            if(otype == OP_SELL)
               tp = NormalizeDouble(GetAveragePrice() - TakeProfitGroupOrder*Point, Digits);
            if((otype == OP_BUY || otype == OP_SELL) && (Drawdown > DrawdownClosingTakeprofitZero))
               tp = NormalizeDouble(GetAveragePrice(), Digits);
           }
        }
     }
   for(int i = OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == otype)
           {
            if(OrderModify(OrderTicket(), OrderOpenPrice(), 0, tp, 0))
               Print("Ордера успешно модифицированы!");
            else
               Print("Ошибка модификации ордеров!");
           }
        }
     }
  }
//+----------------------------------------------------------------------------+
//| Трейлинг стоп групповых ордеров                                            |
//+----------------------------------------------------------------------------+
void TrailingGroupOrder()
  {
   for(int i = OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
           {
            if(OrderType() == OP_BUY && Bid - GetAveragePrice() > TrailingStopGroupOrder*Point)
              {
               if(Bid - GetAveragePrice() > TrailingStopGroupOrder*Point || OrderStopLoss() == 0)
                 {
                  if(OrderStopLoss() < Bid - (TrailingStep + TrailingStopGroupOrder)*Point || OrderStopLoss() == 0)
                    {
                     if(!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid - TrailingStopGroupOrder*Point, Digits), tp, 0))
                        Print("Ошибка модификации групповых ордеров на покупку!");
                    }
                 }
              }
            if(OrderType() == OP_SELL && GetAveragePrice() - Ask > TrailingStopGroupOrder*Point)
              {
               if(GetAveragePrice() - Ask > TrailingStopGroupOrder*Point || OrderStopLoss() == 0)
                 {
                  if(OrderStopLoss() > Ask + (TrailingStep + TrailingStopGroupOrder)*Point || OrderStopLoss() == 0)
                    {
                     if(!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask + TrailingStopGroupOrder*Point, Digits), tp, 0))
                        Print("Ошибка модификации групповых ордеров на продажу!");
                    }
                 }
              }
           }
        }
     }
  }


 
EVGENII SHELIPOV #:

Hello all, I am writing code for a trawl of group orders . The code logic for the trawl is as follows :

Why this duplication

            if(OrderType() == OP_BUY && Bid - GetAveragePrice() > TrailingStopGroupOrder*Point)
              {
               if(Bid - GetAveragePrice() > TrailingStopGroupOrder*Point || OrderStopLoss() == 0)
                 {
                  if(OrderStopLoss() < Bid - (TrailingStep + TrailingStopGroupOrder)*Point || OrderStopLoss() == 0)
                    {

You can do it this way

            if(OrderType() == OP_BUY && Bid - GetAveragePrice() > TrailingStopGroupOrder*Point)
              {
               if(OrderStopLoss() < Bid - (TrailingStep + TrailingStopGroupOrder)*Point || OrderStopLoss() == 0)
                  {


Also show what parameters

TrailingStep                
TakeProfitGroupOrder        
TrailingStopGroupOrder      
Reason: