public double GetReturn(Allocation prevAlloc, Currency ccy = Currency.None) { if (ccy == Currency.None) { ccy = CcyRef; } double res = 0; foreach (CurrencyPair cp in prevAlloc.GetCurrencyPairs()) { double prevXR = prevAlloc.GetImpliedXChangeRate(cp); double nextXR = GetImpliedXChangeRate(cp); if (prevXR != 0 && nextXR != 0) { res += prevAlloc.GetElement(cp.Ccy1).Share *(nextXR / prevXR - 1); } } return(res); }
public Allocation AddTransaction(Transaction tx)//, FXMarket fxMarket) { Allocation res = (Allocation)Clone(); res.CancelFee(); //// Changing the Amounts // Received if ((tx.Type == TransactionType.Deposit && tx.Received.Ccy.IsFiat()) || tx.Type == TransactionType.Trade || tx.Type == TransactionType.Transfer) { AllocationElement RecElement = res.GetElement(tx.Received.Ccy); if (RecElement != null) { //AllocationElement allocIn = res.Dictionary[tx.Received.Ccy]; RecElement.Price.Amount += tx.Received.Amount; } else { res.Data.Add(new AllocationElement(tx.Received.Amount, tx.Received.Ccy)); } } if (tx.Type == TransactionType.Deposit && !tx.Received.Ccy.IsFiat()) { //Console.WriteLine(""); } //TODO: Find Transaction fees if (tx.Type == TransactionType.WithDrawal && tx.Paid.Ccy.IsFiat()) { AllocationElement PaidElmt = res.GetElement(tx.Paid.Ccy); if (PaidElmt != null) { PaidElmt.Price.Amount -= tx.Paid.Amount; } else { throw new Exception("Paid in unavailable currency"); } if (PaidElmt.Price.Amount < 0) { throw new Exception("Paid more than available"); } } // Paid if (tx.Type == TransactionType.Trade && tx.Paid.Ccy != Currency.None) { AllocationElement PaidElement = res.GetElement(tx.Paid.Ccy); if (PaidElement != null) { //AllocationElement alloc = res.Dictionary[tx.Paid.Ccy]; PaidElement.Price.Amount -= tx.Paid.Amount; } else { throw new Exception("Paid in unavailable currency"); } if (PaidElement.Price.Amount < 0) { throw new Exception("Paid more than available"); } } // Fees if (!tx.Fees.IsNull) { AllocationElement FeesElement = res.GetElement(tx.Fees.Ccy); if (FeesElement != null) { //AllocationElement fAlloc = res.Dictionary[tx.Fees.Ccy]; FeesElement.Price.Amount -= tx.Fees.Amount; if (FeesElement.Price.Amount < 0) { throw new Exception("Paid more than available (fees)"); } res.Fees = new AllocationElement(tx.Fees.Amount, tx.Fees.Ccy); } else { throw new Exception("Paid in unavailable currency (fees)"); } } else { res.Fees = new AllocationElement(0, Currency.None); } //res.Update(fxMarket); return(res); }