Position doesn't exist - StopLoss activation

 

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 


 
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. 

 
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  

 
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
 
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:

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)
 

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);
  }
 
Karputov Vladimir:
I have shown you completely working code. Study.
k thank you
 
Jugler1986:
k thank you
I have added the text.
And, in general, I advise to use standard library - a class CTrade
 

Hi @Vladimir Karputov


I have tried your code, it works however digit conversions put tp & sl too far, below is my result;


BID: 1.13468

TP: 1.23454

SL: 1.03454


I have specified SL 20 and TP 50. 


double m_adjusted_point;

               //--- 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=stopLoss*m_adjusted_point;

               double m_take_profit=takeProfit*m_adjusted_point;

               

               //double m_stop_loss=stopLoss*_Point;

               //double m_take_profit=takeProfit*_Point;

            

               MqlTick m_tick;

               SymbolInfoTick(Symbol(),m_tick);

            

               double sl=NormalizeDouble(m_tick.bid-m_stop_loss,Digits());

               double tp=NormalizeDouble(m_tick.bid+m_take_profit,Digits());

               

               my_trade.PositionModify(Symbol(),sl,tp);

 
Jugler1986:

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 


Dear Jugler1986, Is this Issue Resolved ? if so , How ?