public void NewExecutionSellSide_UpdateTrade_OverallPositionIsNegative()
        {
            // Create Trade Processor
            TradeProcessor tradeProcessor = new TradeProcessor(new Security()
            {
                Symbol = "GOOG"
            }, OrderExecutionProvider.Simulated);

            // Create Order Object
            Order order = new Order(OrderExecutionProvider.Simulated);

            {
                // Create Fill Object
                Fill fill = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fill.ExecutionId   = "1";
                fill.ExecutionSide = OrderSide.SELL;
                fill.ExecutionSize = 40;
                // Create Execution Object
                Execution execution = new Execution(fill, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(execution);
            }

            {
                // Create Fill Object
                Fill fillTwo = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fillTwo.ExecutionId   = "2";
                fillTwo.ExecutionSide = OrderSide.BUY;
                fillTwo.ExecutionSize = 20;
                // Create Execution Object
                Execution executionTwo = new Execution(fillTwo, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(executionTwo);
            }

            // Get Trades Map
            var tradeMap = tradeProcessor.OpenTrades;

            // Get Open Trade
            Trade trade = tradeMap.Values.First();

            Assert.AreEqual(-20, tradeProcessor.Position, "Overall Position");
            Assert.AreEqual(TradeSide.Sell, trade.TradeSide, "Trade Side");
            Assert.AreEqual(20, trade.ExecutionDetails["2"], "Execution Size");
            Assert.AreEqual(false, trade.IsComplete(), "Trade.IsComplete");
        }
Beispiel #2
0
        /// <summary>
        /// Called when new Execution is received
        /// </summary>
        /// <param name="execution">Order Execution Object</param>
        public void NewExecutionArrived(Execution execution)
        {
            try
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("New Execution received " + execution, _type.FullName, "NewExecutionArrived");
                }

                // Create Object
                Dictionary <Security, TradeProcessor> tradeProcessorsBySecurityMap;

                // Get Trade Processor object for the received Execution Message's Provider
                if (!_tradeProcessorMap.TryGetValue(execution.OrderExecutionProvider, out tradeProcessorsBySecurityMap))
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("New Trade Processor created for:" + execution.OrderExecutionProvider, _type.FullName, "NewExecutionArrived");
                    }

                    // Initialize a new Trade Processor object
                    TradeProcessor tradeProcessorObject = new TradeProcessor(execution.Fill.Security, execution.OrderExecutionProvider);

                    // Initialize local Map
                    tradeProcessorsBySecurityMap = new Dictionary <Security, TradeProcessor>();

                    // Add to local Map
                    tradeProcessorsBySecurityMap.Add(execution.Fill.Security, tradeProcessorObject);

                    // Add to Global Map
                    _tradeProcessorMap.Add(execution.OrderExecutionProvider, tradeProcessorsBySecurityMap);

                    // Forward Execution to Trade Processor
                    tradeProcessorObject.NewExecutionArrived(execution);
                }
                else
                {
                    TradeProcessor tradeProcessorObject;

                    // Get Trade Processor object for the received Execution Message's Security
                    if (!tradeProcessorsBySecurityMap.TryGetValue(execution.Fill.Security, out tradeProcessorObject))
                    {
                        if (Logger.IsDebugEnabled)
                        {
                            Logger.Debug("New Trade Processor created for:" + execution.Fill.Security, _type.FullName, "NewExecutionArrived");
                        }

                        // Initialize a new Trade Processor object
                        tradeProcessorObject = new TradeProcessor(execution.Fill.Security, execution.OrderExecutionProvider);

                        // Add to local Map
                        tradeProcessorsBySecurityMap.Add(execution.Fill.Security, tradeProcessorObject);
                    }
                    // Forward Execution to Trade Processor
                    tradeProcessorObject.NewExecutionArrived(execution);
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "NewExecutionArrived");
            }
        }
        public void NewExecutionBuySideMultiple_OpenNewTradeMultipleAndUpdateTrade_OverallPositionIsNegativeWithSingleOpenOrderOnSellSide()
        {
            // Create Trade Processor
            TradeProcessor tradeProcessor = new TradeProcessor(new Security()
            {
                Symbol = "GOOG"
            }, OrderExecutionProvider.Simulated);

            // Create Order Object
            Order order = new Order(OrderExecutionProvider.Simulated);

            {
                // Create Fill Object
                Fill fill = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fill.ExecutionId   = "1";
                fill.ExecutionSide = OrderSide.BUY;
                fill.ExecutionSize = 40;
                // Create Execution Object
                Execution execution = new Execution(fill, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(execution);
            }

            {
                // Create Fill Object
                Fill fillTwo = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fillTwo.ExecutionId   = "2";
                fillTwo.ExecutionSide = OrderSide.BUY;
                fillTwo.ExecutionSize = 30;
                // Create Execution Object
                Execution executionTwo = new Execution(fillTwo, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(executionTwo);
            }

            {
                // Create Fill Object
                Fill fillThree = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fillThree.ExecutionId   = "3";
                fillThree.ExecutionSide = OrderSide.SELL;
                fillThree.ExecutionSize = 80;
                // Create Execution Object
                Execution executionThree = new Execution(fillThree, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(executionThree);
            }

            // Get Trades Map
            var tradeMap = tradeProcessor.OpenTrades;

            // Open Trade Count
            var openTradeCount = tradeMap.Count;

            // Get Open Trade Size
            int tradeThreeSize = tradeMap[3].TradeSize;

            Assert.AreEqual(-10, tradeProcessor.Position, "Overall Position");
            Assert.AreEqual(1, openTradeCount, "Open Trade Count");
            Assert.AreEqual(TradeSide.Sell, tradeMap[3].TradeSide, "Trade Three Side");
            Assert.AreEqual(-10, tradeThreeSize, "Trade Three Size");
            Assert.AreEqual(false, tradeMap[3].IsComplete(), "Trade Three Completion Status");
        }
        public void NewExecutionSellSideMultiple_OpenNewTradeMultipleAndUpdateTrade_OverallPositionIsBalanced()
        {
            // Create Trade Processor
            TradeProcessor tradeProcessor = new TradeProcessor(new Security()
            {
                Symbol = "GOOG"
            }, OrderExecutionProvider.Simulated);

            // Create Order Object
            Order order = new Order(OrderExecutionProvider.Simulated);

            {
                // Create Fill Object
                Fill fill = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fill.ExecutionId   = "1";
                fill.ExecutionSide = OrderSide.SELL;
                fill.ExecutionSize = 40;
                // Create Execution Object
                Execution execution = new Execution(fill, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(execution);
            }

            {
                // Create Fill Object
                Fill fillTwo = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fillTwo.ExecutionId   = "2";
                fillTwo.ExecutionSide = OrderSide.SELL;
                fillTwo.ExecutionSize = 50;
                // Create Execution Object
                Execution executionTwo = new Execution(fillTwo, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(executionTwo);
            }

            {
                // Create Fill Object
                Fill fillThree = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fillThree.ExecutionId   = "3";
                fillThree.ExecutionSide = OrderSide.BUY;
                fillThree.ExecutionSize = 90;
                // Create Execution Object
                Execution executionThree = new Execution(fillThree, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(executionThree);
            }

            // Get Trades Map
            var tradeMap = tradeProcessor.OpenTrades;

            // Open Trade Count
            var openTradeCount = tradeMap.Count;

            Assert.AreEqual(0, tradeProcessor.Position, "Overall Position");
            Assert.AreEqual(0, openTradeCount, "Open Trade Count");
        }
        public void NewExecutionBuySideMultiple_OpenNewTradeMultiple_OverallPositionIsPositive()
        {
            // Create Trade Processor
            TradeProcessor tradeProcessor = new TradeProcessor(new Security()
            {
                Symbol = "GOOG"
            }, OrderExecutionProvider.Simulated);

            // Create Order Object
            Order order = new Order(OrderExecutionProvider.Simulated);

            {
                // Create Fill Object
                Fill fill = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fill.ExecutionId   = "1";
                fill.ExecutionSide = OrderSide.BUY;
                fill.ExecutionSize = 40;
                // Create Execution Object
                Execution execution = new Execution(fill, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(execution);
            }

            {
                // Create Fill Object
                Fill fillTwo = new Fill(new Security()
                {
                    Symbol = "GOOG"
                }, OrderExecutionProvider.Simulated, "1");
                fillTwo.ExecutionId   = "2";
                fillTwo.ExecutionSide = OrderSide.BUY;
                fillTwo.ExecutionSize = 40;
                // Create Execution Object
                Execution executionTwo = new Execution(fillTwo, order);
                // Update Trade Processor
                tradeProcessor.NewExecutionArrived(executionTwo);
            }

            // Get Trades Map
            var tradeMap = tradeProcessor.OpenTrades;

            // Open Trade Count
            var openTradeCount = tradeMap.Count;

            // Get Open Trade Size
            int tradeOneSize = tradeMap[1].TradeSize;
            int tradeTwoSize = tradeMap[2].TradeSize;

            Assert.AreEqual(80, tradeProcessor.Position, "Overall Position");
            Assert.AreEqual(2, openTradeCount, "Open Trade Count");

            Assert.AreEqual(TradeSide.Buy, tradeMap[1].TradeSide, "Trade One Side");
            Assert.AreEqual(TradeSide.Buy, tradeMap[2].TradeSide, "Trade Two Side");

            Assert.AreEqual(40, tradeOneSize, "Trade One Size");
            Assert.AreEqual(40, tradeTwoSize, "Trade Two Size");

            Assert.AreEqual(false, tradeMap[1].IsComplete(), "Trade One Completion Status");
            Assert.AreEqual(false, tradeMap[2].IsComplete(), "Trade Two Completion Status");
        }