public void PerfectMatchTest()
        {
            FIFOTradeMatcher matcher = new FIFOTradeMatcher();
            List<Trade> matches = new List<Trade>();

            Equity p = new Equity("Google","GOOG");
            Order order1 = new Order(p, 10, Order.Side.BUY);
            Order order2 = new Order(p, 10, Order.Side.SELL);

            Fill fill1 = new Fill();
            fill1.price = 100;
            fill1.qty = 10;
            fill1.originalOrder = order1;

            Fill fill2 = new Fill();
            fill2.price = 100;
            fill2.qty = 10;
            fill2.originalOrder = order2;

            List<Fill> fills = new List<Fill>();
            fills.Add(fill1);
            fills.Add(fill2);

            matches = matcher.match(fills);

            Assert.IsTrue(matches.Count == 1 && fills.Count == 0);
        }
        public void LopsidedMatchTest()
        {
            FIFOTradeMatcher matcher = new FIFOTradeMatcher();
            List<Trade> matches = new List<Trade>();

            Equity p = new Equity("Google", "GOOG");
            Order order1 = new Order(p, 10, Order.Side.BUY);
            Order order2 = new Order(p, 5, Order.Side.SELL);
            Order order3 = new Order(p, 25, Order.Side.BUY);
            Order order4 = new Order(p, 7, Order.Side.SELL);
            Order order5 = new Order(p, 12, Order.Side.SELL);

            Fill fill1 = new Fill();
            fill1.price = 100;
            fill1.qty = 10;
            fill1.originalOrder = order1;

            Fill fill2 = new Fill();
            fill2.price = 100;
            fill2.qty = 5;
            fill2.originalOrder = order2;

            Fill fill3 = new Fill();
            fill3.price = 100;
            fill3.qty = 20;
            fill3.originalOrder = order3;

            Fill fill4 = new Fill();
            fill4.price = 100;
            fill4.qty = 6;
            fill4.originalOrder = order4;

            Fill fill5 = new Fill();
            fill5.price = 100;
            fill5.qty = 12;
            fill5.originalOrder = order5;

            List<Fill> fills = new List<Fill>();
            fills.Add(fill1);
            fills.Add(fill2);
            fills.Add(fill3);
            fills.Add(fill4);
            fills.Add(fill5); 

            matches = matcher.match(fills);

            Assert.IsTrue(matches.Count == 1 && fills.Count == 3);
        }
        //##################################
        //The following events will be called be called
        //when the broker manager triggers an event
        //##################################

        public void fillReceived(Fill fill) 
        {
            int key = -1;
            
            if(fill != null && fill.originalOrder != null)
                key = fill.originalOrder.internalId;
            else
                log.Error("Did not receive a valid fill message");

            //make sure the fill is for one of my orders
            if(myOpenOrders.ContainsKey(key))
            {
                log.Info("Received a fill for order " + key);
                myOpenOrders.Remove(key);
            }
        }
        public void fillReceived(Fill fill)
        {
            if (fill == null && fill.originalOrder == null)
                log.Error("Did not receive a valid fill message");

            //add orders to the list of unmatched orders
            unmatchedOrders.Add(fill);

            //update the positions
            int qty = fill.qty;
            if(fill.originalOrder.side.Equals(Order.Side.SELL))
                qty *= -1;

            if (positions.ContainsKey(fill.originalOrder.product))
            {
                qty += positions[fill.originalOrder.product];
                positions.Remove(fill.originalOrder.product);
            }

            positions.Add(fill.originalOrder.product, qty);


            //Keep track of the fills for each order
            if (orderToFillMap.ContainsKey(fill.originalOrder))
                orderToFillMap[fill.originalOrder].Add(fill);
            else
            {
                List<Fill> fillList = new List<Fill>();
                fillList.Add(fill);
                orderToFillMap.Add(fill.originalOrder, fillList);
            }

            //match the new fills
            List<Trade> trades = tradeMatcher.match(unmatchedOrders);
            matchedTrades.AddRange(trades);

            //If a trade was matched, inform those that are interested
            if(TradeMatched != null && trades != null && trades.Count() > 0)
                TradeMatched(trades);

            if (PositionChange != null)
                PositionChange(fill.originalOrder.product, qty);
        }
 public void fillReceived(Fill fill)
 {
     win.addUpdateOrder(createOrderGridItem(fill.originalOrder));
 }
        public void fillReceived(Object sender, ExecDetailsEventArgs e)
        {
            if(openOrderContracts.ContainsKey(e.OrderId))
            {
                FlexTrade.Fill fill = new FlexTrade.Fill();
               
                //Get the open orders based on the order ID
                Krs.Ats.IBNet.Order krsOrder = openOrders[e.OrderId];
                FlexTrade.Order ftOrder = ordersOrgFormat[e.OrderId];

                ftOrder.status = Order.OrderStatus.FILL;
                ftOrder.fillPrices.Add(e.Execution.Price);
                ftOrder.fillQuantities.Add(e.Execution.Shares);

                //if completely filled, remove the orders from the list of open orders
                if (ftOrder.fillQuantities.Sum() == ftOrder.orderQuantity)
                {
                    ordersOrgFormat[e.OrderId].status = Order.OrderStatus.FILL;
                    openOrders.Remove(e.OrderId);
                    ordersOrgFormat.Remove(e.OrderId);
                    openOrderContracts.Remove(e.OrderId);
                }
                else
                    ordersOrgFormat[e.OrderId].status = Order.OrderStatus.PARTIAL_FILL;

                //Set the execution values in the order
                fill.originalOrder = ftOrder;
                fill.price = e.Execution.Price;
                fill.qty = e.Execution.Shares;
                fill.time = DateTime.UtcNow;

                if (FillUpdate != null && fill != null)
                    FillUpdate(fill);
            }
        }
        //##################################
        //The following events will be called be called
        //when the broker manager triggers an event
        //##################################

        public void fillReceived(Fill fill) 
        {
            Order key = null;
            
            if(fill != null && fill.originalOrder != null)
                key = fill.originalOrder;
            else
                log.Error("Did not receive a valid fill message");

            //if (myFills.Contains(key.internalId))
            //{
            //    myFills.
            //}
        }
        //When we detect the signal, we perform these actions
        private void tradeOnSignal(Fill fill)
        {
            LimitOrder hedgeOrder;
            List<Order> ordersToCancel = new List<Order>();

            //determine which of the orders where filled, and add the orders
            //that werent filled to a list. Also, based on the type of fill,
            //determine which order needs to be submitted
            if (fill.originalOrder.product.Equals(productA))
            {
                //This was a fill for product A, so cancel the orders for B
                ordersToCancel.Add(myOrders[StrategyOrderType.BUY_B]);
                ordersToCancel.Add(myOrders[StrategyOrderType.SELL_B]);

                if (fill.originalOrder.side.Equals(Order.Side.BUY))
                {
                    //This was a buy of A, so cancel the sell of A and submit a hedge to buy B
                    hedgeOrder = new LimitOrder(productB, fill.qty, Order.Side.SELL, fill.price - sellSpread);
                    ordersToCancel.Add(myOrders[StrategyOrderType.SELL_A]);
                }
                else
                {
                    //This was a sell of A, so cancel the buy of A and submit a hedge to sell B
                    hedgeOrder = new LimitOrder(productB, fill.qty, Order.Side. BUY, fill.price - buySpread);
                    ordersToCancel.Add(myOrders[StrategyOrderType.BUY_A]);
                }
            }
            else
            {
                //This was a fill for product B, so cancel the orders for A
                ordersToCancel.Add(myOrders[StrategyOrderType.BUY_A]);
                ordersToCancel.Add(myOrders[StrategyOrderType.SELL_A]);

                if (fill.originalOrder.side.Equals(Order.Side.BUY))
                {
                    //This was a buy of B, so cancel the sell of B and submit a hedge to buy A
                    hedgeOrder = new LimitOrder(productA, fill.qty, Order.Side.SELL, fill.price - sellSpread);
                    ordersToCancel.Add(myOrders[StrategyOrderType.SELL_B]);
                }
                else
                {
                    //This was a sell of B, so cancel the buy of B and submit a hedge to sell A
                    hedgeOrder = new LimitOrder(productA, fill.qty, Order.Side.BUY, fill.price - buySpread);
                    ordersToCancel.Add(myOrders[StrategyOrderType.BUY_B]);
                }
            }

            //cancel the orders that aren't a part of this trade
            cancelOrders(ordersToCancel);

            //TODO: What happens if this was a partial fill?

            //submit hedge orders
            getFirstBroker().submitOrder(hedgeOrder);
        }