vdemydiuk / mtapi

MetaTrader API (terminal bridge)
http://mtapi4.net/
MIT License
524 stars 283 forks source link

[MT5] PositionPartialClose #186

Closed Xmg92 closed 4 years ago

Xmg92 commented 4 years ago

Hi everyone!

Is there a way to close a partially a position in a Hedge account? I've seen that MQL5 has a PositionPartialClose function (link) but i can't find it in the api.

Is there any way that i can partial close without this function? If not, is it possible to implement it?

Thanks in advance.

KptKuck commented 4 years ago

This function is from the CTrade Library. Look into the library to see how the function is constructed. Then you can rebuild it.

//+------------------------------------------------------------------+ //| Partial close specified opened position (for hedging mode only) | //+------------------------------------------------------------------+ bool CTrade::PositionClosePartial(const string symbol,const double volume,const ulong deviation) { uint retcode=TRADE_RETCODE_REJECT; //--- check stopped if(IsStopped(FUNCTION)) return(false); //--- for hedging mode only if(!IsHedging()) return(false); //--- clean ClearStructures(); //--- check filling if(!FillingCheck(symbol)) return(false); //--- check if(SelectPosition(symbol)) { if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { //--- prepare request for close BUY position m_request.type =ORDER_TYPE_SELL; m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID); } else { //--- prepare request for close SELL position m_request.type =ORDER_TYPE_BUY; m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK); } } else { //--- position not found m_result.retcode=retcode; return(false); } //--- check volume double position_volume=PositionGetDouble(POSITION_VOLUME); if(position_volume>volume) position_volume=volume; //--- setting request m_request.action =TRADE_ACTION_DEAL; m_request.symbol =symbol; m_request.volume =position_volume; m_request.magic =m_magic; m_request.deviation=(deviation==ULONG_MAX) ? m_deviation : deviation; m_request.position =PositionGetInteger(POSITION_TICKET); //--- hedging? just send order return(OrderSend(m_request,m_result)); }

vdemydiuk commented 4 years ago

@Xmg92 I will implement the function in next release.