From 93aed4e44cc07ba9c8f8d0ff0e062442c8c0cbb2 Mon Sep 17 00:00:00 2001 From: Viacheslav Demydiuk Date: Tue, 13 Oct 2020 14:14:59 +0300 Subject: [PATCH] Issue #202: [MT5] added Buy/Sell functions --- MtApi5/MtApi5.csproj | 2 + MtApi5/MtApi5Client.cs | 58 +++++++++++ MtApi5/Requests/BuyRequest.cs | 14 +++ MtApi5/Requests/RequestType.cs | 4 +- MtApi5/Requests/SellRequest.cs | 14 +++ TestClients/MtApi5TestClient/MainWindow.xaml | 5 + TestClients/MtApi5TestClient/ViewModel.cs | 24 +++++ mq5/MtApi5.ex5 | Bin 760622 -> 782920 bytes mq5/MtApi5.mq5 | 100 ++++++++++++++++++- 9 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 MtApi5/Requests/BuyRequest.cs create mode 100644 MtApi5/Requests/SellRequest.cs 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 @@ +