Пример #1
0
        public async Task <double> ValuePortfolio(DateTime timeToSell)
        {
            List <Holding> portfolio = await context.Holdings.Where(o => o.Day == day).ToListAsync();

            double            portfolioCost = 0;
            double            totalValue    = 0;
            List <StockQuote> PurchaseTime  = await GetSnapshot(timeToSell);

            await Task.Run(() =>
            {
                foreach (Holding shareBought in portfolio)
                {
                    string epic    = shareBought.Epic;
                    int quantity   = shareBought.Quantity;
                    portfolioCost += shareBought.PricePaid *shareBought.Quantity;
                    if (shareBought.TimeSold == 0)
                    {
                        StockQuote snap = (from s in PurchaseTime
                                           where
                                           s.Epic == epic
                                           select s).FirstOrDefault();
                        totalValue += snap.Bid *quantity;
                    }
                    else
                    {
                        totalValue += shareBought.SoldPrice *shareBought.Quantity;
                    }
                }
            });

            return(System.Math.Round(totalValue - portfolioCost, 2) - transactionCosts);
        }
Пример #2
0
        public static async Task ShowTime(SettingsManager settings)
        {
            DateTime     tradingDayStart = new DateTime(2020, 8, 13, 9, 30, 15);
            TradeManager trader          = new TradeManager(settings, tradingDayStart);

            trader.JournalOfPrices = await trader.CreateJournal();

            StockQuote sq = trader.JournalOfPrices[0];
            Holding    h  = new Holding();

            Console.WriteLine(h.EpochConvertor(sq.EpochTime));
        }
Пример #3
0
        public async Task <Portfolio> CreatePortfolio(DateTime timeToStartScan,
                                                      DateTime timeCheckpoint,
                                                      DateTime timeToBuy)
        {
            List <string> epics = await GetTopTenGainers(timeToStartScan, timeCheckpoint, timeToBuy);

            long time = await GetEpochTime(timeToBuy);

            StringBuilder sb = new StringBuilder();

            List <StockQuote> PurchaseTime = await GetSnapshot(timeToBuy);

            double    wodge      = settings.wodge;
            double    totalSpent = 0;
            Portfolio shares     = new Portfolio(new List <Holding>(), totalSpent);

            shares.Holdings = await context.Holdings.Where(o => o.Day == day).ToListAsync();

            foreach (Holding h in shares.Holdings)
            {
                h.TimeSold = 0;
            }


            if (shares.Holdings.Count < numberOfHoldings)
            {
                foreach (string epic in epics)
                {
                    if (shares.Holdings.Count >= numberOfHoldings)
                    {
                        break;
                    }

                    StockQuote snap        = (from s in PurchaseTime where s.Epic == epic select s).FirstOrDefault();
                    double     cost        = snap.Ask;
                    short      quantity    = Convert.ToInt16(System.Math.Floor(wodge / cost));
                    Holding    shareBought = new Holding(epic, time, cost, quantity);
                    if (!shares.Holdings.Contains(shareBought))
                    {
                        shares.Holdings.Add(shareBought);
                        totalSpent += quantity * cost;
                        sb.Append(shareBought.Epic);
                        sb.Append(", ");
                    }
                }
                await context.Holdings.AddRangeAsync(shares.Holdings);

                await context.SaveChangesAsync();
            }
            //Console.WriteLine(sb.ToString());
            return(shares);
        }
Пример #4
0
        public async Task <Portfolio> StopLoss(Portfolio portfolio, DateTime stopLossTime, DateTime sellTime)
        {
            await CreateJournal();

            Holding ho            = new Holding();
            long    startStopLoss = await GetEpochTime(stopLossTime);

            long timeToSell = await GetEpochTime(sellTime);

            List <long> epochs = await GetEpochsList();

            portfolio.Holdings = await context.Holdings.Where(o => o.Day == day).ToListAsync();

            foreach (long timeSlot in epochs)
            {
                // Console.WriteLine(ho.EpochConvertor(timeSlot));
                if (timeSlot <= startStopLoss || timeSlot >= timeToSell)
                {
                    continue;
                }

                List <string> looper = (from s in portfolio.Holdings select s.Epic).ToList();
                foreach (string epic in looper)
                {
                    Holding holding = (from h in portfolio.Holdings
                                       where (h.Epic == epic)
                                       select h).FirstOrDefault();

                    if (holding.TimeSold > 0)
                    {
                        continue;
                    }

                    StockQuote sq = (from s in JournalOfPrices
                                     where (s.EpochTime == timeSlot && s.Epic == epic)
                                     select s).FirstOrDefault();

                    if (holding.PeakPrice == 0 || holding.PeakPrice < sq.Ask)
                    {
                        holding.PeakPrice = sq.Ask;
                    }

                    if (sq.Ask < holding.PricePaid)
                    {
                        int nextTimeSlot = epochs.IndexOf(timeSlot) + 1;
                        holding.TimeToSell = epochs[nextTimeSlot];
                        double invested  = holding.PricePaid * holding.Quantity;
                        double worth     = sq.Ask * holding.Quantity;
                        double lostValue = worth - invested;
                        if (lostValue < settings.stopLossPerShare)
                        {
                            int        nxtTime     = epochs.IndexOf((long)holding.TimeToSell);
                            StockQuote stockToSell = (from s in JournalOfPrices
                                                      where (s.EpochTime == epochs[nxtTime] &&
                                                             s.Epic == holding.Epic)
                                                      select s).FirstOrDefault();
                            holding.SoldPrice = stockToSell.Ask;
                            holding.TimeSold  = timeSlot;
                            Console.WriteLine($"Selling {holding.Epic} at {holding.EpochConvertor(timeSlot)}.");
                            portfolio.Holdings.Remove(holding);
                            context.Holdings.Update(holding);
                            await context.SaveChangesAsync();
                        }
                    }
                }
            }
            return(portfolio);
        }