Exemple #1
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);
        }
Exemple #2
0
        public TradeOrder PlaceTrailStopOrder(int parentOrderID, TradeType tradeType, string symbol, double qty, double trailStopPrice, double trailPct, string exchange = null)
        {
            TradeOrder res     = null;
            int        orderID = -1;

            lock (trade_locker)
            {
                if (Broker == Broker.IB)
                {
                    orderID = IBClient.PlaceTrailStopOrder(symbol, qty, trailStopPrice, trailPct, tradeType, exchange);
                }
                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.TrailStopPrice = trailStopPrice;
                res.TrailingPct    = trailPct;

                ParentOrderManager.Instance.AddChildOrder(res);

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

            Log.Info(string.Format("Place trailingstop order ID {0}, TradeType {1}, symbol {2}, qty {3}, trailStoppPrice {4}, trailingPct {5}%, exchange {6}",
                                   orderID, tradeType, symbol, qty, trailStopPrice, trailPct * 100, exchange));

            return(res);
        }