From cacc0ea787198c877b50c30483840f6552bd9b27 Mon Sep 17 00:00:00 2001 From: vdemydiuk Date: Thu, 6 Oct 2016 14:04:17 +0300 Subject: [PATCH] Implemented function PositionOpen. Made small refactoring --- MtApi5/MtApi5Client.cs | 52 ++-- MtApi5/MtCommandType.cs | 5 +- TestClients/MtApi5TestClient/MainWindow.xaml | 8 +- TestClients/MtApi5TestClient/ViewModel.cs | 249 +++++++++---------- mq5/MtApi5.ex5 | Bin 100926 -> 106452 bytes mq5/MtApi5.mq5 | 43 +++- 6 files changed, 206 insertions(+), 151 deletions(-) mode change 100644 => 100755 TestClients/MtApi5TestClient/MainWindow.xaml mode change 100644 => 100755 TestClients/MtApi5TestClient/ViewModel.cs diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs index 698a990d..7613900b 100755 --- a/MtApi5/MtApi5Client.cs +++ b/MtApi5/MtApi5Client.cs @@ -30,11 +30,11 @@ namespace MtApi5 { ConnectionState = Mt5ConnectionState.Disconnected; - mClient.QuoteAdded += new MtClient.MtQuoteHandler(mClient_QuoteAdded); - mClient.QuoteRemoved += new MtClient.MtQuoteHandler(mClient_QuoteRemoved); - mClient.QuoteUpdated += new MtClient.MtQuoteHandler(mClient_QuoteUpdated); - mClient.ServerDisconnected += new EventHandler(mClient_ServerDisconnected); - mClient.ServerFailed += new EventHandler(mClient_ServerFailed); + _client.QuoteAdded += mClient_QuoteAdded; + _client.QuoteRemoved += mClient_QuoteRemoved; + _client.QuoteUpdated += mClient_QuoteUpdated; + _client.ServerDisconnected += mClient_ServerDisconnected; + _client.ServerFailed += mClient_ServerFailed; } /// @@ -79,7 +79,7 @@ namespace MtApi5 /// public IEnumerable GetQuotes() { - var quotes = mClient.GetQuotes(); + var quotes = _client.GetQuotes(); return quotes != null ? (from q in quotes select q.Parse()) : null; } @@ -470,7 +470,6 @@ namespace MtApi5 /// ///Close all open positions. /// - ///OrderCloseAll public bool OrderCloseAll() { return sendCommand(Mt5CommandType.OrderCloseAll, null); @@ -486,6 +485,24 @@ namespace MtApi5 return sendCommand(Mt5CommandType.PositionClose, commandParameters); } + + /// + /// Opens a position with the specified parameters. + /// + /// symbol + /// order type to open position + /// position volume + /// execution price + /// Stop Loss price + /// Take Profit price + /// comment + /// 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 = "") + { + var commandParameters = new ArrayList { symbol, (int) orderType, volume, price, sl, tp, comment }; + + return sendCommand(Mt5CommandType.PositionOpen, commandParameters); + } #endregion #region Account Information functions @@ -1384,8 +1401,8 @@ namespace MtApi5 try { - mClient.Open(host, port); - mClient.Connect(); + _client.Open(host, port); + _client.Connect(); } catch (Exception e) { @@ -1408,8 +1425,8 @@ namespace MtApi5 try { - mClient.Open(port); - mClient.Connect(); + _client.Open(port); + _client.Connect(); } catch (Exception e) { @@ -1427,8 +1444,8 @@ namespace MtApi5 private void Disconnect() { - mClient.Disconnect(); - mClient.Close(); + _client.Disconnect(); + _client.Close(); ConnectionState = Mt5ConnectionState.Disconnected; ConnectionStateChanged.FireEvent(this, new Mt5ConnectionEventArgs(Mt5ConnectionState.Disconnected, "Disconnected")); @@ -1436,7 +1453,7 @@ namespace MtApi5 private T sendCommand(Mt5CommandType commandType, ArrayList commandParameters) { - var response = mClient.SendCommand((int)commandType, commandParameters); + var response = _client.SendCommand((int)commandType, commandParameters); if (response is MtResponseDouble) return (T)Convert.ChangeType(((MtResponseDouble)response).Value, typeof(T)); @@ -1484,10 +1501,7 @@ namespace MtApi5 { if (quote != null) { - if (QuoteUpdated != null) - { - QuoteUpdated(this, quote.Instrument, quote.Bid, quote.Ask); - } + QuoteUpdated?.Invoke(this, quote.Instrument, quote.Bid, quote.Ask); } } @@ -1518,7 +1532,7 @@ namespace MtApi5 #endregion #region Private Fields - private readonly MtClient mClient = new MtClient(); + private readonly MtClient _client = new MtClient(); #endregion } } diff --git a/MtApi5/MtCommandType.cs b/MtApi5/MtCommandType.cs index f9c6a41c..754daa9f 100755 --- a/MtApi5/MtCommandType.cs +++ b/MtApi5/MtCommandType.cs @@ -101,6 +101,9 @@ namespace MtApi5 MarketBookRelease = 61, MarketBookGet = 62, OrderCloseAll = 63, - PositionClose = 64 + + //CTrade + PositionClose = 64, + PositionOpen = 65 } } diff --git a/TestClients/MtApi5TestClient/MainWindow.xaml b/TestClients/MtApi5TestClient/MainWindow.xaml old mode 100644 new mode 100755 index 34cec869..2fc0c85c --- a/TestClients/MtApi5TestClient/MainWindow.xaml +++ b/TestClients/MtApi5TestClient/MainWindow.xaml @@ -355,7 +355,7 @@ - +