예제 #1
0
        public ActionResult ClearHistory()
        {
            using (StockContext db = new StockContext())
            {
                IEnumerable<StockTransaction> transactions = db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]);
                foreach(StockTransaction t in transactions){
                    db.StockTransactions.Remove(t);
                }
                db.SaveChanges();
            }

            //return RedirectToLocal("/Stocks/History");
            return RedirectToAction("History", "Stocks");
        }
예제 #2
0
        public ActionResult DownloadHistory()
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=stockHistory.csv");
            Response.ContentType = "text/csv";

            // Convert List to CSV
            string csv = "";
            using (StockContext db = new StockContext())
            {
                IEnumerable<StockTransaction> transactions = db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]);
                csv = String.Join("\r\n", transactions.Select(x => x.ToCSV()).ToArray());
            }
            Response.Write(csv);
            Response.End();

            return Content(String.Empty);
        }
예제 #3
0
        public ActionResult BuyStock(TransactionModel model)
        {
            if (ModelState.IsValid && model.Ticker != "None")
            {
                using (StockContext db = new StockContext())
                {
                    Stock s = getOneStock(model.Ticker);
                    StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = model.Ticker, Price = s.CurrentPrice * -1, Shares = model.Amount, TransactionDate = DateTime.Now };
                    db.StockTransactions.Add(st);
                    db.SaveChanges();
                }
             //   getNetworth();
                //return RedirectToLocal("/Stocks/Index");
                return RedirectToAction("Index", "Stocks");
            }

            // If we got this far, something failed, redisplay form
             //   ModelState.AddModelError("Amount", "The amount provided is invalid.");
             //   return View("Index", model);
            return RedirectToAction("Index", "Stocks");
        }
예제 #4
0
        //fill a viewbag with the net worth of the user
        public void getNetworth()
        {
            double totalNetValue = 0.0;

            Dictionary<string, List<StockTransaction>> transactionDictonary = new Dictionary<string, List<StockTransaction>>();

            List<Stock> viewStockList = new List<Stock>();

            using (StockContext db = new StockContext())
            {
                foreach (StockTransaction trans in db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]))
                {
                    if (transactionDictonary.ContainsKey(trans.Ticker))
                    {
                        List<StockTransaction> transactionList = transactionDictonary[trans.Ticker];
                        transactionList.Add(trans);
                        transactionDictonary[trans.Ticker] = transactionList;
                    }
                    else
                    {
                        List<StockTransaction> transactionList = new List<StockTransaction>();
                        transactionList.Add(trans);
                        transactionDictonary.Add(trans.Ticker, transactionList);
                    }

                }

                foreach (KeyValuePair<string, List<StockTransaction>> pair in transactionDictonary)
                {
                    Stock stock = getOneStock(pair.Key);
                    stock.Shares = 0;
                    stock.Investment = 0;

                    //add up total shares and investment using stocktransaction history
                    foreach (StockTransaction action in pair.Value)
                    {
                        stock.Shares += action.Shares;
                        stock.Investment += action.Price * Math.Abs(action.Shares);

                    }

                    stock.Investment = Math.Round(stock.Investment, 2);
                    stock.StockValue = Math.Round(stock.Shares * stock.CurrentPrice, 2);
                    stock.NetWorth = Math.Round(stock.StockValue + stock.Investment, 2);
                    totalNetValue += stock.NetWorth;

                    viewStockList.Add(stock);
                }

            }

            ViewBag.totalNetValue = totalNetValue;
        }
예제 #5
0
        public ActionResult UploadHistory(HttpPostedFileBase uploadFile)
        {
            if (uploadFile == null)
            {
                //return RedirectToLocal("/Stocks/History");
                return RedirectToAction("History", "Stocks");
            }
            StreamReader csvreader = new StreamReader(uploadFile.InputStream);

            using (StockContext db = new StockContext())
            {
                IEnumerable<StockTransaction> transactions = db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]);
                foreach (StockTransaction t in transactions)
                {
                    db.StockTransactions.Remove(t);
                }
                db.SaveChanges();
            }

            while (!csvreader.EndOfStream)
            {
                var line = csvreader.ReadLine();
                var values = line.Split(',');
                Debug.WriteLine(values[2] + " " + values[3] + " " + values[4]);
                Debug.WriteLine(line);
                using (StockContext db = new StockContext())
                {
                    StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = values[2], Price = Convert.ToDouble(values[4]), Shares = Convert.ToInt32(values[3]), TransactionDate = Convert.ToDateTime(values[5])};
                    db.StockTransactions.Add(st);
                    db.SaveChanges();
                }
            }
            //return RedirectToLocal("/Stocks/History");
            return RedirectToAction("History", "Stocks");
        }
예제 #6
0
        // Get the list of all stocks that we are currently 'watching'
        public ActionResult StocksList()
        {
            Dictionary<string, List<StockTransaction>> transactionDictonary = new Dictionary<string, List<StockTransaction>>();

            List<Stock> viewStockList = new List<Stock>();

            using (StockContext db = new StockContext())
            {
                foreach (StockTransaction trans in db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]))
                {
                    if(transactionDictonary.ContainsKey(trans.Ticker)){
                        List<StockTransaction> transactionList = transactionDictonary[trans.Ticker];
                        transactionList.Add(trans);
                        transactionDictonary[trans.Ticker] = transactionList;
                    }
                    else
                    {
                        List<StockTransaction> transactionList = new List<StockTransaction>();
                        transactionList.Add(trans);
                        transactionDictonary.Add(trans.Ticker, transactionList);
                    }

                }

                 foreach(KeyValuePair<string, List<StockTransaction>> pair in transactionDictonary){
                     Stock stock = getOneStock(pair.Key);
                     stock.Shares = 0;
                     stock.Investment = 0;

                     //add up total shares and investment using stocktransaction history
                     foreach (StockTransaction action in pair.Value)
                     {
                         stock.Shares += action.Shares;
                         stock.Investment += action.Price*Math.Abs(action.Shares);

                     }

                     stock.Investment = Math.Round(stock.Investment, 2);
                     stock.StockValue = Math.Round(stock.Shares * stock.CurrentPrice, 2);
                     stock.NetWorth = Math.Round(stock.StockValue + stock.Investment, 2);

                     viewStockList.Add(stock);
                 }

            }

            using(CommentContext db = new CommentContext())
            {
                foreach (StockComment com in db.StockComments.ToList())
                {
                    if(com.FBID.Replace(" ","") == ((String)Session["FBID"]).Replace(" ", ""))
                    {
                        if (com.Comment != null && com.Comment != "")
                        {
                            Boolean found = false;

                            foreach (Stock s in viewStockList)
                            {
                                if (s.Ticker.Replace(" ", "") == com.Ticker.Replace(" ", ""))
                                {
                                    found = true;
                                }
                            }
                            if (found == false)
                            {
                                Stock stock = getOneStock(com.Ticker);
                                    stock.Shares = 0;
                                    stock.Investment = 0;
                                    viewStockList.Add(stock);
                            }
                        }
                    }

                }
                ViewBag.slist = viewStockList;

            }

            return PartialView();
        }
예제 #7
0
 public ActionResult StockHistory(String name)
 {
     using (StockContext db = new StockContext())
     {
         if (name == null || name == "")
         {
             ViewBag.sList = db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]);
         }
         else
         {
             ViewBag.sList = db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"] & t.Ticker == name.ToUpper());
         }
     }
     return PartialView();
 }
예제 #8
0
        public ActionResult SellStock(TransactionModel model)
        {
            if (ModelState.IsValid && model.Ticker != "None" && model.Ticker != "NA")
            {
                Dictionary<string, List<StockTransaction>> transactionDictonary = new Dictionary<string, List<StockTransaction>>();

                List<Stock> viewStockList = new List<Stock>();

                using (StockContext db = new StockContext())
                {
                    foreach (StockTransaction trans in db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]))
                    {
                        if (transactionDictonary.ContainsKey(trans.Ticker))
                        {
                            List<StockTransaction> transactionList = transactionDictonary[trans.Ticker];
                            transactionList.Add(trans);
                            transactionDictonary[trans.Ticker] = transactionList;
                        }
                        else
                        {
                            List<StockTransaction> transactionList = new List<StockTransaction>();
                            transactionList.Add(trans);
                            transactionDictonary.Add(trans.Ticker, transactionList);
                        }

                    }

                    int shares = 0;
                    if (transactionDictonary.ContainsKey(model.Ticker))
                    {

                        foreach (StockTransaction trans in transactionDictonary[model.Ticker])
                        {
                            shares += trans.Shares;
                        }

                        if (shares != 0 && shares > model.Amount)
                        {
                            Stock s = getOneStock(model.Ticker);
                            StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = model.Ticker, Price = s.CurrentPrice, Shares = model.Amount * -1, TransactionDate = DateTime.Now };
                            db.StockTransactions.Add(st);
                            db.SaveChanges();
                        }
                        else if (shares != 0 && shares < model.Amount)
                        {
                            Stock s = getOneStock(model.Ticker);
                            StockTransaction st = new StockTransaction { FBID = (String)Session["FBID"], Ticker = model.Ticker, Price = s.CurrentPrice, Shares = shares * -1, TransactionDate = DateTime.Now };
                            db.StockTransactions.Add(st);
                            db.SaveChanges();
                        }
                    }
                }
                //return RedirectToLocal("/Stocks/Index");
                return RedirectToAction("Index", "Stocks");
            }

            // If we got this far, something failed, redisplay form
              //      ModelState.AddModelError("Amount", "The amount provided is invalid.");
              //  return View("Index", model);
            return RedirectToAction("Index", "Stocks");
        }
예제 #9
0
 public ActionResult History()
 {
     using (StockContext db = new StockContext())
     {
         List<StockTransaction> sList = db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]).ToList();
         if (sList.Count() == 0)
         {
             ViewBag.hasHistory = false;
         }
         else
         {
             ViewBag.hasHistory = true;
         }
     }
     return View();
 }
예제 #10
0
        public void getTopFiveStocks()
        {
            Dictionary<string, List<StockTransaction>> transactionDictonary = new Dictionary<string, List<StockTransaction>>();

            List<Stock> viewStockList = new List<Stock>();

            using (StockContext db = new StockContext())
            {
                foreach (StockTransaction trans in db.StockTransactions.ToList().Where(t => t.FBID == (String)Session["FBID"]))
                {
                    if (transactionDictonary.ContainsKey(trans.Ticker))
                    {
                        List<StockTransaction> transactionList = transactionDictonary[trans.Ticker];
                        transactionList.Add(trans);
                        transactionDictonary[trans.Ticker] = transactionList;
                    }
                    else
                    {
                        List<StockTransaction> transactionList = new List<StockTransaction>();
                        transactionList.Add(trans);
                        transactionDictonary.Add(trans.Ticker, transactionList);
                    }
                }

                foreach (KeyValuePair<string, List<StockTransaction>> pair in transactionDictonary)
                {
                    Stock stock = new Stock();
                    stock.Ticker = pair.Key;
                    stock.CurrentPrice = getOneStock(pair.Key).CurrentPrice;
                    stock.Shares = 0;

                    //add up total shares and investment using stocktransaction history
                    foreach (StockTransaction action in pair.Value)
                    {
                        stock.Shares += action.Shares;
                    }

                    stock.StockValue = Math.Round(stock.Shares * stock.CurrentPrice, 2);

                    viewStockList.Add(stock);
                }

            }

            for (int i = 1; i < viewStockList.Count; i++) // Iterate beginning at 1, because we assume that 0 is already sorted
            {
                for (int j = i; j > 0; j--) // Iterate backwards, starting from 'i'
                {
                    Stock cur = viewStockList[j - 1];
                    Stock tbs = viewStockList[j]; // 'tbs' == "to be sorted"
                    if (cur.StockValue < tbs.StockValue) // usually, classes that implement 'CompareTo()' also implement 'operator <()', 'operator >()' and 'operator ==()', so you could have just written 'cur < tbs'
                    {
                        Stock temp = viewStockList[j];
                        viewStockList[j] = viewStockList[j - 1];
                        viewStockList[j - 1] = temp;
                    }
                    else
                        break; // since 'tbs' is no longer > 'cur', it is part of our sorted list. We don't need to sort that particular 'tbs' any further
                }
            }

            ViewBag.stockList = viewStockList.Take(5);
        }