Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MT5] PositionPartialClose #186

Closed
Xmg92 opened this issue Dec 17, 2019 · 2 comments
Closed

[MT5] PositionPartialClose #186

Xmg92 opened this issue Dec 17, 2019 · 2 comments

Comments

@Xmg92
Copy link

Xmg92 commented Dec 17, 2019

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
Copy link

KptKuck commented Jan 18, 2020

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
Copy link
Owner

@Xmg92 I will implement the function in next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
MtApi MT5
Awaiting triage
Development

No branches or pull requests

3 participants