Пример #1
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);
        }