示例#1
0
        public void CashFlowValueWithNoTrades()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(3d, 2d, 5d, 4d, 7d, 6d, 7d, 8d, 5d, 6d);
            var        cashFlow         = new CashFlow(sampleTimeSeries, new TradingRecord());

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(4), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(7), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(9), 1);
        }
示例#2
0
        public void cashFlowValueWithNoTrades()
        {
            ITimeSeries sampleTimeSeries = new MockTimeSeries(3, 2, 5, 4, 7, 6, 7, 8, 5, 6);
            CashFlow    cashFlow         = new CashFlow(sampleTimeSeries, new BaseTradingRecord());

            Assert.AreEqual(cashFlow.GetValue(4), 1);
            Assert.AreEqual(cashFlow.GetValue(7), 1);
            Assert.AreEqual(cashFlow.GetValue(9), 1);
        }
示例#3
0
        public void CashFlowBuyWithOnlyOneTrade()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(1d, 2d);
            var        tradingRecord    = new TradingRecord(Order.BuyAt(0), Order.SellAt(1));

            var cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(1), 2);
        }
示例#4
0
        public void cashFlowBuyWithOnlyOneTrade()
        {
            ITimeSeries    sampleTimeSeries = new MockTimeSeries(1, 2);
            ITradingRecord tradingRecord    = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(1, sampleTimeSeries));

            CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(cashFlow.GetValue(0), 1);
            Assert.AreEqual(cashFlow.GetValue(1), 2);
        }
示例#5
0
        /**
         * Calculates the Maximum drawdown from a cash flow over a series.
         * @param series the time series
         * @param cashFlow the cash flow
         * @return the Maximum drawdown from a cash flow over a series
         */
        private decimal CalculateMaximumDrawdown(ITimeSeries series, CashFlow cashFlow)
        {
            decimal MaximumDrawdown = Decimals.Zero;
            decimal MaxPeak         = Decimals.Zero;

            if (!series.IsEmpty())
            {
                // The series is not empty
                for (int i = series.GetBeginIndex(); i <= series.GetEndIndex(); i++)
                {
                    decimal value = cashFlow.GetValue(i);
                    if (value.IsGreaterThan(MaxPeak))
                    {
                        MaxPeak = value;
                    }

                    decimal drawdown = MaxPeak.Minus(value).DividedBy(MaxPeak);
                    if (drawdown.IsGreaterThan(MaximumDrawdown))
                    {
                        MaximumDrawdown = drawdown;
                    }
                }
            }
            return(MaximumDrawdown);
        }
示例#6
0
        public void reallyLongCashFlow()
        {
            int            size             = 1000000;
            ITimeSeries    sampleTimeSeries = new MockTimeSeries(Enumerable.Range(0, size).Select(_ => new MockBar(10)).Cast <IBar>().ToList());
            ITradingRecord tradingRecord    = new BaseTradingRecord(Order.buyAt(0, sampleTimeSeries), Order.sellAt(size - 1, sampleTimeSeries));
            CashFlow       cashFlow         = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(cashFlow.GetValue(size - 1), 1);
        }
示例#7
0
        public void ReallyLongCashFlow()
        {
            const int  size             = 1000000;
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(Enumerable.Repeat((Tick)GenerateTick.From(10), size).ToList());
            var        tradingRecord    = new TradingRecord(Order.BuyAt(0), Order.SellAt(size - 1));
            var        cashFlow         = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(size - 1), 1);
        }
示例#8
0
        public void CashFlowWithConstrainedSeries()
        {
            var series        = GenerateTimeSeries.From(5d, 6d, 3d, 7d, 8d, 6d, 10d, 15d, 6d);
            var constrained   = series.Subseries(4, 8);
            var tradingRecord = new TradingRecord(Order.BuyAt(4), Order.SellAt(5), Order.BuyAt(6), Order.SellAt(8));

            var flow = new CashFlow(constrained, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(flow.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(1), 1);
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(2), 1);
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(3), 1);
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(4), 1);
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(5), "0.75");
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(6), "0.75");
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(7), "1.125");
            TaTestsUtils.AssertDecimalEquals(flow.GetValue(8), "0.45");
        }
示例#9
0
        public void cashFlowValue()
        {
            // First sample series
            ITimeSeries    sampleTimeSeries = new MockTimeSeries(3, 2, 5, 1000, 5000, 0.0001M, 4M, 7M, 6M, 7, 8, 5, 6);
            ITradingRecord tradingRecord    = new BaseTradingRecord(
                Order.buyAt(0, sampleTimeSeries), Order.sellAt(2, sampleTimeSeries),
                Order.buyAt(6, sampleTimeSeries), Order.sellAt(8, sampleTimeSeries),
                Order.buyAt(9, sampleTimeSeries), Order.sellAt(11, sampleTimeSeries));

            CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(1, cashFlow.GetValue(0));
            Assert.AreEqual(2M / 3, cashFlow.GetValue(1));
            Assert.AreEqual(5M / 3, cashFlow.GetValue(2));
            Assert.AreEqual(5M / 3, cashFlow.GetValue(3));
            Assert.AreEqual(5M / 3, cashFlow.GetValue(4));
            Assert.AreEqual(5M / 3, cashFlow.GetValue(5));
            Assert.AreEqual(5M / 3, cashFlow.GetValue(6));
            Assert.AreEqual(2.9166666666666666666666666667M, cashFlow.GetValue(7)); // 5M / 3M * 7M / 4M
            Assert.AreEqual(5M / 3 * 6M / 4, cashFlow.GetValue(8));
            Assert.AreEqual(5M / 3 * 6M / 4, cashFlow.GetValue(9));
            Assert.AreEqual(2.8571428571428571428571428572M, cashFlow.GetValue(10)); // 5M / 3 * 6M / 4 * 8M / 7
            Assert.AreEqual(1.7857142857142857142857142858M, cashFlow.GetValue(11)); // 5M / 3 * 6M / 4 * 5M / 7
            Assert.AreEqual(1.7857142857142857142857142858M, cashFlow.GetValue(12)); // 5M / 3 * 6M / 4 * 5M / 7

            // Second sample series
            sampleTimeSeries = new MockTimeSeries(5, 6, 3, 7, 8, 6, 10, 15, 6);
            tradingRecord    = new BaseTradingRecord(
                Order.buyAt(4, sampleTimeSeries), Order.sellAt(5, sampleTimeSeries),
                Order.buyAt(6, sampleTimeSeries), Order.sellAt(8, sampleTimeSeries));

            CashFlow flow = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(flow.GetValue(0), 1);
            Assert.AreEqual(flow.GetValue(1), 1);
            Assert.AreEqual(flow.GetValue(2), 1);
            Assert.AreEqual(flow.GetValue(3), 1);
            Assert.AreEqual(flow.GetValue(4), 1);
            Assert.AreEqual(flow.GetValue(5), 0.75M);
            Assert.AreEqual(flow.GetValue(6), 0.75M);
            Assert.AreEqual(flow.GetValue(7), 1.125M);
            Assert.AreEqual(flow.GetValue(8), 0.45M);
        }
示例#10
0
        public void DynamicAddTrade()
        {
            var data = new double[] { 2, 1, 3, 5, 6, 3, 20, 20, 3 };

            // We will rely on this being correct as it duplicates tests above...
            TimeSeries confirmedSeries        = GenerateTimeSeries.From(data);
            var        confirmedTradingRecord = new TradingRecord(Order.BuyAt(0), Order.SellAt(2), Order.BuyAt(3), Order.SellAt(4), Order.BuyAt(5), Order.SellAt(6), Order.BuyAt(7), Order.SellAt(8));
            // use these results to check against the dynamically added Trades
            var confirmedCashFlow = new CashFlow(confirmedSeries, confirmedTradingRecord);

            TimeSeries sampleTimeSeries = new TimeSeries();
            var        tradingRecord    = new TradingRecord();
            var        cashFlow         = new CashFlow(sampleTimeSeries, tradingRecord);

            for (var i = 0; i < data.Length; i++)
            {
                sampleTimeSeries.AddTick(GenerateTick.From((new LocalDateTime()).WithMillisOfSecond(i), data[i]));
                switch (i)
                {
                case 0:     // buy
                case 3:     // buy
                case 5:     // buy
                case 7:     // buy
                    tradingRecord.Enter(i);
                    break;

                case 2:     // sell
                case 4:     // sell
                case 6:     // sell
                case 8:     // sell
                    tradingRecord.Exit(i);
                    cashFlow.AddTrade(tradingRecord.LastTrade);
                    break;

                default:
                    // don't do anything
                    break;
                }
            }

            // Check all the data off...
            Assert.Multiple(() =>
            {
                for (var i = 0; i < data.Length; i++)
                {
                    TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(i), confirmedCashFlow.GetValue(i).ToDouble());
                }
            });
        }
示例#11
0
        public void CashFlowWithSellAndBuyOrders()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(2, 1, 3, 5, 6, 3, 20);
            var        tradingRecord    = new TradingRecord(Order.BuyAt(0), Order.SellAt(1), Order.BuyAt(3), Order.SellAt(4), Order.SellAt(5), Order.BuyAt(6));

            var cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(1), "0.5");
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(2), "0.5");
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(3), "0.5");
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(4), "0.6");
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(5), "0.6");
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(6), "0.09");
        }
示例#12
0
        public void CashFlowShortSell()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(1, 2, 4, 8, 16, 32);
            var        tradingRecord    = new TradingRecord(Order.BuyAt(0), Order.SellAt(2), Order.SellAt(2), Order.BuyAt(4), Order.BuyAt(4), Order.SellAt(5));

            var cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(1), 2);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(2), 4);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(3), 2);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(4), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(5), 2);
        }
示例#13
0
        public void CashFlowValueWithTwoTradesAndLongTimeWithoutOrders()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(1d, 2d, 4d, 8d, 16d, 32d);
            var        tradingRecord    = new TradingRecord(Order.BuyAt(1), Order.SellAt(2), Order.BuyAt(4), Order.SellAt(5));

            var cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(1), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(2), 2);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(3), 2);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(4), 2);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(5), 4);
        }
示例#14
0
        public void cashFlowSell()
        {
            ITimeSeries    sampleTimeSeries = new MockTimeSeries(1, 2, 4, 8, 16, 32);
            ITradingRecord tradingRecord    = new BaseTradingRecord(Order.sellAt(2, sampleTimeSeries), Order.buyAt(3, sampleTimeSeries));

            CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(cashFlow.GetValue(0), 1);
            Assert.AreEqual(cashFlow.GetValue(1), 1);
            Assert.AreEqual(cashFlow.GetValue(2), 1);
            Assert.AreEqual(cashFlow.GetValue(3), 0.5M);
            Assert.AreEqual(cashFlow.GetValue(4), 0.5M);
            Assert.AreEqual(cashFlow.GetValue(5), 0.5M);
        }
示例#15
0
        public void cashFlowWithSellAndBuyOrders()
        {
            ITimeSeries    sampleTimeSeries = new MockTimeSeries(2, 1, 3, 5, 6, 3, 20);
            ITradingRecord tradingRecord    = new BaseTradingRecord(
                Order.buyAt(0, sampleTimeSeries), Order.sellAt(1, sampleTimeSeries),
                Order.buyAt(3, sampleTimeSeries), Order.sellAt(4, sampleTimeSeries),
                Order.sellAt(5, sampleTimeSeries), Order.buyAt(6, sampleTimeSeries));

            CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(cashFlow.GetValue(0), 1);
            Assert.AreEqual(cashFlow.GetValue(1), 0.5M);
            Assert.AreEqual(cashFlow.GetValue(2), 0.5M);
            Assert.AreEqual(cashFlow.GetValue(3), 0.5M);
            Assert.AreEqual(cashFlow.GetValue(4), 0.6M);
            Assert.AreEqual(cashFlow.GetValue(5), 0.6M);
            Assert.AreEqual(cashFlow.GetValue(6), 0.09M);
        }
示例#16
0
        /// <summary>
        /// Calculates the maximum drawdown from a cash flow over a series. </summary>
        /// <param name="series"> the time series </param>
        /// <param name="cashFlow"> the cash flow </param>
        /// <returns> the maximum drawdown from a cash flow over a series </returns>
        private Decimal CalculateMaximumDrawdown(TimeSeries series, CashFlow cashFlow)
        {
            var maximumDrawdown = Decimal.Zero;
            var maxPeak         = Decimal.Zero;

            for (var i = series.Begin; i <= series.End; i++)
            {
                var value = cashFlow.GetValue(i);
                if (value.IsGreaterThan(maxPeak))
                {
                    maxPeak = value;
                }

                var drawdown = maxPeak.Minus(value).DividedBy(maxPeak);
                if (drawdown.IsGreaterThan(maximumDrawdown))
                {
                    maximumDrawdown = drawdown;
                }
            }
            return(maximumDrawdown);
        }
示例#17
0
        public void cashFlowValueWithTwoTradesAndLongTimeWithoutOrders()
        {
            ITimeSeries    sampleTimeSeries = new MockTimeSeries(1, 2, 4, 8, 16, 32);
            ITradingRecord tradingRecord    = new BaseTradingRecord(
                Order.buyAt(1, sampleTimeSeries), Order.sellAt(2, sampleTimeSeries),
                Order.buyAt(4, sampleTimeSeries), Order.sellAt(5, sampleTimeSeries));

            CashFlow cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            Assert.AreEqual(cashFlow.GetValue(0), 1);
            Assert.AreEqual(cashFlow.GetValue(1), 1);
            Assert.AreEqual(cashFlow.GetValue(2), 2);
            Assert.AreEqual(cashFlow.GetValue(3), 2);
            Assert.AreEqual(cashFlow.GetValue(4), 2);
            Assert.AreEqual(cashFlow.GetValue(5), 4);
        }
示例#18
0
        public void CashFlowValue()
        {
            TimeSeries sampleTimeSeries = GenerateTimeSeries.From(3d, 2d, 5d, 1000d, 5000d, 0.0001d, 4d, 7d, 6d, 7d, 8d, 5d, 6d);
            var        tradingRecord    = new TradingRecord(Order.BuyAt(0), Order.SellAt(2), Order.BuyAt(6), Order.SellAt(8), Order.BuyAt(9), Order.SellAt(11));

            var cashFlow = new CashFlow(sampleTimeSeries, tradingRecord);

            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(1), 2d / 3);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(2), 5d / 3);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(3), 5d / 3);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(4), 5d / 3);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(5), 5d / 3);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(6), 5d / 3);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(7), 5d / 3 * 7d / 4);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(8), 5d / 3 * 6d / 4);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(9), 5d / 3 * 6d / 4);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(10), 5d / 3 * 6d / 4 * 8d / 7);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(11), 5d / 3 * 6d / 4 * 5d / 7);
            TaTestsUtils.AssertDecimalEquals(cashFlow.GetValue(12), 5d / 3 * 6d / 4 * 5d / 7);
        }