Exemplo n.º 1
0
        /// <summary>
        /// Background worker to check if the threads are alive.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DateTime  startTime = DateTime.Now;
            Stopwatch stopWatch = new Stopwatch();

            e.Result = "";
            int i = 0;


            foreach (DataSetup ds in threadStatusList)
            {
                while (ds.IsAlive)
                {
                    if (ds.CurrentStep != 0)
                    {
                        System.Threading.Thread.Sleep(1000);
                        backgroundWorker1.ReportProgress(i, ds);
                    }

                    if (backgroundWorker1.CancellationPending)
                    {
                        threadStatusList.Clear();
                        e.Cancel = true;
                        return;
                    }

                    ++i;
                }
                ;
            }
            MessageBox.Show("Download Complete", "Historical Data Downloader", MessageBoxButtons.OK, MessageBoxIcon.Information);

            progressBarSetValue(1);
            //Writing to file
            setPercentComplete("");
            setStatusType("Writing Data");

            List <Data> tempprices = null;

            tempprices = DataBaseHandler.getInstance().getEntries(typeOfData);

            setPercentComplete("100%");
            progressBarVisibility(Visible);



            DataSetup.writeToFile(tempprices, HddMainFormBasic.typeOfData, HddMainFormBasic.fileFormat, DataSetup.fileName, HddMainFormBasic.timeInterval, HddMainFormBasic.offset, HddMainFormBasic.delimiter, HddMainFormBasic.dateFormat);
            tempprices.Clear();
            DataBaseHandler.getInstance().removeAllEntries();
            threadStatusList.Clear();
            pressAbort();
        }
Exemplo n.º 2
0
        public void processData(O2GResponse response, HddMainFormBasic hdd, DataSetup dataSetup)
        {
            bool     outOfRangeDateFound = false;
            DateTime tempEndDate         = new DateTime();

            try
            {
                //Exit thread if the value is false
                if (!keepThreadRunning)
                {
                    return;
                }

                var query1 = HddMainFormBasic.threadDictionary.Keys.FirstOrDefault(t => t == response.RequestID);
                if (query1 != null)
                {
                    HddMainFormBasic.threadDictionary[query1].CallBack = true;
                }


                O2GResponseReaderFactory readerfactory = tss.Session.getResponseReaderFactory();

                O2GMarketDataSnapshotResponseReader reader = readerfactory.createMarketDataSnapshotReader(response);

                DateTime from = new DateTime();
                DateTime to   = new DateTime();
                if (reader.Count > 0)
                {
                    from = reader.getDate(0);
                    to   = reader.getDate(reader.Count - 1);
                }
                Console.WriteLine("Market data received from " + from.ToString("MM/dd/yyyy HH:mm:ss") + " to " + to.ToString("MM/dd/yyyy HH:mm:ss"));

                //insert data into price list
                for (int j = reader.Count - 1; j > -1; j--)
                {
                    prices.Add(new Data(reader.getAskOpen(j), reader.getAskHigh(j), reader.getAskLow(j), reader.getAskClose(j), reader.getBidOpen(j), reader.getBidHigh(j), reader.getBidLow(j), reader.getBidClose(j), reader.getDate(j), reader.getVolume(j)));
                }
                //if less rates are obtained then it means we reached the end.
                //Nothing came back for the timeframe specified means, nothing exist in the remaining timeframe.
                if (reader.Count < 300 || reader.Count == 0)
                {
                    outOfRangeDateFound = true;
                    tempEndDate         = endDate;
                }

                if (!outOfRangeDateFound)
                {
                    removeLastDate(prices[prices.Count - 1].StartTime, prices);

                    tempEndDate = prices[prices.Count - 1].StartTime;
                }
                if (prices.Count > 3000)
                {
                    DataBaseHandler.getInstance().addEntries(prices);
                    prices.Clear();
                    if (timeInterval.Equals("t1"))
                    {
                        Thread.Sleep(2000);
                    }
                }

                //Get total ticks between end date and start date
                long totalTicks = tempEndDate.Ticks - dataSetup.StartDate.Ticks;

                currentStep           = 0;
                currentStep           = ((double)(totalTicks * maxIterations)) / initialTotalTicks;
                dataSetup.currentStep = currentStep;
                tick = reader.Count;
                Time = sw.Elapsed;

                #region repeat process OR quit
                if (outOfRangeDateFound == false)
                {
                    try
                    {
                        //Call HDD function to calculate more data
                        DataSetup ds = new DataSetup();
                        ds = dataSetup;

                        ds.EndDate = tempEndDate;
                        hdd.sendRequest(startDate, tempEndDate, ds);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error: " + e.Message);
                    }
                }
                else
                {
                    Console.WriteLine("Done");

                    #region Final flushing to database
                    try
                    {
                        if (prices.Count > 0)
                        {
                            //Finally write to database
                            DataBaseHandler.getInstance().addEntries(prices);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message, "Error in writing data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        logger.Debug(e.Message);
                        logger.Debug(e.StackTrace);
                    }
                    finally
                    {
                        prices.Clear();
                    }
                    #endregion

                    #region Writing to a File
                    long rows = 0;
                    rows = DataBaseHandler.getInstance().getNumberOfRows();

                    //Calculate the levels first before requesting data.
                    long        templevel = 0;
                    List <long> levels    = new List <long>();
                    levels.Add(0);
                    while (true)
                    {
                        if (templevel + 5000 < rows)
                        {
                            templevel += 5000;
                            levels.Add(templevel);
                        }
                        else
                        {
                            levels.Add(rows);
                            break;
                        }
                    }
                    logger.Debug("Levels of data to be requested from database: " + levels.ToString());
                    dataSetup.isAlive = false;
                    #endregion
                }
                #endregion
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n" + e.Source, "Error");
            }
        }
Exemplo n.º 3
0
        //User clicking start download button
        private void myStartDownloadButton_Click(object sender, EventArgs e)
        {
            //Clear local cache everytime start button is pressed
            DataBaseHandler.getInstance().removeAllEntries();

            //This checks the text on the start download button and changes the state of it depending
            //on what the current state is
            if (myStartDownloadButton.Text.Equals("Abort"))
            {
                myProgressBar.Visible = false;
                logger.Debug("Abort Pressed");
                myStartDownloadButton.Text = "Start Download";


                if (backgroundWorker1.IsBusy)
                {
                    backgroundWorker1.CancelAsync();
                }

                loggedInState();
                DataSetup.keepThreadRunning = false;
            }
            else if (myStartDownloadButton.Text.Equals("Start Download"))
            {
                logger.Debug("Start Download was Pressed");
                DataSetup.keepThreadRunning = true;
                //Lot of error checking
                offset = 0;
                try
                {
                    offset = Double.Parse("0");
                }
                catch (Exception)
                {
                    MessageBox.Show("Offset field has to be numeric", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (DateTime.Parse(myStartDate.Text + " " + myStartTime.Text, Thread.CurrentThread.CurrentCulture) >= DateTime.Parse(myEndDate.Text + " " + myEndTime.Text, Thread.CurrentThread.CurrentCulture))
                {
                    MessageBox.Show("Start time must be before End time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (DateTime.Parse(myStartDate.Text + " " + myStartTime.Text, Thread.CurrentThread.CurrentCulture) >= DateTime.UtcNow.AddHours(offset))
                {
                    MessageBox.Show("Start time cannot be in the future", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (DateTime.Parse(myEndDate.Text + " " + myEndTime.Text) >= DateTime.UtcNow.AddHours(offset))
                {
                    MessageBox.Show("End time cannot be in the future", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (myCurrency.Text.Equals("") || myOutputDirectory.Text.Equals("") || myTimeInterval.Text.Equals("") || myTypeOfData.Text.Equals(""))
                {
                    MessageBox.Show("Please fill out all the fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }


                myProgressBar.Visible      = Visible;
                myStartDownloadButton.Text = "Abort";
                //Set download state
                startedDownloadState();

                string delimit = "";
                timeInterval = myTimeInterval.Text;
                timeInterval = (string)timeIntervalHT[timeInterval];

                delimit    = ",";
                dateFormat = "MM/dd/yyyy,HH:mm:ss";

                extension = ".csv";


                currency          = myCurrency.Text;
                startDate         = DateTime.Parse(myStartDate.Text + " " + myStartTime.Text).AddHours(offset * (-1));
                endDate           = DateTime.Parse(myEndDate.Text + " " + myEndTime.Text).AddHours(offset * (-1));
                adjustedStartDate = startDate.AddMinutes(-10);
                if ((adjustedStartDate.DayOfWeek == DayOfWeek.Sunday && adjustedStartDate.Hour < 17) ||
                    (adjustedStartDate.DayOfWeek == DayOfWeek.Saturday) ||
                    (adjustedStartDate.DayOfWeek == DayOfWeek.Friday && adjustedStartDate.Hour > 15))
                {
                    adjustedStartDate = adjustedStartDate.AddDays(-2);
                }
                typeOfData = myTypeOfData.Text;

                delimiter = delimit;
                logger.Debug("Requesting " + myTimeInterval.Text + " data from: " + myStartDate.Text + " " + myStartTime.Text + " to " + myEndDate.Text + " " + myEndTime.Text);

                int maxInterations = 100;


                fileFormat      = "CSV";
                outputDirectory = myOutputDirectory.Text;
                delimiter       = delimit;

                //Quit if the user tries to do more than 5 years for 1 minute data
                TimeSpan ts = endDate - startDate;

                if (ts.TotalDays > 1800 && timeInterval.Equals("m1"))
                {
                    MessageBox.Show("Date range too big, please use smaller range!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    pressAbort();
                    return;
                }

                DataSetup dataSetup = new DataSetup(startDate,
                                                    endDate,
                                                    tss,
                                                    true,
                                                    maxInterations,
                                                    endDate.Ticks - startDate.Ticks,
                                                    typeOfData,
                                                    timeInterval,
                                                    fileFormat,
                                                    outputDirectory,
                                                    delimiter,
                                                    dateFormat,
                                                    extension,
                                                    currency);
                threadStatusList.Add(dataSetup);
                dataSetup.prepareDownload();
                Thread thread = new Thread(new ThreadStart(() => sendRequest(startDate, endDate, dataSetup)));
                thread.Name = "HDD_BASIC_THREAD";
                thread.Start();
                thread.Join();



                progressBarSetValue(1);
                backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event
            }
        }
Exemplo n.º 4
0
        public void prepareDownload()
        {
            DataBaseHandler.getInstance().removeAllEntries();
            if (Directory.Exists(outputDirectory) == false)
            {
                try
                {
                    Directory.CreateDirectory(outputDirectory);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            if (fileFormat.Equals("Ninja") == false)
            {
                fileName = outputDirectory + "\\" + currency.Replace("/", "") + "_" + timeInterval + "_" + typeOfData + extension;
            }
            else
            {
                fileName = outputDirectory + "\\$" + currency.Replace("/", "") + "." + typeOfData + "_" + extension;
            }
            //Log.logMessageToFile("Thread", "startDate: " + startDate + " endDate: " + endDate + " typeOfData: " + typeOfData + " timeInterval:" + timeInterval + " fileFormat: " + fileFormat + " timeZone: " + " outputDirectory: " + outputDirectory);
            logger.Debug("startDate: " + startDate + " endDate: " + endDate + " typeOfData: " + typeOfData + " timeInterval:" + timeInterval + " fileFormat: " + fileFormat + " timeZone: " + " outputDirectory: " + outputDirectory);
            try
            {
                File.Delete(fileName);
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
                logger.Error(e.StackTrace);
                throw e;
            }
            using (StreamWriter file = new StreamWriter(@fileName))
            {
                if (fileFormat.Equals("CSV") || fileFormat.Equals("ST") || fileFormat.Equals("Custom"))
                {
                    if (timeInterval.Equals("t1") == false)
                    {
                        if (typeOfData.Equals("Bid"))
                        {
                            file.WriteLine("\"Date\"" + delimiter + "\"Time\"" + delimiter + "\"Open\"" + delimiter + "\"High\"" + delimiter + "\"Low\"" + delimiter + "\"Close\"" + delimiter + "\"Total Ticks\"");
                        }
                        else if (typeOfData.Equals("Ask"))
                        {
                            file.WriteLine("\"Date\"" + delimiter + "\"Time\"" + delimiter + "\"Open\"" + delimiter + "\"High\"" + delimiter + "\"Low\"" + delimiter + "\"Close\"" + delimiter + "\"Total Ticks\"");
                        }
                        else
                        {
                            file.WriteLine("\"Date\"" + delimiter + "\"Time\"" + delimiter + "\"OpenBid\"" + delimiter + "\"HighBid\"" + delimiter + "\"LowBid\"" + delimiter + "\"CloseBid\"" + delimiter + "\"OpenAsk\"" + delimiter + "\"HighAsk\"" + delimiter + "\"LowAsk\"" + delimiter + "\"CloseAsk\"" + delimiter + "\"Total Ticks\"");
                        }
                    }
                    else
                    {
                        if (typeOfData.Equals("Bid"))
                        {
                            file.WriteLine("\"Date\"" + delimiter + "\"Time\"" + delimiter + "\"Bid\"");
                        }
                        else if (typeOfData.Equals("Ask"))
                        {
                            file.WriteLine("\"Date\"" + delimiter + "\"Time\"" + delimiter + "\"Ask\"");
                        }
                        else
                        {
                            file.WriteLine("\"Date\"" + delimiter + "\"Time\"" + delimiter + "\"Bid\"" + delimiter + "\"Ask\"");
                        }
                    }
                }
                else if (fileFormat.Equals("MT4"))
                {
                }
            }

            maxIterations     = 100;
            initialTotalTicks = endDate.Ticks - startDate.Ticks;
            currentStep       = maxIterations;
        }