示例#1
0
        public void TodaysPnlResetsEvenWhenTrackerIsNotOpen()
        {
            var trade = new Trade();
            var date  = new DateTime(2000, 1, 1);

            trade.Orders = new List <Order>
            {
                new Order {
                    Instrument = _inst, Quantity = -10, FXRateToBase = 1, Price = 100, BuySell = "BUY", TradeDate = date
                },
                new Order {
                    Instrument = _inst, Quantity = 5, FXRateToBase = 1, Price = 95, BuySell = "SELL", TradeDate = date
                }
            };

            var tracker = new TradeTracker(trade, 1);

            foreach (Order o in trade.Orders)
            {
                tracker.AddOrder(o);
            }

            var data = new Dictionary <int, TimeSeries> {
                { 1, TimeSeriesGenerator.GenerateData(date, date.AddDays(1), 90) }
            };

            foreach (TimeSeries ts in data.Values)
            {
                ts.ProgressTo(date);
            }

            tracker.Update(date, data, null);

            Assert.AreEqual(5 * 5 + 10 * 5, tracker.TodaysPnL);

            data[1].ProgressTo(date.AddDays(1));
            tracker.Update(date.AddDays(1), data, null);
            Assert.AreEqual(0, tracker.TodaysPnL);
        }
示例#2
0
        public void TotalPnlLongIsTheSumOfRealizedAndUnrealizedPnlForCurrencyPositions()
        {
            var trade      = new Trade();
            var date       = new DateTime(2000, 1, 1);
            var fxCurrency = new Currency {
                ID = 2, Name = "CAD"
            };

            trade.FXTransactions = new List <FXTransaction>
            {
                new FXTransaction {
                    FXCurrency = fxCurrency, Quantity = 1000, Proceeds = 1500, Cost = -1500
                },
                new FXTransaction {
                    FXCurrency = fxCurrency, Quantity = -500, Proceeds = -850, Cost = 850
                },
            };

            var tracker = new TradeTracker(trade, 1);

            foreach (FXTransaction fxt in trade.FXTransactions)
            {
                tracker.AddFXTransaction(fxt);
            }

            var data = new Dictionary <int, TimeSeries> {
                { 2, TimeSeriesGenerator.GenerateData(date, date, 1.55m) }
            };

            foreach (TimeSeries ts in data.Values)
            {
                ts.ProgressTo(date);
            }

            tracker.Update(date, new Dictionary <int, TimeSeries>(), data);

            Assert.AreEqual(100 + 500 * (1.55m - 1.5m), tracker.TotalPnL);
        }
示例#3
0
        public void TotalPnlLongIsTheSumOfRealizedAndUnrealizedPnlForShortPositions()
        {
            var trade = new Trade();
            var date  = new DateTime(2000, 1, 1);

            trade.Orders = new List <Order>
            {
                new Order {
                    Instrument = _inst, Quantity = -10, FXRateToBase = 1, Price = 100, BuySell = "BUY", TradeDate = date
                },
                new Order {
                    Instrument = _inst, Quantity = 5, FXRateToBase = 1, Price = 95, BuySell = "SELL", TradeDate = date
                }
            };

            var tracker = new TradeTracker(trade, 1);

            foreach (Order o in trade.Orders)
            {
                tracker.AddOrder(o);
            }

            var data = new Dictionary <int, TimeSeries> {
                { 1, TimeSeriesGenerator.GenerateData(date, date, 90) }
            };

            foreach (TimeSeries ts in data.Values)
            {
                ts.ProgressTo(date);
            }

            tracker.Update(date, data, null);

            Assert.AreEqual(5 * 5 + 10 * 5, tracker.TotalPnL);
            Assert.AreEqual(5 * 5 + 10 * 5, tracker.TotalPnlShort);
        }
示例#4
0
        public void CapitalUsageCorrectlyTracked()
        {
            var inst2 = new Instrument {
                ID = 2, Multiplier = 1, AssetCategory = AssetClass.Stock
            };

            var order1 = new Order
            {
                Instrument   = _inst,
                InstrumentID = _inst.ID,
                Multiplier   = 1,
                FXRateToBase = 1,
                Price        = 10,
                Quantity     = 100,
                CurrencyID   = 1,
                BuySell      = "BUY",
                IsReal       = true,
                TradeDate    = new DateTime(2000, 1, 2)
            };

            var order2 = new Order
            {
                Instrument   = inst2,
                InstrumentID = inst2.ID,
                Multiplier   = 1,
                FXRateToBase = 1,
                Price        = 20,
                Quantity     = -100,
                CurrencyID   = 1,
                BuySell      = "SELL",
                IsReal       = true,
                TradeDate    = new DateTime(2000, 1, 3, 12, 0, 0)
            };

            var order3 = new Order
            {
                Instrument   = inst2,
                InstrumentID = inst2.ID,
                Multiplier   = 1,
                FXRateToBase = 1,
                Price        = 19,
                Quantity     = 100,
                CurrencyID   = 1,
                BuySell      = "BUY",
                IsReal       = true,
                TradeDate    = new DateTime(2000, 1, 3, 13, 0, 0)
            };

            var trade = new Trade
            {
                Orders = new List <Order> {
                    order1, order2, order3
                }
            };

            var trades = new List <Trade> {
                trade
            };

            _data = new Dictionary <int, TimeSeries>
            {
                { 1, TimeSeriesGenerator.GenerateData(new DateTime(2000, 1, 1), new DateTime(2000, 2, 1), 11) },
                { 2, TimeSeriesGenerator.GenerateData(new DateTime(2000, 1, 1), new DateTime(2000, 2, 1), 20) }
            };

            var tracker = new PortfolioTracker(_data, _fxData, trades, "test", new DateTime(2000, 1, 1));

            var date = new DateTime(2000, 1, 1);

            foreach (TimeSeries ts in _data.Values)
            {
                ts.ProgressTo(date);
            }

            tracker.ProcessItemsAt(date);
            tracker.OnDayClose(date, 10000);

            Assert.AreEqual(0, tracker.Capital.Gross.Last());

            date = date.AddDays(1);
            foreach (TimeSeries ts in _data.Values)
            {
                ts.ProgressTo(date);
            }
            tracker.ProcessItemsAt(date);
            tracker.OnDayClose(date, 10000);

            Assert.AreEqual(10 * 100, tracker.Capital.Gross.Last());
            Assert.AreEqual(10 * 100, tracker.Capital.Long.Last());

            date = date.AddDays(1);
            foreach (TimeSeries ts in _data.Values)
            {
                ts.ProgressTo(date);
            }
            tracker.ProcessItemsAt(date);
            tracker.OnDayClose(date, 10000);

            Assert.AreEqual(11 * 100 + 20 * 100, tracker.Capital.Gross.Last());
            Assert.AreEqual(11 * 100, tracker.Capital.Long.Last());
            Assert.AreEqual(20 * 100, tracker.Capital.Short.Last());
        }