Ejemplo n.º 1
0
        public object Clone()
        {
            var dico = AllocationTools <AllocationElement> .DeepCopy(Data);

            var        fees = (AllocationElement)Fees.Clone();
            Allocation res  = new Allocation(CcyRef, dico, fees)
            {
                Total = (Price)Total.Clone()
            };

            return(res);
        }
        private void AddAllocationToHistory(Allocation alloc, FXMarket fx)
        {
            Allocation newAlloc = (Allocation)alloc.Clone();

            newAlloc.CancelFee();
            newAlloc.Update(fx);
            if (History.ContainsKey(fx.Date))
            {
                History[fx.Date] = newAlloc;
            }
            else
            {
                History.Add(fx.Date, newAlloc);
            }
        }
Ejemplo n.º 3
0
        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 List <Tuple <DateTime, double> > GetTimeSeries(ITimeSeriesKey itsk, bool isIndex, DateTime startDate)
        {
            List <Tuple <DateTime, double> > res = new List <Tuple <DateTime, double> >();

            if (itsk.GetKeyType() == TimeSeriesKeyType.AllocationHistory)
            {
                double                 value;
                double                 lastTSValue = Double.NaN;
                Allocation             prevAlloc   = null;
                Currency               ccyRef      = itsk.GetCurrencyRef();
                IEnumerable <DateTime> DateList    = History.Keys.Where(x => x >= startDate);
                DateList = itsk.GetFrequency().GetSchedule(DateList.First(), DateList.Last(), true);
                foreach (DateTime date in DateList)
                {
                    History.TryGetValue(date, out Allocation alloc);
                    if (alloc != null)
                    {
                        if (!isIndex)
                        {
                            double amount = History[date].Total.Amount;
                            value = amount;
                        }
                        else
                        {
                            if (Double.IsNaN(lastTSValue))
                            {
                                value = 10000;
                            }
                            else
                            {
                                double returnAlloc = History[date].GetReturn(prevAlloc, ccyRef);
                                value = Double.IsNaN(lastTSValue) ? 10000 : lastTSValue * (1 + returnAlloc);
                            }
                            prevAlloc   = (Allocation)History[date].Clone();
                            lastTSValue = value;
                        }
                        res.Add(new Tuple <DateTime, double>(date, value));
                    }
                }
            }
            return(res);
        }
        private void AddTransaction(Transaction tx, DateTime nextTxDate, FXMarketHistory fxmh)
        {
            Allocation alloc = GetClosestAllocation(tx.Date, true);
            FXMarket   FX    = fxmh.GetArtificialFXMarket(tx.Date);

            alloc = alloc.AddTransaction(tx, FX);
            alloc.CalculateTotal(FX);
            if (History.ContainsKey(tx.Date))
            {
                tx.Date = tx.Date.AddSeconds(1);
            }
            History.Add(tx.Date, alloc);

            // update the same allocation for the following days
            List <DateTime> datesList = fxmh.ArtificialFXMarkets
                                        .Keys
                                        .Where(x => x > tx.Date && x < nextTxDate)
                                        .Select(x => x).ToList();

            foreach (DateTime date in datesList)
            {
                AddAllocationToHistory(alloc, fxmh.GetArtificialFXMarket(tx.Date));
            }
        }
Ejemplo n.º 6
0
        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);
        }