Ejemplo n.º 1
0
        private bool updateOrder(Order order, IExecutionReportMessage er)
        {
            // stupid protocol...but this is what the API sends...
            if (er.ExecType == ExecTypeEnum.etPartiallyFilled ||
                er.ExecType == ExecTypeEnum.etFilled)
            {
                order.avgPx   = (order.avgPx * order.cumQty + er.OrderQty * er.Price) / (order.cumQty + er.OrderQty);
                order.cumQty += er.OrderQty;
            }
            else if (er.ExecType == ExecTypeEnum.etReplaced)
            {
                if (!PriceUtils.isZero(er.OrderQty))
                {
                    order.quantity = er.OrderQty;
                }

                if (!PriceUtils.isZero(er.Price))
                {
                    order.price = er.Price;
                }
            }

            ExecType? execType  = FieldConverter.convert(er.ExecType, order);
            OrdStatus?ordStatus = FieldConverter.convert(er.OrderStatus, order);

            if (null != execType && null != ordStatus)
            {
                order.exchangeOrderId = er.OrderID;
                order.ordStatus       = ordStatus ?? order.ordStatus;
                order.execType        = execType ?? order.execType;
                order.lastMsg         = er.Text;
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        public void amendOrder(String exchangeOrderId, double price, double qty)
        {
            logger.Info("Amend Order: " + "exchangeOrderId=" + exchangeOrderId + "; price=" + price + "; qty=" + qty);
            Order order;

            if (!orders.TryGetValue(exchangeOrderId, out order))
            {
                throw new DownStreamException("Amend order not found: " + exchangeOrderId);
            }

            if (PriceUtils.EqualGreaterThan(qty, order.quantity))
            {
                throw new DownStreamException("Can't amend quantity up: " + exchangeOrderId);
            }

            if (PriceUtils.EqualLessThan(qty, order.cumQty))
            {
                throw new DownStreamException("Can't amend quantity to equal or less than filled quantity: " + exchangeOrderId);
            }

            if (!PriceUtils.isZero(price) && !PriceUtils.Equal(price, order.price) &&
                !PriceUtils.isZero(qty) && !PriceUtils.Equal(qty, order.quantity))
            {
                throw new DownStreamException("Can't amend both price and quantity: " + exchangeOrderId);
            }

            if (price == order.price && qty == order.quantity)
            {
                throw new DownStreamException("Can't amend price and qty to same value: " + exchangeOrderId);
            }

            OrderMessage.ReplaceOrderMessage replaceOrderMessage = new OrderMessage.ReplaceOrderMessage();
            replaceOrderMessage.AE       = user;
            replaceOrderMessage.Account  = account;
            replaceOrderMessage.BrokerID = brokerID;
            replaceOrderMessage.Symbol   = order.symbol;
            replaceOrderMessage.OrderID  = order.exchangeOrderId;
            replaceOrderMessage.Side     = FieldConverter.convert(order.orderSide);
            if (!PriceUtils.isZero(price) && !PriceUtils.Equal(price, order.price))
            {
                replaceOrderMessage.Price = price;
            }

            if (!PriceUtils.isZero(qty) && !PriceUtils.Equal(qty, order.quantity))
            {
                replaceOrderMessage.OrderQty = (int)qty;
            }
            replaceOrderMessage.Data = formatData(replaceOrderMessage.OrderQty, order.account);

            replaceOrderMessage.NID = exchangeConnection.GenerateUniqueID(this.market, OrderMessage.MessageTypeEnum.mtReplace);
            exchangeConnection.ReplaceOrder(replaceOrderMessage);
        }
Ejemplo n.º 3
0
        public void newOrder(Order order)
        {
            logger.Info("New Order: " + order.ToString());
            if (!placeOrderCount.check(order.symbol))
            {
                string msg = "Max new order count reached: " + this.maxOrderCount;
                logger.Error(msg);
                throw new Exception(msg);
            }

            if (order.orderId == null)
            {
                throw new DownStreamException("Order id can't be null");
            }

            OrderMessage.NewOrderMessage newOrderMsg = new OrderMessage.NewOrderMessage();
            newOrderMsg.AE       = user;
            newOrderMsg.Account  = account;
            newOrderMsg.BrokerID = brokerID;
            newOrderMsg.Symbol   = order.symbol;

            newOrderMsg.OrderType = FieldConverter.convert(order.orderType);
            newOrderMsg.Side      = FieldConverter.convert(order.orderSide);
            newOrderMsg.Price     = order.price;
            newOrderMsg.OrderQty  = (int)order.quantity;
            newOrderMsg.Market    = this.market;
            //TO DO
            if (order.orderType == OrderType.Market)
            {
                newOrderMsg.TimeInForce = OrderMessage.TimeInForceEnum.tifIOC;
            }
            else
            {
                newOrderMsg.TimeInForce = OrderMessage.TimeInForceEnum.tifROD;
            }

            newOrderMsg.Data             = formatData(newOrderMsg.OrderQty, order.account);
            newOrderMsg.TradingSessionID = 0;
            newOrderMsg.PositionEffect   = OrderMessage.PositionEffectEnum.peClose;
            //DateTime dt = new DateTime();
            //newOrderMsg.ClOrdID = dt.ToString("yyyy-MM-dd hh:mm:ss.fff");
            //System.Diagnostics.Debug.WriteLine("ClOrderID: " + newOrderMsg.ClOrdID);

            newOrderMsg.NID = exchangeConnection.GenerateUniqueID(newOrderMsg.Market, OrderMessage.MessageTypeEnum.mtNew);

            pendings[newOrderMsg.NID] = order;
            exchangeConnection.NewOrder(newOrderMsg);
        }
Ejemplo n.º 4
0
        public void cancelOrder(string exchangeOrderId)
        {
            logger.Info("Cancel Order: " + "exchangeOrderId=" + exchangeOrderId);
            Order order;

            if (!orders.TryGetValue(exchangeOrderId, out order))
            {
                throw new DownStreamException("Cancel order not found: " + exchangeOrderId);
            }

            OrderMessage.CancelOrderMessage cancelOrderMessage = new OrderMessage.CancelOrderMessage();
            cancelOrderMessage.AE       = user;
            cancelOrderMessage.Account  = account;
            cancelOrderMessage.BrokerID = brokerID;
            cancelOrderMessage.Symbol   = order.symbol;
            cancelOrderMessage.OrderID  = order.exchangeOrderId;
            cancelOrderMessage.Side     = FieldConverter.convert(order.orderSide);
            cancelOrderMessage.Data     = formatData((int)order.quantity, order.account);

            cancelOrderMessage.NID = exchangeConnection.GenerateUniqueID(this.market, OrderMessage.MessageTypeEnum.mtCancel);
            exchangeConnection.CancelOrder(cancelOrderMessage);
        }
Ejemplo n.º 5
0
        void onExecutionReport(OrderMessage.IExecutionReportMessage msg, OrderConnection.ExecDupEnum possDup)
        {
            Utils.printExecutionReport(msg, possDup);
            Order existingOrder;

            if (null != msg.OrderID && msg.OrderID != "")
            {
                if (this.recovering) // during recovery there won't be an order in the cache
                {
                    recoveryCount++;
                    Order order = new Order(this.id, msg.Symbol, "unknown", msg.Price, msg.OrderQty,
                                            FieldConverter.convert(msg.Side), FieldConverter.convert(msg.OrderType), "recovered");

                    if (updateOrder(order, msg))
                    {
                        orders[order.exchangeOrderId] = order;
                        PersistItem item = persistence.getItem(order.exchangeOrderId);
                        if (null == item) // log error here !!!
                        {
                            logger.Error("Error: Cant find order in peristence: " + order.exchangeOrderId);
                            return;
                        }
                        // This is the only reason we need persistence!
                        order.symbol  = item.symbol;
                        order.orderId = item.orderId;
                        order.account = item.account;

                        logger.Info("Recovery add order: " + order);
                    }
                }
                else
                {
                    if (orders.TryGetValue(msg.OrderID, out existingOrder))
                    {
                        if (updateOrder(existingOrder, msg))
                        {
                            persistence.save(existingOrder);
                            this.onOrder(existingOrder);
                        }
                    }
                    else if (msg.NID != 0 && pendings.TryGetValue(msg.NID, out existingOrder))
                    {
                        if (updateOrder(existingOrder, msg))
                        {
                            pendings.TryRemove(msg.NID, out existingOrder);
                            orders[msg.OrderID] = existingOrder;
                            persistence.save(existingOrder);
                            this.onOrder(existingOrder);
                        }
                    }
                    else
                    {
                        logger.Info("Cant find corresponding order in cache");
                    }
                }
            }
            else
            {
                logger.Info("ExecutionReport OrderId is null or empty");
            }
        }