Example #1
0
        public async Task <IActionResult> UpdateDatabasePrices()
        {
            // 1. Get the tickers for all open trades. (tickers where net quantity !=0)
            // 2. get a hashset of the date each ticker is priced at our db.
            // 3. get latest prices from alphavantage and update the prices for each ticker if we dont have it in our system.

            // I can make 5 api calls a minute. so after every 5 calls. pause for 60 seconds before resuming
            List <string> openTickers  = _adminRepo.GetOpenTradeTickers();
            int           requestsMade = 0;

            foreach (string ticker in openTickers)
            {
                requestsMade++;
                if (requestsMade % 6 == 0)
                {
                    System.Threading.Thread.Sleep(60000);
                }
                HashSet <DateTime> pricedDates           = _adminRepo.GetPriceDates(ticker);
                List <AlphaVantageSecurityData> avPrices = _avConn.GetDailyPrices(ticker);
                foreach (var price in avPrices)
                {
                    if (!pricedDates.Contains(price.Timestamp))
                    {
                        SecurityPrices newPrice = new SecurityPrices {
                            Date = price.Timestamp, ClosePrice = price.Close, Ticker = ticker
                        };
                        _adminRepo.AddSecurityPrice(newPrice);
                        System.Diagnostics.Debug.WriteLine($"Storing: {ticker}| {price.Timestamp}| {price.Close}");
                    }
                }
            }
            await _adminRepo.SaveChangesAsync();

            return(RedirectToAction("AdminPanel", "Admin"));
        }
 void _pricePublisher_PriceUpdated(int securityID, decimal price)
 {
     _dispatcher.BeginInvoke((Action)(() =>
     {
         var viewModel = SecurityPrices.FirstOrDefault(x => x.Security.SecurityID == securityID);
         viewModel.Price = price;
     }));
 }
Example #3
0
        public async Task <IActionResult> AddTrade([Bind("TradeId, Ticker, Quantity, Price, TradeDate, Comments, CreatedTimeStamp, UserId, PortfolioId")] Trade trade)
        {
            if (!ModelState.IsValid)
            {
                return(PartialView("_TradeEntryModalPartial", trade));
            }
            trade.UserId = _userId;

            // check if trade ticker is valid..
            bool validTicker = _repo.IsValidTicker(trade.Ticker);

            if (!validTicker)
            {
                ModelState.AddModelError("Ticker", $"{trade.Ticker} is not a tradeable instrument");
                return(PartialView("_TradeEntryModalPartial", trade));
            }

            _repo.AddTrade(trade);
            await _repo.SaveChangesAsync();

            bool isSecurityStored = _repo.IsSecurityStored(trade.Ticker);

            if (!isSecurityStored)
            {
                // Later on create logic that stores the security price AND MAKE THIS ASYNC so THE SCREEN DOESNT FREEZE
                // also make this try catch in the event that prices are not found
                List <AlphaVantageSecurityData> prices = _avConn.GetDailyPrices(trade.Ticker);

                foreach (AlphaVantageSecurityData price in prices)
                {
                    SecurityPrices newPrice = new SecurityPrices {
                        Date = price.Timestamp, ClosePrice = price.Close, Ticker = trade.Ticker
                    };
                    _repo.AddSecurityPrice(newPrice);
                }
                await _repo.SaveChangesAsync();
            }
            return(PartialView("_TradeEntryModalPartial", trade));
        }
 public void AddSecurityPrice(SecurityPrices price)
 {
     _db.SecurityPrices.Add(price);
 }