diff --git a/TestClients/TestApiClientUI/Form1.cs b/TestClients/TestApiClientUI/Form1.cs index a4ba93e9..a7d1f4fd 100644 --- a/TestClients/TestApiClientUI/Form1.cs +++ b/TestClients/TestApiClientUI/Form1.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Globalization; using System.Windows.Forms; using MtApi; using System.Threading.Tasks; @@ -11,11 +12,17 @@ namespace TestApiClientUI { public partial class Form1 : Form { + #region Fields + private readonly List _groupOrderCommands = new List(); private readonly MtApiClient _apiClient = new MtApiClient(); private readonly TimerTradeMonitor _timerTradeMonitor; private readonly TimeframeTradeMonitor _timeframeTradeMonitor; + private volatile bool _isUiQuoteUpdateReady = true; + + #endregion + public Form1() { InitializeComponent(); @@ -28,53 +35,52 @@ public Form1() _apiClient.ConnectionStateChanged += apiClient_ConnectionStateChanged; _apiClient.OnLastTimeBar += _apiClient_OnLastTimeBar; - initOrderCommandsGroup(); + InitOrderCommandsGroup(); comboBox1.SelectedIndex = 0; comboBox2.SelectedIndex = 0; comboBox4.SelectedIndex = 0; - _timerTradeMonitor = new TimerTradeMonitor(_apiClient); - _timerTradeMonitor.Interval = 10000; // 10 sec + _timerTradeMonitor = new TimerTradeMonitor(_apiClient) { Interval = 10000 }; // 10 sec _timerTradeMonitor.AvailabilityOrdersChanged += _tradeMonitor_AvailabilityOrdersChanged; _timeframeTradeMonitor = new TimeframeTradeMonitor(_apiClient); _timeframeTradeMonitor.AvailabilityOrdersChanged += _tradeMonitor_AvailabilityOrdersChanged; } - private void initOrderCommandsGroup() - { - _groupOrderCommands.Add(closeOrders); - _groupOrderCommands.Add(closeOrdersBy); - _groupOrderCommands.Add(orderClosePrice); - _groupOrderCommands.Add(orderCloseTime); - _groupOrderCommands.Add(orderComment); - _groupOrderCommands.Add(orderCommission); - _groupOrderCommands.Add(orderDelete); - _groupOrderCommands.Add(orderExpiration); - _groupOrderCommands.Add(orderLots); - _groupOrderCommands.Add(orderMagicNumber); - _groupOrderCommands.Add(orderModify); - _groupOrderCommands.Add(orderOpenPrice); - _groupOrderCommands.Add(orderOpenTime); - _groupOrderCommands.Add(orderPrint); - _groupOrderCommands.Add(orderProfit); - _groupOrderCommands.Add(orderSelect); - _groupOrderCommands.Add(ordersHistoryTotal); - _groupOrderCommands.Add(orderStopLoss); - _groupOrderCommands.Add(ordersTotal); - _groupOrderCommands.Add(orderSwap); - _groupOrderCommands.Add(orderSymbol); - _groupOrderCommands.Add(orderTakeProfit); - _groupOrderCommands.Add(orderTicket); - _groupOrderCommands.Add(orderType); + private void InitOrderCommandsGroup() + { + _groupOrderCommands.Add(CloseOrders); + _groupOrderCommands.Add(CloseOrdersBy); + _groupOrderCommands.Add(OrderClosePrice); + _groupOrderCommands.Add(OrderCloseTime); + _groupOrderCommands.Add(OrderComment); + _groupOrderCommands.Add(OrderCommission); + _groupOrderCommands.Add(OrderDelete); + _groupOrderCommands.Add(OrderExpiration); + _groupOrderCommands.Add(OrderLots); + _groupOrderCommands.Add(OrderMagicNumber); + _groupOrderCommands.Add(OrderModify); + _groupOrderCommands.Add(OrderOpenPrice); + _groupOrderCommands.Add(OrderOpenTime); + _groupOrderCommands.Add(OrderPrint); + _groupOrderCommands.Add(OrderProfit); + _groupOrderCommands.Add(OrderSelect); + _groupOrderCommands.Add(OrdersHistoryTotal); + _groupOrderCommands.Add(OrderStopLoss); + _groupOrderCommands.Add(OrdersTotal); + _groupOrderCommands.Add(OrderSwap); + _groupOrderCommands.Add(OrderSymbol); + _groupOrderCommands.Add(OrderTakeProfit); + _groupOrderCommands.Add(OrderTicket); + _groupOrderCommands.Add(OrderType); } private void RunOnUiThread(Action action) { if (!IsDisposed) { - this.BeginInvoke(action); + BeginInvoke(action); } } @@ -109,19 +115,17 @@ private void apiClient_QuoteAdded(object sender, MtQuoteEventArgs e) private void _apiClient_OnLastTimeBar(object sender, TimeBarArgs e) { - string msg = string.Format("TimeBar: Symbol = {0}, OpenTime = {1}, CloseTime = {2}, Open = {3}, Close = {4}, High = {5}, Low = {6}", - e.TimeBar.Symbol, e.TimeBar.OpenTime, e.TimeBar.CloseTime, e.TimeBar.Open, e.TimeBar.Close, e.TimeBar.High, e.TimeBar.Low); + var msg = + $"TimeBar: Symbol = {e.TimeBar.Symbol}, OpenTime = {e.TimeBar.OpenTime}, CloseTime = {e.TimeBar.CloseTime}, Open = {e.TimeBar.Open}, Close = {e.TimeBar.Close}, High = {e.TimeBar.High}, Low = {e.TimeBar.Low}"; Console.WriteLine(msg); AddToLog(msg); } - private volatile bool IsUiQuoteUpdateReady = true; - private void apiClient_QuoteUpdated(object sender, string symbol, double bid, double ask) { - Console.WriteLine("Quote: Symbol = {0}, Bid = {1}, Ask = {2}", symbol, bid, ask); + Console.WriteLine(@"Quote: Symbol = {0}, Bid = {1}, Ask = {2}", symbol, bid, ask); //if UI of quite is busy we are skipping this update - if (IsUiQuoteUpdateReady) + if (_isUiQuoteUpdateReady) { RunOnUiThread(() => ChangeQuote(symbol, bid, ask)); } @@ -129,21 +133,22 @@ private void apiClient_QuoteUpdated(object sender, string symbol, double bid, do private void AddNewQuote(MtQuote quote) { - if (quote != null - && string.IsNullOrEmpty(quote.Instrument) == false + if (quote == null) + return; + + if (string.IsNullOrEmpty(quote.Instrument) == false && listViewQuotes.Items.ContainsKey(quote.Instrument) == false) { - var item = new ListViewItem(quote.Instrument); - item.Name = quote.Instrument; - item.SubItems.Add(quote.Bid.ToString()); - item.SubItems.Add(quote.Ask.ToString()); + var item = new ListViewItem(quote.Instrument) { Name = quote.Instrument }; + item.SubItems.Add(quote.Bid.ToString(CultureInfo.CurrentCulture)); + item.SubItems.Add(quote.Ask.ToString(CultureInfo.CurrentCulture)); item.SubItems.Add("1"); listViewQuotes.Items.Add(item); } else { var item = listViewQuotes.Items[quote.Instrument]; - int feedCount = 0; + int feedCount; int.TryParse(item.SubItems[3].Text, out feedCount); feedCount++; item.SubItems[3].Text = feedCount.ToString(); @@ -152,12 +157,14 @@ private void AddNewQuote(MtQuote quote) private void RemoveQuote(MtQuote quote) { - if (quote != null - && string.IsNullOrEmpty(quote.Instrument) == false - && listViewQuotes.Items.ContainsKey(quote.Instrument) == true) + if (quote == null) + return; + + if (string.IsNullOrEmpty(quote.Instrument) == false + && listViewQuotes.Items.ContainsKey(quote.Instrument)) { var item = listViewQuotes.Items[quote.Instrument]; - int feedCount = 0; + int feedCount; int.TryParse(item.SubItems[3].Text, out feedCount); feedCount--; if (feedCount <= 0) @@ -173,17 +180,19 @@ private void RemoveQuote(MtQuote quote) private void ChangeQuote(string symbol, double bid, double ask) { - IsUiQuoteUpdateReady = false; + _isUiQuoteUpdateReady = false; + if (string.IsNullOrEmpty(symbol) == false) { - if (listViewQuotes.Items.ContainsKey(symbol) == true) + if (listViewQuotes.Items.ContainsKey(symbol)) { var item = listViewQuotes.Items[symbol]; - item.SubItems[1].Text = bid.ToString(); - item.SubItems[2].Text = ask.ToString(); + item.SubItems[1].Text = bid.ToString(CultureInfo.CurrentCulture); + item.SubItems[2].Text = ask.ToString(CultureInfo.CurrentCulture); } } - IsUiQuoteUpdateReady = true; + + _isUiQuoteUpdateReady = true; } private void OnConnected() @@ -206,7 +215,7 @@ private void OnDisconnected() private void buttonConnect_Click(object sender, EventArgs e) { - string serverName = textBoxServerName.Text; + var serverName = textBoxServerName.Text; int port; int.TryParse(textBoxPort.Text, out port); @@ -243,7 +252,7 @@ private void listViewQuotes_SelectedIndexChanged(object sender, EventArgs e) } } - private void closeOrders() + private void CloseOrders() { if (listBoxSendedOrders.SelectedItems.Count > 0) { @@ -253,21 +262,21 @@ private void closeOrders() double price; double.TryParse(textBoxOrderPrice.Text, out price); - int slippage = (int)numericOrderSlippage.Value; + var slippage = (int)numericOrderSlippage.Value; - List sendedOrders = new List(); + var sendedOrders = new List(); foreach (var item in listBoxSendedOrders.SelectedItems) { - int orderId = (int)item; + var orderId = (int)item; var result = _apiClient.OrderClose(orderId, volume, price, slippage); - if (result == true) + if (result) { sendedOrders.Add(orderId); } - AddToLog(string.Format("Closed order result: {0}, ticketId = {1}, volume - {2}, slippage {3}", result, orderId, volume, slippage)); + AddToLog($"Closed order result: {result}, ticketId = {orderId}, volume - {volume}, slippage {slippage}"); } foreach (var orderId in sendedOrders) @@ -278,14 +287,14 @@ private void closeOrders() } } - private void closeOrdersBy() + private void CloseOrdersBy() { if (listBoxSendedOrders.SelectedItems.Count >= 1) { - int ticket = int.Parse(textBoxIndexTicket.Text); - int opposite = (int)listBoxSendedOrders.SelectedItems[0]; + var ticket = int.Parse(textBoxIndexTicket.Text); + var opposite = (int)listBoxSendedOrders.SelectedItems[0]; - Color color = new Color(); + Color color; switch (comboBoxOrderColor.SelectedIndex) { case 0: @@ -297,75 +306,75 @@ private void closeOrdersBy() case 2: color = Color.Red; break; + default: + return; } var result = _apiClient.OrderCloseBy(ticket, opposite, color); - AddToLog(string.Format("ClosedBy order result: {0}, ticketId = {1}, opposite {2}", result, ticket, opposite)); + AddToLog($"ClosedBy order result: {result}, ticketId = {ticket}, opposite {opposite}"); } } - private void orderClosePrice() + private void OrderClosePrice() { var result = _apiClient.OrderClosePrice(); - textBoxOrderPrice.Text = result.ToString(); - AddToLog(string.Format("OrderClosePrice result: {0}", result)); + textBoxOrderPrice.Text = result.ToString(CultureInfo.CurrentCulture); + AddToLog($"OrderClosePrice result: {result}"); } - private void orderCloseTime() + private void OrderCloseTime() { var result = _apiClient.OrderCloseTime(); - AddToLog(string.Format("OrderCloseTime result: {0}", result)); + AddToLog($"OrderCloseTime result: {result}"); } - private void orderComment() + private void OrderComment() { var result = _apiClient.OrderComment(); - textBoxOrderComment.Text = result.ToString(); - AddToLog(string.Format("OrderComment result: {0}", result)); + textBoxOrderComment.Text = result; + AddToLog($"OrderComment result: {result}"); } - private void orderCommission() + private void OrderCommission() { var result = _apiClient.OrderCommission(); - AddToLog(string.Format("OrderCommission result: {0}", result)); + AddToLog($"OrderCommission result: {result}"); } - private void orderDelete() + private void OrderDelete() { if (listBoxSendedOrders.SelectedItems.Count >= 1) { - int ticket = (int)listBoxSendedOrders.SelectedItems[0]; - + var ticket = (int)listBoxSendedOrders.SelectedItems[0]; var result = _apiClient.OrderDelete(ticket); - - AddToLog(string.Format("Delete order result: {0}, ticketId = {1}", result, ticket)); + AddToLog($"Delete order result: {result}, ticketId = {ticket}"); } } - private void orderExpiration() + private void OrderExpiration() { var result = _apiClient.OrderExpiration(); - AddToLog(string.Format("Expiration order result: {0}", result)); + AddToLog($"Expiration order result: {result}"); } - private void orderLots() + private void OrderLots() { var result = _apiClient.OrderLots(); - AddToLog(string.Format("Lots order result: {0}", result)); + AddToLog($"Lots order result: {result}"); } - private void orderMagicNumber() + private void OrderMagicNumber() { var result = _apiClient.OrderMagicNumber(); - AddToLog(string.Format("MagicNumber order result: {0}", result)); + AddToLog($"MagicNumber order result: {result}"); } - private void orderModify() + private void OrderModify() { if (listBoxSendedOrders.SelectedItems.Count >= 1) { - int ticket = (int)listBoxSendedOrders.SelectedItems[0]; + var ticket = (int)listBoxSendedOrders.SelectedItems[0]; double price; double.TryParse(textBoxOrderPrice.Text, out price); @@ -376,54 +385,56 @@ private void orderModify() double takeprofit; double.TryParse(textBoxOrderProffit.Text, out takeprofit); - DateTime expiration = DateTime.Now; + var expiration = DateTime.Now; - Color arrow_color = new Color(); + Color arrowColor; switch (comboBoxOrderColor.SelectedIndex) { case 0: - arrow_color = Color.Green; + arrowColor = Color.Green; break; case 1: - arrow_color = Color.Blue; + arrowColor = Color.Blue; break; case 2: - arrow_color = Color.Red; + arrowColor = Color.Red; break; + default: + return; } - var result = _apiClient.OrderModify(ticket, price, stoploss, takeprofit, expiration, arrow_color); + var result = _apiClient.OrderModify(ticket, price, stoploss, takeprofit, expiration, arrowColor); - AddToLog(string.Format("OrderModify result: {0}", result)); + AddToLog($"OrderModify result: {result}"); } } - private void orderOpenPrice() + private void OrderOpenPrice() { var result = _apiClient.OrderOpenPrice(); - AddToLog(string.Format("OrderOpenPrice result: {0}", result)); + AddToLog($"OrderOpenPrice result: {result}"); } - private void orderOpenTime() + private void OrderOpenTime() { var result = _apiClient.OrderOpenTime(); - AddToLog(string.Format("OpenTime order result: {0}", result)); + AddToLog($"OpenTime order result: {result}"); } - private void orderPrint() + private void OrderPrint() { _apiClient.OrderPrint(); } - private void orderProfit() + private void OrderProfit() { var result = _apiClient.OrderProfit(); - AddToLog(string.Format("Profit order result: {0}", result)); + AddToLog($"Profit order result: {result}"); } - private void orderSelect() + private void OrderSelect() { - int ticket = -1; + var ticket = -1; if (string.IsNullOrEmpty(textBoxIndexTicket.Text) == false) ticket = int.Parse(textBoxIndexTicket.Text); @@ -436,60 +447,60 @@ private void orderSelect() { var result = _apiClient.OrderSelect(ticket, OrderSelectMode.SELECT_BY_POS); - AddToLog(string.Format("OrderSelect result: {0}", result)); + AddToLog($"OrderSelect result: {result}"); } } - private void ordersHistoryTotal() + private void OrdersHistoryTotal() { var result = _apiClient.OrdersHistoryTotal(); - AddToLog(string.Format("OrdersHistoryTotal result: {0}", result)); + AddToLog($"OrdersHistoryTotal result: {result}"); } - private void orderStopLoss() + private void OrderStopLoss() { var result = _apiClient.OrderStopLoss(); - textBoxOrderStoploss.Text = result.ToString(); - AddToLog(string.Format("OrderStopLoss result: {0}", result)); + textBoxOrderStoploss.Text = result.ToString(CultureInfo.CurrentCulture); + AddToLog($"OrderStopLoss result: {result}"); } - private void ordersTotal() + private void OrdersTotal() { var result = _apiClient.OrdersTotal(); - AddToLog(string.Format("OrdersTotal result: {0}", result)); + AddToLog($"OrdersTotal result: {result}"); } - private void orderSwap() + private void OrderSwap() { var result = _apiClient.OrderSwap(); - AddToLog(string.Format("OrderSwap result: {0}", result)); + AddToLog($"OrderSwap result: {result}"); } - private void orderSymbol() + private void OrderSymbol() { var result = _apiClient.OrderSymbol(); textBoxOrderSymbol.Text = result; - AddToLog(string.Format("OrderSymbol result: {0}", result)); + AddToLog($"OrderSymbol result: {result}"); } - private void orderTakeProfit() + private void OrderTakeProfit() { var result = _apiClient.OrderTakeProfit(); - textBoxOrderProffit.Text = result.ToString(); - AddToLog(string.Format("OrderTakeProfit result: {0}", result)); + textBoxOrderProffit.Text = result.ToString(CultureInfo.CurrentCulture); + AddToLog($"OrderTakeProfit result: {result}"); } - private void orderTicket() + private void OrderTicket() { var result = _apiClient.OrderTicket(); - AddToLog(string.Format("OrderTicket result: {0}", result)); + AddToLog($"OrderTicket result: {result}"); } - private void orderType() + private void OrderType() { var result = _apiClient.OrderType(); comboBoxOrderCommand.SelectedIndex = (int)result; - AddToLog(string.Format("OrderType result: {0}", result)); + AddToLog($"OrderType result: {result}"); } private void button2_Click(object sender, EventArgs e) @@ -515,89 +526,91 @@ private void button3_Click(object sender, EventArgs e) case 0: { var result = _apiClient.GetLastError(); - AddToLog(string.Format("GetLastError result: {0}", result)); + AddToLog($"GetLastError result: {result}"); } break; case 1: { var result = _apiClient.IsConnected(); - AddToLog(string.Format("IsConnected result: {0}", result)); + AddToLog($"IsConnected result: {result}"); } break; case 2: { var result = _apiClient.IsDemo(); - AddToLog(string.Format("IsDemo result: {0}", result)); + AddToLog($"IsDemo result: {result}"); } break; case 3: { var result = _apiClient.IsDllsAllowed(); - AddToLog(string.Format("IsDllsAllowed result: {0}", result)); - } + AddToLog($"IsDllsAllowed result: {result}"); + } break; case 4: { var result = _apiClient.IsExpertEnabled(); - AddToLog(string.Format("IsExpertEnabled result: {0}", result)); - } + AddToLog($"IsExpertEnabled result: {result}"); + } break; case 5: { var result = _apiClient.IsLibrariesAllowed(); - AddToLog(string.Format("IsLibrariesAllowed result: {0}", result)); - } + AddToLog($"IsLibrariesAllowed result: {result}"); + } break; case 6: { var result = _apiClient.IsOptimization(); - AddToLog(string.Format("IsOptimization result: {0}", result)); - } + AddToLog($"IsOptimization result: {result}"); + } break; case 7: { var result = _apiClient.IsStopped(); - AddToLog(string.Format("IsStopped result: {0}", result)); - } + AddToLog($"IsStopped result: {result}"); + } break; case 8: { var result = _apiClient.IsTesting(); - AddToLog(string.Format("IsTesting result: {0}", result)); + AddToLog($"IsTesting result: {result}"); } break; case 9: { var result = _apiClient.IsTradeAllowed(); - AddToLog(string.Format("IsTradeAllowed result: {0}", result)); - } + AddToLog($"IsTradeAllowed result: {result}"); + } break; case 10: { var result = _apiClient.IsTradeContextBusy(); - AddToLog(string.Format("IsTradeContextBusy result: {0}", result)); - } + AddToLog($"IsTradeContextBusy result: {result}"); + } break; case 11: { var result = _apiClient.IsVisualMode(); - AddToLog(string.Format("IsVisualMode result: {0}", result)); - } + AddToLog($"IsVisualMode result: {result}"); + } break; case 12: { var result = _apiClient.UninitializeReason(); - AddToLog(string.Format("UninitializeReason result: {0}", result)); - } + AddToLog($"UninitializeReason result: {result}"); + } break; case 13: { - int errorCode = -1; + int errorCode; int.TryParse(textBoxErrorCode.Text, out errorCode); var result = _apiClient.ErrorDescription(errorCode); - AddToLog(string.Format("ErrorDescription result: {0}", result)); - } + AddToLog($"ErrorDescription result: {result}"); + } break; + default: + return; } } @@ -608,99 +621,101 @@ private void button4_Click(object sender, EventArgs e) case 0: { var result = _apiClient.AccountBalance(); - AddToLog(string.Format("AccountBalance result: {0}", result)); + AddToLog($"AccountBalance result: {result}"); } break; case 1: { var result = _apiClient.AccountCredit(); - AddToLog(string.Format("AccountCredit result: {0}", result)); + AddToLog($"AccountCredit result: {result}"); } break; case 2: { var result = _apiClient.AccountCompany(); - AddToLog(string.Format("AccountCompany result: {0}", result)); + AddToLog($"AccountCompany result: {result}"); } break; case 3: { var result = _apiClient.AccountCurrency(); - AddToLog(string.Format("AccountCurrency result: {0}", result)); + AddToLog($"AccountCurrency result: {result}"); } break; case 4: { var result = _apiClient.AccountEquity(); - AddToLog(string.Format("AccountEquity result: {0}", result)); + AddToLog($"AccountEquity result: {result}"); } break; case 5: { var result = _apiClient.AccountFreeMargin(); - AddToLog(string.Format("AccountFreeMargin result: {0}", result)); + AddToLog($"AccountFreeMargin result: {result}"); } break; case 6: { var result = _apiClient.AccountFreeMarginCheck(textBoxAccountInfoSymbol.Text, (TradeOperation)comboBoxAccountInfoCmd.SelectedIndex, int.Parse(textBoxAccountInfoVolume.Text)); - AddToLog(string.Format("AccountFreeMarginCheck result: {0}", result)); + AddToLog($"AccountFreeMarginCheck result: {result}"); } break; case 7: { var result = _apiClient.AccountFreeMarginMode(); - AddToLog(string.Format("AccountFreeMarginMode result: {0}", result)); + AddToLog($"AccountFreeMarginMode result: {result}"); } break; case 8: { var result = _apiClient.AccountLeverage(); - AddToLog(string.Format("AccountLeverage result: {0}", result)); + AddToLog($"AccountLeverage result: {result}"); } break; case 9: { var result = _apiClient.AccountMargin(); - AddToLog(string.Format("AccountMargin result: {0}", result)); + AddToLog($"AccountMargin result: {result}"); } break; case 10: { var result = _apiClient.AccountName(); - AddToLog(string.Format("AccountName result: {0}", result)); + AddToLog($"AccountName result: {result}"); } break; case 11: { var result = _apiClient.AccountNumber(); - AddToLog(string.Format("AccountNumber result: {0}", result)); + AddToLog($"AccountNumber result: {result}"); } break; case 12: { var result = _apiClient.AccountProfit(); - AddToLog(string.Format("AccountProfit result: {0}", result)); + AddToLog($"AccountProfit result: {result}"); } break; case 13: { var result = _apiClient.AccountServer(); - AddToLog(string.Format("AccountServer result: {0}", result)); + AddToLog($"AccountServer result: {result}"); } break; case 14: { var result = _apiClient.AccountStopoutLevel(); - AddToLog(string.Format("AccountStopoutLevel result: {0}", result)); + AddToLog($"AccountStopoutLevel result: {result}"); } break; case 15: { var result = _apiClient.AccountStopoutMode(); - AddToLog(string.Format("AccountStopoutMode result: {0}", result)); + AddToLog($"AccountStopoutMode result: {result}"); } break; + default: + return; } } @@ -710,52 +725,44 @@ private void button5_Click(object sender, EventArgs e) return; var result = _apiClient.MarketInfo(txtMarketInfoSymbol.Text, (MarketInfoModeType)listBoxMarketInfo.SelectedIndex); - AddToLog(string.Format("MarketInfo result: {0}", result)); + AddToLog($"MarketInfo result: {result}"); } private void button6_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; - int timeframeCount = 1; + var symbol = textBoxSelectedSymbol.Text; + int timeframeCount; int.TryParse(textBoxTimeframesCount.Text, out timeframeCount); - Console.WriteLine("Started time: " + DateTime.Now.ToString()); - - List openPriceList = new List(); - - //for (int i = 0; i < COUNT; i++) - //{ - // var price = _apiClient.iOpen(symbol, ChartPeriod.PERIOD_M1, i); - // openPriceList.Add(price); - //} + Console.WriteLine($"Started time: {DateTime.Now}"); var prices = _apiClient.iCloseArray(symbol, ChartPeriod.PERIOD_M1); - openPriceList = new List(prices); + var openPriceList = new List(prices); listBoxProceHistory.DataSource = openPriceList; - Console.WriteLine("Finished time: " + DateTime.Now.ToString()); + Console.WriteLine($"Finished time: {DateTime.Now}"); - using (System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\test.txt")) + using (var file = new System.IO.StreamWriter($@"{System.IO.Path.GetTempPath()}\MtApi\test.txt")) { foreach (var value in openPriceList) { - file.WriteLine(value.ToString()); + file.WriteLine(value); } } } private void button7_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; var barCount = _apiClient.iBars(symbol, ChartPeriod.PERIOD_M1); textBoxTimeframesCount.Text = barCount.ToString(); } private void button8_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; var prices = _apiClient.iHighArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; @@ -763,7 +770,7 @@ private void button8_Click(object sender, EventArgs e) private void button9_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; var prices = _apiClient.iLowArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; @@ -771,7 +778,7 @@ private void button9_Click(object sender, EventArgs e) private void button10_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; var prices = _apiClient.iOpenArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; @@ -796,19 +803,19 @@ private void button12_Click(object sender, EventArgs e) private void button13_Click(object sender, EventArgs e) { var retVal = _apiClient.TimeCurrent(); - AddToLog(string.Format("TimeCurrent result: {0}", retVal)); + AddToLog($"TimeCurrent result: {retVal}"); } private void button14_Click(object sender, EventArgs e) { var retVal = _apiClient.TimeLocal(); - AddToLog(string.Format("TimeLocal result: {0}", retVal)); + AddToLog($"TimeLocal result: {retVal}"); } private void buttonRefreshRates_Click(object sender, EventArgs e) { var retVal = _apiClient.RefreshRates(); - AddToLog(string.Format("RefreshRates result: {0}", retVal)); + AddToLog($"RefreshRates result: {retVal}"); } private void listBoxEventLog_MouseDoubleClick(object sender, MouseEventArgs e) @@ -843,7 +850,7 @@ private async void button1_Click(object sender, EventArgs e) { var symbol = textBoxOrderSymbol.Text; - var cmd = (TradeOperation)(comboBoxOrderCommand.SelectedIndex); + var cmd = (TradeOperation) comboBoxOrderCommand.SelectedIndex; double volume; double.TryParse(textBoxOrderVolume.Text, out volume); @@ -851,7 +858,7 @@ private async void button1_Click(object sender, EventArgs e) double price; double.TryParse(textBoxOrderPrice.Text, out price); - var slippage = (int)numericOrderSlippage.Value; + var slippage = (int) numericOrderSlippage.Value; double stoploss; double.TryParse(textBoxOrderStoploss.Text, out stoploss); @@ -866,7 +873,7 @@ private async void button1_Click(object sender, EventArgs e) var expiration = DateTime.Now; - var arrowColor = new Color(); + Color arrowColor; switch (comboBoxOrderColor.SelectedIndex) { case 0: @@ -878,11 +885,13 @@ private async void button1_Click(object sender, EventArgs e) case 2: arrowColor = Color.Red; break; + default: + return; } var ticket = await Execute(() => _apiClient.OrderSend(symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrowColor)); - AddToLog(string.Format("Sended order result: ticket = {0}", ticket)); + AddToLog($"Sended order result: ticket = {ticket}"); } //GetOrder @@ -897,10 +906,8 @@ private async void button16_Click(object sender, EventArgs e) if (order != null) { var result = - string.Format( - "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}, Swap = {12}, Expiration = {13}, TakeProfit = {14}, StopLoss = {15}", - order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime, order.Swap, order.Expiration, order.TakeProfit, order.StopLoss); - AddToLog(result); + $"Order: Ticket = {order.Ticket}, Symbol = {order.Symbol}, Operation = {order.Operation}, OpenPrice = {order.OpenPrice}, ClosePrice = {order.ClosePrice}, Lots = {order.Lots}, Profit = {order.Profit}, Comment = {order.Comment}, Commission = {order.Commission}, MagicNumber = {order.MagicNumber}, OpenTime = {order.OpenTime}, CloseTime = {order.CloseTime}, Swap = {order.Swap}, Expiration = {order.Expiration}, TakeProfit = {order.TakeProfit}, StopLoss = {order.StopLoss}"; + AddToLog(result); } } @@ -910,16 +917,14 @@ private async void button17_Click(object sender, EventArgs e) var selectSource = (OrderSelectSource)comboBox2.SelectedIndex; - var orders = await Execute(() => _apiClient.GetOrders(selectSource)); ; + var orders = await Execute(() => _apiClient.GetOrders(selectSource)); - if (orders != null && orders.Count() > 0) + if (orders != null && orders.Any()) { foreach (var order in orders) { var result = - string.Format( - "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}, Swap = {12}, Expiration = {13}, TakeProfit = {14}, StopLoss = {15}", - order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime, order.Swap, order.Expiration, order.TakeProfit, order.StopLoss); + $"Order: Ticket = {order.Ticket}, Symbol = {order.Symbol}, Operation = {order.Operation}, OpenPrice = {order.OpenPrice}, ClosePrice = {order.ClosePrice}, Lots = {order.Lots}, Profit = {order.Profit}, Comment = {order.Comment}, Commission = {order.Commission}, MagicNumber = {order.MagicNumber}, OpenTime = {order.OpenTime}, CloseTime = {order.CloseTime}, Swap = {order.Swap}, Expiration = {order.Expiration}, TakeProfit = {order.TakeProfit}, StopLoss = {order.StopLoss}"; AddToLog(result); } } @@ -941,8 +946,8 @@ private async void button18_Click(object sender, EventArgs e) var ticket = await Execute(() => _apiClient.OrderSendBuy(symbol, volume, slippage)); - AddToLog(string.Format("Sended order result: ticketId = {0}, symbol = {1}, volume = {2}, slippage = {3}", - ticket, symbol, volume, slippage)); + AddToLog( + $"Sended order result: ticketId = {ticket}, symbol = {symbol}, volume = {volume}, slippage = {slippage}"); } //OrderSendSell @@ -957,8 +962,7 @@ private async void button19_Click(object sender, EventArgs e) var ticket = await Execute(() => _apiClient.OrderSendSell(symbol, volume, slippage)); - AddToLog(string.Format("Sended order result: ticketId = {0}, symbol = {1}, volume = {2}, slippage = {3}", - ticket, symbol, volume, slippage)); + AddToLog($"Sended order result: ticketId = {ticket}, symbol = {symbol}, volume = {volume}, slippage = {slippage}"); } //OrderClose @@ -967,7 +971,7 @@ private async void button20_Click(object sender, EventArgs e) var ticket = int.Parse(textBoxIndexTicket.Text); var slippage = (int)numericOrderSlippage.Value; - var closed = false; + bool closed; if (checkBox1.Checked) { @@ -980,10 +984,10 @@ private async void button20_Click(object sender, EventArgs e) double price; double.TryParse(textBoxOrderPrice.Text, out price); - closed = await Execute(() => _apiClient.OrderClose(ticket, volume, price, slippage)); + closed = await Execute(() => _apiClient.OrderClose(ticket, volume, price, slippage)); } - AddToLog(string.Format("Close order result: {0}, ticket = {1}", closed, ticket)); + AddToLog($"Close order result: {closed}, ticket = {ticket}"); } //OrderCloseBy @@ -992,9 +996,9 @@ private async void button15_Click(object sender, EventArgs e) var ticket = int.Parse(textBoxIndexTicket.Text); var opposite = int.Parse(textBoxOppositeTicket.Text); - var closed = await Execute(() => _apiClient.OrderCloseBy(ticket, opposite)); ; + var closed = await Execute(() => _apiClient.OrderCloseBy(ticket, opposite)); - AddToLog(string.Format("Close order result: {0}, ticket = {1}", closed, ticket)); + AddToLog($"Close order result: {closed}, ticket = {ticket}"); } //OrderDelete @@ -1004,7 +1008,7 @@ private async void button21_Click(object sender, EventArgs e) var deleted = await Execute(() => _apiClient.OrderDelete(ticket)); - AddToLog(string.Format("Delete order result: {0}, ticket = {1}", deleted, ticket)); + AddToLog($"Delete order result: {deleted}, ticket = {ticket}"); } //OrderModify @@ -1023,7 +1027,7 @@ private async void button22_Click(object sender, EventArgs e) var expiration = DateTime.MinValue; - Color color = new Color(); + Color color; switch (comboBoxOrderColor.SelectedIndex) { case 0: @@ -1035,48 +1039,52 @@ private async void button22_Click(object sender, EventArgs e) case 2: color = Color.Red; break; + default: + return; } var modified = await Execute(() => _apiClient.OrderModify(ticket, price, stoploss, takeprofit, expiration, color)); - AddToLog(string.Format("Modify order result: {0}, ticket = {1}", modified, ticket)); + AddToLog($"Modify order result: {modified}, ticket = {ticket}"); } //iCustom (ZigZag) private async void iCustomBtn_Click(object sender, EventArgs e) { - string symbol = "EURUSD"; - var timeframe = ChartPeriod.PERIOD_H1; - string name = "ZigZag"; + const string symbol = "EURUSD"; + const ChartPeriod timeframe = ChartPeriod.PERIOD_H1; + const string name = "ZigZag"; int[] parameters = { 12, 5, 4 }; - int mode = 0; - int shift = 0; + const int mode = 0; + const int shift = 0; + var retVal = await Execute(() => _apiClient.iCustom(symbol, (int)timeframe, name, parameters, mode, shift)); - AddToLog(string.Format("ICustom result: {0}", retVal)); + AddToLog($"ICustom result: {retVal}"); } //iCustom (Parabolic) private async void button23_Click(object sender, EventArgs e) { - string symbol = "EURUSD"; - var timeframe = ChartPeriod.PERIOD_H1; - string name = "Parabolic"; + const string symbol = "EURUSD"; + const ChartPeriod timeframe = ChartPeriod.PERIOD_H1; + const string name = "Parabolic"; double[] parameters = { 0.02, 0.2 }; - int mode = 0; - int shift = 1; + const int mode = 0; + const int shift = 1; + var retVal = await Execute(() => _apiClient.iCustom(symbol, (int)timeframe, name, parameters, mode, shift)); - AddToLog(string.Format("ICustom result: {0}", retVal)); + AddToLog($"ICustom result: {retVal}"); } //CopyRates private async void button24_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; ENUM_TIMEFRAMES timeframes; - Enum.TryParse(comboBox3.SelectedValue.ToString(), out timeframes); + Enum.TryParse(comboBox3.SelectedValue.ToString(), out timeframes); - int startPos = Convert.ToInt32(numericUpDown1.Value); - int count = Convert.ToInt32(numericUpDown2.Value); + var startPos = Convert.ToInt32(numericUpDown1.Value); + var count = Convert.ToInt32(numericUpDown2.Value); var rates = await Execute(() => _apiClient.CopyRates(symbol, timeframes, startPos, count)); @@ -1084,8 +1092,8 @@ private async void button24_Click(object sender, EventArgs e) { foreach (var r in rates) { - var result = string.Format("Rate: Time = {0}, Open = {1}, High = {2}, Low = {3}, Close = {4}, TickVolume = {5}, Spread = {6}, RealVolume = {7}", - r.Time, r.Open, r.High, r.Low, r.Close, r.TickVolume, r.Spread, r.RealVolume); + var result = + $"Rate: Time = {r.Time}, Open = {r.Open}, High = {r.High}, Low = {r.Low}, Close = {r.Close}, TickVolume = {r.TickVolume}, Spread = {r.Spread}, RealVolume = {r.RealVolume}"; AddToLog(result); } } @@ -1098,12 +1106,12 @@ private async void button24_Click(object sender, EventArgs e) //CopyRates private async void button25_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; ENUM_TIMEFRAMES timeframes; - Enum.TryParse(comboBox3.SelectedValue.ToString(), out timeframes); + Enum.TryParse(comboBox3.SelectedValue.ToString(), out timeframes); - DateTime startTime = dateTimePicker1.Value; - int count = Convert.ToInt32(numericUpDown2.Value); + var startTime = dateTimePicker1.Value; + var count = Convert.ToInt32(numericUpDown2.Value); var rates = await Execute(() => _apiClient.CopyRates(symbol, timeframes, startTime, count)); @@ -1111,8 +1119,8 @@ private async void button25_Click(object sender, EventArgs e) { foreach (var r in rates) { - var result = string.Format("Rate: Time = {0}, Open = {1}, High = {2}, Low = {3}, Close = {4}, TickVolume = {5}, Spread = {6}, RealVolume = {7}", - r.Time, r.Open, r.High, r.Low, r.Close, r.TickVolume, r.Spread, r.RealVolume); + var result = + $"Rate: Time = {r.Time}, Open = {r.Open}, High = {r.High}, Low = {r.Low}, Close = {r.Close}, TickVolume = {r.TickVolume}, Spread = {r.Spread}, RealVolume = {r.RealVolume}"; AddToLog(result); } } @@ -1125,12 +1133,12 @@ private async void button25_Click(object sender, EventArgs e) //CopyRates private async void button26_Click(object sender, EventArgs e) { - string symbol = textBoxSelectedSymbol.Text; + var symbol = textBoxSelectedSymbol.Text; ENUM_TIMEFRAMES timeframes; Enum.TryParse(comboBox3.SelectedValue.ToString(), out timeframes); - DateTime startTime = dateTimePicker1.Value; - DateTime stopTime = dateTimePicker1.Value; + var startTime = dateTimePicker1.Value; + var stopTime = dateTimePicker1.Value; var rates = await Execute(() => _apiClient.CopyRates(symbol, timeframes, startTime, stopTime)); @@ -1138,8 +1146,8 @@ private async void button26_Click(object sender, EventArgs e) { foreach (var r in rates) { - var result = string.Format("Rate: Time = {0}, Open = {1}, High = {2}, Low = {3}, Close = {4}, TickVolume = {5}, Spread = {6}, RealVolume = {7}", - r.Time, r.Open, r.High, r.Low, r.Close, r.TickVolume, r.Spread, r.RealVolume); + var result = + $"Rate: Time = {r.Time}, Open = {r.Open}, High = {r.High}, Low = {r.Low}, Close = {r.Close}, TickVolume = {r.TickVolume}, Spread = {r.Spread}, RealVolume = {r.RealVolume}"; AddToLog(result); } } @@ -1156,25 +1164,26 @@ private void button27_Click(object sender, EventArgs e) if (!string.IsNullOrEmpty(msg)) { _apiClient.Print(msg); - AddToLog(string.Format("Print executed")); + AddToLog("Print executed"); } } //SymbolsTotal private async void button28_Click(object sender, EventArgs e) { - var result_true = await Execute(() => _apiClient.SymbolsTotal(true)); - AddToLog($"SymbolsTotal [true]: result = {result_true}"); + var resultTrue = await Execute(() => _apiClient.SymbolsTotal(true)); + AddToLog($"SymbolsTotal [true]: result = {resultTrue}"); - var result_false = await Execute(() => _apiClient.SymbolsTotal(false)); - AddToLog($"SymbolsTotal [false]: result = {result_false}"); + var resultFalse = await Execute(() => _apiClient.SymbolsTotal(false)); + AddToLog($"SymbolsTotal [false]: result = {resultFalse}"); } //SymbolName private async void button29_Click(object sender, EventArgs e) { - int pos = 1; - bool selected = false; + const int pos = 1; + const bool selected = false; + var result = await Execute(() => _apiClient.SymbolName(pos, selected)); AddToLog($"SymbolName [true]: result = {result}"); } @@ -1183,7 +1192,8 @@ private async void button29_Click(object sender, EventArgs e) private async void button30_Click(object sender, EventArgs e) { var symbol = textBox1.Text; - bool select = true; + const bool @select = true; + var result = await Execute(() => _apiClient.SymbolSelect(symbol, select)); AddToLog($"SymbolSelect [true]: result = {result}"); }