why err = Position doesnt exist.

 

how to solve it ?

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+ For Position Sell Breakeven Trailing
   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
   if (ForAllSymbol || PositionGetSymbol(i) == Symbols)
   {
   MqlTradeRequest request = {0};
   MqlTradeResult result = {0};
   request.action = TRADE_ACTION_SLTP;
   request.symbol = Symbol();
  
   if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
   {
      if ((Enabled_Breakeven) && (Breakeven > 0))
      {
      if ((PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) + PositionGetDouble(POSITION_COMMISSION)) > 0.0)
      {
      request.sl = PositionGetDouble(POSITION_PRICE_OPEN);
      request.tp = TakeProfitLevel;
      bool Send = OrderSend(request, result);
      Print("ProceedBreakevenSell : ", result.price);
      Print("ProceedBreakevenSell : ", PositionGetDouble(POSITION_SL));
      }
      }
      
      if ((Enabled_Trailing) && (Trailing > 0))
      {
      if (((PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) + PositionGetDouble(POSITION_COMMISSION)) > 0.0))
      if ((PositionGetDouble(POSITION_PRICE_OPEN) - SymbolInfoDouble(Symbols, SYMBOL_ASK)) > (Trailing * _Point + PipsStep * _Point))
      if ((PositionGetDouble(POSITION_SL) > (SymbolInfoDouble(Symbols, SYMBOL_ASK) + Trailing * _Point)))
      if ((PositionGetDouble(POSITION_SL) - SymbolInfoDouble(Symbols, SYMBOL_ASK)) > MinStopLossLevel * _Point)
      {
      request.sl = SymbolInfoDouble(_Symbol, SYMBOL_ASK) + Trailing * _Point - PipsStep * _Point;
      request.tp = TakeProfitLevel;
      bool Send = OrderSend(request, result);
      Print("ProceedTrailingSell : ", result.price);
      Print("ProceedTrailingSell : ", PositionGetDouble(POSITION_SL));
      }
      }
   }
  
   }
   }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+


 

 
If you get the server-error "Position doesn't exist" it mostly has been already closed by the server due to you SL or TP.
 
forextime8:

how to solve it ?

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+ For Position Sell Breakeven Trailing
   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
   if (ForAllSymbol || PositionGetSymbol(i) == Symbols)
  


 

If ForAllSymbol is true your position is never selected.
 
Alain Verleyen:
If ForAllSymbol is true your position is never selected.

yes, i use false.

but still the err. 

why ? 

Carl Schreiber:
If you get the server-error "Position doesn't exist" it mostly has been already closed by the server due to you SL or TP.

 isnt the looping only counting the number of open positions.

so, it wont be already closed. 

 
forextime8:

yes, i use false.

but still the err. 

why ? 

Carl Schreiber:
If you get the server-error "Position doesn't exist" it mostly has been already closed by the server due to you SL or TP.

 isnt the looping only counting the number of open positions.

so, it wont be already closed. 

You said you have this error, but there is no error processing in your code. Where do you get this error exactly, and when ?
 
Alain Verleyen:
You said you have this error, but there is no error processing in your code. Where do you get this error exactly, and when ?

im coding it and testing it now.

 

 

 
forextime8:

im coding it and testing it now.

 

 

...but there is no error processing in your code.

Add error processing, print values to find why it happens.

EDIT: the code you posted only process SELL positions, and your screenshot show a BUY. You should provide complete and correct code and information if you want help.

 
Alain Verleyen:

Add error processing, print values to find why it happens.

thanks for remind.

is err 4756

ERR_TRADE_SEND_FAILED
4756
Trade request sending failed


 

 

Forum on trading, automated trading systems and testing trading strategies

why err = Position doesnt exist.

Alain Verleyen, 2016.12.10 14:58

Add error processing, print values to find why it happens.

EDIT: the code you posted only process SELL positions, and your screenshot show a BUY. You should provide complete and correct code and information if you want help.

Anyway. If you are using an hedging account, you have to specify the position id you want to change. Please check the documentation :

SL & TP Modification

Trade order to modify the StopLoss and/or TakeProfit price levels. It requires to specify the following 4 fields:

  • action
  • symbol
  • sl
  • tp
  • position


 
Alain Verleyen:

Anyway. If you are using an hedging account, you have to specify the position id you want to change. Please check the documentation :


yes, i'm trying in MT5 hedge acc,

i added this

ulong  Position_Ticket = PositionGetTicket(i);

but still no working, still the error position doesnt exist.

maybe something wrong on here, but i dont where the mistake.

   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
   if (For_AllSymbol || PositionGetSymbol(i) == Symbol())
   {
      ulong  Order_Ticket = OrderGetTicket(i);
      ulong  Position_Ticket = PositionGetTicket(i);
      string Position_Symbol = PositionGetString(POSITION_SYMBOL);
      int    Position_Digits = (int)SymbolInfoInteger(Position_Symbol, SYMBOL_DIGITS);
      ulong  Position_Magic = PositionGetInteger(POSITION_MAGIC);
      double Position_Volume = PositionGetDouble(POSITION_VOLUME);
      double Position_SL = PositionGetDouble(POSITION_SL);
      double Position_TP = PositionGetDouble(POSITION_TP);
      ENUM_POSITION_TYPE Position_Type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      
      if (Position_Type == POSITION_TYPE_BUY)
      {
       ...function to do anything
      }
   }
   }
 
forextime8:

yes, i'm trying in MT5 hedge acc,

i added this

ulong  Position_Ticket = PositionGetTicket(i);

but still no working, still the error position doesnt exist.

maybe something wrong on here, but i dont where the mistake.

   for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
   if (For_AllSymbol || PositionGetSymbol(i) == Symbol())
   {
      ulong  Order_Ticket = OrderGetTicket(i);
      ulong  Position_Ticket = PositionGetTicket(i);
      string Position_Symbol = PositionGetString(POSITION_SYMBOL);
      int    Position_Digits = (int)SymbolInfoInteger(Position_Symbol, SYMBOL_DIGITS);
      ulong  Position_Magic = PositionGetInteger(POSITION_MAGIC);
      double Position_Volume = PositionGetDouble(POSITION_VOLUME);
      double Position_SL = PositionGetDouble(POSITION_SL);
      double Position_TP = PositionGetDouble(POSITION_TP);
      ENUM_POSITION_TYPE Position_Type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      
      if (Position_Type == POSITION_TYPE_BUY)
      {
       ...function to do anything
      }
   }
   }
Please post all the relevant code if you want help.
Reason: