public async Task <Domain.Position> GetOrCreateEmptyPosition(Domain.StrategysStock strategysStock)
        {
            var strategysStockDb = GetStatagysStockFromDb(strategysStock);

            if (strategysStockDb == null)
            {
                _context.StratagysStocks.Add(new StratagysStock
                {
                    StockSymbol      = strategysStock.StockSymbol,
                    Strategy         = strategysStock.Strategy,
                    TradingFrequency = strategysStock.TradingFrequency
                });

                await _context.SaveChangesAsync();

                strategysStockDb = GetStatagysStockFromDb(strategysStock);
            }

            return(new Domain.Position
            {
                StockSymbol = strategysStockDb.StockSymbol,
                NumberOfShares = strategysStockDb?.Orders
                                 ?.Select(x => x.ActualSharesBought ?? 0)
                                 ?.Aggregate((x, y) => x + y) ?? 0
            });
        }
 public IEnumerable <Domain.Order> GetCompletedOrders(Domain.StrategysStock strategysStock)
 => GetStatagysStockFromDb(strategysStock)?.Orders
 ?.Where(x => x.ActualCostPerShare.HasValue && x.ActualSharesBought.HasValue)
 ?.Select(x => new Domain.Order
 {
     MarketPrice     = x.ActualCostPerShare.Value,
     OrderPlacedTime = x.OrderPlaced.UtcDateTime,
     SharesBought    = x.ActualSharesBought.Value
 }) ?? Enumerable.Empty <Domain.Order>();
        public async Task AddPendingOrder(Domain.StrategysStock strategysStock, Domain.Order order)
        {
            var stratagysStock = GetStatagysStockFromDb(strategysStock);

            _context.Add(new Order
            {
                OrderPlaced           = order.OrderPlacedTime,
                AttemptedCostPerShare = order.MarketPrice,
                AttemptedSharesBought = order.SharesBought,
                PositionId            = stratagysStock.Id
            });

            await _context.SaveChangesAsync();
        }
 private StratagysStock GetStatagysStockFromDb(Domain.StrategysStock strategysStock)
 => _context.StratagysStocks.SingleOrDefault(x =>
                                             x.StockSymbol == strategysStock.StockSymbol &&
                                             x.Strategy == strategysStock.Strategy &&
                                             x.TradingFrequency == strategysStock.TradingFrequency);