using System; using System.Collections.Generic; using System.Linq; using BrokerInterfaceETrade.Api.Models; namespace BrokerInterfaceETrade.Api { public class ETradeAccount { public string Name { get; } public string Id { get; } public string IdKey { get; } public string Description { get; } public string Mode { get; } public string QuoteMode { get { if (this.BalanceDetail?.quoteMode == null) return "None"; switch (this.BalanceDetail.quoteMode) { case 0: return "Realtime"; case 1: return "Delayed"; case 2: return "Closing"; case 3: return "AHT Realtime"; case 4: return "AHT Before Open"; case 5: return "AHT Closing"; default: return "None"; } } } public bool Linked { get; set; } public string Status { 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; Status = account.accountStatus; Mode = account.accountMode; Description = account.accountDesc; Orders = new List(); Positions = new List(); } public int ManagedOrderCount() { return Orders.Where(o => o.IsLocalOrder()).Count(); } public bool UpdateAccount2(BalanceResponse balance, PortfolioResponse portfolio) { bool issues = false; BalanceDetail = balance; //When there are no positons if (portfolio == null || portfolio.AccountPortfolio == null) { RealizedProfitLoss = 0; UnRealizedProfitLoss = 0; return issues; } else { //P/L Values need further ressearch RealizedProfitLoss = portfolio.Totals.todaysGainLoss; UnRealizedProfitLoss = portfolio.Totals.totalGainLoss; } //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); } } }