Пример #1
0
        /// <summary>
        /// Downloads and parses the list of limit orders from SQL table
        /// </summary>
        /// <returns></returns>
        public List <LimitOrder> LoadLimitOrdersFromDatabase()
        {
            var limitOrders    = new List <LimitOrder>();
            var storageString  = ConfigurationManager.AppSettings["StorageConnectionString"];
            var downloadString = @"SELECT * FROM dbo.MyLimitOrders;";
            var limitDBResults = new List <LimitOrderDbResult>();

            using (var connection = new SqlConnection(storageString))
            {
                using (var command = new SqlCommand(downloadString, connection))
                {
                    connection.Open();
                    var reader = command.ExecuteReader();

                    if (reader != null && reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            var tradeType     = reader.GetString(1);
                            var ticker        = reader.GetString(2);
                            var shares        = reader.GetInt32(3);
                            var limit         = reader.GetDecimal(4);
                            var securityType  = reader.GetString(5);
                            var orderDuration = reader.GetString(6);

                            var newResult = new LimitOrderDbResult(tradeType, ticker, shares, limit, securityType, orderDuration);
                            limitDBResults.Add(newResult);
                        }
                    }
                }
            }

            foreach (var result in limitDBResults)
            {
                var newSecurity = new Security();

                if (result.SecurityType == null)
                {
                    continue;
                }

                if (result.SecurityType == "Stock")
                {
                    newSecurity = new Stock("", result.Ticker, "", 0, 0);
                }
                else if (result.SecurityType == "Mutual Fund")
                {
                    newSecurity = new MutualFund("", result.Ticker, "", 0, 0);
                }

                var newTrade      = new Trade(result.TradeType, newSecurity, result.Ticker, result.Shares, "Limit", result.Limit, result.OrderDuration);
                var newLimitOrder = new LimitOrder(newTrade);
                limitOrders.Add(newLimitOrder);
            }

            return(limitOrders);
        }
        /// <summary>
        /// Downloads and parses the list of limit orders from SQL table
        /// </summary>
        /// <returns></returns>
        public async Task LoadLimitOrdersFromDatabase()
        {
            var downloadString = @"SELECT * FROM dbo.MyLimitOrders;";
            var limitDBResults = new List <LimitOrderDbResult>();

            using (var connection = new SqlConnection(_storageString))
            {
                connection.Open();
                using (var command = new SqlCommand(downloadString, connection))
                {
                    var reader = await command.ExecuteReaderAsync();

                    if (reader != null && reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            var tradeType     = reader.GetString(1);
                            var ticker        = reader.GetString(2);
                            var shares        = reader.GetInt32(3);
                            var limit         = reader.GetDecimal(4);
                            var securityType  = reader.GetString(5);
                            var orderDuration = reader.GetString(6);

                            var newResult = new LimitOrderDbResult(tradeType, ticker, shares, limit, securityType, orderDuration);
                            limitDBResults.Add(newResult);
                        }
                    }
                }
            }

            foreach (var result in limitDBResults)
            {
                var newSecurity = new Security();

                if (result.SecurityType == null)
                {
                    continue;
                }

                if (result.SecurityType == "Stock")
                {
                    newSecurity = new Stock("", result.Ticker, "", 0, 0);
                }
                else if (result.SecurityType == "Mutual Fund")
                {
                    newSecurity = new MutualFund("", result.Ticker, "", 0, 0);
                }

                var newTrade      = new Trade(result.TradeType, newSecurity, result.Ticker, result.Shares, "Limit", result.Limit, result.OrderDuration);
                var newLimitOrder = new LimitOrder(newTrade);
                _myLimitOrders.Add(newLimitOrder);
            }

            //Send List<LimitOrder> to all startup listeners
            Messenger.Default.Send <LimitOrderMessage>(new LimitOrderMessage(_myLimitOrders, true));
        }