コード例 #1
0
ファイル: Controller.cs プロジェクト: grim888/order-execution
 public Controller(EClientSocket clientSocket, SimpleLogger log, Parameters @params, Strategy strategy, Broker broker, Positions positions, FuturesContract futures, Prices prices, Transactions transactions, Slack slack)
 {
     this.@params      = @params;
     this.log          = log;
     this.clientSocket = clientSocket;
     this.strategy     = strategy;
     this.broker       = broker;
     this.futures      = futures;
     this.prices       = prices;
     this.transactions = transactions;
     this.slack        = slack;
 }
コード例 #2
0
        public static void Main(string[] args)
        {
            EWrapperImpl  ibClient     = new EWrapperImpl();
            EClientSocket clientSocket = ibClient.ClientSocket;


            Console.ForegroundColor = ConsoleColor.DarkGreen;
            string logo = @"
 /$$$$$$$  /$$                     /$$        /$$$$$$                      /$$   /$$               /$$       /$$$$$$                        
| $$__  $$| $$                    | $$       /$$__  $$                    |__/  | $$              | $$      |_  $$_/                        
| $$  \ $$| $$  /$$$$$$   /$$$$$$$| $$   /$$| $$  \__/  /$$$$$$   /$$$$$$  /$$ /$$$$$$    /$$$$$$ | $$        | $$   /$$$$$$$   /$$$$$$$    
| $$$$$$$ | $$ /$$__  $$ /$$_____/| $$  /$$/| $$       |____  $$ /$$__  $$| $$|_  $$_/   |____  $$| $$        | $$  | $$__  $$ /$$_____/    
| $$__  $$| $$| $$  \ $$| $$      | $$$$$$/ | $$        /$$$$$$$| $$  \ $$| $$  | $$      /$$$$$$$| $$        | $$  | $$  \ $$| $$          
| $$  \ $$| $$| $$  | $$| $$      | $$_  $$ | $$    $$ /$$__  $$| $$  | $$| $$  | $$ /$$ /$$__  $$| $$        | $$  | $$  | $$| $$          
| $$$$$$$/| $$|  $$$$$$/|  $$$$$$$| $$ \  $$|  $$$$$$/|  $$$$$$$| $$$$$$$/| $$  |  $$$$/|  $$$$$$$| $$       /$$$$$$| $$  | $$|  $$$$$$$ /$$
|_______/ |__/ \______/  \_______/|__/  \__/ \______/  \_______/| $$____/ |__/   \___/   \_______/|__/      |______/|__/  |__/ \_______/|__/
                                                                | $$                                                                        
                                                                | $$                                                                        
                                                                |__/                                                                        

                            ";

            Console.WriteLine(logo);
            Console.ResetColor();

            Console.Write("BlockShift Trading App");
            Console.Write("\n");
            Console.Write("DISCLAIMER: USING THIS SOFTWARE AT MY OWN RISK\n");
            Console.Write("\n");

            try
            {
                ConsoleWindow.QuickEditMode(false);

                Parameters @params = new Parameters(Path.Combine(Environment.CurrentDirectory, "config.conf"));

                SimpleLogger log = new SimpleLogger();

                Slack slack = new Slack(@params);

                Prices prices = new Prices(log, @params);

                FuturesContract futures = new FuturesContract(@params);

                Strategy strategy = new Strategy(log);

                Broker broker = new Broker(clientSocket, log, @params, slack);

                Positions positions = new Positions(log, @params, slack);

                Transactions transactions = new Transactions(log);

                Controller ctl = new Controller(clientSocket, log, @params, strategy, broker, positions, futures, prices, transactions, slack);

                broker.ConnectionToBroker(strategy, positions);

                ctl.Run();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message.ToString());
                Console.Write(ex.StackTrace.ToString());
            }


            Console.ReadLine();
        }
コード例 #3
0
ファイル: Controller.cs プロジェクト: grim888/order-execution
        private double GetBrokerLimitPrice(FuturesContract futuresContract, OrderDetails orderDetails, Prices prices, Strategy strategy, Tuple <bool, bool, int> db, SignalFile signalFile)
        {
            double tick       = FuturesContract.GetTickSize(futuresContract);
            double limitPrice = 0;

            switch (strategy.Symbol)
            {
            case "QM":
                //tick *= (db.Item3 == 0 || db.Item3 != 0) ? @params.crudeSpread : 1;
                //if (signalFile.Macd == 1)
                //{
                //    tick *= (signalFile.Trend < 40 && orderDetails.BuyOrSell == "SELL") ? @params.crudeSpread : 4;
                //}
                //else if (signalFile.Macd == 2)
                //{
                //    tick *= (signalFile.Trend < 40 && orderDetails.BuyOrSell == "SELL") ? @params.crudeSpread : 3;
                //}
                //if (signalFile.Macd == -1)
                //{
                //    tick *= (signalFile.Trend < 40 && orderDetails.BuyOrSell == "BUY") ? @params.crudeSpread : 4;
                //}
                //else if (signalFile.Macd == -2)
                //{
                //    tick *= (signalFile.Trend < 40 && orderDetails.BuyOrSell == "BUY") ? @params.crudeSpread : 3;
                //}

                if (orderDetails.BuyOrSell == "BUY")
                {
                    //If Up trend than our bid is spread = 2 and offer is  @params.crudeSpread = 5
                    //tick *= (signalFile.EMAAngle < 40 && signalFile.EMAAngle > -40) ? @params.crudeSpread : 8;
                    tick *= (signalFile.Trend < 40) ? @params.crudeSpread : 4;

                    limitPrice  = Math.Min(prices.BidPrice, signalFile.LimitPrice);    //prices.BidPrice;
                    limitPrice -= tick;
                }
                else if (orderDetails.BuyOrSell == "SELL")
                {
                    //If Down trend than our offer is spread = 2 and out bid is @params.crudeSpread = 5
                    //tick *= (signalFile.EMAAngle < 40 && signalFile.EMAAngle > -40) ? @params.crudeSpread : 8;
                    tick *= (signalFile.Trend < 40) ? @params.crudeSpread : 4;

                    limitPrice  = Math.Max(prices.AskPrice, signalFile.LimitPrice);    //prices.AskPrice;
                    limitPrice += tick;
                }
                return(limitPrice);

            case "MES":
                //tick *= (db.Item3 == 0 || db.Item3 != 0) ? @params.sp500Spread : 1;
                //tick *= (signalFile.EMAAngle < 40 && signalFile.EMAAngle > -40) ? @params.sp500Spread : @params.sp500Spread ;
                tick *= (signalFile.Trend < 40) ? @params.sp500Spread : 5;

                if (orderDetails.BuyOrSell == "BUY")
                {
                    limitPrice  = Math.Min(prices.BidPrice, signalFile.LimitPrice);
                    limitPrice -= tick;
                }
                else if (orderDetails.BuyOrSell == "SELL")
                {
                    limitPrice  = Math.Max(prices.AskPrice, signalFile.LimitPrice);
                    limitPrice += tick;
                }
                return(limitPrice);
            }
            return(limitPrice);
        }
コード例 #4
0
ファイル: Controller.cs プロジェクト: grim888/order-execution
        private double GetPrices(FuturesContract futuresContract, SignalFile signalFile, Strategy strategy, OrderDetails orderDetails, Prices prices, Tuple <bool, bool, int> db)
        {
            double limitPrice = 0;

            if (prices.BidPrice > 0 && prices.AskPrice > 0)
            {
                limitPrice = GetBrokerLimitPrice(futuresContract, orderDetails, prices, strategy, db, signalFile);
            }
            else if (limitPrice <= 0)
            {
                limitPrice = GetSignalFileLimitPrice(futuresContract, orderDetails, signalFile, strategy, db);
            }

            return(limitPrice);
        }
コード例 #5
0
ファイル: Controller.cs プロジェクト: grim888/order-execution
        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);
        }
コード例 #6
0
        public Prices StartListener(EClientSocket clientSocket, Contract contract, Strategy strategy, Prices prices, SimpleLogger log)
        {
            string ticker;
            string strBid;
            string strAsk;

            double bid;
            double ask;

            int maxLoopCount = 30;

            Prices.IBDepthBidPriceFlag = false;
            Prices.IBDepthAskPriceFlag = false;

            if (strategy.Symbol != "MGC")
            {
                int cnt = 0;

                clientSocket.reqMarketDepth(strategy.StrategyId, contract, 1, null);

                while (true)
                {
                    ticker = SignalFile.GetWantedText(Prices.SideOne, "TickerId:");
                    strBid = SignalFile.GetWantedText(Prices.SideOne, "Price:");
                    strAsk = SignalFile.GetWantedText(Prices.SideTwo, "Price:");

                    if (Prices.IBDepthBidPriceFlag && Prices.IBDepthAskPriceFlag)
                    {
                        bid = Convert.ToDouble(strBid);
                        ask = Convert.ToDouble(strAsk);

                        if (bid > 0 && ask > 0)
                        {
                            prices.BidPrice = bid;
                            prices.AskPrice = ask;

                            clientSocket.cancelMktDepth(strategy.StrategyId);
                            log.Info($"Received Market Depth for TickerId:{ticker}, Symbol:{strategy.Symbol}, Bid:{strBid}, Ask:{strAsk}");
                            break;
                        }
                        else
                        {
                            log.Info($"Unable to get Market Depth for TickerId:{ticker}, Market Depth Bid:{strBid}, Ask:{strAsk}");
                            clientSocket.cancelMktDepth(strategy.StrategyId);
                            break;
                        }
                    }
                    if (cnt > maxLoopCount)
                    {
                        clientSocket.cancelMktDepth(strategy.StrategyId);

                        log.Warning($"Unable to get Market Depth for TickerId:{ticker}, Symbol:{strategy.Symbol}, Market Depth Bid:{strBid}, Ask:{strAsk}");
                        break;
                    }

                    log.Info($"Getting Market Depth... {cnt}");
                    cnt++;
                }
            }

            if (prices.BidPrice <= 0 || prices.AskPrice <= 0)
            {
                int cnt = 0;

                clientSocket.reqMktData(strategy.StrategyId, contract, "", false, null);

                Prices.IBBidPriceFlag = false;
                Prices.IBAskPriceFlag = false;

                while (true)
                {
                    ticker = SignalFile.GetWantedText(Prices.SideOne, "TickerId:");
                    strBid = SignalFile.GetWantedText(Prices.FieldOne, "Price:");
                    strAsk = SignalFile.GetWantedText(Prices.FieldTwo, "Price:");

                    bid = Convert.ToDouble(strBid);
                    ask = Convert.ToDouble(strAsk);

                    if (Prices.IBBidPriceFlag && Prices.IBAskPriceFlag)
                    {
                        if (bid > 0 && ask > 0)
                        {
                            prices.BidPrice = bid;
                            prices.AskPrice = ask;

                            clientSocket.cancelMktData(strategy.StrategyId);
                            log.Info($"Received Snapshot Data for TickerId:{ticker}, Symbol:{strategy.Symbol}, Bid:{strBid}, Ask:{strAsk}");
                            break;
                        }
                        else
                        {
                            log.Info($"Unable to get Snapshot Data for TickerId:{ticker}, Symbol:{strategy.Symbol}, Bid:{strBid}, Ask:{strAsk}");
                            clientSocket.cancelMktDepth(strategy.StrategyId);
                            break;
                        }
                    }
                    if (cnt >= maxLoopCount)
                    {
                        clientSocket.cancelMktData(strategy.StrategyId);

                        log.Warning($"Unable to get Snapshot Data for TickerId:{ticker}, Symbol:{strategy.Symbol}, Bid:{strBid}, Ask:{strAsk}");
                        break;
                    }

                    log.Info($"Getting Price Snapshot Data... {cnt}");
                    cnt++;
                }
            }
            return(prices);
        }