示例#1
0
文件: Scanner.cs 项目: mcorrera/IB
        /// <summary>
        /// Adds the scanner TickerList to the database
        /// </summary>
        /// <param name="reqId">The ID of the scanner</param>
        private void writeScanner(int reqId)
        {
            Log.Info(0, string.Format("writeScanner Start " + reqId));

            // Only add the list if we are not busy
            if (Program.myIB.myRequests.NumActiveRequests < 10)
            {
                if (reqId == 300 && actives.Contracts.Count() != 0)
                {
                    MSQL.AddSecurityList(actives);
                    actives.Clear();
                }

                if (reqId == 301 && gainers.Contracts.Count() != 0)
                {
                    MSQL.AddSecurityList(gainers);
                    gainers.Clear();
                }

                if (reqId == 302 && losers.Contracts.Count() != 0)
                {
                    MSQL.AddSecurityList(losers);
                    losers.Clear();
                }
            }

            Log.Info(0, string.Format("writeScanner End. " + reqId));
        }
示例#2
0
        /// <summary>
        /// Called when all of the bars have been received
        /// </summary>
        /// <param name="reqId"></param>
        public void EndHistoricalBar(int reqId)
        {
            myRequests.Release(reqId);
            int    conId   = myRequests.GetConId(reqId);
            int    routeId = myRequests.GetRouteId(reqId);
            string symbol  = myRequests.GetSymbol(reqId);

            if (symbol == null)
            {
                Log.Error(3, string.Format("EndHistoricalBar Null Symbol ConId= {0}", conId));
            }
            Security tempSecurity = new Security
            {
                ConId   = conId,
                Symbol  = symbol,
                SecType = Security.SecurityType.STK
            };

            switch (routeId)
            {
            case 0:
                // Route = is an update to an existing security in the DB
                Log.Data(2, string.Format("EndHistoricalBar {0,-10} {1,-8}", conId, symbol));
                myBDL.Trim(TradingTime.TradingMinDate, TradingTime.TradingMaxDate);
                MSQL.WriteBarListDB(reqId, conId, symbol, myBDL, true);
                break;

            case 1:
                // Route = 1 means this is a new security to the DB
                Log.Data(3, string.Format("EndHistoricalBar Route 1 {0,-10} {1,-8}", conId, symbol));
                //Remove any days that our outside our date range
                myBDL.Trim(TradingTime.TradingMinDate, TradingTime.TradingMaxDate);

                // Only add if Average Daily Volume is above minimum
                if (myBDL.CalcAvgVolume() > 2000)
                {
                    Log.Data(3, string.Format("Added Volume {0,-10} {1,-8} {2,8}", conId, symbol, myBDL.CalcAvgVolume()));
                    MSQL.WriteBarListDB(reqId, conId, symbol, myBDL, true);
                    IB.masterSecurityList.Add(tempSecurity);
                }
                else
                {
                    // Average Daily Volume is too small, do not add to the DB
                    Log.Data(3, string.Format("Skipped Volume {0,-10} {1,-8} {2,8}", conId, symbol, myBDL.CalcAvgVolume()));
                    BlackList.Add(tempSecurity);
                    Program.myIB.myRequests.Delete(reqId);
                    Program.myIB.myBDL.Clear();
                }
                break;

            default:
                Log.Error(3, string.Format("RouteId Error = {0}", routeId));
                break;
            }
        }
示例#3
0
文件: Program.cs 项目: mcorrera/IB
        public static void Main(string[] args)
        {
            // Third Branch
            myIB = new IB();
            //myIB.myScanner.ScanOn();


            string previousDay = TradingTime.PreviousTradingDay(DateTime.Now).ToString("yyyy-MM-dd");
            string currentDay  = DateTime.Now.ToString("yyyy-MM-dd");

            while (TradingTime.IsPreOpening)
            {
                TradingTime.Display();
                MSQL.DailyUpdate();
                DataRequests.GetMostActive(previousDay, 150).Reverse().DisplayScreen();


                Console.WriteLine("MaxActiveRequests= {0} MaxOpenRequests= {1} OldestRequest= {2}", myIB.myRequests.MaxActiveRequests, myIB.myRequests.MaxOpenRequests, myIB.myRequests.OldestRequest);
            }

            while (TradingTime.IsMarketOpen)
            {
                TradingTime.Display();
                MSQL.DailyUpdate();
                DataRequests.GetMostActive(currentDay, 150).Reverse().DisplayScreen();
                Console.WriteLine("MaxActiveRequests= {0} MaxOpenRequests= {1} OldestRequest= {2}", myIB.myRequests.MaxActiveRequests, myIB.myRequests.MaxOpenRequests, myIB.myRequests.OldestRequest);
            }

            while (TradingTime.IsAfterClose)
            {
                TradingTime.Display();
                MSQL.RedoByEditdate();
                MSQL.CheckAllTradingDays();
                MSQL.DeleteVolume(1200);
                MSQL.DailyUpdate();

                DataRequests.GetMostActive(currentDay, 150).Reverse().DisplayScreen();
                Console.WriteLine("MaxActiveRequests= {0} MaxOpenRequests= {1} OldestRequest= {2}", myIB.myRequests.MaxActiveRequests, myIB.myRequests.MaxOpenRequests, myIB.myRequests.OldestRequest);
            }
            myIB.ShutDown();
        }
示例#4
0
        public static void DailyUpdate()
        {
            int      counter   = 1;
            int      routeId   = 0;
            DateTime startTime = DateTime.Now;
            DateTime currentTime;
            TimeSpan elapsedTime = new TimeSpan();

            string daysToUpdate     = "1 D";
            string firstTradingDay  = "";
            int    numberSecurities = IB.masterSecurityList.Contracts.Count();

            Log.Info(2, string.Format("Start UpdateHist"));

            foreach (Security tempSecurity in IB.masterSecurityList.Contracts.ToList())
            {
                #region Calculate Time Remaining

                currentTime = DateTime.Now;
                elapsedTime = currentTime - startTime;
                string   elapsed          = string.Format("E={0:D2}:{1:D2}:{2:D2}", elapsedTime.Hours, elapsedTime.Minutes, elapsedTime.Seconds);
                TimeSpan timeLeft         = (numberSecurities - counter) * (elapsedTime / counter);
                string   Remaining        = string.Format("Remaining={0:D2}:{1:D2}:{2:D2}", timeLeft.Hours, timeLeft.Minutes, timeLeft.Seconds);
                double   percentCompleted = counter / numberSecurities;

                Log.Data(1, string.Format("{0,-10} {1,-10} ActiveReq={2,2} OpenReq={2,2} " + elapsed + "  " + Remaining, tempSecurity.Symbol, tempSecurity.ConId, Program.myIB.myRequests.NumActiveRequests, Program.myIB.myRequests.NumOpenRequests));
                counter++;

                #endregion Calculate Time Remaining

                daysToUpdate = MSQL.DaysToUpdate(tempSecurity.Symbol).ToString() + " D";
                Program.myIB.RequestBarIB(routeId, tempSecurity.ConId, tempSecurity.Symbol, firstTradingDay, daysToUpdate);
                Thread.Sleep(Program.myIB.myRequests.DelayTime);
            }
            Log.Info(3, string.Format("End UpdateHist Total Time= {0} Time/Rec= {1}", elapsedTime, elapsedTime / numberSecurities));
        }