예제 #1
0
        private static void UpdatePortfolio(Dictionary <string, Tuple <int, int> > buyOrSellInfo, DateTime forDate)
        {
            foreach (var item in buyOrSellInfo)
            {
                var stock  = item.Key;
                var isSell = (item.Value.Item2 > DistinctSelles);
                var isBuy  = (item.Value.Item1 >= DistinctByers) && !isSell;


                var repository  = new Repository.Repository();
                var stockEntity = repository.GetPortfolio().Result.FirstOrDefault(s => s.Stock.Equals(stock));
                var ticker      = repository.GetTickers().FirstOrDefault(t => t.FullName.Equals(stock));
                //post in not one of the stocks in list to follow
                if (ticker == null)
                {
                    continue;
                }
                var price = QuoteService.GetHistoricalPrice(ticker.TickerName, forDate);
                if (price.Last == null)
                {
                    continue;
                }

                var addBuyToPortfolio  = (stockEntity == null && isBuy);
                var addSellToPortfolio = stockEntity != null && isSell;
                var calcPrice          = Double.Parse(price.Last, System.Globalization.CultureInfo.InvariantCulture);
                var number             = 10000 / Double.Parse(price.Last,
                                                              System.Globalization.CultureInfo.
                                                              InvariantCulture);

                if (addBuyToPortfolio)
                {
                    repository.StoreTransaction(stock, forDate, "Köp", number, calcPrice, 10000, 0);
                    repository.AddPostToPortfolio(new Portfolio
                    {
                        Stock        = stock,
                        BuyPrice     = calcPrice,
                        BuyAmount    = 10000,
                        BuyNumber    = number,
                        BuyDate      = forDate,
                        CurrentPrice = calcPrice
                    }
                                                  );
                }
                else if (addSellToPortfolio)
                {
                    if (forDate > stockEntity.BuyDate)
                    {
                        var entity = repository.Context.Portfolio.First(i => i.Id.Equals(stockEntity.Id));
                        repository.Context.Portfolio.Remove(entity);
                        repository.SaveChanges();
                        var todaysPrice = Double.Parse(QuoteService.GetTodaysPrice(ticker.TickerName).Last, System.Globalization.CultureInfo.InvariantCulture);
                        repository.StoreTransaction(stock, forDate, "Försäljning", stockEntity.BuyNumber, todaysPrice,
                                                    (calcPrice * stockEntity.BuyNumber),
                                                    (calcPrice * stockEntity.BuyNumber) - 10000);
                    }
                }
            }
        }
예제 #2
0
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                var repository = new Repository.Repository();
                var portfolio  = repository.GetPortfolio();
                foreach (var item in portfolio.Result)
                {
                    var currentItem = item;
                    var ticker      = repository.GetTickers().FirstOrDefault(t => t.FullName.Equals(currentItem.Stock));
                    //post in not one of the stocks in list to follow
                    if (ticker == null)
                    {
                        continue;
                    }

                    var date = DateTime.Now;
                    if (date.DayOfWeek.Equals(DayOfWeek.Sunday))
                    {
                        date = DateTime.Now.AddDays(-3);
                    }
                    if (date.DayOfWeek.Equals((DayOfWeek.Saturday)))
                    {
                        date = DateTime.Now.AddDays(-2);
                    }

                    var price = QuoteService.GetTodaysPrice(ticker.TickerName);
                    if (price.Last == null)
                    {
                        continue;
                    }
                    var calcPrice = Double.Parse(price.Last, System.Globalization.CultureInfo.InvariantCulture);
                    if (item.BuyDate.AddMonths(3) < DateTime.Now)
                    {
                        AutomaticSell(item, calcPrice);
                    }
                    else
                    {
                        var entity = repository.Context.Portfolio.FirstOrDefault(i => i.Id.Equals(currentItem.Id));
                        entity.CurrentPrice = calcPrice;
                        repository.SaveChanges();
                    }
                }
                Logger.AddMessage("[OK] Updateportfoliojob " + DateTime.Now);
            }
            catch (Exception ex)
            {
                Logger.AddMessage("[ERROR] Updateportfoliojob " + ex.InnerException);
            }
        }