Update SL each new bar

1
Hi, could someone help me updating the stop loss (of the 3 orders) to the last value of "stop" parameter?
I am trying but doesnt seem to work. Thanks in advance!

Code: Select all

      double entry = iCustom(NULL,0,"entry",11,0);
      double stop = iCustom(NULL,0,"stop",1,0);
      double tp1 = iCustom(NULL,0,"tp1",7,0);
      double tp2 = iCustom(NULL,0,"tp2",9,0);

void OnTick() 
{
   static datetime last_time = Time[0];
   if(last_time < Time[0])
   {
      last_time = Time[0];
      
      
      if (Ask == entry && OrdersTotal() == 0)
      int buyticket = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3,stop,tp1, NULL, 0, 0, Green);
         
      if (Ask == entry && OrdersTotal() == 1)
      int buyticket2 = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3,stop,tp2, NULL, 0, 0, Green);   
      
      if (Ask == entry && OrdersTotal() == 2)
      int buyticket3 = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3,stop,0, NULL, 0, 0, Green);  
         

     for (int i = OrdersTotal() - 1; i >= 0; i--);
     {
      // Select an order by index i, selecting by position and from the pool of market trades.
      // If the selection is successful, proceed with the update.
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         // Check if the order is for the current chart's currency pair.
         if (OrderSymbol() == Symbol())
            double OpenPrice = entry;
         double StopLossPrice = NormalizeDouble(stop,Digits()); //new stop loss

         if(!OrderModify(OrderTicket(), OrderOpenPrice(), StopLossPrice, OrderTakeProfit(), OrderExpiration()))
           {
            Print("No se puede editar el SL de la orden ",OrderTicket(), " al precio ",StopLossPrice," por el error ",GetLastError());
           }
        }
        }
        }
        }


Re: Update SL each new bar

2
ffsss wrote: Sun Aug 08, 2021 1:31 am Hi, could someone help me updating the stop loss (of the 3 orders) to the last value of "stop" parameter?
I am trying but doesnt seem to work. Thanks in advance!

Code: Select all

      double entry = iCustom(NULL,0,"entry",11,0);
      double stop = iCustom(NULL,0,"stop",1,0);
      double tp1 = iCustom(NULL,0,"tp1",7,0);
      double tp2 = iCustom(NULL,0,"tp2",9,0);

void OnTick() 
{
   static datetime last_time = Time[0];
   if(last_time < Time[0])
   {
      last_time = Time[0];
      
      
      if (Ask == entry && OrdersTotal() == 0)
      int buyticket = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3,stop,tp1, NULL, 0, 0, Green);
         
      if (Ask == entry && OrdersTotal() == 1)
      int buyticket2 = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3,stop,tp2, NULL, 0, 0, Green);   
      
      if (Ask == entry && OrdersTotal() == 2)
      int buyticket3 = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3,stop,0, NULL, 0, 0, Green);  
         

     for (int i = OrdersTotal() - 1; i >= 0; i--);
     {
      // Select an order by index i, selecting by position and from the pool of market trades.
      // If the selection is successful, proceed with the update.
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         // Check if the order is for the current chart's currency pair.
         if (OrderSymbol() == Symbol())
            double OpenPrice = entry;
         double StopLossPrice = NormalizeDouble(stop,Digits()); //new stop loss

         if(!OrderModify(OrderTicket(), OrderOpenPrice(), StopLossPrice, OrderTakeProfit(), OrderExpiration()))
           {
            Print("No se puede editar el SL de la orden ",OrderTicket(), " al precio ",StopLossPrice," por el error ",GetLastError());
           }
        }
        }
        }
        }
Hey,
Think this may be an easy one for you, you just missed the line to modify the order before checking if it was modified:

Code: Select all

 // Check if the order is for the current chart's currency pair.
         if (OrderSymbol() == Symbol())
            double OpenPrice = entry;
         double StopLossPrice = NormalizeDouble(stop,Digits()); //new stop loss

[Highlight]OrderModify(OrderTicket(), OrderOpenPrice(), StopLossPrice, OrderTakeProfit(), OrderExpiration())[/Highlight]

         if(!OrderModify(OrderTicket(), OrderOpenPrice(), StopLossPrice, OrderTakeProfit(), OrderExpiration()))


Best Regards and hope you get it to work ^_^

Who is online

Users browsing this forum: No registered users and 14 guests