Issue #186: Implemented function PositionClosePartial

This commit is contained in:
Viacheslav Demydiuk
2020-08-24 22:43:20 +03:00
parent e0daf9a82e
commit 4349eb3538
4 changed files with 110 additions and 0 deletions
+2
View File
@@ -102,6 +102,8 @@ namespace MtApi5
PositionClose = 64,
PositionOpen = 65,
PositionModify = 6066,
PositionClosePartial_bySymbol = 6067,
PositionClosePartial_byTicket = 6068,
//PositionOpenWithResult = 1065,
//Backtesting
+28
View File
@@ -655,6 +655,34 @@ namespace MtApi5
{
return PositionOpen(symbol, orderType, volume, price, sl, tp, "", out result);
}
/// <summary>
/// Partially closes a position on a specified symbol in case of a "hedging" accounting.
/// </summary>
/// <param name="symbol">Name of a trading instrument, on which a position is closed partially.</param>
/// <param name="volume"> Volume, by which a position should be decreased. If the value exceeds the volume of a partially closed position, it is closed in full. No position in the opposite direction is opened.</param>
/// <param name="deviation">The maximum deviation from the current price (in points).</param>
/// <returns>true if the basic check of structures is successful, otherwise false.</returns>
public bool PositionClosePartial(string symbol, double volume, ulong deviation = ulong.MaxValue)
{
var commandParameters = new ArrayList { symbol, volume, deviation };
return SendCommand<bool>(Mt5CommandType.PositionClosePartial_bySymbol, commandParameters);
}
/// <summary>
/// Partially closes a position on a specified symbol in case of a "hedging" accounting.
/// </summary>
/// <param name="ticket">Closed position ticket.</param>
/// <param name="volume"> Volume, by which a position should be decreased. If the value exceeds the volume of a partially closed position, it is closed in full. No position in the opposite direction is opened.</param>
/// <param name="deviation">The maximum deviation from the current price (in points).</param>
/// <returns>true if the basic check of structures is successful, otherwise false.</returns>
public bool PositionClosePartial(ulong ticket, double volume, ulong deviation = ulong.MaxValue)
{
var commandParameters = new ArrayList { ticket, volume, deviation };
return SendCommand<bool>(Mt5CommandType.PositionClosePartial_byTicket, commandParameters);
}
#endregion
#region Account Information functions
BIN
View File
Binary file not shown.
+80
View File
@@ -572,6 +572,12 @@ int executeCommand()
case 6066: //PositionModify
Execute_PositionModify();
break;
case 6067: //PositionClosePartial_bySymbol
Execute_PositionClosePartial_bySymbol();
break;
case 6068: //Execute_PositionClosePartial_byTicket
Execute_PositionClosePartial_byTicket();
break;
case 66: //BacktestingReady
Execute_BacktestingReady();
break;
@@ -3313,6 +3319,80 @@ void Execute_PositionModify()
}
}
void Execute_PositionClosePartial_bySymbol()
{
string symbol;
double volume;
ulong deviation;
if (!getStringValue(ExpertHandle, 0, symbol, _error))
{
PrintParamError("PositionClosePartial (1)", "symbol", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getDoubleValue(ExpertHandle, 1, volume, _error))
{
PrintParamError("PositionClosePartial (1)", "volume", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getULongValue(ExpertHandle, 2, deviation, _error))
{
PrintParamError("PositionClosePartial (1)", "deviation", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
CTrade trade;
bool ok = trade.PositionClosePartial(symbol, volume, deviation);
#ifdef __DEBUG_LOG__
Print("command PositionClosePartial (1): result = ", ok);
#endif
if (!sendBooleanResponse(ExpertHandle, ok, _response_error))
{
PrintResponseError("PositionClosePartial (1)", _response_error);
}
}
void Execute_PositionClosePartial_byTicket()
{
ulong ticket;
double volume;
ulong deviation;
if (!getULongValue(ExpertHandle, 0, ticket, _error))
{
PrintParamError("PositionClosePartial (2)", "ticket", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getDoubleValue(ExpertHandle, 1, volume, _error))
{
PrintParamError("PositionClosePartial (2)", "volume", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getULongValue(ExpertHandle, 2, deviation, _error))
{
PrintParamError("PositionClosePartial (2)", "deviation", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
CTrade trade;
bool ok = trade.PositionClosePartial(ticket, volume, deviation);
#ifdef __DEBUG_LOG__
Print("command PositionClosePartial (2): result = ", ok);
#endif
if (!sendBooleanResponse(ExpertHandle, ok, _response_error))
{
PrintResponseError("PositionClosePartial (2)", _response_error);
}
}
void Execute_PositionOpen(bool isTradeResultRequired)
{
string symbol;