private void Engine() { int MaxTry = 5; int tryIndex = 0; while (ThreadStart) { lock (syncLock) { if (!ThreadStart) { //ThreadMutex.ReleaseMutex(); break; } //ThreadStop = false; try { //var time = m_BinanceApi.GetServerTime(); var account = m_BinanceApi.GetAccountInfo(); if (account == null) { break; } bool found = false; for (int i = 0; i < account.Balances.Count(); i++) { Balance balance = account.Balances.ElementAt(i); if (balance.Free == 0.0m && balance.Locked == 0.0m) { continue; } found = false; foreach (DataGridViewRow row in this.BALANCE_GRID.Rows) { if (row.Cells[0].Value == null) { continue; } if (row.Cells[0].Value.ToString() == balance.Asset) { row.Cells[1].Value = balance.Free.ToString(); row.Cells[2].Value = balance.Locked.ToString(); found = true; break; } } if (found) { continue; } string[] newBalance = { balance.Asset, balance.Free.ToString(), balance.Locked.ToString() }; if (BALANCE_GRID.InvokeRequired) { BALANCE_GRID.Invoke(new Action(() => this.BALANCE_GRID.Rows.Add(newBalance))); } else { this.BALANCE_GRID.Rows.Add(newBalance); } } foreach (HmaStrategy strg in m_HmaStrategies) { try { var price = m_BinanceApi.GetCurrentPrice(strg.GetSymbol()); if (price == 0) { continue; } var Trades = m_BinanceApi.GetTradesList(strg.GetSymbol()); found = false; for (int i = Trades.Count() - 1; i >= 0; i--) { var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); //var date = new DateTime(2019, 3, 5); DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime tradTime = start.AddMilliseconds(Trades.ElementAt(i).Time).ToLocalTime(); if (tradTime < date) { break; } found = false; foreach (DataGridViewRow row in this.HISTORY_ORDERS_GRID.Rows) { if (row.Cells[0].Value == null) { continue; } if (row.Cells[0].Value.ToString().ToInt() != Trades.ElementAt(i).Id) { continue; } found = true; break; } if (found) { continue; } var hisOrder = Trades.ElementAt(i); string[] newHisOrder = { hisOrder.Id.ToString(), hisOrder.CommissionAsset, tradTime.ToString(), hisOrder.Quantity.ToString("N4"), hisOrder.Price.ToString("N8"), hisOrder.Commission.ToString("N4") }; if (HISTORY_ORDERS_GRID.InvokeRequired) { HISTORY_ORDERS_GRID.Invoke(new Action(() => HISTORY_ORDERS_GRID.Rows.Add(newHisOrder))); } else { HISTORY_ORDERS_GRID.Rows.Add(newHisOrder); } } found = false; HMA_RESULT hma = strg.Engine(m_BinanceApi); foreach (DataGridViewRow row in this.QUOTES_GRID.Rows) { if (row.Cells[0].Value == null) { continue; } if (row.Cells[0].Value.ToString() != strg.GetSymbol()) { continue; } if (hma.Trend == ENUM_TREND.UP_TREND) { row.DefaultCellStyle.BackColor = Color.Green; } else if (hma.Trend == ENUM_TREND.DOWN_TREND) { row.DefaultCellStyle.BackColor = Color.Red; } else { row.DefaultCellStyle.BackColor = Color.Yellow; } row.Cells[1].Value = price.ToString(); row.Cells[2].Value = hma.Value.ToString("N8"); found = true; } if (found) { continue; } string[] newRow = { strg.GetSymbol(), price.ToString(), hma.Value.ToString("N8") }; if (QUOTES_GRID.InvokeRequired) { QUOTES_GRID.Invoke(new Action(() => QUOTES_GRID.Rows.Add(newRow))); } else { QUOTES_GRID.Rows.Add(newRow); } } catch (Exception e) { throw e; } } } catch (Exception e) { string message; if (e.InnerException == null) { message = e.Message; } else { message = e.InnerException.Message; } string caption = "Exception Error"; MessageBoxEx(message, caption); Thread.Sleep(1000); if (tryIndex > MaxTry) { m_HmaStrategies.Clear(); Symbols = null; nSymbolsCount = 0; if (START_BUTTON.InvokeRequired) { this.START_BUTTON.Invoke(new Action(() => this.START_BUTTON.BackColor = Color.FromArgb(200, 200, 200, 200))); } else { this.START_BUTTON.BackColor = Color.FromArgb(200, 200, 200, 200); } if (START_BUTTON.InvokeRequired) { this.START_BUTTON.Invoke(new Action(() => this.START_BUTTON.Text = "START")); } else { this.START_BUTTON.Text = "START"; } ThreadStart = false; //ThreadMutex.ReleaseMutex(); break; } tryIndex++; } Thread.Sleep(1); Thread.Sleep(ThreadStep); } //ThreadMutex.ReleaseMutex(); } //ThreadStop = true; StartButtonStopView(); }