//delete data from selected instruments private void ClearDataBtn_ItemClick(object sender, RoutedEventArgs routedEventArgs) { IList selectedInstruments = InstrumentsGrid.SelectedItems; if (selectedInstruments.Count == 0) { return; } if (selectedInstruments.Count == 1) { var inst = (Instrument)selectedInstruments[0]; MessageBoxResult res = MessageBox.Show( string.Format("Are you sure you want to delete all data from {0} @ {1}?", inst.Symbol, inst.Datasource.Name), "Delete", MessageBoxButton.YesNo); if (res == MessageBoxResult.No) { return; } } else { MessageBoxResult res = MessageBox.Show( string.Format("Are you sure you want to delete all data from {0} instruments?", selectedInstruments.Count), "Delete", MessageBoxButton.YesNo); if (res == MessageBoxResult.No) { return; } } using (IDataStorage storage = DataStorageFactory.Get()) { //todo: remove dependency on local storage here, use client instead foreach (Instrument i in selectedInstruments) { try { storage.DeleteAllInstrumentData(i); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } StatusBarLabel.Content = "Instrument data deleted"; }
public DataEditWindow(Instrument instrument) { InitializeComponent(); DataContext = this; Data = new ObservableCollection <OHLCBar>(); //grab and update the instrument //TODO rewrite to use client using (var context = new MyDBContext()) { context.Instruments.Attach(instrument); context.Entry(instrument).Reload(); TheInstrument = instrument; } string timezone = TheInstrument.Exchange == null ? "UTC" : TheInstrument.Exchange.Timezone; _tzInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone); StartTime = new DateTime(1950, 1, 1, 0, 0, 0, 0); EndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0, 0); if (!TheInstrument.ID.HasValue) { return; } //grab the data info //todo remove dependency on DataStorageFactory using (var localStorage = DataStorageFactory.Get()) { var storageInfo = localStorage.GetStorageInfo(TheInstrument.ID.Value); if (storageInfo.Count == 0) //if it doesn't have any data, we just exit { MessageBox.Show("This instrument has no data."); Hide(); } else { foreach (StoredDataInfo s in storageInfo) //fill the resolution box { ResolutionComboBox.Items.Add(s.Frequency); } } } }
private void SaveChangesBtn_Click(object sender, RoutedEventArgs e) { //todo remove dependency on DataStorageFactory using (var localStorage = DataStorageFactory.Get()) { try { localStorage.UpdateData(Data.ToList(), TheInstrument, _loadedFrequency, true); MessageBox.Show("Successfully updated data."); } catch (Exception ex) { MessageBox.Show("Error while updating data: " + ex.Message); } } }
private void DeleteBtn_Click(object sender, RoutedEventArgs e) { //get the selected bars var rows = DataGrid.SelectedItems; if (rows.Count < 0) { return; } var result = MessageBox.Show(string.Format("Are you sure you want to delete {0} rows?", rows.Count), "Delete Rows", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) { return; } var toDelete = rows.Cast <OHLCBar>().ToList(); //data is stored in the db at exchange time. But we may have loaded it at UTC or local. //If so, we must convert it back. foreach (OHLCBar b in toDelete) { if (_loadedTimeZone == "UTC") { b.DT = TimeZoneInfo.ConvertTimeFromUtc(b.DT, _tzInfo); } else if (_loadedTimeZone == "Local") { b.DT = TimeZoneInfo.ConvertTime(b.DT, TimeZoneInfo.Local, _tzInfo); } } //todo remove dependency on DataStorageFactory using (var localStorage = DataStorageFactory.Get()) { localStorage.DeleteData(TheInstrument, _loadedFrequency, toDelete); } foreach (OHLCBar bar in toDelete) { Data.Remove(bar); } }
//check the latest date we have available in local storage, then request historical data from that date to the current time private void UpdateHistoricalDataBtn_ItemClick(object sender, RoutedEventArgs routedEventArgs) { var frequency = (BarSize)((MenuItem)sender).Tag; List <Instrument> selectedInstruments = InstrumentsGrid.SelectedItems.Cast <Instrument>().ToList(); int requestCount = 0; using (IDataStorage localStorage = DataStorageFactory.Get()) { foreach (Instrument i in selectedInstruments) { if (!i.ID.HasValue) { continue; } //TODO: add GetStorageInfo to client through REST, deprecate zmq, then remove dependency on DataStorageFactory here List <StoredDataInfo> storageInfo = localStorage.GetStorageInfo(i.ID.Value); if (storageInfo.Any(x => x.Frequency == frequency)) { StoredDataInfo relevantStorageInfo = storageInfo.First(x => x.Frequency == frequency); _client.RequestHistoricalData(new HistoricalDataRequest( i, frequency, relevantStorageInfo.LatestDate + frequency.ToTimeSpan(), DateTime.Now, DataLocation.ExternalOnly, true)); requestCount++; } } } if (_progressBar.Value >= _progressBar.Maximum) { _progressBar.Maximum = requestCount; _progressBar.Value = 0; } else { _progressBar.Maximum += requestCount; } }
private void LoadDataBtn_Click(object sender, RoutedEventArgs e) { Data.Clear(); //grab the data //todo remove dependency on DataStorageFactory using (var localStorage = DataStorageFactory.Get()) { var bars = localStorage.GetData(TheInstrument, StartTime, EndTime, (BarSize)ResolutionComboBox.SelectedItem); //find largest significant decimal by sampling the prices at the start and end of the series var decPlaces = new List <int>(); for (int i = 0; i < Math.Min(bars.Count, 20); i++) { decPlaces.Add(bars[i].Open.CountDecimalPlaces()); decPlaces.Add(bars[bars.Count - 1 - i].Close.CountDecimalPlaces()); } //set the column format to use that number so we don't get any useless trailing 0s SetPriceColumnFormat(decPlaces.Max()); foreach (OHLCBar b in bars) { //do any required time zone coversions if (TimezoneComboBox.Text == "UTC") { b.DT = TimeZoneInfo.ConvertTimeToUtc(b.DT, _tzInfo); } else if (TimezoneComboBox.Text == "Local") { b.DT = TimeZoneInfo.ConvertTime(b.DT, _tzInfo, TimeZoneInfo.Local); } Data.Add(b); } _loadedFrequency = (BarSize)ResolutionComboBox.SelectedItem; _loadedTimeZone = TimezoneComboBox.Text; } StatusLabel.Content = string.Format("Loaded {0} Bars", Data.Count); }
private void ImportBtn_Click(object sender, RoutedEventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); //check that we've got the relevant data needed if (!Data.Columns.Contains("Date") && !Data.Columns.Contains("DateTime")) { MessageBox.Show("Must have a date column."); return; } if ((BarSize)FrequencyComboBox.SelectedItem < BarSize.OneDay && !Data.Columns.Contains("DateTime") && !Data.Columns.Contains("Time")) { MessageBox.Show("Must have time column at this frequency"); return; } if (!Data.Columns.Contains("Open") || !Data.Columns.Contains("High") || !Data.Columns.Contains("Low") || !Data.Columns.Contains("Close")) { MessageBox.Show("Must have all OHLC columns."); return; } TimeZoneInfo tzInfo = TimeZoneInfo.Utc; //make sure the timezone is set, and get it if (!string.IsNullOrEmpty(_instrument.Exchange?.Timezone)) { tzInfo = TimeZoneInfo.FindSystemTimeZoneById(_instrument.Exchange.Timezone); } //get the multipliers decimal priceMultiplier; int volumeMultiplier; bool parseWorked = decimal.TryParse(PriceMultiplier.Text, out priceMultiplier); if (!parseWorked) { priceMultiplier = 1; } parseWorked = int.TryParse(VolumeMultiplier.Text, out volumeMultiplier); if (!parseWorked) { volumeMultiplier = 1; } //lines to skip int toSkip; parseWorked = int.TryParse(StartingLine.Text, out toSkip); if (!parseWorked) { toSkip = 1; } //get the frequency var frequency = (BarSize)FrequencyComboBox.SelectedItem; //separator char[] separator = DelimiterBox.Text.ToCharArray(); List <OHLCBar> bars = new List <OHLCBar>(); string[] columns = new string[Data.Columns.Count]; for (int i = 0; i < Data.Columns.Count; i++) { columns[i] = Data.Columns[i].ColumnName; } //determining time: if the freq is >= one day, then the time is simply the session end for this day Dictionary <int, TimeSpan> sessionEndTimes = new Dictionary <int, TimeSpan>(); //1 day and up: we can load it all in one go with no trouble, also may require adjustment bool periodicSaving = frequency < BarSize.OneDay; OHLCBar bar; var barsCount = 0; using (StreamReader sr = new StreamReader(FilePathTextBox.Text)) { string line; while ((line = sr.ReadLine()) != null) { barsCount++; if (barsCount < toSkip) { continue; } try { bar = ParseLine(line.Split(separator), columns, priceMultiplier, volumeMultiplier); } catch (Exception ex) { MessageBox.Show("Importing error: " + ex.Message + "\n" + line); return; } //only add the bar if it falls within the specified date range if (bar.DT >= MinDT.Value && bar.DT <= MaxDT.Value) { bars.Add(bar); } //with 30 bars, we make a check to ensure that the user has entered the correct frequency if (bars.Count == 30) { //the reason we have to use a bunch of bars and look for the most frequent timespan between them //is that session breaks, daily breaks, weekends, etc. can have different timespans despite the //correct frequency being chosen List <int> secDiffs = new List <int>(); for (int i = 1; i < bars.Count; i++) { secDiffs.Add((int)Math.Round((bars[i].DT - bars[i - 1].DT).TotalSeconds)); } int mostFrequent = secDiffs.MostFrequent(); if ((int)Math.Round(frequency.ToTimeSpan().TotalSeconds) != Math.Abs(mostFrequent)) { MessageBox.Show("You appear to have selected the wrong frequency."); return; } } if (periodicSaving && bars.Count > 1000) { //convert to exchange timezone ConvertTimeZone(bars, tzInfo); //low frequencies, < 1 day. No adjustment required and inserting data at intervals instead of all at once IDataClient client; //client.PushData(new DataAdditionRequest(frequency, _instrument, bars, OverwriteCheckbox.IsChecked.HasValue && OverwriteCheckbox.IsChecked.Value, false)); //todo remove dependency on DataStorageFactory using (var storage = DataStorageFactory.Get()) { try { storage.AddData(bars, _instrument, frequency, OverwriteCheckbox.IsChecked.HasValue && OverwriteCheckbox.IsChecked.Value, false); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } bars.Clear(); } } } if (bars.Count == 0) { return; } //convert to exchange timezone ConvertTimeZone(bars, tzInfo); //if only the date column is set, we need to get the session info and generate the closing time ourselves if (frequency >= BarSize.OneDay && !Data.Columns.Contains("Time") && !Data.Columns.Contains("DateTime")) { //get the closing time for every day of the week var dotwValues = MyUtils.GetEnumValues <DayOfTheWeek>(); foreach (DayOfTheWeek d in dotwValues) { if (_instrument.Sessions.Any(x => x.ClosingDay == d && x.IsSessionEnd)) { var endTime = _instrument.Sessions.First(x => x.ClosingDay == d && x.IsSessionEnd).ClosingTime; sessionEndTimes.Add((int)d, endTime); } else { sessionEndTimes.Add((int)d, TimeSpan.FromSeconds(0)); } } for (int i = 0; i < bars.Count; i++) { int dayOfWeek = bars[i].DT.DayOfWeek.ToInt(); bars[i].DT = bars[i].DT.Date + sessionEndTimes[dayOfWeek]; } } //if there are no dividends/splits, but there IS an adjclose column, use that to adjust data right here //if there are divs/splits, adjustment will be done by the local storage if (frequency >= BarSize.OneDay && !Data.Columns.Contains("Dividends") && !Data.Columns.Contains("Splits") && Data.Columns.Contains("AdjClose")) { //if we have an adjusted close to work off of, we just use the ratio to get the OHL for (int i = 0; i < bars.Count; i++) { if (bars[i].AdjClose == null) { continue; } decimal ratio = bars[i].AdjClose.Value / bars[i].Close; bars[i].AdjOpen = bars[i].Open * ratio; bars[i].AdjHigh = bars[i].High * ratio; bars[i].AdjLow = bars[i].Low * ratio; } } //sort by date if (frequency >= BarSize.OneDay) { bars.Sort((x, y) => x.DT.CompareTo(y.DT)); } //try to import //todo remove dependency on DataStorageFactory using (var storage = DataStorageFactory.Get()) { try { storage.AddData(bars, _instrument, frequency, OverwriteCheckbox.IsChecked.HasValue && OverwriteCheckbox.IsChecked.Value, frequency >= BarSize.OneDay); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } sw.Stop(); MessageBox.Show(string.Format("Imported {0} bars in {1} ms.", barsCount, sw.ElapsedMilliseconds)); }