diff --git a/MtApi5/MtApi5.csproj b/MtApi5/MtApi5.csproj
index d83a75da..a765925a 100755
--- a/MtApi5/MtApi5.csproj
+++ b/MtApi5/MtApi5.csproj
@@ -85,6 +85,8 @@
+
+
diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs
index 1cecd379..84d9dd8b 100755
--- a/MtApi5/MtApi5Client.cs
+++ b/MtApi5/MtApi5Client.cs
@@ -536,6 +536,36 @@ namespace MtApi5
return SendCommand(Mt5CommandType.PositionClose, commandParameters);
}
+ ///
+ ///Closes a position with the specified ticket.
+ ///
+ ///Ticket of the closed position.
+ ///Maximal deviation from the current price (in points).
+ /// output result
+ public bool PositionClose(ulong ticket, ulong deviation, out MqlTradeResult result)
+ {
+ Log.Debug($"PositionClose: ticket = {ticket}, deviation = {deviation}");
+
+ var response = SendRequest(new PositionCloseRequest
+ {
+ Ticket = ticket,
+ Deviation = deviation
+ });
+
+ result = response?.TradeResult;
+ return response != null && response.RetVal;
+ }
+
+ ///
+ ///Closes a position with the specified ticket.
+ ///
+ ///Ticket of the closed position.
+ /// output result
+ public bool PositionClose(ulong ticket, out MqlTradeResult result)
+ {
+ return PositionClose(ticket, ulong.MaxValue, out result);
+ }
+
///
/// Opens a position with the specified parameters.
///
@@ -566,7 +596,7 @@ namespace MtApi5
/// comment
/// output result
/// true - successful check of the basic structures, otherwise - false.
- public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment , out MqlTradeResult result)
+ public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment, out MqlTradeResult result)
{
Log.Debug($"PositionOpen: symbol = {symbol}, orderType = {orderType}, volume = {volume}, price = {price}, sl = {sl}, tp = {tp}, comment = {comment}");
@@ -584,15 +614,31 @@ namespace MtApi5
result = response?.TradeResult;
return response != null && response.RetVal;
}
+
+ ///
+ /// Opens a position with the specified parameters.
+ ///
+ /// symbol
+ /// order type to open position
+ /// position volume
+ /// execution price
+ /// Stop Loss price
+ /// Take Profit price
+ /// output result
+ /// true - successful check of the basic structures, otherwise - false.
+ public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, out MqlTradeResult result)
+ {
+ return PositionOpen(symbol, orderType, volume, price, sl, tp, "", out result);
+ }
#endregion
#region Account Information functions
- ///
- ///Returns the value of the corresponding account property.
- ///
- ///Identifier of the property.
- public double AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE propertyId)
+ ///
+ ///Returns the value of the corresponding account property.
+ ///
+ ///Identifier of the property.
+ public double AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE propertyId)
{
var commandParameters = new ArrayList { (int)propertyId };
diff --git a/MtApi5/Requests/PositionCloseRequest.cs b/MtApi5/Requests/PositionCloseRequest.cs
new file mode 100644
index 00000000..3695397c
--- /dev/null
+++ b/MtApi5/Requests/PositionCloseRequest.cs
@@ -0,0 +1,10 @@
+namespace MtApi5.Requests
+{
+ internal class PositionCloseRequest : RequestBase
+ {
+ public override RequestType RequestType => RequestType.PositionClose;
+
+ public ulong Ticket { get; set; }
+ public ulong Deviation { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/MtApi5/Requests/PositionCloseResult.cs b/MtApi5/Requests/PositionCloseResult.cs
new file mode 100644
index 00000000..7fb411a0
--- /dev/null
+++ b/MtApi5/Requests/PositionCloseResult.cs
@@ -0,0 +1,8 @@
+namespace MtApi5.Requests
+{
+ internal class PositionCloseResult
+ {
+ public bool RetVal { get; set; }
+ public MqlTradeResult TradeResult { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/MtApi5/Requests/RequestType.cs b/MtApi5/Requests/RequestType.cs
index d0ac6c5a..6f3ab373 100755
--- a/MtApi5/Requests/RequestType.cs
+++ b/MtApi5/Requests/RequestType.cs
@@ -14,6 +14,7 @@ namespace MtApi5.Requests
IndicatorCreate = 7,
SymbolInfoString = 8,
ChartTimePriceToXY = 9,
- ChartXYToTimePrice = 10
+ ChartXYToTimePrice = 10,
+ PositionClose = 11
}
}
\ No newline at end of file
diff --git a/TestClients/MtApi5TestClient/MainWindow.xaml b/TestClients/MtApi5TestClient/MainWindow.xaml
index 5a280a7f..62c9e970 100755
--- a/TestClients/MtApi5TestClient/MainWindow.xaml
+++ b/TestClients/MtApi5TestClient/MainWindow.xaml
@@ -481,10 +481,19 @@
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TestClients/MtApi5TestClient/ViewModel.cs b/TestClients/MtApi5TestClient/ViewModel.cs
index f4b553db..76773acd 100755
--- a/TestClients/MtApi5TestClient/ViewModel.cs
+++ b/TestClients/MtApi5TestClient/ViewModel.cs
@@ -66,6 +66,7 @@ namespace MtApi5TestClient
public DelegateCommand MarketBookGetCommand { get; private set; }
public DelegateCommand PositionOpenCommand { get; private set; }
+ public DelegateCommand PositionCloseCommand { get; private set; }
public DelegateCommand GetLastErrorCommand { get; private set; }
public DelegateCommand ResetLastErrorCommand { get; private set; }
@@ -263,6 +264,17 @@ namespace MtApi5TestClient
OnPropertyChanged("GlobalVarValue");
}
}
+
+ private ulong _positionTicketValue;
+ public ulong PositionTicketValue
+ {
+ get { return _positionTicketValue; }
+ set
+ {
+ _positionTicketValue = value;
+ OnPropertyChanged("PositionTicketValue");
+ }
+ }
#endregion
#region Public Methods
@@ -358,6 +370,7 @@ namespace MtApi5TestClient
MarketBookGetCommand = new DelegateCommand(ExecuteMarketBookGet);
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
+ PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
PrintCommand = new DelegateCommand(ExecutePrint);
AlertCommand = new DelegateCommand(ExecuteAlert);
@@ -1098,6 +1111,15 @@ namespace MtApi5TestClient
AddLog($"PositionOpen: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
}
+ private async void ExecutePositionClose(object obj)
+ {
+ var ticket = PositionTicketValue;
+ MqlTradeResult tradeResult = null;
+
+ var retVal = await Execute(() => _mtApiClient.PositionClose(ticket, out tradeResult));
+ AddLog($"PositionClose: ticket {ticket} retVal = {retVal}, result = {tradeResult}");
+ }
+
private async void ExecutePrint(object obj)
{
var message = MessageText;
diff --git a/mq5/MtApi5.ex5 b/mq5/MtApi5.ex5
index 19f554c3..edbc3036 100755
Binary files a/mq5/MtApi5.ex5 and b/mq5/MtApi5.ex5 differ
diff --git a/mq5/MtApi5.mq5 b/mq5/MtApi5.mq5
index 0f471700..841f31c8 100644
--- a/mq5/MtApi5.mq5
+++ b/mq5/MtApi5.mq5
@@ -41,7 +41,7 @@
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
#import
-#define __DEBUG_LOG__
+//#define __DEBUG_LOG__
input int Port = 8228;
@@ -6700,6 +6700,9 @@ string OnRequest(string json)
case 10: //ChartXYToTimePrice
response = ExecuteRequest_ChartXYToTimePrice(jo);
break;
+ case 11: //PositionClose
+ response = ExecuteRequest_PositionClose(jo);
+ break;
default:
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
response = CreateErrorResponse(-1, "Unknown request type");
@@ -7180,6 +7183,37 @@ string ExecuteRequest_ChartXYToTimePrice(JSONObject *jo)
return CreateSuccessResponse("Value", result_value_jo);
}
+string ExecuteRequest_PositionClose(JSONObject *jo)
+{
+ //Ticket
+ CHECK_JSON_VALUE(jo, "Ticket", CreateErrorResponse(-1, "Undefinded mandatory parameter Ticket"));
+ ulong ticket = jo.getLong("Ticket");
+
+ //Deviation
+ CHECK_JSON_VALUE(jo, "Deviation", CreateErrorResponse(-1, "Undefinded mandatory parameter Deviation"));
+ ulong deviation = jo.getLong("Deviation");
+
+#ifdef __DEBUG_LOG__
+ PrintFormat("%s: Ticket = %d, Deviation = %d", __FUNCTION__, ticket, deviation);
+#endif
+
+ CTrade trade;
+ bool ok = trade.PositionClose(ticket, deviation);
+
+ MqlTradeResult trade_result={0};
+ trade.Result(trade_result);
+
+#ifdef __DEBUG_LOG__
+ Print("ExecuteRequest_PositionClose: retcode = ", trade.ResultRetcode());
+#endif
+
+ JSONObject* result_value_jo = new JSONObject();
+ result_value_jo.put("RetVal", new JSONBool(ok));
+ result_value_jo.put("TradeResult", MqlTradeResultToJson(trade_result));
+
+ return CreateSuccessResponse("Value", result_value_jo);
+}
+
//------------ Events -------------------------------------------------------
enum MtEventTypes