Exemple #1
0
        public void RequestBarIB(int routeId, int conId, DateTime endDate)
        {
            Contract histContract = new Contract
            {
                Symbol      = DataRequests.GetSymbol(conId),
                ConId       = conId,
                Exchange    = "SMART",
                PrimaryExch = "ISLAND",
                SecType     = "STK",
                Currency    = "USD"
            };

            Security tempSecurity = new Security
            {
                Symbol  = DataRequests.GetSymbol(conId),
                ConId   = conId,
                SecType = Security.SecurityType.STK,
            };

            if (!BlackList.Contains(tempSecurity))
            {
                int reqId = myRequests.NextReqId;
                myRequests.Add(reqId, routeId, conId, histContract.Symbol);
                string date = endDate.Date.AddDays(1).ToString("yyyyMMdd HH:mm:ss");
                Program.myIB.ibClient.ClientSocket.reqHistoricalData(reqId, histContract, date, "1 D", "1 day", "TRADES", 1, 1, false, null);
            }
        }
Exemple #2
0
 public static void CheckAllTradingDays()
 {
     Log.Info(3, string.Format("Start CheckAllTradingDays"));
     foreach (Security tempSecurity in IB.masterSecurityList.Contracts.ToList())
     {
         DataRequests.GetAllBars(tempSecurity.Symbol).CheckTradingDays();
     }
     Log.Info(3, string.Format("End CheckAllTradingDays"));
 }
Exemple #3
0
        public static void RedoByYear()
        {
            int      counter    = 1;
            int      routeId    = 0;
            int      numUpdated = 0;
            DateTime cutoffDate = new DateTime(2015, 1, 1);
            DateTime startDate  = new DateTime(2017, 01, 01);
            DateTime endDate    = new DateTime(2017, 12, 31);

            DateTime startTime = DateTime.Now;
            DateTime currentTime;
            TimeSpan elapsedTime;

            string duration = TradingTime.GetNumTradingDays(startDate, endDate).ToString() + " D";

            int numberSecurities = IB.masterSecurityList.Contracts.Count();

            Log.Info(3, string.Format("Start RedoByYear"));

            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

                BarDataList myBDL = new BarDataList();
                myBDL = DataRequests.GetAllBars(tempSecurity.Symbol);

                int numBadEditDates = myBDL.getNumBadEditDates(startDate, endDate, cutoffDate);

                if (numBadEditDates > 14)
                {
                    numUpdated++;
                    Program.myIB.RequestBarIB(routeId, tempSecurity.ConId, tempSecurity.Symbol, endDate.ToString("yyyyMMdd hh:MM:ss"), duration);
                    Log.Info(3, string.Format("RedoByYear Requesting Symbol= {0,-10} Bad Edits={1,-5}", tempSecurity.Symbol, numBadEditDates));
                    Thread.Sleep(Program.myIB.myRequests.DelayTime);
                }
                else
                {
                    Log.Info(3, string.Format("RedoByYear Skipping Symbol={0,-10} Bad Edits={1,-5}", tempSecurity.Symbol, numBadEditDates));
                }
            }
            Log.Info(3, string.Format("End RedoByYear - Updated={0} Total={1}", numUpdated, numberSecurities));
        }
Exemple #4
0
        public static void RedoByEditdate()
        {
            int counter    = 1;
            int numUpdated = 0;
            int routeId    = 0;

            DateTime startTime = DateTime.Now;
            DateTime currentTime;
            DateTime cutoffDate;
            TimeSpan elapsedTime;

            //foreach (Security tempSecurity in IB.masterSecurityList.Contracts.AsEnumerable().Reverse().ToList())

            Log.Info(3, string.Format("Start RedoByEditdate"));
            int numberSecurities = IB.masterSecurityList.Contracts.Count();

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

                currentTime = DateTime.Now;
                elapsedTime = currentTime - startTime;
                string   Elapsed          = string.Format("Elapsed={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}:{3:D2}", timeLeft.Days, timeLeft.Hours, timeLeft.Minutes, timeLeft.Seconds);
                double   percentCompleted = counter / numberSecurities;

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

                #endregion Calculate Time Remaining

                BarDataList myBDL = new BarDataList();
                myBDL = DataRequests.GetAllBars(tempSecurity.Symbol);

                foreach (BarDataItem tempBar in myBDL.Items)
                {
                    cutoffDate = tempBar.Time.AddDays(7);
                    if (tempBar.EditTime < cutoffDate && DateTime.Now > cutoffDate)
                    {
                        Program.myIB.RequestBarIB(routeId, myBDL.ConId, tempBar.Time);
                        numUpdated++;
                        Thread.Sleep(Program.myIB.myRequests.DelayTime);
                    }
                }
            }
            Log.Info(3, string.Format("End RedoByEditdate - {0} Updated", numUpdated));
        }
Exemple #5
0
        /// <summary>
        /// Deletes from DB is average daily volume fals below minVolume and saves data to file
        /// </summary>
        /// <param name="minVolume">The minimun volume to delete</param>
        public static void DeleteVolume(int minVolume)

        {
            int count = 0;

            foreach (Security tempSecurity in IB.masterSecurityList.Contracts.ToList())
            {
                int avgVol = DataRequests.GetAverageVolume(tempSecurity.Symbol, 100);
                if ((avgVol < minVolume) && (avgVol != -1))
                {
                    Log.Info(2, string.Format("DeleteVolume Conid={0,-10} Symbol={1,-8} Avg. Vol={2,-8} Count={3,-4}", tempSecurity.ConId, tempSecurity.Symbol, avgVol, count++));
                    SaveData(tempSecurity.Symbol);
                    Delete(tempSecurity);
                }
            }
        }
Exemple #6
0
        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();
        }
Exemple #7
0
        /// <summary>
        /// Attempts to add a SecurityList to the database
        /// </summary>
        /// <param name="newSecurityList"></param>
        /// <returns></returns>
        public static int AddSecurityList(SecurityList newSecurityList)

        {
            int       numberAdded = 0;
            Stopwatch stopwatch   = new Stopwatch();

            stopwatch.Start();
            Log.Info(0, string.Format("Start AddList"));
            foreach (Security addSecurity in newSecurityList.Contracts)
            {
                if (DataRequests.GetSymbol(addSecurity.ConId) == null)
                {
                    if (AddSecurity(addSecurity))
                    {
                        numberAdded++;
                    }
                }
            }
            stopwatch.Stop();
            Log.Info(0, string.Format("End AddList - Added {0} Time={1}", numberAdded, stopwatch.Elapsed.TotalMilliseconds));
            return(numberAdded);
        }
Exemple #8
0
        private static void SaveData(string symbol)
        {
            string dataFileName = symbol + ".txt";

            // Set a variable to the Documents path.
            string myDocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            // Set a variable to the Stocks path.
            myDocumentsPath = "D:\\OneDrive\\Stocks\\Deleted\\";

            // Use full path to create a handle to the error file
            System.IO.StreamWriter dataFile = new StreamWriter(Path.Combine(myDocumentsPath, dataFileName), false);

            BarDataList mBarDataList = new BarDataList();

            mBarDataList = DataRequests.GetAllBars(symbol);

            foreach (BarDataItem mItem in mBarDataList.Items)
            {
                dataFile.WriteLine(string.Format("{0:MM/dd/yyyy} {1:0.000} {2:0.000} {3:0.000} {4:0.000} {5,-8} {6:yyyyMMdd HH:mm:ss}",
                                                 mItem.Time, mItem.Open, mItem.High, mItem.Low, mItem.Close, mItem.Volume, mItem.EditTime));
            }
            dataFile.Close();
        }
Exemple #9
0
        /// <summary>
        /// Load all of the values from the MSQL database
        /// </summary>
        public void LoadAll()

        {
            this.Contracts = DataRequests.GetAllSecurities().Contracts;
        }