public async Task <float> CalculateNetProfitAsync()
        {
            //Get list of all stocks
            List <string> stocks = new List <string>();

            foreach (EquityHolding eh in EquityHoldings)
            {
                if (stocks.Contains(eh.Symbol.Trim().ToUpper()) == false)
                {
                    stocks.Add(eh.Symbol.Trim().ToUpper());
                }
            }

            //Get the equity data as a batch
            BatchStockDataProvider bsdp = new BatchStockDataProvider();

            EquitySummaryData[] esds = await bsdp.GetBatchEquitySummaryData(stocks.ToArray());

            //Add up our portfolio value
            float PortValue = 0;

            foreach (EquityHolding eh in EquityHoldings)
            {
                foreach (EquitySummaryData esd in esds)
                {
                    if (esd.StockSymbol.ToUpper().Trim() == eh.Symbol.ToUpper().Trim())
                    {
                        float thisstockval = eh.Quantity * esd.Price;
                        PortValue = PortValue + thisstockval;
                    }
                }
            }

            //Find how much cash was invested
            float CashInvested = 0;

            foreach (CashTransaction ct in CashTransactionLog)
            {
                if (ct.ChangeType == CashTransactionType.Edit) //Only count it if it was added. If it was a charge for trading (commission), do not count it.
                {
                    CashInvested = CashInvested + ct.CashChange;
                }
            }


            return(PortValue + Cash - CashInvested);
        }
        public async Task <string[]> PrepareTweetsForUpcomingEarningsCallsAsync(DateTime day)
        {
            List <string> ToReturn = new List <string>();

            //Get stocks
            EarningsCalendarProvider ecp = new EarningsCalendarProvider();

            string[] stocks = await ecp.GetCompaniesReportingEarningsAsync(day);

            if (stocks.Length == 0)
            {
                ToReturn.Add("No earnings calls planned for " + day.ToShortDateString() + "! Until next time.");
                return(ToReturn.ToArray());
            }
            BatchStockDataProvider bsdp = new BatchStockDataProvider();

            EquitySummaryData[] data = await bsdp.GetBatchEquitySummaryData(stocks);


            //Rank by market cap
            List <EquitySummaryData> DataAsList = data.ToList();
            List <EquitySummaryData> Filter1    = new List <EquitySummaryData>();

            do
            {
                EquitySummaryData winner = DataAsList[0];
                foreach (EquitySummaryData esd in DataAsList)
                {
                    if (esd.MarketCap > winner.MarketCap)
                    {
                        winner = esd;
                    }
                }
                Filter1.Add(winner);
                DataAsList.Remove(winner);
            } while (DataAsList.Count != 0);



            int    t            = 1;
            string CurrentTweet = "";

            //First tweet
            DateTimeFormatInfo dtfo      = new DateTimeFormatInfo();
            string             monthName = dtfo.GetMonthName(day.Month);

            CurrentTweet = "Upcoming earnings calls on " + day.DayOfWeek.ToString() + ", " + monthName + " " + day.Day.ToString() + " " + day.Year.ToString() + ":";

            //Add all
            foreach (EquitySummaryData esd in Filter1)
            {
                string MyPiece = t.ToString() + ". " + "$" + esd.StockSymbol.ToUpper().Trim() + " " + esd.Name;

                //Propose what it would be
                string proposal = CurrentTweet + "\n" + MyPiece;

                if (proposal.Length <= 280) //If it fits, keep it in the current tweet
                {
                    CurrentTweet = proposal;
                }
                else //If it doesn't fit in the current tweet, commit that tweet to the Tweet list and start another
                {
                    ToReturn.Add(CurrentTweet);
                    CurrentTweet = MyPiece;
                }

                t = t + 1;
            }

            //Add the current tweet that is being worked on to the list
            ToReturn.Add(CurrentTweet);


            return(ToReturn.ToArray());
        }
        public async Task <EquityHoldingPerformance[]> CalculateEquityHoldingPerformances()
        {
            List <EquityHoldingPerformance> ToReturn = new List <EquityHoldingPerformance>();

            //Check if there are no holdings
            if (EquityHoldings.Count == 0)
            {
                return(ToReturn.ToArray());
            }

            //Get a list of all equities
            List <string> Stocks = new List <string>();

            foreach (EquityHolding eh in EquityHoldings)
            {
                if (Stocks.Contains(eh.Symbol.Trim().ToUpper()) == false)
                {
                    Stocks.Add(eh.Symbol.Trim().ToUpper());
                }
            }

            //Get all stock data
            BatchStockDataProvider bsdp = new BatchStockDataProvider();

            EquitySummaryData[] ESDs = await bsdp.GetBatchEquitySummaryData(Stocks.ToArray());

            //Check if we have all of them
            if (Stocks.Count != ESDs.Length)
            {
                string DontHave = "";
                foreach (string s in Stocks)
                {
                    bool HasIt = false;
                    foreach (EquitySummaryData esd in ESDs)
                    {
                        if (esd.StockSymbol.Trim().ToUpper() == s.Trim().ToUpper())
                        {
                            HasIt = true;
                        }
                    }
                    if (HasIt == false)
                    {
                        DontHave = DontHave + s + ",";
                    }
                }
                throw new Exception("Unable to calculate all of the performance logs for your holdings because access of data failed: " + DontHave);
            }


            //Calculate the performances
            foreach (string s in Stocks)
            {
                EquityHoldingPerformance ehp = new EquityHoldingPerformance();

                //Find the right Equity Data
                EquitySummaryData esd = null;
                foreach (EquitySummaryData es in ESDs)
                {
                    if (es.StockSymbol.Trim().ToUpper() == s.Trim().ToUpper())
                    {
                        esd = es;
                    }
                }
                if (esd == null)
                {
                    throw new Exception("Fatal error while calculating performance for " + s + ".");
                }

                //Find the right equity holding
                EquityHolding eh = null;
                foreach (EquityHolding h in EquityHoldings)
                {
                    if (h.Symbol.Trim().ToUpper() == s.Trim().ToUpper())
                    {
                        eh = h;
                    }
                }
                if (eh == null)
                {
                    throw new Exception("Fatal error while find holding for " + s + ".");
                }

                //Add symbol
                ehp.Symbol = s.Trim().ToUpper();

                //Get dollars invested
                ehp.DollarsInvested = eh.AverageCostBasis * eh.Quantity;

                //Get holding value
                ehp.HoldingValue = eh.Quantity * esd.Price;

                //Get dollar profit
                ehp.DollarProfit = ehp.HoldingValue - ehp.DollarsInvested;

                //Get percent profit
                ehp.PercentProfit = ehp.DollarProfit / ehp.DollarsInvested;

                ToReturn.Add(ehp);
            }


            //Return them
            return(ToReturn.ToArray());
        }