Invalid Stops, Failed to modify open position

 

Hi, I don't know what's wrong, I'm traying to modify an open order when there is only one order opened, but keep getting this error.

What can I modify to make it work?

this is the code

         newTPb= level4bbb;
         newSLb= level3bbb;

         total; // number of open positions
         //--- iterate over all open positions

         ulong  position_ticket=PositionGetTicket(0);// ticket of the position
         string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol
         ENUM_POSITION_TYPE type=POSITION_TYPE_BUY;//(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
         double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
         double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position


         newTakeProfitLevelb =NormalizeDouble(newTPb,_Digits);
         newStopLossLevelb =NormalizeDouble(newSLb,_Digits);
         Print("newTP ",newTakeProfitLevelb);
         Print("newSL ",newStopLossLevelb);
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // symbol
         request.sl      =newStopLossLevelb;                  // Stop Loss of the position
         request.tp      =newTakeProfitLevelb;               // Take Profit of the position
         request.magic=Expert_MagicNumber;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

 
Javier Santiago Gaston De Iriarte Cabrera: Hi, I don't know what's wrong, I'm traying to modify an open order when there is only one order opened, but keep getting this error. What can I modify to make it work?

this is the code

Please be clear—is it an Order or a Position, that you are trying to update?

Your code suggests you are trying to modify a Position and not an Order.

Please inform us of the error code as well as the result return code.

Show us the log output of both the Experts and Journal regarding the attempt.

Remember that the stops prices need to be aligned to the tick size, and not with NormalizeDouble.

You also have to verify that the previous and new stops are actually different, or else you will get an error.

 
Fernando Carreiro #:

Please be clear—is it an Order or a Position, that you are trying to update?

Your code suggests you are trying to modify a Position and not an Order.

Please inform us of the error code as well as the result return code.

Show us the log output of both the Experts and Journal regarding the attempt.

Remember that the stops prices need to be aligned to the tick size, and not with NormalizeDouble.

You also have to verify that the previous and new stops are actually different, or else you will get an error.

I made it with this:


ulong ticket;
      MqlTradeRequest request = {};
      MqlTradeResult  result  = {};
      double newsl;

      for(int i=0; i<PositionsTotal(); i++)
           {
            ticket=PositionGetTicket(i);
            if(ticket>0)
              {
               request.action   = TRADE_ACTION_SLTP; // type of trade operation
               request.position = ticket;   // ticket of the position
               request.symbol   = PositionGetString(POSITION_SYMBOL);     // symbol
               request.sl       = PositionGetDouble(POSITION_SL);                // Stop Loss of the position
               request.tp       = PositionGetDouble(POSITION_TP);                // Take Profit of the position
               request.magic    = expert;                                    // MagicNumber of the position


               newsl = NormalizeDouble(newSLb,_Digits);
               if(newsl>request.sl)
                 {
                  request.sl = newsl;
                  if(!OrderSend(request,result))
                    {
                     PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
                    }
                  //--- information about the operation
                  PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
                 }
              }
           }
        }
 
  1. You failed to address any of the questions I asked.
  2. Your new code is guaranteed to ALWAYS give an error.
    Did I not state ... "You also have to verify that the previous and new stops are actually different, or else you will get an error."?
 
Since you insist in ignoring my questions and advice, and only answering one or two of the less important ones, I will leave you to your own to figure it out.
 
Fernando Carreiro #:
Since you insist in ignoring my questions and advice, and only answering one or two of the less important ones, I will leave you to your own to figure it out.

I've responded to the q's in the last message I deleted.

But I will respond again

Yes, its a position (I eaven changed the title)

I now can't get the error because for some reason I don't know it passes the change of sl and tp


I use normalizedouble and prizes are different, but no error is shown. What can I do?


if(total>0)
     {
      MqlTradeRequest request;
      MqlTradeResult  result;
      MqlTick tick;
      double last=0;

      if(SymbolInfoTick(_Symbol,tick))
         last=tick.last;

      Last = NormalizeDouble(last,_Digits);

      double array_ma22[];
      ArraySetAsSeries(array_ma22,true);
      int start_pos22=0,count22=3;
      if(!iGetArray22(handle_iMA22,0,start_pos22,count22,array_ma22))
         return;


      //Print("ya hay ordenes abiertas");
      Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      //+------------------------------------------------------------------+
      //| Global variables                                                 |
      //+------------------------------------------------------------------+
      if(var_sobrecomprado==1 && Orden=="Sell")
        {
         if(Last <array_ma22[0])
           {

            newTPs= level4sss;
            newSLs= array_ma22[0];
            total; // number of open positions



            ulong ticket;
            MqlTradeRequest request = {};
            MqlTradeResult  result  = {};
            double newsl;
            double newtp;

            for(int i=0; i<PositionsTotal(); i++)
              {
               ticket=PositionGetTicket(i);
               if(ticket>0)
                 {
                  request.action   = TRADE_ACTION_SLTP; // type of trade operation
                  request.position = ticket;   // ticket of the position
                  request.symbol   = PositionGetString(POSITION_SYMBOL);     // symbol
                  request.sl       = PositionGetDouble(POSITION_SL);                // Stop Loss of the position
                  request.tp       = PositionGetDouble(POSITION_TP);                // Take Profit of the position
                  request.magic    = Expert_MagicNumber;                                    // MagicNumber of the position


                  newsl = NormalizeDouble(newSLs,_Digits);
                  newtp = NormalizeDouble(request.tp + 1*_Point,_Digits);
                  if(newsl<request.sl)
                    {
                     request.sl = newsl;
                     request.tp = newtp;
                     if(!OrderSend(request,result))
                       {
                        PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
                       }
                     //--- information about the operation
                     PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
                     return;
                    }
                 }
              }
           }
        }
 
Javier Santiago Gaston De Iriarte Cabrera #:

I've responded to the q's in the last message I deleted.

But I will respond again

Yes, its a position (I eaven changed the title)

I now can't get the error because for some reason I don't know it passes the change of sl and tp


I use normalizedouble and prizes are different, but no error is shown. What can I do?


I've tried with bode codes ... and now ... it doesnt give errors because it doesnt try to set new sl and tp.

 
Javier Santiago Gaston De Iriarte Cabrera #:

I've tried with bode codes ... and now ... it doesnt give errors because it doesnt try to set new sl and tp.

this is that part of the code

   if(total>0)
     {
      MqlTradeRequest request;
      MqlTradeResult  result;
      MqlTick tick;
      double last=0;

      if(SymbolInfoTick(_Symbol,tick))
         last=tick.last;

      Last = NormalizeDouble(last,_Digits);

      double array_ma22[];
      ArraySetAsSeries(array_ma22,true);
      int start_pos22=0,count22=3;
      if(!iGetArray22(handle_iMA22,0,start_pos22,count22,array_ma22))
         return;


      //Print("ya hay ordenes abiertas");
      Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
      //+------------------------------------------------------------------+
      //| Global variables                                                 |
      //+------------------------------------------------------------------+
      if(var_sobrecomprado==1 && Orden=="Sell")
        {
         if(Last <array_ma22[0])
           {


            newTPb= level4sss;
            newSLb= array_ma22[0];

            // number of open positions
            //--- iterate over all open positions

            ulong  position_ticket=PositionGetTicket(0);// ticket of the position
            string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol
            ENUM_POSITION_TYPE type=POSITION_TYPE_BUY;//(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
            double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
            double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position


            newTakeProfitLevelb =NormalizeDouble(newTPb +1*_Point,_Digits);
            newStopLossLevelb =NormalizeDouble(newSLb,_Digits);
            Print("newTP ",newTakeProfitLevelb);
            Print("newSL ",newStopLossLevelb);
            //--- zeroing the request and result values
            ZeroMemory(request);
            ZeroMemory(result);
            //--- setting the operation parameters
            request.action  =TRADE_ACTION_SLTP; // type of trade operation
            request.position=position_ticket;   // ticket of the position
            request.symbol=position_symbol;     // symbol
            request.sl      =newStopLossLevelb;                  // Stop Loss of the position
            request.tp      =newTakeProfitLevelb;               // Take Profit of the position
            request.magic=Expert_MagicNumber;         // MagicNumber of the position
            //--- output information about the modification
            PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
            //--- send the request
            if(!OrderSend(request,result))
               PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
            //--- information about the operation
            PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
           }
        }

      if(var_sobrecomprado==1 && Last <(level3sss) && Orden=="Sell" && Last < array_ma22[0])
        {



         newTPb= level4sss;
         newSLb= level3sss;

         // number of open positions
         //--- iterate over all open positions

         ulong  position_ticket=PositionGetTicket(0);// ticket of the position
         string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol
         ENUM_POSITION_TYPE type=POSITION_TYPE_BUY;//(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
         double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
         double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position


         newTakeProfitLevelb =NormalizeDouble(newTPb +1*_Point,_Digits);
         newStopLossLevelb =NormalizeDouble(newSLb,_Digits);
         Print("newTP ",newTakeProfitLevelb);
         Print("newSL ",newStopLossLevelb);
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // symbol
         request.sl      =newStopLossLevelb;                  // Stop Loss of the position
         request.tp      =newTakeProfitLevelb;               // Take Profit of the position
         request.magic=Expert_MagicNumber;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);


        }



      if(var_sobrevendido==-1 && Orden=="Buy")
        {
         if(Last >array_ma22[0])
           {




            newTPb= level4bbb;
            newSLb= array_ma22[0];

            // number of open positions
            //--- iterate over all open positions

            ulong  position_ticket=PositionGetTicket(0);// ticket of the position
            string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol
            ENUM_POSITION_TYPE type=POSITION_TYPE_BUY;//(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
            double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
            double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position


            newTakeProfitLevelb =NormalizeDouble(newTPb +1*_Point,_Digits);
            newStopLossLevelb =NormalizeDouble(newSLb,_Digits);
            Print("newTP ",newTakeProfitLevelb);
            Print("newSL ",newStopLossLevelb);
            //--- zeroing the request and result values
            ZeroMemory(request);
            ZeroMemory(result);
            //--- setting the operation parameters
            request.action  =TRADE_ACTION_SLTP; // type of trade operation
            request.position=position_ticket;   // ticket of the position
            request.symbol=position_symbol;     // symbol
            request.sl      =newStopLossLevelb;                  // Stop Loss of the position
            request.tp      =newTakeProfitLevelb;               // Take Profit of the position
            request.magic=Expert_MagicNumber;         // MagicNumber of the position
            //--- output information about the modification
            PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
            //--- send the request
            if(!OrderSend(request,result))
               PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
            //--- information about the operation
            PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
           }
        }



      if(var_sobrevendido==-1 && Precio >(level3bbb) && Orden=="Buy" &&  Last > array_ma22[0])
        {






         newTPb= level4bbb;
         newSLb= level3bbb;
         // number of open positions
         //--- iterate over all open positions

         ulong  position_ticket=PositionGetTicket(0);// ticket of the position
         string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol
         ENUM_POSITION_TYPE type=POSITION_TYPE_BUY;//(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position
         double sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position
         double tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position


         newTakeProfitLevelb =NormalizeDouble(newTPb +1*_Point,_Digits);
         newStopLossLevelb =NormalizeDouble(newSLb,_Digits);
         Print("newTP ",newTakeProfitLevelb);
         Print("newSL ",newStopLossLevelb);
         //--- zeroing the request and result values
         ZeroMemory(request);
         ZeroMemory(result);
         //--- setting the operation parameters
         request.action  =TRADE_ACTION_SLTP; // type of trade operation
         request.position=position_ticket;   // ticket of the position
         request.symbol=position_symbol;     // symbol
         request.sl      =newStopLossLevelb;                  // Stop Loss of the position
         request.tp      =newTakeProfitLevelb;               // Take Profit of the position
         request.magic=Expert_MagicNumber;         // MagicNumber of the position
         //--- output information about the modification
         PrintFormat("Modify #%I64d %s %s",position_ticket,position_symbol,EnumToString(type));
         //--- send the request
         if(!OrderSend(request,result))
            PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the request, output the error code
         //--- information about the operation
         PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

        }


     }
  }

and now attepts to change the tp and sl


why?

 

please help, I just need to finish this.


I answered all your q's (1)

 
Javier Santiago Gaston De Iriarte Cabrera #:

please help, I just need to finish this.


I answered all your q's (1)

edit: I was no choosing well the conditions ... but still can't make the sl and tp change ... I will try changing the conditions to pass ...

 
2023.06.17 23:10:57.330 Core 01 2022.09.02 07:04:19   failed modify #6 sell 0.1 EURUSD sl: 0.99646, tp: 0.99099 -> sl: 0.99646, tp: 0.99099 [Invalid stops]



2023.06.17 23:10:57.330 Core 01 2022.09.02 07:04:19   OrderSend error 4756



but I'm using this:

newTakeProfitLevelb =NormalizeDouble(newTPb +5*_Point,_Digits);
Reason: