Exemplo n.º 1
0
 public async Task GetPriceHistoryDifferentialAsync(object analyticsDateRange)
 {
     _logger.LogInformation($"GetPriceHistoryDifferentialAsync {Context.ConnectionId}");
     using Entities.StocksDbContext stocksContext = new Entities.StocksDbContext(new Microsoft.EntityFrameworkCore.DbContextOptions <Entities.StocksDbContext>());
     DateTimeRange dateTimeRange = JsonConvert.DeserializeObject <DateTimeRange>(analyticsDateRange.ToString());
     // debugging hub methods not running concurrently
     // Task.Delay proves the concurrency issue is not with the PriceHistoryDifferential query
     //await Task.Delay(5000);
     await Clients.Caller.SendCoreAsync("drawPriceHistoryDifferential", new object[]
     {
         JsonConvert.SerializeObject(CompiledQueries.PriceHistoryDifferential(dateTimeRange))
     });
 }
Exemplo n.º 2
0
        public static async Task <IEnumerable <Models.PriceDelta> > GetQuotesAsync(HttpClient httpClient, Entities.StocksDbContext stocksDbContext, Dictionary <string, string> settings, IEnumerable <Models.PriceDelta> priceDeltas)
        {
            foreach (Models.PriceDelta priceDelta in priceDeltas)
            {
                Models.TdAmeritrade.Quote.Quote quote = await TdAmeritrade.Quote.GetQuoteAsync(httpClient, stocksDbContext, settings, priceDelta.Instrument.Symbol);

                priceDelta.Quotes.Enqueue(quote);
                if (priceDelta.Quotes.Count > 1)
                {
                    Models.TdAmeritrade.Quote.Quote previousQuote = priceDelta.Quotes.Dequeue();
                    priceDelta.QuoteDelta        = quote.Mark - previousQuote.Mark;
                    priceDelta.QuoteDeltaPercent = (quote.Mark / previousQuote.Mark - 1) * 100;
                    if (priceDelta.QuoteDelta > 0)
                    {
                        priceDelta.DeltaIndex++;
                    }

                    if (priceDelta.QuoteDelta < 0)
                    {
                        priceDelta.DeltaIndex--;
                    }
                }

                if (priceDelta.Position == null)
                {
                }

                else
                {
                    priceDelta.PositionDelta        = quote.Mark - priceDelta.Position.AveragePrice;
                    priceDelta.PositionDeltaPercent = (quote.Mark / priceDelta.Position.AveragePrice - 1) * 100;
                }
            }

            return(priceDeltas);
        }
Exemplo n.º 3
0
        public static async Task <IEnumerable <Models.PriceDelta> > GetPriceDeltasAsync(HttpClient httpClient, Entities.StocksDbContext stocksDbContext, Dictionary <string, string> settings, Models.TdAmeritrade.Account.Account account, IEnumerable <Models.PriceDelta> priceDeltas = null)
        {
            List <Models.PriceDelta> priceDeltasList = priceDeltas == null ? new List <Models.PriceDelta>() : priceDeltas.ToList();
            IEnumerable <Models.TdAmeritrade.Watchlist.Watchlist> accountWatchlists = await TdAmeritrade.Watchlist.GetWatchListsForSingleAccountAsync(httpClient, stocksDbContext, settings, account.SecuritiesAccount.AccountId);

            IEnumerable <Models.TdAmeritrade.Account.Instrument> positionInstruments        = account.SecuritiesAccount.Positions.Select(x => x.Instrument);
            IEnumerable <Models.TdAmeritrade.Account.Instrument> stocksWatchlistInstruments = accountWatchlists.Single(x => x.AccountId == account.SecuritiesAccount.AccountId && x.Name == "Stocks").WatchlistItems.Select(x => x.Instrument).Where(x => !positionInstruments.Any(y => y.Symbol == x.Symbol));
            IEnumerable <Models.TdAmeritrade.Account.Instrument> instruments = positionInstruments.Concat(stocksWatchlistInstruments);
            List <Models.PriceDelta> priceDeltasToRemove = priceDeltasList.Where(x => x.AccountId == account.SecuritiesAccount.AccountId && !instruments.Any(y => y.Symbol == x.Instrument.Symbol)).ToList();

            foreach (Models.PriceDelta priceDelta in priceDeltasToRemove)
            {
                priceDeltasList.Remove(priceDelta);
            }

            foreach (Models.TdAmeritrade.Account.Instrument instrument in instruments)
            {
                if (priceDeltasList.Any(x => x.AccountId == account.SecuritiesAccount.AccountId && x.Instrument.Symbol == instrument.Symbol))
                {
                    Models.PriceDelta priceDelta = priceDeltasList.Single(x => x.AccountId == account.SecuritiesAccount.AccountId && x.Instrument.Symbol == instrument.Symbol);
                    if (priceDelta.Position == null)
                    {
                    }

                    else
                    {
                        priceDeltasList[priceDeltasList.IndexOf(priceDelta)].Position = account.SecuritiesAccount.Positions.Single(x => x.Instrument.Symbol == instrument.Symbol);
                    }
                }

                else
                {
                    Models.PriceDelta priceDelta = new Models.PriceDelta()
                    {
                        Instrument = instrument,
                        AccountId  = account.SecuritiesAccount.AccountId,
                        Position   = account.SecuritiesAccount.Positions.SingleOrDefault(x => x.Instrument.Symbol == instrument.Symbol),
                        Quotes     = new Queue <Models.TdAmeritrade.Quote.Quote>()
                    };
                    priceDeltasList.Add(priceDelta);
                }
            }

            return(priceDeltasList.AsEnumerable());
        }