Esempio n. 1
0
 public void CancelOrders(List <TradeOrder> orders)
 {
     lock (trade_locker)
     {
         foreach (var order in orders)
         {
             try
             {
                 Log.Info(string.Format("Cancel orderID: {0}", order.OrderID));
                 if (Broker == Broker.IB)
                 {
                     IBClient.ClientSocket.cancelOrder(order.OrderID);
                 }
                 else if (Broker == Broker.TD)
                 {
                     TDClient.CancelOrder(order.OrderID);
                 }
             }
             catch (Exception ex)
             {
                 Log.Error("Failed to cancel order. Error: " + ex.Message);
             }
         }
     }
 }
Esempio n. 2
0
        public TradeOrder PlaceOrder(int parentOrderID, TradeType tradeType, string symbol, double price, double qty, string exchange = null, OrderType orderType = OrderType.LMT)
        {
            if (tradeType != TradeType.Buy && tradeType != TradeType.Sell)
            {
                throw new Exception("Unsupported TradeType: " + tradeType);
            }
            TradeOrder res = null;

            var parent = ParentOrderManager.Instance.GetParentOrderByParentID(parentOrderID);

            if (tradeType == TradeType.Sell && parent.Qty == 0)
            {
                Log.Error("Parent zero qty");
                return(res);
            }
            if (tradeType == TradeType.Sell && parent.Qty <= qty)
            {
                qty = parent.Qty;
                Log.Info("Reduce qty to parent open qty " + qty);
            }



            lock (trade_locker)
            {
                int orderID = -1;
                if (Broker == Broker.IB)
                {
                    orderID = IBClient.PlaceOrder(symbol, price, qty, tradeType, exchange, orderType);
                }
                else
                {
                    orderID = TDClient.PlaceOrder(symbol, price, qty, tradeType, exchange, orderType);
                }
                res = new TradeOrder();
                res.ParentOrderID = parentOrderID;
                res.OrderID       = orderID;
                res.Status        = TradeOrderStatus.PendingSubmit;
                res.Side          = tradeType.ToString();
                res.Price         = price;

                ParentOrderManager.Instance.AddChildOrder(res);

                if (IsInitialized)
                {
                    StateManager.Save();
                }
            }

            Log.Info(string.Format("Place order ID {0}, TradeType {1}, symbol {2}, price {3}, qty {4}, exchange {5}, parentOrderID {6}",
                                   res.OrderID, tradeType, symbol, price, qty, exchange, parentOrderID));

            return(res);
        }
Esempio n. 3
0
 public void RequestGlobalCancel()
 {
     lock (trade_locker)
     {
         try
         {
             Log.Info("Request Global cancel!");
             if (Broker == Broker.IB)
             {
                 IBClient.ClientSocket.reqGlobalCancel();
             }
             else if (Broker == Broker.TD)
             {
                 TDClient.RequestGlobalCancel();
             }
         }catch (Exception ex)
         {
             Log.Error("Failed to request global cancel. Error: " + ex.Message);
             Log.Error(ex.StackTrace);
         }
     }
 }
Esempio n. 4
0
 public TDOrder GetTDOrderById(int orderId)
 {
     return(TDClient.GetOrderByID(orderId));
 }