public void onRequestCompleted(string requestID, O2GResponse response) { Console.WriteLine("Request completed ID: " + requestID); switch (response.Type) { case O2GResponseType.GetOffers: { Console.WriteLine("Received offers"); logger.Debug("Received Offers"); LoginForm.getInstance().fillRatesGrid(); break; } case O2GResponseType.MarketDataSnapshot: { //All threads are saved to the thread dictionary if (HddMainFormBasic.ThreadDictionary.ContainsKey(requestID)) { DataSetup dataSetup = HddMainFormBasic.ThreadDictionary[requestID]; dataSetup.processData(response, HddMainFormBasic.getInstance(), dataSetup); HddMainFormBasic.ThreadDictionary.Remove(requestID); } break; } } }
public void onRequestFailed(string requestID, string error) { Console.WriteLine("requestID: " + requestID + ", Error: " + error); logger.Debug("requestID: " + requestID + ", Error: " + error); if (HddMainFormBasic.ThreadDictionary.ContainsKey(requestID)) { DataSetup ds = HddMainFormBasic.ThreadDictionary[requestID]; ds.IsAlive = false; HddMainFormBasic.ThreadDictionary[requestID] = ds; } }
/// <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(); }
/// <summary> /// This runs when progress is being updated /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { DataSetup ds = new DataSetup(); ds = (DataSetup)e.UserState; double currentStep = ds.CurrentStep; if (ds.VariableChanged) { currentThreadTicks = ds.Time.Ticks; ds.VariableChanged = false; } //Calculate the estimated time remainining //Formula: A = Total ticks for days / number of ticks per request // Estimated Time = time it took per request * A; if (ds.Tick != 0) { try { TimeSpan span = endDate.Subtract(startDate); TimeSpan estimatedTime = TimeSpan.FromTicks(span.Ticks / ds.Tick); TimeSpan countDownTime = TimeSpan.FromTicks(estimatedTime.Ticks * Convert.ToInt32(Math.Floor(currentStep)) / ds.MaxIterations); setPercentComplete(countDownTime.ToString(@"hh\:mm\:ss")); } catch (InvalidOperationException ex) { throw ex; } } if (ds.MaxIterations - Convert.ToInt32(Math.Floor(currentStep)) >= 0) { progressBarSetValue(ds.MaxIterations - Convert.ToInt32(Math.Floor(currentStep))); } else { progressBarSetValue(1); } }
/// <summary> /// Send request to API for data /// </summary> /// <param name="adjustedStartDate"></param> /// <param name="endDate"></param> /// <param name="dataSetup"></param> public void sendRequest(DateTime adjustedStartDate, DateTime endDate, DataSetup dataSetup) { try { O2GRequestFactory factory = tss.Session.getRequestFactory(); O2GTimeframeCollection timeframes = factory.Timeframes; O2GTimeframe tfo = timeframes[timeInterval]; O2GRequest request = factory.createMarketDataSnapshotRequestInstrument(currency, tfo, 300); factory.fillMarketDataSnapshotRequestTime(request, adjustedStartDate, endDate, true); Console.WriteLine("Requesting from " + adjustedStartDate.ToString("MM/dd/yyyy HH:mm:ss") + " to " + endDate.ToString("MM/dd/yyyy HH:mm:ss")); logger.Debug("Requesting from " + adjustedStartDate.ToString("MM/dd/yyyy HH:mm:ss") + " to " + endDate.ToString("MM/dd/yyyy HH:mm:ss")); tss.Session.sendRequest(request); threadDictionary.Add(request.RequestID, dataSetup); } catch (Exception e) { MessageBox.Show("Error: " + e.Message); } }
//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 } }
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"); } }