protected void RejectNew(QuickFix.SessionID session, string symbol, string order_id, int qty, OrderAction side, string reply_text)
        {
            IOrder         fake_order = new FakeOrderForRejectReport(qty, side);
            OrderFixBridge order      = new OrderFixBridge(fake_order, order_id, session);

            SendExecutionReport(order, symbol, 0, 0, reply_text);
        }
        public static void FromFIX_SendOrder(OrderFixBridge orderFixBridge, string instrumentId, OrderAction orderAction, OrderType orderType, int quantity, double limitPrice, double stopPrice, string signalName)
        {
            lock (locker_)
            {
                MarketDataDef mdd = s_fixConnectors.GetMarketDataDefForSymbol(instrumentId);
                if (mdd == null)
                {
                    throw new Exception("No FixConnector available for symbol to create order on '" + instrumentId + "'.");
                }

                mdd.fixConnector.FromFIX_SendOrder(orderFixBridge, mdd.internal_barinprogress, orderAction, orderType, quantity, limitPrice, stopPrice, signalName);
            }
        }
        public static void FromFIX_SendOrder(OrderFixBridge orderFixBridge, string instrumentId, OrderAction orderAction, OrderType orderType, int quantity, double limitPrice, double stopPrice, string signalName)
        {
            lock (locker_)
            {
                MarketDataDef mdd = s_fixConnectors.GetMarketDataDefForSymbol(instrumentId);
                if (mdd == null)
                {
                    throw new Exception("No FixConnector available for symbol to create order on '" + instrumentId + "'.");
                }

                mdd.fixConnector.FromFIX_SendOrder(orderFixBridge, mdd.internal_barinprogress, orderAction, orderType, quantity, limitPrice, stopPrice, signalName);
            }
        }
        public OrderFixBridge AddOrGet(IOrder order, string clordid, QuickFix.SessionID session)
        {
            if (order == null && string.IsNullOrEmpty(clordid))
            {
                throw new Exception("internal error: order is null and clordid empty");
            }

            OrderFixBridge r = null;

            if (order == null) // lookup by clordid
            {
                r = m_orders.Find(s => s.QuickFixId == clordid);
                if (r == null)
                {
                    m_orders.Add(r = new OrderFixBridge(null, clordid, session));
                }
            }
            else  // lookup by order
            {
                r = m_orders.Find(s => s.Order == order);
                if (r == null)
                {
                    m_orders.Add(r = new OrderFixBridge(order, clordid, session));
                }
            }

            // update session
            if (r.OrderSessionID == null && session != null)
            {
                r.OrderSessionID = session;
            }

            //update fix id
            if (string.IsNullOrEmpty(r.QuickFixId) && !string.IsNullOrEmpty(clordid))
            {
                r.QuickFixId = clordid;
            }

            return(r);
        }
        public OrderFixBridge AddOrGet(IOrder order, string clordid, QuickFix.SessionID session)
        {
            if (order == null && string.IsNullOrEmpty(clordid))
                throw new Exception("internal error: order is null and clordid empty");

            OrderFixBridge r = null;

            if (order == null) // lookup by clordid
            {
                r = m_orders.Find(s => s.QuickFixId == clordid);
                if (r == null)
                    m_orders.Add(r = new OrderFixBridge(null, clordid, session));
            }
            else  // lookup by order
            {
                r = m_orders.Find(s => s.Order == order);
                if (r == null)
                    m_orders.Add(r = new OrderFixBridge(order, clordid, session));

            }

            // update session
            if (r.OrderSessionID == null && session != null)
                r.OrderSessionID = session;

            //update fix id
            if (string.IsNullOrEmpty(r.QuickFixId) && !string.IsNullOrEmpty(clordid))
                r.QuickFixId = clordid;

            return r;
        }
        protected void SendExecutionReport(OrderFixBridge order, string symbol, double execQty, double execPrice, string text)
        {
            if (order == null)
            {
                throw new Exception("[QuickFixApp.SendExecutionReport] OrderFixBridge order is null ");
            }
            IOrder o = order.Order;

            if (o == null)
            {
                throw new Exception("[QuickFixApp.SendExecutionReport] IOrder is null ");
            }
            if (string.IsNullOrEmpty(symbol))
            {
                throw new Exception("[QuickFixApp.SendExecutionReport] symbol is empty");
            }

            QuickFix.SessionID session = order.OrderSessionID;
            if (session == null)
            {
                throw new Exception("[QuickFixApp.SendExecutionReport] session is null ");
            }

            QuickFix.FIX42.ExecutionReport msg = new QuickFix.FIX42.ExecutionReport();
            msg.OrderID       = new QuickFix.Fields.OrderID(Helper.GetUniqueID("EXECID"));
            msg.HandlInst     = new QuickFix.Fields.HandlInst(QuickFix.Fields.HandlInst.AUTOMATED_EXECUTION_ORDER_PRIVATE_NO_BROKER_INTERVENTION);
            msg.Symbol        = new QuickFix.Fields.Symbol(symbol);
            msg.Side          = Converter.c(o.OrderAction);
            msg.LeavesQty     = new QuickFix.Fields.LeavesQty(Convert.ToDecimal(Math.Max(0, o.Quantity - o.Filled)));
            msg.CumQty        = new QuickFix.Fields.CumQty(Convert.ToDecimal(o.Filled));
            msg.AvgPx         = new QuickFix.Fields.AvgPx(Convert.ToDecimal(o.AvgFillPrice));
            msg.TransactTime  = new QuickFix.Fields.TransactTime(o.Time, true);
            msg.Text          = new QuickFix.Fields.Text(text);
            msg.ExecTransType = new QuickFix.Fields.ExecTransType(QuickFix.Fields.ExecTransType.NEW);

            switch (o.OrderState)
            {
            // pending : ignored
            case OrderState.Accepted:
            case OrderState.PendingChange:
            case OrderState.PendingSubmit:
            case OrderState.PendingCancel:
                return;

            case OrderState.Working:
                msg.OrdStatus = new QuickFix.Fields.OrdStatus(QuickFix.Fields.OrdStatus.NEW);

                msg.ExecID   = new QuickFix.Fields.ExecID(Helper.GetUniqueID("NEW"));
                msg.ExecType = new QuickFix.Fields.ExecType(QuickFix.Fields.ExecType.NEW);

                break;

            case OrderState.Cancelled:
                msg.OrdStatus = new QuickFix.Fields.OrdStatus(QuickFix.Fields.OrdStatus.CANCELED);
                msg.ExecID    = new QuickFix.Fields.ExecID(Helper.GetUniqueID("CANCELED"));
                msg.ExecType  = new QuickFix.Fields.ExecType(QuickFix.Fields.ExecType.CANCELED);

                break;

            case OrderState.Filled:
            case OrderState.PartFilled:

                if (double.IsNaN(execQty))
                {
                    return;
                }
                if (double.IsNaN(execPrice))
                {
                    return;
                }

                bool full = o.OrderState == OrderState.Filled;

                msg.OrdStatus  = new QuickFix.Fields.OrdStatus(full ? QuickFix.Fields.OrdStatus.FILLED : QuickFix.Fields.OrdStatus.PARTIALLY_FILLED);
                msg.ExecID     = new QuickFix.Fields.ExecID(Helper.GetUniqueID("EXEC"));
                msg.ExecType   = new QuickFix.Fields.ExecType(full ? QuickFix.Fields.ExecType.FILL : QuickFix.Fields.ExecType.PARTIAL_FILL);
                msg.LastShares = new QuickFix.Fields.LastShares(Convert.ToDecimal(execQty));
                msg.LastPx     = new QuickFix.Fields.LastPx(Convert.ToDecimal(execPrice));

                break;

            case OrderState.Rejected:

                msg.OrdStatus   = new QuickFix.Fields.OrdStatus(QuickFix.Fields.OrdStatus.REJECTED);
                msg.ExecType    = new QuickFix.Fields.ExecType(QuickFix.Fields.ExecType.REJECTED);
                msg.OrigClOrdID = new QuickFix.Fields.OrigClOrdID(order.QuickFixId);
                msg.ExecID      = new QuickFix.Fields.ExecID(Helper.GetUniqueID("REJ"));
                break;


            case OrderState.Unknown:
                throw new Exception("[QuickFixApp.SendExecutionReport] IOrder should not have unknown status at this point !");
            }

            QuickFix.Session.SendToTarget(msg, session);
        }
        protected void SendExecutionReport(OrderFixBridge order, string symbol, double execQty, double execPrice, string text)
        {
            if (order == null) throw new Exception("[QuickFixApp.SendExecutionReport] OrderFixBridge order is null ");
            IOrder o = order.Order;

            if (o == null) throw new Exception("[QuickFixApp.SendExecutionReport] IOrder is null ");
            if (string.IsNullOrEmpty(symbol)) throw new Exception("[QuickFixApp.SendExecutionReport] symbol is empty");

            QuickFix.SessionID session = order.OrderSessionID;
            if (session == null) throw new Exception("[QuickFixApp.SendExecutionReport] session is null ");

            QuickFix.FIX42.ExecutionReport msg = new QuickFix.FIX42.ExecutionReport();
            msg.OrderID = new QuickFix.Fields.OrderID(Helper.GetUniqueID("EXECID"));
            msg.HandlInst = new QuickFix.Fields.HandlInst(QuickFix.Fields.HandlInst.AUTOMATED_EXECUTION_ORDER_PRIVATE_NO_BROKER_INTERVENTION);
            msg.Symbol = new QuickFix.Fields.Symbol(symbol);
            msg.Side = Converter.c(o.OrderAction);
            msg.LeavesQty = new QuickFix.Fields.LeavesQty(Convert.ToDecimal(Math.Max(0,o.Quantity-o.Filled)));
            msg.CumQty = new QuickFix.Fields.CumQty(Convert.ToDecimal(o.Filled));
            msg.AvgPx = new QuickFix.Fields.AvgPx(Convert.ToDecimal(o.AvgFillPrice));
            msg.TransactTime = new QuickFix.Fields.TransactTime(o.Time, true);
            msg.Text = new QuickFix.Fields.Text(text);
            msg.ExecTransType = new QuickFix.Fields.ExecTransType(QuickFix.Fields.ExecTransType.NEW);

            switch (o.OrderState)
            {
                     // pending : ignored
                case OrderState.Accepted:
                case OrderState.PendingChange:
                case OrderState.PendingSubmit:
                case OrderState.PendingCancel:
                    return;

                case OrderState.Working:
                    msg.OrdStatus = new QuickFix.Fields.OrdStatus(QuickFix.Fields.OrdStatus.NEW);

                    msg.ExecID = new QuickFix.Fields.ExecID(Helper.GetUniqueID("NEW"));
                    msg.ExecType = new QuickFix.Fields.ExecType(QuickFix.Fields.ExecType.NEW);

                    break;

                case OrderState.Cancelled:
                    msg.OrdStatus = new QuickFix.Fields.OrdStatus(QuickFix.Fields.OrdStatus.CANCELED);
                    msg.ExecID = new QuickFix.Fields.ExecID(Helper.GetUniqueID("CANCELED"));
                    msg.ExecType = new QuickFix.Fields.ExecType(QuickFix.Fields.ExecType.CANCELED);

                    break;

                case OrderState.Filled:
                case OrderState.PartFilled:

                    if (double.IsNaN(execQty)) return;
                    if (double.IsNaN(execPrice)) return;

                    bool full = o.OrderState == OrderState.Filled;

                    msg.OrdStatus = new QuickFix.Fields.OrdStatus(full ? QuickFix.Fields.OrdStatus.FILLED : QuickFix.Fields.OrdStatus.PARTIALLY_FILLED);
                    msg.ExecID = new QuickFix.Fields.ExecID(Helper.GetUniqueID("EXEC"));
                    msg.ExecType = new QuickFix.Fields.ExecType(full ? QuickFix.Fields.ExecType.FILL : QuickFix.Fields.ExecType.PARTIAL_FILL);
                    msg.LastShares = new QuickFix.Fields.LastShares(Convert.ToDecimal(execQty));
                    msg.LastPx = new QuickFix.Fields.LastPx(Convert.ToDecimal(execPrice));

                    break;

                case OrderState.Rejected:

                    msg.OrdStatus = new QuickFix.Fields.OrdStatus(QuickFix.Fields.OrdStatus.REJECTED);
                    msg.ExecType = new QuickFix.Fields.ExecType(QuickFix.Fields.ExecType.REJECTED);
                    msg.OrigClOrdID = new QuickFix.Fields.OrigClOrdID(order.QuickFixId);
                    msg.ExecID = new QuickFix.Fields.ExecID(Helper.GetUniqueID("REJ"));
                    break;

                case OrderState.Unknown:
                    throw new Exception("[QuickFixApp.SendExecutionReport] IOrder should not have unknown status at this point !");
            }

            QuickFix.Session.SendToTarget(msg, session);
        }
 protected void RejectNew(QuickFix.SessionID session, string symbol, string order_id, int qty, OrderAction side, string reply_text)
 {
     IOrder fake_order = new FakeOrderForRejectReport(qty, side);
     OrderFixBridge order = new OrderFixBridge(fake_order, order_id, session);
     SendExecutionReport(order, symbol, 0, 0, reply_text);
 }