Пример #1
0
        public async Task GenerateClosePriceWithVolume(string symbol, string interval)
        {
            var instrument = await _instrumentRepository.GetInstrument(symbol);

            var chart = await _chartRepository.GetChartMonthlyAsync(symbol, interval, 6, instrument.Precision);

            var maxhour = chart.Quotations.Max(x => x.Time.Hour);
            var minhour = chart.Quotations.Min(x => x.Time.Hour);

            var maxMinute = chart.Quotations.Max(x => x.Time.Minute);
            var minMinute = chart.Quotations.Min(x => x.Time.Minute);

            var maxDay = chart.Quotations.Max(x => (int)x.Time.DayOfWeek);
            var minDay = chart.Quotations.Min(x => (int)x.Time.DayOfWeek);

            using (var textWriter = new StreamWriter(@"cloePriceOhlc.csv"))
            {
                var writer = new CsvWriter(textWriter, CultureInfo.CurrentCulture);
                writer.Configuration.Delimiter = ",";
                writer.WriteField("Hour");
                writer.WriteField("Minute");
                writer.WriteField("Day");
                writer.WriteField("Close");
                writer.WriteField("Open");
                writer.WriteField("Low");
                writer.WriteField("High");
                writer.WriteField("Volume");
                writer.WriteField("Time");
                writer.NextRecord();

                foreach (var item in chart.Quotations)
                {
                    var hourNormalization   = (item.Time.Hour - minhour) / (maxhour - minhour);
                    var minuteNormalization = (item.Time.Minute - minMinute) / (maxMinute - minMinute);
                    var dayNormalization    = ((int)item.Time.DayOfWeek - minDay) / (maxDay - minDay);

                    writer.WriteField(hourNormalization);
                    writer.WriteField(minuteNormalization);
                    writer.WriteField(dayNormalization);
                    writer.WriteField(item.Close.ToString().Replace(",", "."));
                    writer.WriteField(item.Open.ToString().Replace(",", "."));
                    writer.WriteField(item.Low.ToString().Replace(",", "."));
                    writer.WriteField(item.High.ToString().Replace(",", "."));
                    writer.WriteField(item.Volume);
                    writer.WriteField(item.Time);
                    writer.NextRecord();
                }
            }
            using (var textWriter = new StreamWriter(@"resultsOhlc.csv"))
            {
                var writer = new CsvWriter(textWriter, CultureInfo.CurrentCulture);
                writer.Configuration.Delimiter = ",";
                writer.WriteField("Buy");
                writer.WriteField("Sell");
                writer.WriteField("None");
                writer.WriteField("Time");
                writer.NextRecord();

                for (var i = 120; i < chart.Quotations.Count - 15; i++)
                {
                    var futurePrices = chart.Quotations.GetRange(i, 15).Select(x => new { x.Close, x.Low, x.High }).ToArray();
                    var futureFirst  = futurePrices.First();
                    var futureMin    = futurePrices.Min(x => x.Low);
                    var futureMax    = futurePrices.Max(x => x.High);


                    //bad signal high pin
                    var badSignal = false;
                    foreach (var item in futurePrices)
                    {
                        if (item.High - item.Low > 40)
                        {
                            badSignal = true;
                        }
                    }
                    for (int j = 0; j <= futurePrices.Count() - 2; j++)
                    {
                        if (Math.Abs(futurePrices[j].Close - futurePrices[j + 1].Close) > 40)
                        {
                            badSignal = true;
                        }
                    }


                    var time = chart.Quotations[i].Time;

                    if (badSignal)
                    {
                        writer.WriteField(false);
                        writer.WriteField(false);
                        writer.WriteField(true);
                        writer.WriteField(time);
                        writer.NextRecord();
                        continue;
                    }

                    if (futureMax - futureFirst.High > 20 && futureFirst.Low - futureMin < 10)
                    {
                        writer.WriteField(true);
                        writer.WriteField(false);
                        writer.WriteField(false);
                        writer.WriteField(time);
                        writer.NextRecord();
                    }
                    else if (futureFirst.Low - futureMin > 20 && futureMax - futureFirst.High < 10)
                    {
                        writer.WriteField(false);
                        writer.WriteField(true);
                        writer.WriteField(false);
                        writer.WriteField(time);
                        writer.NextRecord();
                    }
                    else
                    {
                        writer.WriteField(false);
                        writer.WriteField(false);
                        writer.WriteField(true);
                        writer.WriteField(time);
                        writer.NextRecord();
                    }
                }
            }
        }