public FrmFillDownload() { var hourago = TT_Info.ToRestTimestamp(DateTime.UtcNow - new TimeSpan(1, 0, 0)); InitializeComponent(); InitializeColumnList(); clbColumns.Items.Clear(); foreach (FillColumn column in m_TradePaneColumns) { clbColumns.Items.Add(column, true); } this.FormClosing += FrmFillDownload_FormClosing; cbFileMode.DataSource = Enum.GetValues(typeof(FileMode)); LoadSettings(); this.DragOver += FrmFillDownload_DragOver; this.DragDrop += Debug_DragDrop; FDLog.LogMessage("Starting up C# Fill Downloader v0.0.1"); }
private void DebugFillFiles(DragEventArgs e) { string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); int i; var fields = GetReportItems(); for (i = 0; i < s.Length; i++) { Console.WriteLine(); using (StreamReader file = File.OpenText(s[i])) using (JsonTextReader reader = new JsonTextReader(file)) { TT_Fill fill = new TT_Fill(JToken.ReadFrom(reader)); FDLog.LogMessage(String.Format("Parsing fill from file {0}", s[i])); string row = ""; foreach (var field in fields) { try { row += field.DisplayField(fill) + ","; } catch (Exception ex) { row += ","; FDLog.LogError("Error parsing fill column " + field.ColumnName + " for fill " + fill.RecordID + Environment.NewLine + ex.Message); } } FDLog.LogMessage(row); } } }
private void Debug_DragDrop(object sender, DragEventArgs e) { if (!RestManager.IsAuthorized()) { string app_key = txtSecret.Text.Split(':')[0]; string app_secret = txtSecret.Text; RestManager.Init(app_key, app_secret, txtEnvironment.Text); if (!RestManager.IsAuthorized()) { MessageBox.Show("Rest API was not able to log in with provided App Key and Secret"); return; } else { FDLog.LogMessage("Successfully logged in with app key and secret"); RestManager.OnTokenError += RestManager_OnTokenError; } } string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); Action debug = () => { DebugFillFiles(e); }; this.BeginInvoke(debug); }
private void btnStart_Click(object sender, EventArgs e) { // Validate our inputs int interval = 0; if (!int.TryParse(txtFrequency.Text, out interval)) { MessageBox.Show("Error parsing frequency \"" + txtFrequency.Text + "\"."); return; } if (interval <= 0) { MessageBox.Show("Frequency must be greater than zero."); return; } TimeSpan start_time = dtpStartTime.Value.TimeOfDay; TimeSpan end_time = dtpEndTime.Value.TimeOfDay; if (start_time > end_time) { MessageBox.Show("Error: Start time must come before end time"); return; } // Try to log in to the REST API string app_key = txtSecret.Text.Split(':')[0]; string app_secret = txtSecret.Text; RestManager.Init(app_key, app_secret, txtEnvironment.Text, txtURL.Text); if (!RestManager.IsAuthorized()) { MessageBox.Show("Rest API was not able to log in with provided App Key and Secret"); return; } else { FDLog.LogMessage("Successfully logged in with app key and secret"); RestManager.OnTokenError += RestManager_OnTokenError; } DateTime start_date = default(DateTime); if (dtpStartDate.CustomFormat != " ") { // TimeStamp correction so it properly reflects midnight of // this day in local time start_date = dtpStartDate.Value.Date - TimeZoneInfo.Local.BaseUtcOffset; } else { start_date = DateTime.Today - TimeZoneInfo.Local.BaseUtcOffset; } if (!chkSunday.Checked && !chkMonday.Checked && !chkTuesday.Checked && !chkWednesday.Checked && !chkThursday.Checked && !chkFriday.Checked && !chkSaturday.Checked) { MessageBox.Show("Must select at least one day to run downloader."); return; } bool[] days_to_run = new bool[7]; days_to_run[0] = chkSunday.Checked; days_to_run[1] = chkMonday.Checked; days_to_run[2] = chkTuesday.Checked; days_to_run[3] = chkWednesday.Checked; days_to_run[4] = chkThursday.Checked; days_to_run[5] = chkFriday.Checked; days_to_run[6] = chkSaturday.Checked; try { FileMode mode = (FileMode)cbFileMode.SelectedItem; m_outputFile = FillFile.GetFillFile(mode, txtOutput.Text, GetReportItems()); } catch (Exception ex) { MessageBox.Show("Error creating output file: " + ex.Message); return; } clbColumns.Enabled = false; btnStart.Text = "Stop Downloading"; btnStart.Click -= btnStart_Click; btnStart.Click += btnStart_Close; btnBrowse.Enabled = false; FDLog.LogMessage("Beginning downloads..."); m_fillThread = new FillDownloadThread(start_time, end_time, new TimeSpan(0, interval, 0), days_to_run, start_date); m_fillThread.FillDownload += fillThread_OnFillDownload; m_fillThread.OnError += OnError; m_fillThread.Start(); }
private void DownloadFills() { // Perform REST request/response to download fill data, specifying our cached minimum timestamp as a starting point. // On a successful response the timestamp will be updated so we run no risk of downloading duplicate fills. bool should_continue = false; List <TT_Fill> fills = new List <TT_Fill>(); do { should_continue = false; var min_param = new RestSharp.Parameter("minTimestamp", TT_Info.ToRestTimestamp(m_minTimeStamp).ToString(), RestSharp.ParameterType.QueryString); RestSharp.IRestResponse result = RestManager.GetRequest("ledger", "fills", min_param); if (result.StatusCode == System.Net.HttpStatusCode.GatewayTimeout) { should_continue = true; DateTime max_time = DateTime.Now; int retry_count = 0; for (retry_count = 0; retry_count < max_retries; ++retry_count) { FDLog.LogMessage("Fill request timed out. Retrying...."); max_time = m_minTimeStamp + TimeSpan.FromTicks((max_time - m_minTimeStamp).Ticks / 2); var max_param = new RestSharp.Parameter("maxTimestamp", TT_Info.ToRestTimestamp(max_time).ToString(), RestSharp.ParameterType.QueryString); result = RestManager.GetRequest("ledger", "fills", min_param, max_param); if (result.StatusCode == System.Net.HttpStatusCode.OK) { m_minTimeStamp = max_time; break; } else if (result.StatusCode != System.Net.HttpStatusCode.GatewayTimeout) { throw new Exception(String.Format("Request for fills unsuccessful. (minTimestamp={0}) - Status: {1} - Error Message: {2}", min_param.Value.ToString(), result.StatusCode.ToString(), result.ErrorMessage)); } if (retry_count == max_retries) { throw new Exception("Request for fills unsuccessful. Max Retries exceeded."); } } } else if (result.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception(String.Format("Request for fills unsuccessful. (minTimestamp={0}) - Status: {1} - Error Message: {2}", min_param.Value.ToString(), result.StatusCode.ToString(), result.ErrorMessage)); } JObject json_data = JObject.Parse(result.Content); FDLog.LogMessage(String.Format("Downloaded {0} fills.", json_data["fills"].Count())); foreach (var fill in json_data["fills"]) { fills.Add(new TT_Fill(fill)); } fills.Sort((f1, f2) => f1.UtcTimeStamp.CompareTo(f2.UtcTimeStamp)); RaiseFillDownloadEvent(fills); if (fills.Count > 0) { m_minTimeStamp = new DateTime(fills[fills.Count - 1].UtcTimeStamp.Ticks + 1); } should_continue |= (fills.Count == TT_Info.MAX_RESPONSE_FILLS); should_continue &= m_running; }while (should_continue); }