Пример #1
0
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            chartIndex.Series[0].Points.Clear();
            chartIndexWoTrend.Series[0].Points.Clear();
            chartSpectrum.Series[0].Points.Clear();
            tbHarmonics.Text = "";
            tbHarmAmpls.Text = "";

            var usePeriodAxis = cbSpectrX.SelectedIndex == 1;
            var dataCount     = int.Parse(tbDataCount.Text);

            // индекс
            var trendType = cbTrendType.SelectedIndex == 1
                                ? IndexFFT.TrendReducion.MovingAverage
                                : IndexFFT.TrendReducion.Linear;
            var data = IndexFFT.GetIndexData(cbIndexFunction.Text,
                                             tbFolder.Text,
                                             int.Parse(tbInterval.Text),
                                             dataCount,
                                             trendType,
                                             int.Parse(tbMAPeriod.Text),
                                             cbUseStartDate.Checked ? (DateTime?)dpStartDate.Value : null,
                                             cbIndexType.SelectedIndex == 1
                                    ? IndexFFT.CurrencyIndexType.AdditiveMomentumAccumulated
                                    : IndexFFT.CurrencyIndexType.AdditiveWeightedToOne);

            if (data.Length == 0)
            {
                return;
            }
            var i = 0;

            foreach (var ind in data)
            {
                chartIndex.Series[0].Points.Add(new SeriesPoint((double)(i++), ind));
            }

            // индекс без тренда
            var dataWT = IndexFFT.RemoveTrend(data, trendType, int.Parse(tbMAPeriod.Text));

            i = 0;
            foreach (var ind in dataWT)
            {
                chartIndexWoTrend.Series[0].Points.Add(new SeriesPoint((double)(i++), ind));
            }

            // спектр
            var spectrum = IndexFFT.CalcFFT(dataWT, dataCount);
            int nBands   = dataCount / 10;

            for (i = 10; i < nBands; i++)
            {
                var x = usePeriodAxis ? (double)dataCount / i : (double)i;
                chartSpectrum.Series[0].Points.Add(new SeriesPoint(x, spectrum[i]));
            }

            // преобладающие гармоники
            var sb         = new StringBuilder();
            var peakPeriod = int.Parse(tbHarmPeak.Text);

            // T - amplitude
            var peaks = new List <Cortege2 <double, double> >();

            for (i = 10; i < nBands; i++)
            {
                var isPeak = true;
                for (var j = i - peakPeriod; j < i + peakPeriod; j++)
                {
                    if (j < 0 || j == i)
                    {
                        continue;
                    }
                    if (spectrum[i] < spectrum[j])
                    {
                        isPeak = false;
                        break;
                    }
                }
                if (!isPeak)
                {
                    continue;
                }

                var T    = dataCount / i;
                var ampl = spectrum[i];
                peaks.Add(new Cortege2 <double, double> {
                    a = T, b = ampl
                });
            }

            peaks = peaks.OrderByDescending(p => p.b).ToList();
            foreach (var peak in peaks)
            {
                sb.AppendLine(string.Format("Период: {0:f2}. Амплитуда {1:f4}", peak.a, peak.b));
            }
            tbHarmonics.Text = sb.ToString();
            sb = new StringBuilder();

            peaks = peaks.OrderBy(p => p.a).ToList();
            foreach (var peak in peaks)
            {
                sb.AppendLine(string.Format("Период: {0:f2}. Амплитуда {1:f4}", peak.a, peak.b));
            }
            tbHarmAmpls.Text = sb.ToString();
        }
Пример #2
0
        private void btnFormQuote_Click(object sender, EventArgs e)
        {
            var ciEur = new CurrencyIndexInfo(tbEur.Text);
            var ciUsd = new CurrencyIndexInfo(tbUsd.Text);

            var currencyStream = new Dictionary <string, CurrencyStream>();
            // открыть потоки чтения
            var allPairs = ciEur.pairs.Union(ciUsd.pairs).ToList();

            if (!allPairs.Contains("eur"))
            {
                allPairs.Add("eur");
            }
            foreach (var curName in allPairs)
            {
                currencyStream.Add(curName, new CurrencyStream(curName, quotePath));
            }

            // сессия (разрешенные часы)
            int startDay = 1, endDay = 6;
            int startHr = 1, endHr = 1;
            var sessParts = tbSession.Text.Split(new[] { ' ', ':', '-', ',', '.' }, StringSplitOptions.RemoveEmptyEntries);

            if (sessParts.Length == 4)
            {
                startDay = int.Parse(sessParts[0]);
                startHr  = int.Parse(sessParts[1]);
                endDay   = int.Parse(sessParts[2]);
                endHr    = int.Parse(sessParts[3]);
            }
            var k = tbMultiplier.Text.ToDecimalArrayUniform();

            // считать индекс
            try
            {
                if (!startDate.HasValue)
                {
                    startDate = IndexFFT.GetFirstDateFromCurStreams(currencyStream, false);
                }
                DateTime date = startDate.Value;

                using (var swEur = new StreamWriter(tbFolder.Text + ".i1.quote"))
                    using (var swUsd = new StreamWriter(tbFolder.Text + ".i2.quote"))
                        using (var swEurUsd = new StreamWriter(tbFolder.Text + ".fr.quote"))
                        {// eur0.3349# jpy0.1652 gbp0.082# cad0.2887 sek0.0189 chf0.027
                            double?prevEur = null, prevUsd = null;
                            for (var i = 0; i < maxDataLen; i++)
                            {
                                var eur = ciEur.CalculateIndexMultiplicative(date, currencyStream);
                                var usd = ciUsd.CalculateIndexMultiplicative(date, currencyStream);
                                if (ciEur.EndOfStream || ciUsd.EndOfStream)
                                {
                                    break;
                                }
                                eur = eur * (double)k[0];
                                usd = usd * (double)k[1];

                                var duplicates = false;
                                if (prevEur.HasValue)
                                {
                                    if (prevEur.Value == eur && prevUsd.Value == usd)
                                    {
                                        duplicates = true;
                                    }
                                }

                                if (!duplicates)
                                {
                                    prevEur = eur;
                                    prevUsd = usd;

                                    // 11.370;11.350;03.01.2009 23:04:00
                                    var dateStr = date.ToString("dd.MM.yyyy HH:mm:ss");
                                    var line    = string.Format(CultureInfo.InvariantCulture,
                                                                "{0:f4};{0:f4};{1}", eur, dateStr);
                                    swEur.WriteLine(line);
                                    line = string.Format(CultureInfo.InvariantCulture,
                                                         "{0:f4};{0:f4};{1}", usd, dateStr);
                                    swUsd.WriteLine(line);
                                    if (eur > 0 && usd > 0)
                                    {
                                        var fract = (double)k[2] * eur / usd;
                                        line = string.Format(CultureInfo.InvariantCulture,
                                                             "{0:f4};{0:f4};{1}", fract, dateStr);
                                        swEurUsd.WriteLine(line);
                                    }
                                }

                                date = date.AddMinutes(intervalMinutes);
                                // пропуск выходных
                                var weekDay = (int)date.DayOfWeek;
                                if (weekDay == 0)
                                {
                                    weekDay = 7;
                                }

                                if ((weekDay > endDay || (weekDay == endDay && date.Hour >= endHr))
                                    ||
                                    (weekDay < startDay || (weekDay == startDay && date.Hour < startHr)))
                                {
                                    var newWeekDay = date.Date.AddDays(7 - weekDay + startDay);
                                    date = newWeekDay.AddHours(startHr);
                                }
                            }
                        }
            }
            finally
            {   // закрыть потоки чтения
                foreach (var cs in currencyStream)
                {
                    cs.Value.CloseStream();
                }
            }
        }