Beispiel #1
0
        public Portfolio GetPortfolio(string tradingAccountID)
        {
            Portfolio portfolio = new Portfolio();
            Buy       buy       = null;
            Sell      sell      = null;

            using (SqlConnection con = new SqlConnection(databaseConnection))
            {
                using (SqlCommand cmd = new SqlCommand("spGetPortfolio", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("TradingAccountID", tradingAccountID));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        portfolio.PortfolioID      = Convert.ToInt32(sqlReader["PortfolioID"].ToString());
                        portfolio.TradingAccountID = sqlReader["TradingAccountID"].ToString();
                    }
                }
                con.Close();
            }

            portfolio.Buys = new List <Buy>();

            using (SqlConnection con = new SqlConnection(databaseConnection))
            {
                using (SqlCommand cmd = new SqlCommand("spGetBuys", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@PortfolioID", portfolio.PortfolioID));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        if (Convert.ToInt32(sqlReader["Quantity"].ToString()) > 0)
                        {
                            buy                   = new Buy();
                            buy.BuyID             = Convert.ToInt32(sqlReader["BuyID"].ToString());
                            buy.TradeDate         = Convert.ToDateTime(sqlReader["TradeDate"].ToString());
                            buy.PortfolioId       = Convert.ToInt32(sqlReader["PortfolioId"].ToString());
                            buy.TickerSymbol      = sqlReader["TickerSymbol"].ToString();
                            buy.Quantity          = Convert.ToInt32(sqlReader["Quantity"].ToString());
                            buy.PurchasePrice     = Convert.ToDecimal(sqlReader["PurchasePrice"].ToString());
                            buy.TransactionAmount = Convert.ToDecimal(sqlReader["TransactionAmount"].ToString());
                            buy.TransactionCost   = Convert.ToDecimal(sqlReader["TransactionCost"].ToString());
                        }
                    }
                }
                con.Close();
            }

            portfolio.Sells = new List <Sell>();

            using (SqlConnection con = new SqlConnection(databaseConnection))
            {
                using (SqlCommand cmd = new SqlCommand("spGetSells", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@PortfolioID", portfolio.PortfolioID));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        sell                   = new Sell();
                        sell.SellID            = Convert.ToInt32(sqlReader["SellID"].ToString());
                        sell.TradeDate         = Convert.ToDateTime(sqlReader["TradeDate"].ToString());
                        sell.BuyID             = Convert.ToInt32(sqlReader["BuyID"].ToString());
                        sell.TickerSymbol      = sqlReader["TickerSymbol"].ToString();
                        sell.Quantity          = Convert.ToInt32(sqlReader["Quantity"].ToString());
                        sell.PurchasePrice     = Convert.ToDecimal(sqlReader["PurchasePrice"].ToString());
                        sell.SoldPrice         = Convert.ToDecimal(sqlReader["SoldPrice"].ToString());
                        sell.TransactionAmount = Convert.ToDecimal(sqlReader["TransactionAmount"].ToString());
                        sell.TransactionCost   = Convert.ToDecimal(sqlReader["TransactionCost"].ToString());
                        sell.PortfolioID       = Convert.ToInt32(sqlReader["PortfolioID"].ToString());

                        portfolio.Sells.Add(sell);
                    }
                }
                con.Close();
            }
            return(portfolio);
        }
Beispiel #2
0
        public Buy Finalizebuy(Buy model, string userID)
        {
            Owner            owner      = null;
            TraderAccount    account    = null;
            Portfolio        portfolio  = null;
            List <Portfolio> portfolios = new List <Portfolio>();
            int last = 0;


            using (SqlConnection con = new SqlConnection(databaseConnection))
            {
                using (SqlCommand cmd = new SqlCommand("spGetOwner", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID", userID));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        owner         = new Owner();
                        owner.OwnerID = sqlReader["OwnerID"].ToString();
                        owner.UserID  = sqlReader["UserID"].ToString();
                    }
                }
                con.Close();
            }

            using (SqlConnection con = new SqlConnection(databaseConnection))
            {
                using (SqlCommand cmd = new SqlCommand("spTradingAccount", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@OwnerID", owner.OwnerID));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        account = new TraderAccount();
                        account.TradingAccountID = sqlReader["TradingAccountID"].ToString();
                        account.OwnerID          = sqlReader["OwnerID"].ToString();
                        account.CreationDate     = Convert.ToDateTime(sqlReader["CreationDate"].ToString());
                        account.Balance          = Convert.ToDecimal(sqlReader["Balance"].ToString());
                    }
                }
                con.Close();
            }

            using (SqlConnection con = new SqlConnection(databaseConnection))
            {
                using (SqlCommand cmd = new SqlCommand("spGetPortfolio", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@TradingAccountID", account.TradingAccountID));
                    con.Open();
                    cmd.ExecuteNonQuery();
                    SqlDataReader sqlReader = cmd.ExecuteReader();
                    while (sqlReader.Read())
                    {
                        portfolio                  = new Portfolio();
                        portfolio.PortfolioID      = Convert.ToInt32(sqlReader["PortfolioID"].ToString());
                        portfolio.TradingAccountID = sqlReader["TradingAccountID"].ToString();
                    }
                }
                con.Close();
            }
            if (portfolio == null)
            {
                using (SqlConnection con = new SqlConnection(databaseConnection))
                {
                    using (SqlCommand cmd = new SqlCommand("spGetLastPortfolio", con))
                    {
                        con.Open();
                        cmd.ExecuteNonQuery();
                        SqlDataReader sqlReader = cmd.ExecuteReader();
                        while (sqlReader.Read())
                        {
                            last = Convert.ToInt32(sqlReader["PortfolioID"].ToString());
                        }
                    }
                    con.Close();
                }

                DefaultConnection.Portfolios.Add(new Portfolio()
                {
                    PortfolioID      = last + 1,
                    TradingAccountID = account.TradingAccountID
                });
                DefaultConnection.SaveChanges();
                model.PortfolioId = last + 1;
                DefaultConnection.Buys.Add(model);
                DefaultConnection.SaveChanges();
                account.Balance = account.Balance - model.TransactionAmount;
                DefaultConnection.Entry(account).State = EntityState.Modified;
                DefaultConnection.SaveChanges();
            }
            else
            {
                model.PortfolioId = portfolio.PortfolioID;
                DefaultConnection.Buys.Add(model);
                DefaultConnection.SaveChanges();
                account.Balance = account.Balance - model.TransactionAmount;
                DefaultConnection.Entry(account).State = EntityState.Modified;
                DefaultConnection.SaveChanges();
            }

            return(model);
        }