示例#1
0
        public void TestOrderCanceledTriggeredAfterNotSendingOrder()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            using var monitoredManager = manager.Monitor();
            orderCreator.Limit(new SOrderParameters()).Send(20);
            manager.TriggerOrders(new Bar()
            {
                Low = 21
            });
            orderCreator.Limit(new SOrderParameters()).Send(20);
            manager.TriggerOrders(new Bar()
            {
                Low = 22
            });

            monitoredManager.Should()
            .Raise("OrderValidated")
            .WithArgs <OrderInfo>(info => info.Price == 20)
            .OnlyOnce();
            monitoredManager.Should().NotRaise("OrderCanceled");
            Assert.Equal(0, manager.StrategyInfo.MarketPosition);

            // If we don't send the order again, it should be canceled
            manager.TriggerOrders(new Bar()
            {
                Low = 23
            });
            monitoredManager.Should().Raise("OrderCanceled");
            Assert.Equal(0, manager.StrategyInfo.MarketPosition);
        }
示例#2
0
        public void TestOrderValidatedTriggeredAgain()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            using var monitoredManager = manager.Monitor();
            orderCreator.Limit(new SOrderParameters()).Send(20);
            manager.TriggerOrders(new Bar()
            {
                Low = 19
            });

            Assert.Equal(100, manager.StrategyInfo.MarketPosition);
            monitoredManager.Should()
            .Raise("OrderValidated")
            .WithArgs <OrderInfo>(info => info.Price == 20)
            .OnlyOnce()
            .Reset();

            // The same order is allowed to be sent again after being filled
            orderCreator.Limit(new SOrderParameters()).Send(20);
            monitoredManager.Should()
            .Raise("OrderValidated")
            .WithArgs <OrderInfo>(info => info.Price == 20)
            .OnlyOnce();
        }
示例#3
0
        public void TestNoPositions()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            Assert.Equal(0, manager.StrategyInfo.MarketPosition);
        }
示例#4
0
        public void TestLimitOrderNotTriggeredImmediately()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            orderCreator.Limit(new SOrderParameters(EOrderAction.Buy)).Send(40);

            Assert.Equal(0, manager.StrategyInfo.MarketPosition);
        }
示例#5
0
        public void TestUpdateMarketPosition()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            var order = orderCreator.MarketThisBar(
                new SOrderParameters(EOrderAction.Buy));

            order.Send();

            Assert.Equal(100, manager.StrategyInfo.MarketPosition);
        }
示例#6
0
        public void TestOrderValidatedTriggered()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            using var monitoredManager = manager.Monitor();
            orderCreator.Limit(new SOrderParameters()).Send(20);

            monitoredManager.Should()
            .Raise("OrderValidated")
            .WithArgs <OrderInfo>(info => info.Price == 20);
        }
示例#7
0
        public void TestLimitBuyOrderTriggered()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            orderCreator.Limit(new SOrderParameters(EOrderAction.Buy)).Send(40);
            manager.TriggerOrders(new Bar()
            {
                Low = 39
            });

            Assert.Equal(100, manager.StrategyInfo.MarketPosition);
        }
示例#8
0
        public void TestLimitSellOrderTriggered()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            orderCreator.Limit(new SOrderParameters(EOrderAction.SellShort)).Send(40);
            manager.TriggerOrders(new Bar()
            {
                High = 41
            });

            Assert.Equal(-100, manager.StrategyInfo.MarketPosition);
        }
示例#9
0
        public void TestMarketNextBar()
        {
            var orderCreator = new OrderCreator();

            using var monitoredOrderCreator = orderCreator.Monitor();
            var order = orderCreator.MarketNextBar(new SOrderParameters(EOrderAction.Sell));

            order.Send();
            monitoredOrderCreator
            .Should().Raise("OrderSent")
            .WithArgs <OrderInfo>(info => info.Order == order)
            .WithArgs <OrderInfo>(info => info.OrderAction == EOrderAction.Sell)
            .WithArgs <OrderInfo>(info => info.Size == 100);
        }
示例#10
0
        public void TestMarketThisBar()
        {
            var orderCreator = new OrderCreator();

            using var monitoredOrderCreator = orderCreator.Monitor();
            var order = orderCreator.MarketThisBar(
                new SOrderParameters(Contracts.CreateUserSpecified(10), EOrderAction.Buy));

            order.Send(99);
            monitoredOrderCreator
            .Should().Raise("OrderSent")
            .WithArgs <OrderInfo>(info => info.Order == order)
            .WithArgs <OrderInfo>(info => info.OrderAction == EOrderAction.Buy)
            .WithArgs <OrderInfo>(info => info.Size == 99);
        }
示例#11
0
        public void TestLimit()
        {
            var orderCreator = new OrderCreator();

            using var monitoredOrderCreator = orderCreator.Monitor();
            var order = orderCreator.Limit(new SOrderParameters(EOrderAction.Buy));

            order.Send(15);
            monitoredOrderCreator
            .Should().Raise("OrderSent")
            .WithArgs <OrderInfo>(info => info.Order == order)
            .WithArgs <OrderInfo>(info => info.Price == 15)
            .WithArgs <OrderInfo>(info => info.OrderAction == EOrderAction.Buy)
            .WithArgs <OrderInfo>(info => info.Size == 100);
        }
示例#12
0
        public void TestUpdateMarketPositionMultipleOrders()
        {
            var orderCreator = new OrderCreator();
            var manager      = new StrategyManager(orderCreator);

            orderCreator.MarketThisBar(
                new SOrderParameters(EOrderAction.Buy)).Send();
            orderCreator.MarketThisBar(
                new SOrderParameters(EOrderAction.Buy)).Send();
            orderCreator.MarketThisBar(
                new SOrderParameters(
                    Contracts.Default, EOrderAction.Sell, OrderExit.FromAll)).Send();

            Assert.Equal(0, manager.StrategyInfo.MarketPosition);
        }
示例#13
0
        /// <summary>
        /// Instantiates a new instance.
        /// </summary>
        /// <param name="bars">The list of Bar instances to use during the backtest.</param>
        public StrategyBacktester(IEnumerable <Bar> bars)
        {
            var orderCreator = new OrderCreator();

            orderCreator.OrderSent += (order) => OrderSent.SafeTrigger(order);

            var manager = new StrategyManager(orderCreator);

            StrategyInfo = manager.StrategyInfo;

            playableBars = new PlayableBars(bars);

            runner = new StrategyRunner <T>(
                playableBars.ToSingleDataStream(),
                orderCreator: orderCreator,
                strategyInfo: StrategyInfo);
            Strategy = runner.Strategy;
        }