private void UpdateHistoricalData(BarSize barSize)
        {
            var frequency = barSize; //(BarSize) ((MenuItem) sender).Tag;

            int requestCount = 0;

            using (var localStorage = DataStorageFactory.Get())
            {
                foreach (var instrument in selectedInstruments)
                {
                    var storageInfo = localStorage.GetStoredDataInfo(instrument.ID);
                    if (storageInfo.Any(x => x.Frequency == frequency))
                    {
                        var relevantStorageInfo = storageInfo.First(x => x.Frequency == frequency);
                        Client.RequestHistoricalData(new HistoricalDataRequest(
                                                         instrument,
                                                         frequency,
                                                         relevantStorageInfo.LatestDate +
                                                         frequency.ToTimeSpan(),
                                                         DateTime.Now,
                                                         DataLocation.ExternalOnly,
                                                         true));
                        requestCount++;
                    }
                }
            }

            if (ProgressBarValue >= ProgressBarMaximum)
            {
                ProgressBarMaximum = requestCount;
                ProgressBarValue   = 0;
            }
            else
            {
                ProgressBarMaximum += requestCount;
            }
        }
Beispiel #2
0
        private void ImportBtn_Click()
        {
            var 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 (SelectedBarSize < 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;
            }

            //make sure the timezone is set, and get it
            if (string.IsNullOrEmpty(_instrument.Exchange.Timezone))
            {
                MessageBox.Show("Instrument's exchange has no set timezone, can't import.");
                return;
            }

            var tzInfo = TimeZoneInfo.FindSystemTimeZoneById(_instrument.Exchange.Timezone);

            //get the multipliers
            var parseWorked = decimal.TryParse(PriceMultiplier, out decimal priceMultiplier);

            if (!parseWorked)
            {
                priceMultiplier = 1;
            }
            parseWorked = int.TryParse(VolumeMultiplier, out int volumeMultiplier);
            if (!parseWorked)
            {
                volumeMultiplier = 1;
            }

            //lines to skip
            parseWorked = int.TryParse(StartingLine, out int toSkip);
            if (!parseWorked)
            {
                toSkip = 1;
            }

            //get the frequency
            var frequency = SelectedBarSize;

            //separator
            var separator = SelectedDelimeter.ToCharArray();

            var bars = new List <OHLCBar>();

            var columns = new string[Data.Columns.Count];

            for (var 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
            var sessionEndTimes = new Dictionary <int, TimeSpan>();

            //1 day and up: we can load it all in one go with no trouble, also may require adjustment
            var     periodicSaving = frequency < BarSize.OneDay;
            OHLCBar bar;
            var     barsCount = 0;

            using (var sr = new StreamReader(FilePath))
            {
                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);
                        return;
                    }

                    //only add the bar if it falls within the specified date range
                    if (bar.DateTimeClose >= MinDT && bar.DateTimeClose <= MaxDT)
                    {
                        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
                        var secDiffs = new List <int>();
                        for (var i = 1; i < bars.Count; i++)
                        {
                            secDiffs.Add(
                                (int)Math.Round((bars[i].DateTimeClose - bars[i - 1].DateTimeClose).TotalSeconds));
                        }

                        var mostFrequent = secDiffs.MostFrequent();
                        if ((int)Math.Round(frequency.ToTimeSpan().TotalSeconds) != 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
                        using (var storage = DataStorageFactory.Get())
                        {
                            try
                            {
                                storage.AddData(bars, _instrument, frequency, IsOverwrite, 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 (var 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));
                    }
                }

                foreach (var t in bars)
                {
                    var dayOfWeek = t.DateTimeClose.DayOfWeek.ToInt();
                    t.DateTimeClose = t.DateTimeClose.Date + sessionEndTimes[dayOfWeek];
                }
            }



            //sort by date
            if (frequency >= BarSize.OneDay)
            {
                bars.Sort((x, y) => x.DateTimeClose.CompareTo(y.DateTimeClose));
            }

            //try to import
            using (var storage = DataStorageFactory.Get())
            {
                try
                {
                    storage.AddData(bars,
                                    _instrument,
                                    frequency,
                                    IsOverwrite,
                                    frequency >= BarSize.OneDay);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
            sw.Stop();
            MessageBox.Show($"Imported {barsCount} bars in {sw.ElapsedMilliseconds} ms.");
        }