Download MetaTrader 5

Closing a position

To add comments, please log in or register
Stephen Njuki
5180
Stephen Njuki 2016.04.14 18:30 
How does one close a position under the new hedging system via an MqlTradeRequest?
Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.15 08:07  
STEPHEN NJUKI:
How does one close a position under the new hedging system via an MqlTradeRequest?
When modifying or closing a position in the hedging system, make sure to specify its ticket (MqlTradeRequest::ticket). You can specify a ticket in the netting system as well, however positions are identified by a symbol name.
MetaTrader 5 features hedging position accounting system
MetaTrader 5 features hedging position accounting system
  • 2016.03.21
  • MetaQuotes Software Corp.
  • www.mql5.com
In order to expand possibilities of retail Forex traders, we have added the second accounting system — hedging. Now, it is possible to have multiple positions per symbol, including oppositely directed ones. This paves the way to implementing trading strategies based on the so-called "locking" — if the price moves against a trader, they can open a position in the opposite direction.
Stephen Njuki
5180
Stephen Njuki 2016.04.15 16:10  
Alain Verleyen:
When modifying or closing a position in the hedging system, make sure to specify its ticket (MqlTradeRequest::ticket). You can specify a ticket in the netting system as well, however positions are identified by a symbol name.

Hi

I had seen the article which is why I tried the code below. However I get the error invalid request. Already contacted service desk and they are yet to revert.

       for(int i=0;i<PositionsTotal();i++)
        {
            if(PositionSelect(PositionGetSymbol(i)))
              {
                  if(PositionGetSymbol(i)==SymbolName)
                    {
                        if(SymbolInfoTick(SymbolName,__tick))
                           {
      
                              float _bid=(float)__tick.bid,_ask=(float)__tick.ask;
                              
                              long _position_ticket = PositionGetInteger(POSITION_TICKET);
                              
                              __request.symbol = PositionGetSymbol(i);
                              __request.action = TRADE_ACTION_CLOSE_BY;
                              __request.position = _position_ticket;
                              
                              __request.type_filling = ORDER_FILLING_FOK;
                              __request.type_time = ORDER_TIME_GTC;
                              __request.magic = MagicNumber;
                              
                              if(OrderCheck(__request,__check)){
                                 if(!OrderSend(__request,__result)){
                                    _close_error=GetLastError();
                                    printf(__FUNCTION__+" RESULT ON "+SymbolName+" "+EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE))+" could not be closed "+
                                          ", WITH ask: "+DoubleToString(_ask)+", bid: "+DoubleToString(_bid)+
                                          " err # "+IntegerToString(_close_error)+" returned code: "+IntegerToString(__result.retcode)+", broker comments: "+__result.comment);
                                    return(_close_error);}
                                 else{
                                    _close_error=GetLastError();
                                    if(_close_error!=0&&(__result.retcode!=TRADE_RETCODE_PLACED||__result.retcode!=TRADE_RETCODE_DONE)){
                                       printf(__FUNCTION__+" "+SymbolName+" "+EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE))+"  may not be closed "+
                                          ", WITH ask: "+DoubleToString(_ask)+", bid: "+DoubleToString(_bid)+
                                          " err # "+IntegerToString(_close_error)+" returned code: "+IntegerToString(__result.retcode)+", broker comments: "+__result.comment);}}}
                                 else{
                                    _close_error=GetLastError();
                                    printf(__FUNCTION__+" CHECK ON "+SymbolName+" "+EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE))+" could not be closed "+
                                             ", WITH ask: "+DoubleToString(_ask)+", bid: "+DoubleToString(_bid)+
                                             " err # "+IntegerToString(_close_error)+" returned code: "+IntegerToString(__check.retcode)+", broker comments: "+__check.comment);
                                    return(_close_error); }
                              
                              }
                              
                    }
              }
        }
Stephen Njuki
5180
Stephen Njuki 2016.04.19 19:18  
You edited your original answer to my question so my comment and code no longer follow what you had written...
Stephen Njuki
5180
Stephen Njuki 2016.04.19 19:22  
According to this  position is a ticket.
Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.19 20:40  
STEPHEN NJUKI:
You edited your original answer to my question so my comment and code no longer follow what you had written...
Who are you talking to ?
Stephen Njuki
5180
Stephen Njuki 2016.04.20 00:32  
Alain Verleyen:
Who are you talking to ?
Sorry, there was only one respondent here. 
Any way no worries service desk said they will revert.
Alain Verleyen
Moderator
31085
Alain Verleyen 2016.04.20 10:10  
STEPHEN NJUKI:
Sorry, there was only one respondent here. 
Any way no worries service desk said they will revert.
I don't know what you are talking about, I didn't change anything.
Stephen Njuki
5180
Stephen Njuki 2016.04.20 12:33  
Then I guess I had not properly read your earlier reply. Sorry
To add comments, please log in or register