Example #1
0
        public static void LoadPositions(Strategy strategy, Positions positions, SimpleLogger log)
        {
            foreach (string line in Positions.PositionList)
            {
                string symbol = SignalFile.GetWantedText(line, "Symbol:");

                if (strategy.Symbol == symbol)
                {
                    positions.IBSymbol = strategy.Symbol;
                    string pos = SignalFile.GetWantedText(line, "Position:");
                    positions.IBCurrentPosition = Convert.ToInt32(pos);
                    break;
                }
            }

            if (string.IsNullOrEmpty(positions.IBSymbol))
            {
                int strategyPosition = 0;//DBFunctions.GetCurrentStrategyPositionFromDB(log, strategy);

                Positions.SetIBPosition(strategy, strategyPosition, positions);

                log.Broker($"IB Position does not exist. Setting position for symbol: {strategy.Symbol}, position: {strategyPosition}");
                //log.Broker($"Using curr_Strategy_Position table for Positions Update. symbol: {strategy.Symbol}, position: {strategyPosition}");
            }
        }
Example #2
0
        private bool CancelOpenOrders(Strategy strategy, bool dbconflict, OrderDetails orderDetails, bool allOpenOrder)
        {
            bool openOrderstatus = false;

            OpenOrder.OpenOrderFlag = false;
            int cnt = 0;

            if (strategy.OrderType == "LMT")
            {
                OpenOrder.OpenOrderList.DataList.Clear();

                clientSocket.reqAllOpenOrders();

                while (!OpenOrder.OpenOrderFlag)
                {
                    if (OpenOrder.OpenOrderFlag)
                    {
                        break;
                    }

                    if (cnt > @params.maxLoopCount)
                    {
                        log.Warning("[IB WARNING] Could not update IB Open Order.");
                        break;
                    }

                    log.Warning($"Waiting for IB Open Order to Update. Try # {cnt}");
                    cnt++;
                }

                List <int> cancelList = new List <int>();

                foreach (string openOrder in OpenOrder.OpenOrderList.DataList)
                {
                    OpenOrder.Symbol     = SignalFile.GetWantedText(openOrder, "Symbol:");
                    OpenOrder.OrderId    = SignalFile.GetWantedText(openOrder, "Id:");
                    OpenOrder.OrderState = SignalFile.GetWantedText(openOrder, "OrderState:");
                    OpenOrder.Action     = SignalFile.GetWantedText(openOrder, "Action:");

                    if (allOpenOrder && OpenOrder.Symbol == strategy.Symbol && OpenOrder.OrderState.Contains("Submit"))
                    {
                        int id = Convert.ToInt32(OpenOrder.OrderId);
                        cancelList.Add(id);
                    }
                    else if (dbconflict && OpenOrder.Action == orderDetails.BuyOrSell && OpenOrder.Symbol == strategy.Symbol && OpenOrder.OrderState.Contains("Submit"))
                    {
                        int id = Convert.ToInt32(OpenOrder.OrderId);
                        cancelList.Add(id);
                    }
                    else if (!dbconflict && OpenOrder.Symbol == strategy.Symbol && OpenOrder.OrderState.Contains("Submit"))
                    {
                        int id = Convert.ToInt32(OpenOrder.OrderId);
                        cancelList.Add(id);
                    }
                }

                if (cancelList.Count() > 0)
                {
                    foreach (int id in cancelList)
                    {
                        clientSocket.cancelOrder(id);
                        log.Warning($"Cancelling open order for {strategy.StrategyName} OrderId: {id}.");
                        openOrderstatus = true;
                    }
                }
            }

            return(openOrderstatus);
        }
Example #3
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);
        }