Пример #1
0
        public async Task <TradingHistoryModel> EditTradingHistory(TradingHistoryModel history)
        {
            if (history == null)
            {
                throw new ArgumentNullException(nameof(history));
            }

            using (var db = new AuctionContext())
            {
                var modifiedRecord = db.TradingHistories.FirstOrDefault(h => h.Id == history.Id);
                if (modifiedRecord == null)
                {
                    return(null);
                }

                var trader     = db.Traders.FirstOrDefault(t => t.Id == (history.Trader == null ? 0 : history.Trader.Id)) ?? modifiedRecord.Trader;
                var lot        = db.Lots.FirstOrDefault(L => L.Id == (history.Lot == null ? 0 : history.Lot.Id)) ?? modifiedRecord.Lot;
                var auctioneer = db.Auctioneers.FirstOrDefault(a => a.Id == (history.Auctioneer == null ? 0 : history.Auctioneer.Id)) ?? modifiedRecord.Auctioneer;


                modifiedRecord.Lot           = lot;
                modifiedRecord.Trader        = trader;
                modifiedRecord.Auctioneer    = auctioneer;
                modifiedRecord.LotId         = lot.Id;
                modifiedRecord.TraderId      = trader.Id;
                modifiedRecord.AuctioneerId  = auctioneer.Id;
                modifiedRecord.BidTime       = history.BidTime;
                modifiedRecord.BidOrder      = history.BidOrder;
                modifiedRecord.RecordedPrice = history.RecordedPrice;

                await db.SaveChangesAsync();

                return(history);
            }
        }
Пример #2
0
 public Account(int id, string name)
 {
     this.id             = id;
     this.Name           = name;
     this.Portfolio      = new PortfolioModel();
     this.TradingHistory = new TradingHistoryModel();
 }
Пример #3
0
        public static TradingHistory ToTradingHistory(
            this TradingHistoryModel tradingHistory,
            Auctioneer auctioneer,
            Trader trader,
            Lot lot)
        {
            if (tradingHistory == null)
            {
                throw new ArgumentNullException(nameof(tradingHistory));
            }

            if (auctioneer == null)
            {
                throw new ArgumentNullException(nameof(auctioneer));
            }

            if (trader == null)
            {
                throw new ArgumentNullException(nameof(trader));
            }

            if (lot == null)
            {
                throw new ArgumentNullException(nameof(lot));
            }

            return(new TradingHistory
            {
                AuctioneerId = auctioneer.Id,
                LotId = lot.Id,
                TraderId = trader.Id,
                BidTime = tradingHistory.BidTime,
                BidOrder = tradingHistory.BidOrder,
                RecordedPrice = tradingHistory.RecordedPrice,
                Auctioneer = auctioneer,
                Lot = lot,
                Trader = trader
            });
        }
Пример #4
0
        public async Task <TradingHistoryModel> AddTradingHistory(TradingHistoryModel history)
        {
            if (history == null)
            {
                throw new ArgumentNullException(nameof(history));
            }

            using (var db = new AuctionContext())
            {
                //var trader = db.Traders.FirstOrDefault(t => t.Id == (history.Trader == null ? 0 : history.Trader.Id));
                //var lot = db.Lots.FirstOrDefault(L => L.Id == (history.Lot == null ? 0 : history.Lot.Id));
                //var Aunctioneer = db.Lots.FirstOrDefault(a => a.Id == (history.Auctioneer == null ? 0 : history.Auctioneer.Id));

                var trader     = db.Traders.FirstOrDefault(t => t.Id == history.Trader.Id);
                var lot        = db.Lots.FirstOrDefault(L => L.Id == history.Lot.Id);
                var auctioneer = db.Auctioneers.FirstOrDefault(a => a.Id == history.Auctioneer.Id);

                var newRecord = new TradingHistory
                {
                    Lot           = lot,
                    Trader        = trader,
                    Auctioneer    = auctioneer,
                    LotId         = lot.Id,
                    TraderId      = trader.Id,
                    AuctioneerId  = auctioneer.Id,
                    BidTime       = history.BidTime,
                    BidOrder      = history.BidOrder,
                    RecordedPrice = history.RecordedPrice,
                };

                db.TradingHistories.Add(newRecord);
                await db.SaveChangesAsync();

                history.Id = newRecord.Id;
                return(history);
            }
        }
Пример #5
0
        public async Task <IHttpActionResult> Paginate(TradingHistoryModel tradingHistoryModel)
        {
            var user = await this.usersService.GetUserByCurrentSessionToken();

            var entriesQuery = await this.entriesService.GetAllQueryableByCriteriaAsync(w => w.Section.Account.UserId == user.UserId);

            if (tradingHistoryModel.AccountId.HasValue)
            {
                entriesQuery = entriesQuery.Where(w => w.Section.AccountId == tradingHistoryModel.AccountId);
            }

            if (tradingHistoryModel.StartDate.HasValue)
            {
                entriesQuery = entriesQuery.Where(w => DbFunctions.TruncateTime(w.DateAndTime) >= DbFunctions.TruncateTime(tradingHistoryModel.StartDate));
            }

            if (tradingHistoryModel.EndDate.HasValue)
            {
                entriesQuery = entriesQuery.Where(w => DbFunctions.TruncateTime(w.DateAndTime) <= DbFunctions.TruncateTime(tradingHistoryModel.EndDate));
            }

            if (tradingHistoryModel.ParityId.HasValue)
            {
                entriesQuery = entriesQuery.Where(w => w.ParityId == tradingHistoryModel.ParityId);
            }

            if (tradingHistoryModel.DayOfWeek != null)
            {
                var dayOfWeek = (int)tradingHistoryModel.DayOfWeek;
                entriesQuery = entriesQuery.Where(w => SqlFunctions.DatePart("dw", w.DateAndTime) == dayOfWeek);
            }

            entriesQuery = entriesQuery.Where(w => !w.Deleted && !w.Section.Deleted && !w.Section.Account.Deleted);

            var total = entriesQuery.Count();

            var entries = await entriesQuery
                          .OrderByDescending(w => w.DateAndTime)
                          .Skip((tradingHistoryModel.Page - 1) * tradingHistoryModel.Count)
                          .Take(tradingHistoryModel.Count)
                          .Include(w => w.Parity)
                          .ToListAsync();

            var tradingHistoryDtos = new List <TradingHistoryDto>();

            foreach (var entry in entries)
            {
                tradingHistoryDtos.Add(new TradingHistoryDto()
                {
                    DateAndTime  = entry.DateAndTime,
                    ParityId     = entry.ParityId,
                    ParityName   = entry.Parity.Name,
                    AccountId    = entry.Section.AccountId,
                    Value        = entry.Value,
                    CurrencyName = entry.Section.Account.Currency.Name,
                    Result       = entry.Win ? (entry.Value * (entry.Payout / 100)) : -entry.Value,
                    Equity       = entry.Win ? ((entry.Value * (entry.Payout / 100)) + entry.Value) : 0
                });
            }

            if (tradingHistoryDtos.Count() == 0)
            {
                return(this.Ok());
            }

            var totalProfit     = entriesQuery.Sum(entry => entry.Win ? (entry.Value * (entry.Payout / 100)) : -entry.Value);
            var totalEquity     = entriesQuery.Sum(entry => entry.Win ? ((entry.Value * (entry.Payout / 100)) + entry.Value) : 0);
            var totalInvestment = entriesQuery.Sum(entry => entry.Value);
            var currencyName    = tradingHistoryDtos.FirstOrDefault()?.CurrencyName;

            return(this.Ok(new
            {
                Results = tradingHistoryDtos,
                Profit = totalProfit,
                Equity = totalEquity,
                Investment = totalInvestment,
                CurrencyName = currencyName,
                Total = total
            }));
        }