Esempio n. 1
0
        public int cancelOrder(OrderStruct os)  //will add check in case of already filled, no cancel.
        {
            OrderDAO    ord  = new OrderDAO();
            OrderStruct dbos = new OrderStruct(8, 8);

            if (ord.getOrderFromDB(os.OrderNo, ref dbos) < 0)
            {
                Console.WriteLine("Error : Unable to get order details from DB");
                return(-1);
            }
            Console.WriteLine("DBG: truct val");
            dbos.display();
            string ordStatus = new string(dbos.OrderStatus);

            if (ordStatus.Equals("CANCELED") || ordStatus.Equals("COMPLETED"))
            {
                Console.WriteLine("Order is already " + ordStatus);
                return(-1);
            }
            Console.WriteLine("Order No [" + dbos.OrderNo + "] : Status [" + ordStatus + "]");
            //return -1;
            if (ord.cancelOrder(dbos) < 0)
            {
                return(-1);
            }
            Console.WriteLine("TODO: Send FIX - cancel for os.ID");
            //Send FIX - cancel for os.ID
            return(0);
        }
Esempio n. 2
0
 public int insertOrder(OrderStruct os)
 {
     try
     {
         using (var cmd = new SqlCommand())
         {
             cmd.Connection = conn;
             int orderID = getNextSeq(orderIDSeq);
             int orderNo = getNextSeq(orderSeq);
             Console.WriteLine("Next Seq : {0}", orderNo);
             string symbol = sanitiseField(os.symbol);
             string query  = "INSERT INTO ORDERS (ID, OrderNo, OrderStatus,symbol,price, strike, quantity,direction,version,expiry,callput, exch, machineID,userID,insertTS) VALUES ("
                             + orderID + "," + orderNo + "," + "'NEW'" + "," + "'" + symbol + "'" + "," + os.price + "," + os.strike + "," + os.quantity + ",'"
                             + os.direction + "',1,'" + new string(os.expiry) + "','" + new string(os.callput) + "','" + new string(os.exch) + "','" + new string(os.machineID)
                             + "','" + new string(os.userID) + "',SYSDATETIME());";
             cmd.CommandText = query;
             Console.WriteLine("query : {0}", query);
             cmd.ExecuteNonQuery();
             return(orderNo);
         }
     }
     catch (Exception ex)
     {
         Console.Write("Exception (Insert) : " + ex.Message);
     }
     return(-1);
 }
Esempio n. 3
0
            private OrderStruct getStructFromBytes(byte[] bytesFrom)
            {
                IntPtr ptPoit = Marshal.AllocHGlobal(bytesFrom.Length);

                Marshal.Copy(bytesFrom, 0, ptPoit, bytesFrom.Length);
                OrderStruct os = (OrderStruct)Marshal.PtrToStructure(ptPoit, typeof(OrderStruct));

                //Console.WriteLine("Order : "+os.methodID);
                os.display();
                return(os);
            }
Esempio n. 4
0
        public int cancelOrder(OrderStruct os)
        {//will add check in case of already filled, no cancel.
            OrderDAO    ord  = new OrderDAO();
            OrderStruct dbos = new OrderStruct(11, 8);

            if (ord.getOrderFromDB(os.OrderNo, ref dbos) < 0)
            {
                Console.WriteLine("Error : Unable to get order details from DB");
                return(-1);
            }
            Console.WriteLine("DBG: truct val");
            dbos.display();
            string ordStatus = new string(dbos.OrderStatus);

            //if(ordStatus.Equals("CANCELED") || ordStatus.Equals("COMPLETED"))
            //{
            //    Console.WriteLine("Order is already "+ordStatus);
            //    return -1;
            //}
            Console.WriteLine("Order No [" + dbos.OrderNo + "] : Status [" + ordStatus + "]");
            //return -1;
            int ordNo = ord.cancelOrder(ref dbos);

            if (ordNo < 0)
            {
                return(-1);
            }
            Console.WriteLine("TODO: Send FIX - cancel for os.ID");
            //Send FIX - cancel for os.ID

            OrigClOrdID origCLOrdID = new OrigClOrdID(Convert.ToString(dbos.fixAcceptedID));
            ClOrdID     OrdId       = new ClOrdID(Convert.ToString(ordNo));
            string      symboldata  = sanitiseField(os.symbol);
            Symbol      symbol      = new Symbol(symboldata);
            Side        side        = new Side(Side.BUY);

            if (os.direction == 'S')
            {
                side = new Side(Side.SELL);
            }

            Console.WriteLine("cancelling origOrdId : " + dbos.fixAcceptedID + " newOrdId : " + ordNo);
            Console.WriteLine("symboldata : " + symboldata + " side : " + side);

            var orderCan = new QuickFix.FIX42.OrderCancelRequest(origCLOrdID, OrdId, symbol, side,
                                                                 new TransactTime(DateTime.Now.ToUniversalTime()));

            Session.SendToTarget(orderCan, FixClient.MySess);
            return(os.OrderNo);
        }
Esempio n. 5
0
        public int amendOrder(ref OrderStruct os)
        {
            if (!isOrderAmendPossible(os.OrderStatus))
            {
                return(-1);
            }
            OrderDAO ord   = new OrderDAO();
            int      ordNo = ord.amendOrder(ref os);

            Console.WriteLine("TODO: Send FIX - cancel for os.LinkedOrderID ; Send FIX - New for os.ID");
            //Send FIX - cancel for os.LinkedOrderID
            //Send FIX - New for os.ID
            return(ordNo);
        }
Esempio n. 6
0
        public int FIXResponseHandler(Int32 OrderID, string ExchangeOrderId, Int32 origOrdID, char status, string status_msg, string other_msg, float price, char orderType)
        {
            string      data = "";
            OrderDAO    ord  = new OrderDAO();
            OrderStruct os   = new OrderStruct();
            int         iret = -1;

            if (status == 'X') // i am considering the reject of amendment.
            {                  // rejected so move the order to originalID
                iret = ord.setOrdersWithReplacedFixResponse(origOrdID, getExecType(status), ExchangeOrderId, ref os);
            }
            else if (status == '5')
            { // accepted the amendment i.e. Replaced so add the new ord no.
                iret = ord.setOrdersWithReplacedFixResponse(OrderID, getExecType(status), ExchangeOrderId, ref os);
            } // In case of Order Rejected - call DB for reject status.
            else if (status == 'D')
            {
                iret = ord.setOrdersWithFixResponseRestated(OrderID, getExecType(status), ExchangeOrderId, price, orderType, ref os);

                os.price     = price;
                os.orderType = Convert.ToInt32(orderType);
            }
            else
            {
                iret = ord.setOrdersWithFixResponse(OrderID, getExecType(status), ExchangeOrderId, ref os);
            }
            if (iret == -1)
            {
                Console.WriteLine("Error from DB [" + data + "]");
                //return -1;
            }
            else
            {
                Console.WriteLine("Order Updated");
            }
            data = new string(os.machineID) + ":" + new string(os.userID) + ":" + os.OrderNo + ":" + new string(os.OrderStatus) + ":" + ExchangeOrderId + ":" + status + ":" + status_msg + ":" + other_msg;
            OMFillPub ofp = new OMFillPub();

            if (ofp.zmqUpdates(data) == 0)
            {
                Console.WriteLine("Zmq update success");
            }
            else
            {
                Console.WriteLine("Zmq update fail");
            }
            ord.addFIXResponse(OrderID, ExchangeOrderId, status, status_msg, other_msg);
            return(0);
        }
Esempio n. 7
0
        public int addOrder(OrderStruct os)
        {
            OrderDAO odao = new OrderDAO();

            Console.WriteLine("Going to call DB");
            int ordNo = odao.insertOrder(os);

            ClOrdID OrdId  = new ClOrdID(Convert.ToString(os.ID));
            string  symbol = sanitiseField(os.symbol);
            var     order  = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), new Symbol(symbol), new Side(Side.BUY),
                                                               new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));

            order.Price = new Price((decimal)os.price);
            order.SetField(new OrderQty(1));

            Session.SendToTarget(order, FixClient.MySess);
            return(ordNo);
        }
Esempio n. 8
0
        public int FIXResponseCanModRejectHandler(Int32 OrderID, string ExchangeOrderId, Int32 origOrdID, char status, string status_msg, string other_msg)
        {
            string      data = "";
            OrderDAO    ord  = new OrderDAO();
            OrderStruct os   = new OrderStruct();
            int         iret = -1;

            Console.WriteLine("FIXResponseCanModRejectHandler : " + status);
            if (status == '1')
            { // rejected so move the order to originalID
                //iret = ord.setOrdersWithReplacedFixResponse(origOrdID, "CANCEL_REJECTED", ExchangeOrderId, ref os);
                iret = ord.setOrdersWithFixResponse(OrderID, "CANCEL_REJECTED", ExchangeOrderId, ref os);
            }
            else if (status == '2')
            { // rejected so move the order to originalID
                //iret = ord.setOrdersWithReplacedFixResponse(origOrdID, "MODIFY_REJECTED", ExchangeOrderId, ref os);
                iret = ord.setOrdersWithFixResponse(OrderID, "MODIFY_REJECTED", ExchangeOrderId, ref os);
            }
            if (iret == -1)
            {
                Console.WriteLine("Error from DB [" + data + "]");
                //return -1;
            }
            else
            {
                Console.WriteLine("Order Updated");
            }
            data = new string(os.machineID) + ":" + new string(os.userID) + ":" + os.OrderNo + ":" + new string(os.OrderStatus) + ":" + ExchangeOrderId + ":" + status + ":" + status_msg + ":" + other_msg;
            OMFillPub ofp = new OMFillPub();

            if (ofp.zmqUpdates(data) == 0)
            {
                Console.WriteLine("Zmq update success");
            }
            else
            {
                Console.WriteLine("Zmq update fail");
            }
            ord.addFIXResponse(OrderID, ExchangeOrderId, status, status_msg, other_msg);
            return(0);
        }
Esempio n. 9
0
 public int cancelOrder(OrderStruct os)//Update the previouss and add new
 {
     try
     {
         int ver     = -1;
         int id      = -1;
         int orderID = getNextSeq(orderIDSeq);
         //getLastVersion(os.OrderNo, ref ver, ref id);
         if (ver == 0)
         {
             ver = 1;
         }
         else
         {
             ver++;
         }
         //Console.WriteLine("DBG: " + os.OrderNo + " : " + ver + " : " + id);
         //os.display();
         //return -1;
         using (var cmd = new SqlCommand())
         {
             cmd.Connection = conn;
             string symbol = sanitiseField(os.symbol);
             string query  = "INSERT INTO ORDERS (ID,OrderNo,LinkedOrderID,OrderStatus,symbol,price, strike, quantity,direction,version,expiry, callput, exch, machineID,userID,insertTS) VALUES ("
                             + orderID + "," + os.OrderNo + "," + os.ID + "," + "'CANCELED'" + ",'" + symbol + "'," + os.price + "," + os.strike + "," + os.quantity + ",'"
                             + os.direction + "'," + os.version + ",'" + new string(os.expiry) + "','" + new string(os.callput) + "','" + new string(os.exch) + "','"
                             + new string(os.machineID) + "','" + new string(os.userID) + "',SYSDATETIME());";
             cmd.CommandText = query;
             Console.WriteLine("query : {0}", query);
             cmd.ExecuteNonQuery();
             return(0);
         }
     }
     catch (Exception ex)
     {
         Console.Write("Exception (Cancel) : " + ex.Message);
     }
     return(-1);
 }
Esempio n. 10
0
        public int FIXResponseHandlerForFill(int OrderID, string ExchangeOrderId, float qty, float price, float filledQty, string status, char exectype)
        {
            OrderDAO    ord     = new OrderDAO();
            OrderStruct os      = new OrderStruct();
            int         orderNo = ord.getOrderNoFromOrderID(OrderID);

            ord.addOrderFillsFromFIX(OrderID, orderNo, ExchangeOrderId, qty, price, filledQty, status);
            ord.getOrderFromDB(orderNo, ref os);
            float     pos       = ord.getCurrentPosition(os.tokenID, new string(os.userID));
            string    other_msg = Convert.ToString(pos);
            string    data      = new string(os.machineID) + ":" + new string(os.userID) + ":" + os.OrderNo + ":" + new string(os.OrderStatus) + ":" + ExchangeOrderId + ":" + exectype + ":" + status + ":" + other_msg;
            OMFillPub ofp       = new OMFillPub();

            if (ofp.zmqUpdates(data) == 0)
            {
                Console.WriteLine("Zmq update success");
            }
            else
            {
                Console.WriteLine("Zmq update fail");
            }
            return(0);
        }
Esempio n. 11
0
        public int amendOrder(ref OrderStruct os)
        {
            if (!isOrderAmendPossible(os.OrderStatus))
            {
                return(-1);
            }
            OrderDAO ord      = new OrderDAO();
            int      newOrdID = ord.amendOrder(ref os); // OrderID is the last FixAccepted version

            Console.WriteLine("TODO: Send FIX - cancel for os.LinkedOrderID ; Send FIX - New for os.ID");
            OrigClOrdID origCLOrdID = new OrigClOrdID(Convert.ToString(os.ID));
            ClOrdID     OrdId       = new ClOrdID(Convert.ToString(newOrdID));
            string      symboldata  = sanitiseField(os.symbol);
            Symbol      symbol      = new Symbol(symboldata);
            Side        side        = new Side(Side.BUY);

            if (os.direction == 'S')
            {
                side = new Side(Side.SELL);
            }
            OrdType ordType = new OrdType(OrdType.LIMIT);

            if (os.orderType == 1)
            {
                ordType = new OrdType(OrdType.MARKET);
            }
            var orderMod = new QuickFix.FIX42.OrderCancelReplaceRequest(origCLOrdID, OrdId, new HandlInst('1'), symbol, side, new TransactTime(DateTime.Now.ToUniversalTime()), ordType);
            int Qty      = (int)os.quantity;

            orderMod.Price = new Price((decimal)os.price);
            orderMod.SetField(new OrderQty(Qty));
            Console.WriteLine("Modifying origOrdId/FixAcceptedID : " + os.ID + " newOrderID : " + newOrdID);
            Console.WriteLine("symboldata : " + symboldata + " side : " + side + " Price : " + os.price + " qty : " + os.quantity);
            Session.SendToTarget(orderMod, FixClient.MySess);
            return(os.OrderNo);
        }
Esempio n. 12
0
        public int addOrder(OrderStruct os)
        {
            OrderDAO odao = new OrderDAO();

            Console.WriteLine("Going to call DB");
            int ordNo = odao.insertOrder(ref os);

            // Keep the OrderNO for client and pass the OrderID to FIX.
            ClOrdID OrdId      = new ClOrdID(Convert.ToString(os.ID));
            string  symboldata = sanitiseField(os.symbol);

            //check Futures or Options Order
            string exch = new string(os.exch);

            if (exch.Equals("STK"))
            {
                if (os.direction == 'B')
                {
                    Symbol symbol = new Symbol(symboldata);
                    Side   side   = new Side(Side.BUY);
                    int    Qty    = (int)os.quantity;

                    var orderNFO = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), symbol, side, new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));

                    orderNFO.SetField(new OrderQty(Qty));
                    orderNFO.ExDestination = new ExDestination("NS");
                    orderNFO.SecurityType  = new SecurityType(SecurityType.COMMON_STOCK);
                    //string expiry = new string(os.expiry);
                    //orderNFO.MaturityMonthYear = new MaturityMonthYear(expiry.Substring(0, 6));
                    orderNFO.Account = new Account("D2");
                    OrdType ordType = new OrdType(OrdType.LIMIT);
                    if (os.orderType == 1)
                    {
                        ordType = new OrdType(OrdType.MARKET);
                    }
                    orderNFO.OrdType = ordType;

                    orderNFO.Price = new Price((decimal)os.price);

                    Console.WriteLine("STOCK BUY Order for : " + symbol + "orderType : " + ordType);

                    //Send Future order to fix server
                    Session.SendToTarget(orderNFO, FixClient.MySess);
                }
                else if (os.direction == 'S')
                {
                    Symbol symbol = new Symbol(symboldata);
                    Side   side   = new Side(Side.SELL);
                    int    Qty    = (int)os.quantity;

                    var orderNFO = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), symbol, side,
                                                                     new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));

                    orderNFO.SetField(new OrderQty(Qty));

                    orderNFO.ExDestination = new ExDestination("NS");
                    orderNFO.SecurityType  = new SecurityType(SecurityType.COMMON_STOCK);
                    //string expiry = new string(os.expiry);
                    //orderNFO.MaturityMonthYear = new MaturityMonthYear(expiry.Substring(0, 6));
                    orderNFO.Account = new Account("D2");
                    OrdType ordType = new OrdType(OrdType.LIMIT);
                    if (os.orderType == 1)
                    {
                        ordType = new OrdType(OrdType.MARKET);
                    }
                    orderNFO.OrdType = ordType;
                    orderNFO.Price   = new Price((decimal)os.price);
                    Console.WriteLine("STOCK SELL Order for : " + symbol + "orderType : " + ordType);
                    //Send Future order to fix server
                    Session.SendToTarget(orderNFO, FixClient.MySess);
                }
            }
            else if (exch.Equals("NFO"))
            {
                if (os.direction == 'B')
                {
                    Symbol symbol = new Symbol(symboldata);
                    Side   side   = new Side(Side.BUY);
                    int    Qty    = (int)os.quantity;

                    var orderNFO = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), symbol, side,
                                                                     new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));
                    orderNFO.Price = new Price((decimal)os.price);
                    orderNFO.SetField(new OrderQty(Qty));

                    orderNFO.ExDestination = new ExDestination("NS");
                    orderNFO.SecurityType  = new SecurityType(SecurityType.FUTURE);
                    string expiry = new string(os.expiry);
                    orderNFO.MaturityMonthYear = new MaturityMonthYear(expiry.Substring(0, 6));
                    orderNFO.Account           = new Account("D2");
                    OrdType ordType = new OrdType(OrdType.LIMIT);
                    if (os.orderType == 1)
                    {
                        ordType = new OrdType(OrdType.MARKET);
                    }
                    orderNFO.OrdType = ordType;
                    Console.WriteLine("NFO BUY Order for ExpiryDate " + expiry.Substring(0, 6));

                    //Send Future order to fix server
                    Session.SendToTarget(orderNFO, FixClient.MySess);
                }
                else if (os.direction == 'S')
                {
                    Symbol symbol = new Symbol(symboldata);
                    Side   side   = new Side(Side.SELL);
                    int    Qty    = (int)os.quantity;

                    var orderNFO = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), symbol, side,
                                                                     new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));
                    orderNFO.Price = new Price((decimal)os.price);
                    orderNFO.SetField(new OrderQty(Qty));

                    orderNFO.ExDestination = new ExDestination("NS");
                    orderNFO.SecurityType  = new SecurityType(SecurityType.FUTURE);
                    string expiry = new string(os.expiry);
                    orderNFO.MaturityMonthYear = new MaturityMonthYear(expiry.Substring(0, 6));
                    orderNFO.Account           = new Account("D2");
                    OrdType ordType = new OrdType(OrdType.LIMIT);
                    if (os.orderType == 1)
                    {
                        ordType = new OrdType(OrdType.MARKET);
                    }
                    orderNFO.OrdType = ordType;

                    Console.WriteLine("NFO SELL Order for ExpiryDate " + expiry.Substring(0, 6));
                    //Send Future order to fix server
                    Session.SendToTarget(orderNFO, FixClient.MySess);
                }
            }
            else if (exch.Equals("NOP"))
            {
                if (os.direction == 'B')
                {
                    Symbol symbol  = new Symbol(symboldata);
                    Side   side    = new Side(Side.BUY);
                    int    Qty     = (int)os.quantity;
                    string callput = new string(os.callput);

                    var orderNOP = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), symbol, side,
                                                                     new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));
                    orderNOP.Price = new Price((decimal)os.price);
                    orderNOP.SetField(new OrderQty(Qty));

                    orderNOP.ExDestination = new ExDestination("NS");
                    orderNOP.SecurityType  = new SecurityType(SecurityType.OPTION);

                    string expiry = new string(os.expiry);
                    orderNOP.MaturityMonthYear = new MaturityMonthYear(expiry.Substring(0, 6));
                    orderNOP.StrikePrice       = new StrikePrice((decimal)(os.strike));
                    if (callput.Equals("CE")) // 0 = PE & 1 = CE
                    {
                        orderNOP.PutOrCall = new PutOrCall(1);
                    }
                    else if (callput.Equals("PE")) // 0 = PE & 1 = CE
                    {
                        orderNOP.PutOrCall = new PutOrCall(0);
                    }
                    orderNOP.Account = new Account("D2");
                    OrdType ordType = new OrdType(OrdType.LIMIT);
                    if (os.orderType == 1)
                    {
                        ordType = new OrdType(OrdType.MARKET);
                    }
                    orderNOP.OrdType = ordType;
                    Console.WriteLine("NOP BUY Order for ExpiryDate " + expiry.Substring(0, 6));
                    //Send Option order to fix server
                    Session.SendToTarget(orderNOP, FixClient.MySess);
                }
                else if (os.direction == 'S')
                {
                    Symbol symbol  = new Symbol(symboldata);
                    Side   side    = new Side(Side.SELL);
                    int    Qty     = (int)os.quantity;
                    string callput = new string(os.callput);

                    var orderNOP = new QuickFix.FIX42.NewOrderSingle(OrdId, new HandlInst('1'), symbol, side,
                                                                     new TransactTime(DateTime.Now.ToUniversalTime()), new OrdType(OrdType.LIMIT));
                    orderNOP.Price = new Price((decimal)os.price);
                    orderNOP.SetField(new OrderQty(Qty));

                    orderNOP.ExDestination = new ExDestination("NS");
                    orderNOP.SecurityType  = new SecurityType(SecurityType.OPTION);

                    string expiry = new string(os.expiry);
                    orderNOP.MaturityMonthYear = new MaturityMonthYear(expiry.Substring(0, 6));
                    orderNOP.StrikePrice       = new StrikePrice((decimal)(os.strike));
                    if (callput.Equals("CE")) // 0 = PE & 1 = CE
                    {
                        orderNOP.PutOrCall = new PutOrCall(1);
                    }
                    else if (callput.Equals("PE")) // 0 = PE & 1 = CE
                    {
                        orderNOP.PutOrCall = new PutOrCall(0);
                    }
                    orderNOP.Account = new Account("D2");
                    OrdType ordType = new OrdType(OrdType.LIMIT);
                    if (os.orderType == 1)
                    {
                        ordType = new OrdType(OrdType.MARKET);
                    }
                    orderNOP.OrdType = ordType;

                    Console.WriteLine("NOP SELL Order for ExpiryDate " + expiry.Substring(0, 6));
                    //Send Option order to fix server
                    Session.SendToTarget(orderNOP, FixClient.MySess);
                }
            }
            return(ordNo);
        }
Esempio n. 13
0
            private void doProcessing()
            {
                string reqType = null;

                Byte[] sendBytes      = null;
                string serverResponse = null;

                try
                {
                    NetworkStream networkStream = clientSocket.GetStream();
                    Console.WriteLine("Lengeth : " + clientSocket.ReceiveBufferSize);
                    byte[] bytesFrom = new byte[(Int32)clientSocket.ReceiveBufferSize + 1];
                    networkStream.Read(bytesFrom, 0, (Int32)clientSocket.ReceiveBufferSize);
                    //reqType = System.Text.Encoding.ASCII.GetString(bytesFrom, 0, 10);
                    Console.WriteLine(" >> " + "From client-" + clNo);
                    OrderStruct os = getStructFromBytes(bytesFrom);
                    RequestType rt = getReqType(os.methodID);

                    switch (rt)
                    {
                    case RequestType.INS:
                        //DO inert activity
                        //OrderStruct osIns = getStructFromBytes(bytesFrom);
                        Orders ord     = new Orders();
                        int    OrderNo = -1;
                        if ((OrderNo = ord.addOrder(os)) < 0)
                        {
                            serverResponse = "FAILURE:INS:ORDID:" + Convert.ToString(OrderNo);
                        }
                        else
                        {
                            serverResponse = "SUCCESS:INS:ORDID:" + Convert.ToString(OrderNo);
                        }
                        break;

                    case RequestType.AMD:
                        //Do amendment activity, cancel the previous and boo new.
                        //OrderStruct osAmd = getStructFromBytes(bytesFrom);
                        Orders AmdOrd     = new Orders();
                        int    newOrderNo = -1;
                        int    oldOrderNo = os.OrderNo;
                        if ((newOrderNo = AmdOrd.amendOrder(ref os)) < 0)
                        {
                            serverResponse = "FAILURE:AMD:OLD-ORDID:" + oldOrderNo;
                        }
                        else
                        {
                            serverResponse = "SUCCESS:AMD:OLD-ORDID:" + oldOrderNo + ":NEW-ORDID:" + newOrderNo;
                        }
                        break;

                    case RequestType.CAN:
                        //Do cancel activity
                        Orders ordCan     = new Orders();
                        int    OrderNoCan = -1;
                        if ((OrderNoCan = ordCan.cancelOrder(os)) < 0)
                        {
                            serverResponse = "FAILURE:CAN:ORDID:" + Convert.ToString(os.OrderNo);
                        }
                        else
                        {
                            serverResponse = "SUCCESS:CAN:ORDID:" + Convert.ToString(OrderNoCan);
                        }
                        break;

                    case RequestType.FIL:
                        //Do cancel activity
                        Orders          ordFilDummy = new Orders();
                        OrderFillStruct ofs         = new OrderFillStruct();
                        Random          r           = new Random();
                        ofs.FillID         = r.Next();
                        ofs.OrderNo        = os.OrderNo;
                        ofs.Quantity       = os.quantity;
                        ofs.FilledQuantity = os.quantity;
                        ofs.Price          = os.price;
                        if (ordFilDummy.addFills(ofs) < 0)
                        {
                            serverResponse = "FAILURE:FIL:ORDID:" + Convert.ToString(os.OrderNo);
                        }
                        else
                        {
                            serverResponse = "SUCCESS:FIL:ORDID:" + Convert.ToString(os.OrderNo);
                        }
                        break;

                    case RequestType.EOD:
                        Orders eodObj      = new Orders();
                        int    totalStatus = -1;
                        if ((totalStatus = eodObj.takeEODPositions()) < 0)
                        {
                            serverResponse = "FAILURE:EOD:" + totalStatus;
                        }
                        else
                        {
                            serverResponse = "FAILURE:EOD:" + totalStatus;
                        }
                        break;

                    default:
                        //Retrn generic error
                        reqType        = System.Text.Encoding.ASCII.GetString(bytesFrom);
                        serverResponse = "ERROR:UNK:ORDID:[" + reqType + "]";
                        break;
                    }
                    sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();
                    this.clientSocket.Close();
                    Console.WriteLine(" >> Closing connection : " + serverResponse);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception :  >> " + ex.ToString());
                }
            }
Esempio n. 14
0
 public int getOrderFromDB(int OrderNo, ref OrderStruct os)
 {
     try
     {
         //TODO: Add the fills from fills tble to struct;
         using (var cmd = new SqlCommand())
         {
             cmd.Connection  = conn;
             cmd.CommandText = "SELECT ID,OrderNo,OrderStatus,symbol,price, quantity,direction,version,machineID,userID,insertTS FROM ORDERS where version = (select max(version) from orders where orderNo = " + OrderNo + ") and orderNo = " + OrderNo + ";";
             using (var reader = cmd.ExecuteReader())
             {
                 while (reader.Read())
                 {
                     var tmp = reader["ID"];
                     if (tmp != DBNull.Value)
                     {
                         os.ID = Convert.ToInt32((Int64)tmp);
                     }
                     tmp = reader["OrderNo"];
                     if (tmp != DBNull.Value)
                     {
                         os.OrderNo = Convert.ToInt32((Int64)tmp);
                     }
                     tmp = reader["OrderStatus"];
                     if (tmp != DBNull.Value)
                     {
                         os.OrderStatus = ((string)tmp).ToCharArray();
                     }
                     tmp = reader["symbol"];
                     if (tmp != DBNull.Value)
                     {
                         os.symbol = ((string)tmp).ToCharArray();
                     }
                     tmp = reader["price"];
                     if (tmp != DBNull.Value)
                     {
                         os.price = (float)((Decimal)tmp);
                     }
                     tmp = reader["quantity"];
                     if (tmp != DBNull.Value)
                     {
                         os.quantity = (float)((Decimal)tmp);
                     }
                     tmp = reader["direction"];
                     if (tmp != DBNull.Value)
                     {
                         os.direction = ((string)tmp).ToCharArray()[0];
                     }
                     tmp = reader["version"];
                     if (tmp != DBNull.Value)
                     {
                         os.version = (int)Convert.ToInt32((Int32)tmp);
                     }
                     tmp = reader["machineID"];
                     if (tmp != DBNull.Value)
                     {
                         os.machineID = ((string)tmp).ToCharArray();
                     }
                     tmp = reader["userID"];
                     if (tmp != DBNull.Value)
                     {
                         os.userID = ((string)tmp).ToCharArray();
                     }
                 }
             }
         }
         return(0);
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception(getOrderStatusFromDB) : " + e.Message + "\n" + e.StackTrace);
         return(-1);
     }
 }