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 @@ - - -