Download MetaTrader 5

Position doesn't exist - StopLoss activation

To add comments, please log in or register
Jugler1986
22
Jugler1986 2016.08.17 10:35 

Hi folks,

got an issue with an self programmed EA.

After opening my position my EA is advised to add a SL to the position. But I always get the hint below: 

"2016.08.17 10:29:39.064 Trades '3895623': failed modify  buy 0.00  sl: 0.00000, tp: 0.00000 -> sl: 1.11690, tp: 1.15690 [Position doesn't exist]"

It even calculates the SLTP values but "allthough" i See that the position is opened, it refuses to see it by itself. Hope you can give me some hints.

Thank you.

Jugler 


Vladimir Karputov
Moderator
49430
Vladimir Karputov 2016.08.17 10:41  
Jugler1986:

...

"2016.08.17 10:29:39.064 Trades '3895623': failed modify  buy 0.00  sl: 0.00000, tp: 0.00000 -> sl: 1.11690, tp: 1.15690 [Position doesn't exist]"

...


I underlined your mistake

 

Added later:

Use standard library "CTrade" - you'll have a lot fewer mistakes. 

Jugler1986
22
Jugler1986 2016.08.17 10:59  
Karputov Vladimir:

I underlined your mistake

 

Added later:

Use standard library "CTrade" - you'll have a lot fewer mistakes. 

Thanks for your fast Reply Vladimir.

I'm sorry I can't see the mistakes in the underlining. Is it the function modify, or the buy 0.00 price. And why does it say "Position doesn't exist" instead of "hey your modify is wrong (what so ever)"

I thought the problem is something like "order ticket number is 1" but EA searches for "order ticket 2" and for that reason it answers "Position doesn't exist".

 

By the way I thought up to now, that i use the standard CTrade

 

thx  

Vladimir Karputov
Moderator
49430
Vladimir Karputov 2016.08.17 12:17  
Jugler1986:

Thanks for your fast Reply Vladimir.

I'm sorry I can't see the mistakes in the underlining. Is it the function modify, or the buy 0.00 price. And why does it say "Position doesn't exist" instead of "hey your modify is wrong (what so ever)"

I thought the problem is something like "order ticket number is 1" but EA searches for "order ticket 2" and for that reason it answers "Position doesn't exist".

 

By the way I thought up to now, that i use the standard CTrade

 

thx  

Example of netting:

first open the deal and get the position. Then, after 60 seconds, change the stop loss level:

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                              Copyright © 2016, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#include <Trade\Trade.mqh>
//--- variable
CTrade   my_trade;         // object CTrade
int      my_stop_loss=30;  // stop level
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   my_trade.Buy(0.01);
   Sleep(6000);

   double m_adjusted_point;         // point value adjusted for 3 or 5 points
//--- tuning for 3 or 5 digits
   int digits_adjust=1;
   if(Digits()==3 || Digits()==5)
      digits_adjust=10;
   m_adjusted_point=Point()*digits_adjust;

   double m_stop_loss=my_stop_loss*m_adjusted_point;

   MqlTick m_tick;
   SymbolInfoTick(Symbol(),m_tick);

   double sl=NormalizeDouble(m_tick.bid-m_stop_loss,Digits());
   my_trade.PositionModify(Symbol(),sl,0);
  }
//+------------------------------------------------------------------+

and the tab "Journal":

2016.08.17 13:12:23.319 Trades  '1783501': modify #94479740 buy 0.01 USDJPY -> sl: 100.475, tp: 0.000 done in 225 ms

 

If there is a position in the terminal - hence modification is successful. 

Perhaps you need to do so: before modifying the position, you need to make: This position exists in the terminal? 

Files:
Test.mq5 3 kb
Jugler1986
22
Jugler1986 2016.08.18 12:14  
Karputov Vladimir:
Example of netting:

first open the deal and get the position. Then, after 60 seconds, change the stop loss level:

and the tab "Journal":

 

If there is a position in the terminal - hence modification is successful. 

Perhaps you need to do so: before modifying the position, you need to make: This position exists in the terminal? 

Okay. Thanks for your help but it is just not working. just send you the Code of trade.buy

 

Maybe you'll finde an error there why it does not work 

glBuyPlaced = Trade.Buy(_Symbol,tradeSize);
if(glBuyPlaced == true)  
                        {
                                do(Sleep(1000)); 
                                while(PositionSelect(_Symbol) == false);
                                double openPrice = PositionOpenPrice(_Symbol);
                                
                                double buyStop = BuyStopLoss(_Symbol,StopLoss,openPrice);
                                if(buyStop > 0) AdjustBelowStopLevel(_Symbol,buyStop);
                                
                                double buyProfit = BuyTakeProfit(_Symbol,TakeProfit,openPrice);
                                if(buyProfit > 0) AdjustAboveStopLevel(_Symbol,buyProfit);
                                
                                if(buyStop > 0 || buyProfit > 0) Trade.ModifyPosition(_Symbol,buyStop,buyProfit);
                                glSellPlaced = false;
// Modify position
bool CTrade::ModifyPosition(string pSymbol,double pStop,double pProfit=0.000000)
{
        request.action = TRADE_ACTION_SLTP;
        request.symbol = pSymbol;
        request.sl = pStop;
        request.tp = pProfit;
        
        // Order loop
        int retryCount = 0;
        int checkCode = 0;
        
        do 
        {
                OrderSend(request,result);
                
                
                checkCode = CheckReturnCode(result.retcode);
                                
                if(checkCode == CHECK_RETCODE_OK) break;
                else if(checkCode == CHECK_RETCODE_ERROR)
                {
                        string errDesc = TradeServerReturnCodeDescription(result.retcode);
                        Alert("Modify position: Error ",result.retcode," - ",errDesc);
                        break;
                }
                else
                {
                        Print("Server error detected, retrying...");
                        Sleep(RETRY_DELAY);
                        retryCount++;
                }
        }
        while(retryCount < MAX_RETRIES);
        
        if(retryCount >= MAX_RETRIES)
        {
                string errDesc = TradeServerReturnCodeDescription(result.retcode);
                Alert("Max retries exceeded: Error ",result.retcode," - ",errDesc);
        }
        
        string errDesc = TradeServerReturnCodeDescription(result.retcode);
        Print("Modify position:",result.retcode," - ",errDesc,", SL: ",request.sl,", TP: ",request.tp,", Bid: ",SymbolInfoDouble(pSymbol,SYMBOL_BID),", Ask: ",SymbolInfoDouble(pSymbol,SYMBOL_ASK),", Stop Level: ",SymbolInfoInteger(pSymbol,SYMBOL_TRADE_STOPS_LEVEL));
        
        if(checkCode == CHECK_RETCODE_OK) 
        {
                Comment("Position modified on ",pSymbol,", SL: ",request.sl,", TP: ",request.tp);
                return(true);
        }
        else return(false);
}
Jugler1986
22
Jugler1986 2016.08.18 12:16  
Jugler1986:

Okay. Thanks for your help but it is just not working. just send you the Code of trade.buy

 

Maybe you'll finde an error there why it does not work 

 

While Debugging it just jumps from this line

checkCode = CheckReturnCode(result.retcode);
                                
                if(checkCode == CHECK_RETCODE_OK) break;

 to this one. So The code is working but i don't get why there is the RETCODE10036 Error...

 

else if(checkCode == CHECK_RETCODE_ERROR)
Vladimir Karputov
Moderator
49430
Vladimir Karputov 2016.08.18 12:39  

I have shown you completely working code. Study.

 

It is added later:

before use, all structures need to be NULLIFIED! For example so:

//+------------------------------------------------------------------+
//| Clear structures m_request,m_result and m_check_result           |
//+------------------------------------------------------------------+
void CTrade::ClearStructures(void)
  {
   ZeroMemory(m_request);
   ZeroMemory(m_result);
   ZeroMemory(m_check_result);
  }
Jugler1986
22
Jugler1986 2016.08.18 12:54  
Karputov Vladimir:
I have shown you completely working code. Study.
k thank you
Vladimir Karputov
Moderator
49430
Vladimir Karputov 2016.08.18 12:56  
Jugler1986:
k thank you
I have added the text.
And, in general, I advise to use standard library - a class CTrade
To add comments, please log in or register