public void OpenBalance(Customer user, int currency) { //Check if user balance already exists var exists = context.UserBalance.Any(s => s.Currency == currency && s.Customer == user); if (exists) { throw new ArgumentException("User has ballance in this currency"); } var balance = new CustomerBalance() { Customer = user, Currency = currency, Balance = 0, }; var history = new BalanceHistory() { Currency = currency, Customer = user, Date = DateTime.Now, TrackedBalance = balance, Amount = 0 }; context.AddRange(balance, history); context.SaveChanges(); }
public void AddTransaction(Transaction transaction) { if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } var customer = transaction.Customer; var balance = context.UserBalance.Where(s => (s.Customer == customer) && s.Currency == transaction.Currency).FirstOrDefault(); //Balance is tracked and saved automatically balance.Balance += transaction.Amount; var history = new BalanceHistory() { Date = transaction.Date, Currency = transaction.Currency, Amount = balance.Balance, TrackedBalance = balance, Customer = customer, }; context.BalanceHistories.Add(history); context.Transactions.Add(transaction); context.SaveChanges(); }
private void SaveBalance(BalanceHistory balanceHistory, string name) { balanceHistory.Name = name; _db.Insert(balanceHistory); _log.LogInformation($"Saved new balance in database for: {name}"); _log.LogInformation("Adding balance to cache"); _lastBalances[name] = balanceHistory; }
public SendBalanceInfoCommand( BalanceHistory current, BalanceHistory lastBalance, IEnumerable <WalletBalance> walletBalances, string accountName) { Current = current; LastBalance = lastBalance; WalletBalances = walletBalances; AccountName = accountName; }
private async Task SaveBalance(BalanceHistory balanceHistory, string name) { var balanceHistories = _context.BalanceHistories; balanceHistory.Name = name; balanceHistories.Add(balanceHistory); _context.BalanceHistories.Add(balanceHistory); _log.LogInformation($"Saved new balance in database for: {name}"); _lastBalances[name] = balanceHistory; await _context.SaveChangesAsync(); }
public async Task <BalanceHistory> AddBalance(decimal balance, decimal dollarAmount, string name) { var balanceHistory = new BalanceHistory { DateTime = DateTime.Now, Balance = balance, DollarAmount = dollarAmount, Name = name }; _log.LogInformation($"Adding balance to database: {name} - {balance}"); await SaveBalance(balanceHistory, name); return(balanceHistory); }
public async Task <BalanceHistory> GetBalance24HoursAgo(string name) { var dateTime = DateTime.Now - TimeSpan.FromHours(24); BalanceHistory hour24Balance; var histories = _lastBalances.Values.Where(x => x.DateTime.Hour == dateTime.Hour && x.DateTime.Day == dateTime.Day && x.DateTime.Month == dateTime.Month && x.DateTime.Year == dateTime.Year && x.Name == name) .ToList(); if (histories.Count == 0) { _log.LogInformation($"Retrieving 24 hour balance from database for: {name}"); var collection = _context.BalanceHistories; var balanceHistories = await collection.Where(x => x.Name == name).OrderByDescending(x => x.DateTime).ToListAsync(); histories = balanceHistories.FindAll(x => x.DateTime.Hour == dateTime.Hour && x.DateTime.Day == dateTime.Day && x.DateTime.Month == dateTime.Month && x.DateTime.Year == dateTime.Year) .ToList(); if (!histories.Any()) { _log.LogWarning($"Could not find a 24 hour balance for: {name}"); hour24Balance = new BalanceHistory { Balance = 0, ReportingAmount = 0, ReportingCurrency = _config.ReportingCurrency, Name = name }; return(hour24Balance); } } var orderByDescending = histories.OrderByDescending(x => x.DateTime); hour24Balance = orderByDescending.FirstOrDefault(); return(hour24Balance); }
public BalanceHistory GetBalance24HoursAgo(string name) { var dateTime = DateTime.Now - TimeSpan.FromHours(24); BalanceHistory hour24Balance; var histories = _lastBalances.Values.Where(x => x.DateTime.Hour == dateTime.Hour && x.DateTime.Day == dateTime.Day && x.DateTime.Month == dateTime.Month && x.DateTime.Year == dateTime.Year && x.Name == name) .ToList(); if (histories.Count == 0) { _log.LogInformation($"Retrieving 24 hour balance from database for: {name}"); var liteCollection = _db.Database.GetCollection <BalanceHistory>(); var balanceHistories = liteCollection.Find(x => x.Name == name).OrderByDescending(x => x.DateTime).ToList(); histories = balanceHistories.FindAll(x => x.DateTime.Hour == dateTime.Hour && x.DateTime.Day == dateTime.Day && x.DateTime.Month == dateTime.Month && x.DateTime.Year == dateTime.Year) .ToList(); if (!histories.Any()) { _log.LogWarning($"Could not find a 24 hour balance for: {name}"); hour24Balance = new BalanceHistory { Balance = 0, DollarAmount = 0, Name = name }; return(hour24Balance); } } var orderByDescending = histories.OrderByDescending(x => x.DateTime); hour24Balance = orderByDescending.FirstOrDefault(); return(hour24Balance); }
public bool UpdateSession(string pair, double bid, double ask, string closeDate) { List <Trade> trades = this.SessionUser.Accounts.Primary.Trades.Where(x => x.Pair == pair).ToList(); foreach (var trade in trades) { trade.CloseDate = closeDate; if (trade.Long) { trade.ClosePrice = ask; if ((trade.ClosePrice > trade.TakeProfit) || (trade.ClosePrice < trade.StopLoss)) { closeTrade(trade); } } else { trade.ClosePrice = bid; if ((trade.ClosePrice < trade.TakeProfit) || (trade.ClosePrice > trade.StopLoss)) { closeTrade(trade); } } } string tradeDay = DateTime.Parse(closeDate).ToString("yyyy-MM-dd"); this.CurrentTime = tradeDay; var history = new BalanceHistory { Date = tradeDay, Amount = this.SessionUser.Accounts.Primary.NetAssetValue }; this.SessionUser.Accounts.Primary.UpdateHistory(history); return(true); }
public void AddBalanceHistory(BalanceHistory balanceHistory, CharterTrip charterTrip) { balanceHistory.CharterTripId = charterTrip.CharterTripId; _balanceHistoryRepository.Add(balanceHistory); _balanceHistoryRepository.SaveChanges(); }
public void DeleteBalanceHistory(BalanceHistory balanceHistory) { _balanceHistoryRepository.Remove(balanceHistory); _balanceHistoryRepository.SaveChanges(); }
public bool updateBalanceHistory(int orderID, int vendorID, string Type, bool edit = false, decimal amount = 0) { BalanceHistory balanceHistory = new BalanceHistory(); VMMixData mixData = new VMMixData(); decimal vendorCurrentInitialBalance = 0; var isDateExist = db.BalanceHistory.Where(x => x.Date.Day == DateTime.Now.Day && x.Date.Month == DateTime.Now.Month && x.Date.Year == DateTime.Now.Year && x.UserMasId == vendorID).ToList(); var userHistory = db.BalanceHistory.Where(x => x.UserMasId == vendorID).ToList(); BalanceHistory lastHistory = new BalanceHistory(); PurchaseOrder PurchaseData = new PurchaseOrder(); SalesOrder SalesData = new SalesOrder(); Payment PaymentOrCollectionData = new Payment(); if (Type == "purchase") { PurchaseData = db.PurchaseOrder.SingleOrDefault(x => x.Id == orderID); mixData.quantity = PurchaseData.OrderQuantity; mixData.unitPrice = PurchaseData.UnitPrice; } if (Type == "sales") { SalesData = db.SalesOrder.SingleOrDefault(x => x.Id == orderID); mixData.quantity = SalesData.OrderQuantity; mixData.unitPrice = SalesData.UnitPrice; } if ((Type == "payment" || Type == "collection") && edit == false) { PaymentOrCollectionData = db.Payment.SingleOrDefault(x => x.Id == orderID); mixData.amount = PaymentOrCollectionData.Amount; } else if ((Type == "payment" || Type == "collection") && edit == true) { PaymentOrCollectionData = db.Payment.SingleOrDefault(x => x.Id == orderID); mixData.amount = amount; } var update = false; if (userHistory.Count > 0) { if (isDateExist.Count > 0) { update = true; lastHistory = isDateExist[0]; balanceHistory = lastHistory; vendorCurrentInitialBalance = currentBalance(Type, lastHistory.initialBalance, mixData); } else { lastHistory = userHistory[userHistory.Count - 1]; vendorCurrentInitialBalance = currentBalance(Type, lastHistory.initialBalance, mixData); } } else { vendorCurrentInitialBalance = db.UserMas.SingleOrDefault(userMas => userMas.Id == vendorID).InitialBalance; vendorCurrentInitialBalance = currentBalance(Type, lastHistory.initialBalance, mixData); } balanceHistory.UserMasId = vendorID; balanceHistory.initialBalance = vendorCurrentInitialBalance; balanceHistory.Date = DateTime.Now; if (update) { db.Entry(balanceHistory).State = EntityState.Modified; db.SaveChanges(); } else { db.BalanceHistory.Add(balanceHistory); db.SaveChanges(); } return(true); }
public async Task <int> UpsetAsync(IEnumerable <BalanceHistoryEntity> entities) { var result = await BalanceHistory.UpsertRange(entities).On(e => new { e.OperationId, e.WalletId, e.Symbol }).NoUpdate().RunAsync(); return(result); }