[invalid stops] during modification

 
void Close_position_withDistance2(double distance_)
  {
   double open_price = 0;
   datetime last_position_time = 0;
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      ulong position_ticket = PositionGetTicket(i);
      double bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
      double ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);

      if(PositionGetDouble(POSITION_SL) == 0)
        {
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY && bid > PositionGetDouble(POSITION_PRICE_OPEN)+(distance_*Point()))
           {
            if(PositionGetDouble(POSITION_VOLUME) > lot_size)
              {
               trade.PositionModify(position_ticket,PositionGetDouble(POSITION_PRICE_OPEN),0);
               bool result = trade.PositionClosePartial(position_ticket,lot_size);
               Print("close buy ",lot_size," with distance");
               if(result == false)
                 {
                  Print("Close ", lot_size, " Of Position Not true! ",GetLastError());
                  ResetLastError();
                 }
               continue;
              }
            else
               if(Sum_Of_Lot_Position_At_Risk(POSITION_TYPE_SELL,magicNum) > Sum_Of_Lot_Position_RiskFree(POSITION_TYPE_BUY,magicNum)
                 )
                 {
                  trade.PositionModify(position_ticket,PositionGetDouble(POSITION_PRICE_OPEN),0);
                  continue;
                 }
               else
                 {
                  trade.PositionClose(position_ticket);
                  Print("close buy ",lot_size," with distance");
                 }
           }
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL && ask < PositionGetDouble(POSITION_PRICE_OPEN)-(distance_*Point()))
           {
            if(PositionGetDouble(POSITION_VOLUME) > lot_size)
              {
               trade.PositionModify(position_ticket,PositionGetDouble(POSITION_PRICE_OPEN),0);
               bool result = trade.PositionClosePartial(position_ticket,lot_size);
               Print("close sell ",lot_size," with distance");

               if(result == false)
                 {
                  Print("Close ", lot_size, " Of Position Not true! ",GetLastError());
                  ResetLastError();
                 }
               continue;
              }

            if(Sum_Of_Lot_Position_At_Risk(POSITION_TYPE_BUY,magicNum) > Sum_Of_Lot_Position_RiskFree(POSITION_TYPE_SELL,magicNum))
              {
               trade.PositionModify(position_ticket,PositionGetDouble(POSITION_PRICE_OPEN),0);
               Print("position modify ticket: ",position_ticket," stoplost : ",PositionGetDouble(POSITION_PRICE_OPEN));
               continue;
              }
            else
              {
               trade.PositionClose(position_ticket);
               Print("close sell ",lot_size," with distance");
              }
           }
        }
     }
  }
 

i want modify my sl and set it to open price(break even) in position  in for loop.but this function use another open price to set sl and i have error 

 

Please search before you post!

The "Invalid stops" question has been asked and answered countless times on this forum — https://www.mql5.com/en/search#!keyword=Invalid%20stops

Also read the following ... Attempt to modify order or position within the SYMBOL_TRADE_FREEZE_LEVEL freeze level

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
Fernando Carreiro #:

Please search before you post!

The "Invalid stops" question has been asked and answered countless times on this forum — https://www.mql5.com/en/search#!keyword=Invalid%20stops

Also read the following ... Attempt to modify order or position within the SYMBOL_TRADE_FREEZE_LEVEL freeze level


the distance is very long 300 point in eurusd.

i can,t resolve it.