using System; using System.Collections.Generic; using System.Linq; using BrokerInterfaceETrade.Api.Models; namespace BrokerInterfaceETradeServer.Models { public class ETradeAccount { public string Name { get; } public string Id { get; } public string IdKey { get; } public string InstitutionType { get; } public double AccountValue { get; private set; } public double BuyingPower { get; private set; } public double AvailableFunds { get; private set; } public double RealizedProfitLoss { get; set; } public double UnRealizedProfitLoss { get; set; } public BalanceResponse BalanceDetail { get; set; } public List Positions { get; private set; } public List Orders { get; set; } public ETradeAccount(eAccount account) { Name = string.IsNullOrWhiteSpace(account.accountName) ? account.accountDesc : account.accountName; Id = account.accountId; IdKey = account.accountIdKey; InstitutionType = account.institutionType; Orders = new List(); Positions = new List(); } public bool UpdateAccount2(BalanceResponse balance, PortfolioResponse portfolio) { bool issues = false; BalanceDetail = balance; //P/L Values need further ressearch RealizedProfitLoss = portfolio.Totals.todaysGainLoss; UnRealizedProfitLoss = portfolio.Totals.totalGainLoss; //When there are no positons if (portfolio.AccountPortfolio == null) return issues; //Update Positions foreach (var position in portfolio.AccountPortfolio[0].Position) { if (position.costPerShare == 0) { issues = true; } if (Positions == null) { Positions.Add(position); } else { var positionIndex = Positions.FindIndex(p => p.Product.symbol == position.Product.symbol); if (positionIndex < 0) Positions.Add(position); else { Positions[positionIndex].costPerShare = position.costPerShare; Positions[positionIndex].quantity = position.quantity; } } } return issues; } public int PortfolioRefreshIssues { get; set; } public void AddPositions(List positions ) { Positions = positions; } public int AddOrder(OrderWrapper order) { Orders.Add(order); return getOrderIndex(order.OrderId); } public void ConvertOrder(OrderWrapper order, string newOrderId) { var oi = Orders.FindIndex(o => o.OrderId == order.OrderId); if (oi < 0) { //This should never happen order.OrderId = newOrderId; Orders.Add(order); } else { order.LocalOrderId = order.OrderId; order.OrderId = newOrderId; order.LocalOrderTriggerType = null; Orders[oi] = order; } } public int getOrderIndex(string orderId ) { return Orders.FindIndex(o => o.OrderId == orderId); } public List getOrders(bool includeLocalOrders , string status, DateTime filter) { if (includeLocalOrders) return Orders.Where(o => o.Status == status && o.PlacedTime.Date < filter.Date).ToList(); else return Orders.Where(o => !o.IsLocalOrder() && o.Status == status && o.PlacedTime.Date < filter.Date).ToList(); } public void updatePositionRealizedProfitLoss( OrderWrapper order ) { var positionIndex = Positions.FindIndex(p => p.Product.symbol == order.Symbol); if (positionIndex < 0) return; if (order.OrderAction == "BUY_TO_COVER") { Positions[positionIndex].RealizedProfitLoss += ((Positions[positionIndex].costPerShare - order.AverageExecutionPrice) * order.FilledQuantity).Value; } else { Positions[positionIndex].RealizedProfitLoss += ((order.AverageExecutionPrice - Positions[positionIndex].costPerShare) * order.FilledQuantity).Value; } } public ePosition GetPosition(string symbol) { return Positions.FirstOrDefault(p => p.Product.symbol == symbol); } public OrderWrapper getOrder(string orderId) { return Orders.FirstOrDefault(o => o.OrderId == orderId); } } }