private void DisplayResult(LoginResult loginResult) { if (!loginResult.IsError) { //MessageBox.Show(loginResult.Error.ToString()); MainWindowScreen mws = new MainWindowScreen { LoginForm = this }; mws.Show(); } else if (loginResult.Error.ToString().Equals("unauthorized")) { MessageBox.Show(loginResult.Error.ToString()); Hide(); Login(); } else if (loginResult.IsError) { MessageBox.Show(loginResult.Error.ToString()); Close(); } }
/// <summary> /// This method performs trading data analysis and trades execution /// </summary> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public async Task TradeExecute(DateTime start, DateTime end) { MainWindowScreen mw = new MainWindowScreen(); MessageBox.Show("Click OK to start trades simulation run..."); /// SQL query below takes only active symbols in static table and only those symbols containing factdata entries query = @"SELECT distinct st.symbol_short FROM algotrade.static st, algotrade.factdata fd where st.status = 'A' and fd.symbol = st.symbol_short; "; DbCallSymbols(query); if (mySymbols.Count >= 1 && mySymbols.Count <= Int32.MaxValue) // This code is to check Integer overflow { foreach (string SelectedSymbol in mySymbols) { Symbol = SelectedSymbol; CleanData(Symbol); QueryDates = $"select date from algotrade.factdata where date between '{start:yyyy-MM-dd}' and '{end:yyyy-MM-dd}'"; try { MySqlCommand comm = new MySqlCommand(QueryDates, connection); connection.Open(); MySqlDataReader reader = comm.ExecuteReader(); while (reader.Read()) { myDates.Insert(0, reader.GetValue(0)); } } catch (Exception e) { MessageBox.Show(e.ToString()); throw; } finally { connection.Close(); } // Define nearest start and end dates string query_sdate = $"SELECT date FROM algotrade.factdata WHERE date >= '{start:yyyy-MM-dd}' and date <= NOW() and Symbol = '" + Symbol + "' ORDER BY date LIMIT 1;"; string query_ed = "SELECT date FROM algotrade.factdata WHERE date >= (select max(date) from algotrade.factdata where Symbol = '" + Symbol + "') and date <= NOW() and Symbol = '" + Symbol + "' ORDER BY date LIMIT 1;"; var startdate = Convert.ToDateTime(DbCallDate(query_sdate)); var enddate = Convert.ToDateTime(DbCallDate(query_ed)); foreach (DateTime day in EachCalendarDay(startdate, enddate)) { if (myDates.Contains(day)) { Trade_date = String.Format("{0:yyyy-MM-dd}", day); query1 = @"SELECT date, symbol, lastPrice, DMA_50, DMA_200, ACT FROM algotrade.factdata where symbol = '" + Symbol + "' and date = '" + String.Format("{0:yyyy-MM-dd}", day) + "';"; DbCall(query1); if (ACT.Equals("Buy")) { try { buy_update = "call _sp_BuyUpdate('" + String.Format("{0:yyyy-MM-dd}", day) + "', '" + Symbol + "', '" + Math.Round(Price, 2) + "')"; mw.DatabaseCalls(buy_update); Pos_and_cash_calc(); string PosAndCashUpdate = "call _sp_BuyPosAndCashUpdate('" + String.Format("{0:yyyy-MM-dd}", day) + "', '" + Symbol + "', '" + Math.Round(Price, 2) + "', '" + Math.Round(Open_pos, 2) + "', '" + Math.Round(Day_trade, 2) + "', '" + Math.Round(Close_pos, 2) + "', '" + Math.Round(Open_cash, 2) + "', '" + Math.Round(Day_cash, 2) + "', '" + Math.Round(Close_cash, 2) + "')"; mw.DatabaseCalls(PosAndCashUpdate); } catch (NullReferenceException e) { MessageBox.Show(e.ToString()); throw; } } else if (ACT.Equals("Sell")) { try { Pos_and_cash_calc(); if (Open_pos >= 1) { sell_update = "call _sp_SellUpdate('" + String.Format("{0:yyyy-MM-dd}", day) + "', '" + Symbol + "', '" + Math.Round(Price, 2) + "')"; mw.DatabaseCalls(sell_update); Pos_and_cash_calc(); string PosAndCashUpdate = "call _sp_SellPosAndCashUpdate('" + String.Format("{0:yyyy-MM-dd}", day) + "', '" + Symbol + "', '" + Math.Round(Price, 2) + "', '" + Math.Round(Open_pos, 2) + "', '" + Math.Round(Day_trade, 2) + "', '" + Math.Round(Close_pos, 2) + "', '" + Math.Round(Open_cash, 2) + "', '" + Math.Round(Day_cash, 2) + "', '" + Math.Round(Close_cash, 2) + "')"; mw.DatabaseCalls(PosAndCashUpdate); } } catch (NullReferenceException e) { MessageBox.Show(e.ToString()); throw; } } } } } } MessageBox.Show("Trade analysis completed!!!"); await Task.CompletedTask; } // This is IEnumerable generating dates in range