Error Message: failed modify [Position doesn't exist]

To add comments, please log in or register
EANOOB
19
EANOOB  

Subject: Error message: failed modify #2 sell 0.10 EURUSD sl: 0.00000, tp: 0.00000 -> sl: 1.22924, tp: 1.20924 [Position doesn't exist]


Hi,

I''m new to mql5 and I'm having an issue getting the same error message over and over again no matter what I try.

I have not been able to find any previous posts with a solution that works for me on this. 

input double TradeVolume = 1;
input int StopLoss = 200;
input int TakeProfit = 300;

bool glBuyPlaced, glSellPlaced;

int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
MqlTradeRequest request;
MqlTradeResult result;
ZeroMemory(request);

bool openPosition = PositionSelect(_Symbol);
long positionType = PositionGetInteger(POSITION_TYPE);

double currentVolume = 0 ;
if(openPosition == true) {
      currentVolume= PositionGetDouble(POSITION_VOLUME);

    }
if(glBuyPlaced == false && openPosition == false){
      request.action = TRADE_ACTION_DEAL;
      request.type = ORDER_TYPE_BUY;
      request.symbol = _Symbol;
      request.volume = TradeVolume + currentVolume;
      request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      request.sl = 0;
      request.tp = 0;
      request.deviation = 50;
      
      bool sent = OrderSend(request, result);
      
      if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE){
         request.action = TRADE_ACTION_SLTP;
         
         PositionSelect(_Symbol);
         double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         
         if(StopLoss > 0){
          request.sl = positionOpenPrice - (StopLoss * _Point);
          }
         if(TakeProfit > 0){
          request.tp = positionOpenPrice + (TakeProfit * _Point);
         }
         
         if(request.sl > 0 && request.tp > 0) {
          OrderSend(request, result);
         }
         
         glBuyPlaced = true;
         glSellPlaced = false;
     
    }
   
  }
//+------------------------------------------------------------------+
//| Trade function                                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
//---
   
  }
//+------------------------------------------------------------------+

2018.04.02 15:23:27.594 2018.03.01 00:00:00   failed modify #2 sell 0.10 EURUSD sl: 0.00000, tp: 0.00000 -> sl: 1.22924, tp: 1.20924 [Position doesn't exist]

Above is the code I have so far, the EA is incomplete yet but that is everything I have for now and when I run it I get the above error message. I have tried modifying the sl and tp after the trade is placed with the code from the "Expert Advisor Programming for metatrader 5" book and I still get the same error when I do this so I think the error must be in the above code somewhere. 

Any help would be greatly appreciated.

Thanks for taking the time to read this. 


Steve 

Alain Verleyen
39054
Alain Verleyen  

I suppose it's an hedging account. In this case to modify an existing position you need to specify the ticket.

  request.action = TRADE_ACTION_SLTP;
  request.position = result.order;

You should better add your sl/tp to your initial order.

EANOOB
19
EANOOB  
Alain Verleyen:

I suppose it's an hedging account. In this case to modify an existing position you need to specify the ticket.

You should better add your sl/tp to your initial order.

Thanks for your help and fast reply. 
To add comments, please log in or register