diff --git a/MtApi5/MtApi5.csproj b/MtApi5/MtApi5.csproj
index d11ca2de..6d71bd26 100755
--- a/MtApi5/MtApi5.csproj
+++ b/MtApi5/MtApi5.csproj
@@ -78,6 +78,7 @@
+
@@ -96,6 +97,7 @@
+
diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs
index dfe730e4..6355fa45 100755
--- a/MtApi5/MtApi5Client.cs
+++ b/MtApi5/MtApi5Client.cs
@@ -683,6 +683,64 @@ namespace MtApi5
return SendCommand(Mt5CommandType.PositionClosePartial_byTicket, commandParameters);
}
+
+ ///
+ /// Opens a long position with specified parameters with current market Ask price
+ ///
+ /// output result
+ /// Requested position volume.
+ /// Position symbol. If it is not specified, the current symbol will be used.
+ /// Execution price.
+ /// Stop Loss price.
+ /// Take Profit price.
+ /// Comment.
+ /// true - successful check of the structures, otherwise - false.
+ public bool Buy(out MqlTradeResult result, double volume, string symbol = null, double price = 0.0, double sl = 0.0, double tp = 0.0, string comment = null)
+ {
+ Log.Debug($"Buy: volume = {volume}, symbol = {symbol}, sl = {sl}, tp = {tp}, comment = {comment}");
+
+ var response = SendRequest(new BuyRequest
+ {
+ Volume = volume,
+ Symbol = symbol,
+ Price = price,
+ Sl = sl,
+ Tp = tp,
+ Comment = comment
+ });
+
+ result = response?.TradeResult;
+ return response != null && response.RetVal;
+ }
+
+ ///
+ /// Opens a short position with specified parameters with current market Bid price
+ ///
+ /// output result
+ /// Requested position volume.
+ /// Position symbol. If it is not specified, the current symbol will be used.
+ /// Execution price.
+ /// Stop Loss price.
+ /// Take Profit price.
+ /// Comment.
+ /// true - successful check of the structures, otherwise - false.
+ public bool Sell(out MqlTradeResult result, double volume, string symbol = null, double price = 0.0, double sl = 0.0, double tp = 0.0, string comment = null)
+ {
+ Log.Debug($"Sell: volume = {volume}, symbol = {symbol}, sl = {sl}, tp = {tp}, comment = {comment}");
+
+ var response = SendRequest(new SellRequest
+ {
+ Volume = volume,
+ Symbol = symbol,
+ Price = price,
+ Sl = sl,
+ Tp = tp,
+ Comment = comment
+ });
+
+ result = response?.TradeResult;
+ return response != null && response.RetVal;
+ }
#endregion
#region Account Information functions
diff --git a/MtApi5/Requests/BuyRequest.cs b/MtApi5/Requests/BuyRequest.cs
new file mode 100644
index 00000000..911a0fcf
--- /dev/null
+++ b/MtApi5/Requests/BuyRequest.cs
@@ -0,0 +1,14 @@
+namespace MtApi5.Requests
+{
+ internal class BuyRequest : RequestBase
+ {
+ public override RequestType RequestType => RequestType.Buy;
+
+ public double Volume { get; set; }
+ public string Symbol { get; set; }
+ public double Price { get; set; }
+ public double Sl { get; set; }
+ public double Tp { get; set; }
+ public string Comment { get; set; }
+ }
+}
diff --git a/MtApi5/Requests/RequestType.cs b/MtApi5/Requests/RequestType.cs
index 1355b273..38c7dfd3 100755
--- a/MtApi5/Requests/RequestType.cs
+++ b/MtApi5/Requests/RequestType.cs
@@ -16,6 +16,8 @@ namespace MtApi5.Requests
ChartTimePriceToXY = 9,
ChartXYToTimePrice = 10,
PositionClose = 11,
- SymbolInfoTick = 12
+ SymbolInfoTick = 12,
+ Buy = 13,
+ Sell = 14
}
}
\ No newline at end of file
diff --git a/MtApi5/Requests/SellRequest.cs b/MtApi5/Requests/SellRequest.cs
new file mode 100644
index 00000000..f276a8cc
--- /dev/null
+++ b/MtApi5/Requests/SellRequest.cs
@@ -0,0 +1,14 @@
+namespace MtApi5.Requests
+{
+ internal class SellRequest : RequestBase
+ {
+ public override RequestType RequestType => RequestType.Sell;
+
+ public double Volume { get; set; }
+ public string Symbol { get; set; }
+ public double Price { get; set; }
+ public double Sl { get; set; }
+ public double Tp { get; set; }
+ public string Comment { get; set; }
+ }
+}
diff --git a/TestClients/MtApi5TestClient/MainWindow.xaml b/TestClients/MtApi5TestClient/MainWindow.xaml
index 8c280363..f9a59336 100755
--- a/TestClients/MtApi5TestClient/MainWindow.xaml
+++ b/TestClients/MtApi5TestClient/MainWindow.xaml
@@ -497,6 +497,7 @@
+
@@ -505,6 +506,10 @@
+
+
+
+
diff --git a/TestClients/MtApi5TestClient/ViewModel.cs b/TestClients/MtApi5TestClient/ViewModel.cs
index bf373118..5b42282c 100755
--- a/TestClients/MtApi5TestClient/ViewModel.cs
+++ b/TestClients/MtApi5TestClient/ViewModel.cs
@@ -73,6 +73,8 @@ namespace MtApi5TestClient
public DelegateCommand PositionOpenCommand { get; private set; }
public DelegateCommand PositionCloseCommand { get; private set; }
public DelegateCommand PositionCloseAllCommand { get; private set; }
+ public DelegateCommand BuyCommand { get; private set; }
+ public DelegateCommand SellCommand { get; private set; }
public DelegateCommand GetLastErrorCommand { get; private set; }
public DelegateCommand ResetLastErrorCommand { get; private set; }
@@ -386,6 +388,8 @@ namespace MtApi5TestClient
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
PositionCloseAllCommand = new DelegateCommand(ExecutePositionCloseAll);
+ BuyCommand = new DelegateCommand(ExecuteBuy);
+ SellCommand = new DelegateCommand(ExecuteSell);
PrintCommand = new DelegateCommand(ExecutePrint);
AlertCommand = new DelegateCommand(ExecuteAlert);
@@ -1211,6 +1215,26 @@ namespace MtApi5TestClient
AddLog($"PositionCloseAll: count = {retVal}");
}
+ private async void ExecuteBuy(object obj)
+ {
+ const string symbol = "EURUSD";
+ const double volume = 0.1;
+ MqlTradeResult tradeResult = null;
+
+ var retVal = await Execute(() => _mtApiClient.Buy(out tradeResult, volume, symbol));
+ AddLog($"Buy: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
+ }
+
+ private async void ExecuteSell(object obj)
+ {
+ const string symbol = "EURUSD";
+ const double volume = 0.1;
+ MqlTradeResult tradeResult = null;
+
+ var retVal = await Execute(() => _mtApiClient.Sell(out tradeResult, volume, symbol));
+ AddLog($"Sell: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
+ }
+
private async void ExecutePrint(object obj)
{
var message = MessageText;
diff --git a/mq5/MtApi5.ex5 b/mq5/MtApi5.ex5
index d094e354..8b946f38 100644
Binary files a/mq5/MtApi5.ex5 and b/mq5/MtApi5.ex5 differ
diff --git a/mq5/MtApi5.mq5 b/mq5/MtApi5.mq5
index 4a9a1054..4baafd49 100644
--- a/mq5/MtApi5.mq5
+++ b/mq5/MtApi5.mq5
@@ -1,7 +1,7 @@
#property copyright "Vyacheslav Demidyuk"
#property link ""
-#property version "1.7"
+#property version "1.8"
#property description "MtApi (MT5) connection expert"
#include
@@ -6956,6 +6956,12 @@ string OnRequest(string json)
case 12: //SymbolInfoTick
response = ExecuteRequest_SymbolInfoTick(jo);
break;
+ case 13: //Buy
+ response = ExecuteRequest_Buy(jo);
+ break;
+ case 14: //Sell
+ response = ExecuteRequest_Sell(jo);
+ break;
default:
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
response = CreateErrorResponse(-1, "Unknown request type");
@@ -7487,6 +7493,98 @@ string ExecuteRequest_SymbolInfoTick(JSONObject *jo)
return CreateSuccessResponse("Value", MqlTickToJson(tick));
}
+string ExecuteRequest_Buy(JSONObject *jo)
+{
+ //Symbol
+ string symbol=Symbol();
+ if (jo.getValue("Symbol") != NULL)
+ symbol = jo.getString("Symbol");
+
+ //Volume
+ CHECK_JSON_VALUE(jo, "Volume", CreateErrorResponse(-1, "Undefinded mandatory parameter Volume"));
+ double volume = jo.getDouble("Volume");
+
+ //Price
+ CHECK_JSON_VALUE(jo, "Price", CreateErrorResponse(-1, "Undefinded mandatory parameter Price"));
+ double price = jo.getDouble("Price");
+
+ //Sl
+ CHECK_JSON_VALUE(jo, "Sl", CreateErrorResponse(-1, "Undefinded mandatory parameter Sl"));
+ double sl = jo.getDouble("Sl");
+
+ //Tp
+ CHECK_JSON_VALUE(jo, "Tp", CreateErrorResponse(-1, "Undefinded mandatory parameter Tp"));
+ double tp = jo.getDouble("Tp");
+
+ //Comment
+ string comment="";
+ if (jo.getValue("Comment") != NULL)
+ comment = jo.getString("Comment");
+
+#ifdef __DEBUG_LOG__
+ PrintFormat("%s: symbol = %s, volume = %f, price = %f, sl = %f, tp = %f, comment = %s",
+ __FUNCTION__, symbol, volume, price, sl, tp, comment);
+#endif
+
+ CTrade trade;
+ bool ok = trade.Buy(volume, symbol, price, sl, tp, comment);
+
+ MqlTradeResult trade_result={0};
+ trade.Result(trade_result);
+
+ 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);
+}
+
+string ExecuteRequest_Sell(JSONObject *jo)
+{
+ //Symbol
+ string symbol=Symbol();
+ if (jo.getValue("Symbol") != NULL)
+ symbol = jo.getString("Symbol");
+
+ //Volume
+ CHECK_JSON_VALUE(jo, "Volume", CreateErrorResponse(-1, "Undefinded mandatory parameter Volume"));
+ double volume = jo.getDouble("Volume");
+
+ //Price
+ CHECK_JSON_VALUE(jo, "Price", CreateErrorResponse(-1, "Undefinded mandatory parameter Price"));
+ double price = jo.getDouble("Price");
+
+ //Sl
+ CHECK_JSON_VALUE(jo, "Sl", CreateErrorResponse(-1, "Undefinded mandatory parameter Sl"));
+ double sl = jo.getDouble("Sl");
+
+ //Tp
+ CHECK_JSON_VALUE(jo, "Tp", CreateErrorResponse(-1, "Undefinded mandatory parameter Tp"));
+ double tp = jo.getDouble("Tp");
+
+ //Comment
+ string comment="";
+ if (jo.getValue("Comment") != NULL)
+ comment = jo.getString("Comment");
+
+#ifdef __DEBUG_LOG__
+ PrintFormat("%s: symbol = %s, volume = %f, price = %f, sl = %f, tp = %f, comment = %s",
+ __FUNCTION__, symbol, volume, price, sl, tp, comment);
+#endif
+
+ CTrade trade;
+ bool ok = trade.Sell(volume, symbol, price, sl, tp, comment);
+
+ MqlTradeResult trade_result={0};
+ trade.Result(trade_result);
+
+ 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