public void IncrementAmount(AssetCost assetCost) { Amount += assetCost.RemainingQuantity; PreviousCosts.Add(assetCost); if (Amount - assetCost.RemainingQuantity < 0 && Amount >= 0) { ResolvedBreachCount++; if (assetCost.Item is ITrade trade) { var buyText = trade.IsBuyer ? "BUY" : "SELL"; _breachLogs.Add($"debreach {trade.Time} {trade.GetTimeReadable():G} {trade.GetSymbol()} {buyText} {Name} {trade.Commission}"); } } }
public ImmutableArray <CapitalGain> Process(ITrade trade) { var baseCcy = trade.Base; var baseAsset = GetAsset(baseCcy); var quoteCcy = trade.Quote; var quoteAsset = GetAsset(quoteCcy); if (trade.IsBuyer) { // we are selling trade.GetQuoteQuantity of quoteAsset. // to calculate capital gain we need to price this quantity at the time it has been bought var proceed = _fiatConversion.GetProceed(trade); var assetCost = new AssetCost(trade.Time, trade.Quantity, proceed / trade.Quantity, trade); baseAsset.IncrementAmount(assetCost); return(quoteAsset.DecrementAmount(trade.GetQuoteQuantity(), trade) .Select(t => { var multiplier = t.RemainingQuantity / trade.GetQuoteQuantity(); return new CapitalGain { Asset = quoteCcy, SoldTime = trade.Time, BoughtTime = t.Time, Quantity = t.RemainingQuantity, Cost = t.CostPerUnit * t.RemainingQuantity, Proceed = proceed * multiplier, BoughtTag = GetTag(t.Item) }; }) .Concat(GetFeeCapitalGains(trade)) .ToImmutableArray()); } else { // we are selling trade.Quantity of baseAsset. // to calculate capital gain we need to price this quantity at the time it has been bought var proceed = _fiatConversion.GetProceed(trade); var assetCost = new AssetCost(trade.Time, trade.GetQuoteQuantity(), proceed / trade.GetQuoteQuantity(), trade); quoteAsset.IncrementAmount(assetCost); return(baseAsset.DecrementAmount(trade.Quantity, trade) .Select(t => { var multiplier = t.RemainingQuantity / trade.Quantity; return new CapitalGain { Asset = baseCcy, SoldTime = trade.Time, BoughtTime = t.Time, Quantity = t.RemainingQuantity, Cost = t.CostPerUnit * t.RemainingQuantity, Proceed = proceed * multiplier, BoughtTag = GetTag(t.Item) }; }) .Concat(GetFeeCapitalGains(trade)) .ToImmutableArray()); } }