예제 #1
0
        private double ExecuteOrder(FuturesContract futuresContract, SignalFile signalFile, OrderDetails orderDetails, Strategy strategy, SimpleLogger log, Tuple <bool, bool, int> db)
        {
            Prices prices = new Prices();

            prices = prices.StartListener(clientSocket, contract, strategy, prices, log);

            double limitPrice = GetPrices(futuresContract, signalFile, strategy, orderDetails, prices, db);

            if (limitPrice <= 0)
            {
                limitPrice = signalFile.LimitPrice;
                log.Warning($"LimitPrice for {strategy.StrategyName} is null using signalFile price {signalFile.LimitPrice}");
            }

            //Use market orders overnight so we don't miss getting filled
            //string orderType = OverNight(signalFile, db);

            //if (string.IsNullOrEmpty(orderType))
            //{
            //    orderType = strategy.OrderType;
            //}

            Order        parent    = new Order();
            Order        openOrder = new Order();
            List <Order> orders    = new List <Order>();

            if (db.Item3 != 0) //If there is a position
            {
                //Use market orders overnight so we don't miss getting filled
                string orderType = OverNight(signalFile, db);

                //if (string.IsNullOrEmpty(orderType))
                //{
                //    orderType = strategy.OrderType;
                //}

                //Close position with market order
                parent = new Order()
                {
                    OrderId = OrderDetails.NextOrderId++,

                    Action        = orderDetails.BuyOrSell,
                    OrderType     = (string.IsNullOrEmpty(orderType) ? strategy.OrderType : orderType),     //strategy.OrderType, //"MKT",
                    TotalQuantity = strategy.LotSize,
                    LmtPrice      = (orderDetails.BuyOrSell == "SELL") ? prices.BidPrice : prices.AskPrice, //limitPrice,
                    AuxPrice      = (orderDetails.BuyOrSell == "SELL") ? prices.BidPrice : prices.AskPrice, //limitPrice,
                    Tif           = "GTC",

                    Transmit = true
                };

                openOrder = new Order()
                {
                    OrderId       = OrderDetails.NextOrderId++,
                    Action        = orderDetails.BuyOrSell,
                    OrderType     = strategy.OrderType, //(strategy.OrderType == "MKT" ? strategy.OrderType : orderType),
                    TotalQuantity = strategy.LotSize,
                    LmtPrice      = limitPrice,
                    AuxPrice      = limitPrice,
                    Tif           = "GTC",
                    Transmit      = true
                };

                orders = new List <Order>()
                {
                    parent,
                    openOrder
                };
            }
            else
            {
                openOrder = new Order()
                {
                    OrderId = OrderDetails.NextOrderId++,

                    Action        = orderDetails.BuyOrSell,
                    OrderType     = strategy.OrderType, //(strategy.OrderType == "MKT" ? strategy.OrderType : orderType),
                    TotalQuantity = orderDetails.LotSize,
                    LmtPrice      = limitPrice,
                    AuxPrice      = limitPrice,
                    Tif           = "GTC",

                    Transmit = true
                };
                orders = new List <Order>()
                {
                    openOrder
                };
            }

            foreach (Order o in orders)
            {
                clientSocket.placeOrder(o.OrderId, contract, o);
            }

            orderDetails.OrderId     = parent.OrderId.ToString();
            orderDetails.OrderStatus = "Submitted";

            if (db.Item3 != 0)
            {
                log.Signal($"Sent Closing {parent.Action} {parent.OrderType} Order for {strategy.Symbol} LimitPrice:{parent.LmtPrice} LotSize:{parent.TotalQuantity} OrderId:{parent.OrderId}");
                log.Signal($"Sent Opening {openOrder.Action} {openOrder.OrderType} Order for {strategy.Symbol} LimitPrice:{openOrder.LmtPrice} LotSize:{openOrder.TotalQuantity} OrderId:{openOrder.OrderId}");
            }
            else
            {
                log.Signal($"Sent Opening {openOrder.Action} {openOrder.OrderType} Order for {strategy.Symbol} LimitPrice:{openOrder.LmtPrice} LotSize:{openOrder.TotalQuantity} OrderId:{openOrder.OrderId}");
            }

            OrderDetails.OrderIdList.DataList.Add(orderDetails.OrderId);

            return(limitPrice);
        }