Пример #1
0
        public List <Candlestick[]> GetCandlesticksEachDate()
        {
            DateTime oldDate = new DateTime();

            Candlestick[]        hmList = null;
            List <Candlestick[]> result = new List <Candlestick[]>();
            TimeZoneInfo         est    = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

            foreach (var c in candlesticks)
            {
                if (IsSummerTime != null && IsSummerTime.Value == false && est.IsDaylightSavingTime(c.DateTime))
                {
                    continue;
                }



                DateTime shiftedTime = c.DateTime.AddHours(this.ShiftHour);

                if (!oldDate.Equals(shiftedTime.Date))
                {
                    hmList = new Candlestick[NumOfCandlesInDay];
                    result.Add(hmList);
                    oldDate = shiftedTime.Date;
                }
                hmList[GetArrayIndex(new TimeSpan(shiftedTime.Hour, shiftedTime.Minute, 0))] = c;
//				hmList[shiftedTime.Hour * 2 + (shiftedTime.Minute == 30 ? 1 : 0)] = c;
            }
            return(result);
        }
Пример #2
0
        private void 日時ベスト特定日時検証(object sender, EventArgs e)
        {
            RunTask(sender, (Report report) => {
                report.Version = 1;
                report.Comment = "0000-0430_0500-0700";
                report.SetHeader("date", "balance", "isbuy", "isplus");
                List <Candlestick> list       = new List <Candlestick>(new CandlesticksReader().Read(DATA_PATH + @"\m30-5y.csv"));
                DateTime oldDate              = new DateTime();
                Candlestick[] hmList          = null;
                List <Candlestick[]> dateList = new List <Candlestick[]>();
                foreach (var c in list)
                {
                    if (!oldDate.Equals(c.Time.Date))
                    {
                        hmList = new Candlestick[48];
                        dateList.Add(hmList);
                        oldDate = c.Time.Date;
                    }
                    hmList[c.Time.Hour * 2 + (c.Time.Minute == 30 ? 1 : 0)] = c;
                }

                foreach (var t in CheckTrade(new CandlesticksReader().Read(DATA_PATH + @"\m30-5y.csv"), new int[] { 0, 9, 10, 14 }))
                {
                    report.WriteLine(t.Item1.Year + "/" + t.Item1.Month + "/" + t.Item1.Day, t.Item2, (t.Item3 ? "1" : "0"), (t.Item2 > 0 ? "1" : "0"));
                }
            });
        }
Пример #3
0
        private void LoadChart()
        {
            using (DBUtils.OpenThreadConnection()) {
                DateTime start = NormalizeDateTime(dateTime.AddDays(-1));

                chart1.Series.Clear();
                chart1.ChartAreas.Clear();
                chart1.Titles.Clear();

                var candles = Candlestick.Aggregate(new CandlesticksGetter()
                {
                    Granularity = "M10",
                    Start       = start,
                    End         = start.AddDays(1)
                }.Execute().ToList(), 2).ToList();

                var positions = new OrderBookPricePointDao(
                    DBUtils.GetConnection()).GetPositionSummaryGroupByOrderBookDateTime(start, dateTime)
                                .Select(p => new Tuple <DateTime, float, float>(NormalizeDateTime(p.Item1), p.Item2, p.Item3)).ToList();

                DateTime startChart = candles[0].DateTime;
                DateTime endChart   = candles[candles.Count() - 1].DateTime.AddMinutes(20);
                if (candles.Where(c => !c.IsNull).Count() >= 1)
                {
                    CreatePositionSmummaryChartArea(positions, startChart, endChart);
                    CreateCandlesticksChartArea(candles, positions, startChart, endChart);
                }



                nextDayButton.Enabled = dateTime.AddDays(1) <= DateTime.Now;
                dateLabel.Text        = dateTime.ToString("M/dd");
            }
        }
Пример #4
0
            public float Settle(Candlestick candlestick, int volume)
            {
                if (volume > this.volume)
                {
                    throw new Exception();
                }

                this.volume -= volume;
                float d = (candlestick.Close - current.Candlestick.Close) * volume;

                if (tradeType == TradeType.Ask)
                {
                    return(d);
                }
                else
                {
                    return(-d);
                }
            }
Пример #5
0
 private void 近傍ポジション変化_Click(object sender, EventArgs e)
 {
     RunTask(sender, report => {
         report.Version         = 3;
         report.IsForceOverride = true;
         report.Comment         = "";
         report.SetHeader("date_time", "pre_pl", "cur_pl", "pre_ps", "cur_ps", "low", "high", "close");
         using (DBUtils.OpenThreadConnection()) {
             OrderBookDao.Entity preOrderBook = null;
             foreach (var orderBook in GetOrderBooks())
             {
                 if (preOrderBook != null)
                 {
                     var candle = Candlestick.Aggregate(new CandlesticksGetter()
                     {
                         Granularity = "M10",
                         Start       = orderBook.NormalizedDateTime.AddMinutes(-20),
                         Count       = 2
                     }.Execute());
                     if (orderBook.NormalizedDateTime == new DateTime(2016, 2, 22, 10, 40, 0, DateTimeKind.Local))
                     {
                         int a = 1;
                     }
                     float prePl = 0, prePs = 0;
                     float curPl = 0, curPs = 0;
                     foreach (var pp in GetRangePricePoint(orderBook, candle.Low, candle.High))
                     {
                         curPl += pp.Pl;
                         curPs += pp.Ps;
                     }
                     foreach (var pp in GetRangePricePoint(preOrderBook, candle.Low, candle.High))
                     {
                         prePl += pp.Pl;
                         prePs += pp.Ps;
                     }
                     report.WriteLine(orderBook.DateTime, prePl, curPl, prePs, curPs, candle.Low, candle.High, candle.Close);
                 }
                 preOrderBook = orderBook;
             }
         }
     });
 }
Пример #6
0
        public List <Candlestick[]> GetCandlesticksEachDate()
        {
            DateTime oldDate = new DateTime();

            Candlestick[]        hmList = null;
            List <Candlestick[]> result = new List <Candlestick[]>();

            foreach (var c in candlesticksM30)
            {
                DateTime shiftedTime = c.Time.AddHours(this.ShiftHour);

                if (!oldDate.Equals(shiftedTime.Date))
                {
                    hmList = new Candlestick[48];
                    result.Add(hmList);
                    oldDate = shiftedTime.Date;
                }
                hmList[shiftedTime.Hour * 2 + (shiftedTime.Minute == 30 ? 1 : 0)] = c;
            }
            return(result);
        }
Пример #7
0
        static public Candlestick Aggregate(IEnumerable <Candlestick> l)
        {
            Candlestick result = new Candlestick();

            result.High = float.MinValue;
            result.Low  = float.MaxValue;
            bool isOpen = true;

            foreach (var c in l)
            {
                if (isOpen)
                {
                    result.DateTime = c.DateTime;
                    if (c.IsNull)
                    {
                        result.Open = 0;
                    }
                    else
                    {
                        result.Open = c.Open;
                    }
                    isOpen = false;
                }
                if (c.IsNull)
                {
                    continue;
                }
                result.Close = c.Close;
                if (result.IsNull == false)
                {
                    result.High = Math.Max(result.High, c.High);
                    result.Low  = Math.Min(result.Low, c.Low);
                }
            }
            if (isOpen)
            {
                throw new Exception();
            }
            return(result);
        }
Пример #8
0
        public IEnumerable <Candlestick> Read(string filePath)
        {
            var    file = new StreamReader(filePath);
            string line;

            while (true)
            {
                line = file.ReadLine();
                if (line == null)
                {
                    break;
                }
                var         e = line.Split(',');
                Candlestick c = new Candlestick();
                c.DateTime = DateTime.Parse(e[0] + " " + e[1]);
                c.Open     = float.Parse(e[2]);
                c.High     = float.Parse(e[3]);
                c.Low      = float.Parse(e[4]);
                c.Close    = float.Parse(e[5]);
                yield return(c);
            }
            file.Close();
        }
Пример #9
0
        private IEnumerable <Tuple <DateTime, float> > CheckWeekTrade(IEnumerable <Candlestick> list, int[] tradeTimes)
        {
            DayOfWeek oldDayOfWeek = new DayOfWeek();

            Candlestick[]        hmList   = null;
            List <Candlestick[]> dateList = new List <Candlestick[]>();

            foreach (var c in list)
            {
                if (hmList == null || oldDayOfWeek > c.Time.DayOfWeek)
                {
                    hmList = new Candlestick[48 * 7];
                    dateList.Add(hmList);
                }
                oldDayOfWeek = c.Time.DayOfWeek;
                hmList[(int)c.Time.DayOfWeek * 48 + c.Time.Hour * 2 + (c.Time.Minute == 30 ? 1 : 0)] = c;
            }

            foreach (var hml in dateList)
            {
                if (tradeTimes.Count(tt => hml[tt].Open == 0) >= 1)
                {
                    continue;
                }

                float sd = hml[tradeTimes[1]].Close - hml[tradeTimes[0]].Open;
                float ed = hml[tradeTimes[3]].Close - hml[tradeTimes[2]].Open;
                if (sd > 0)
                {
                    yield return(new Tuple <DateTime, float>(hml[tradeTimes[0]].Time, -ed));
                }
                else if (sd < 0)
                {
                    yield return(new Tuple <DateTime, float>(hml[tradeTimes[0]].Time, ed));
                }
            }
        }
Пример #10
0
        private IEnumerable <Tuple <DateTime, float, bool> > CheckTrade(IEnumerable <Candlestick> list, int[] tradeTimes)
        {
            DateTime oldDate = new DateTime();

            Candlestick[]        hmList   = null;
            List <Candlestick[]> dateList = new List <Candlestick[]>();

            foreach (var c in list)
            {
                if (!oldDate.Equals(c.Time.Date))
                {
                    hmList = new Candlestick[48];
                    dateList.Add(hmList);
                    oldDate = c.Time.Date;
                }
                hmList[c.Time.Hour * 2 + (c.Time.Minute == 30 ? 1 : 0)] = c;
            }

            foreach (var hml in dateList)
            {
                if (tradeTimes.Count(tt => hml[tt].Open == 0) >= 1)
                {
                    continue;
                }

                float sd = hml[tradeTimes[1]].Close - hml[tradeTimes[0]].Open;
                float ed = hml[tradeTimes[3]].Close - hml[tradeTimes[2]].Open;
                if (sd > 0)
                {
                    yield return(new Tuple <DateTime, float, bool>(hml[tradeTimes[0]].Time, -ed, false));
                }
                else if (sd < 0)
                {
                    yield return(new Tuple <DateTime, float, bool>(hml[tradeTimes[0]].Time, ed, true));
                }
            }
        }
Пример #11
0
        static public Candlestick Create(IEnumerable <Candlestick> l)
        {
            Candlestick result = new Candlestick();

            result.High = float.MinValue;
            result.Low  = float.MaxValue;
            bool isOpen = true;

            foreach (var c in l)
            {
                if (isOpen)
                {
                    result.Open = c.Open;
                    isOpen      = false;
                }
                else
                {
                    result.Close = c.Close;
                }
                result.High = Math.Max(result.High, c.High);
                result.Low  = Math.Min(result.Low, c.Low);
            }
            return(result);
        }
Пример #12
0
 public Candlestick Add(Candlestick candle)
 {
     return(Candlestick.Aggregate(new Candlestick[] { this, candle }));
 }
Пример #13
0
 private IEnumerable <Candlestick> GetM30Candles(TimeSpan span)
 {
     return(Candlestick.Aggregate(GetM10Candles(span), 3));
 }
Пример #14
0
 public Current(CurrentEnumrator enumerator, Candlestick candlestick)
 {
     this.enumerator  = enumerator;
     this.candlestick = candlestick;
 }
Пример #15
0
        private IEnumerable <Tuple <int[], int[]> > GetBestTradeTimePinBar(IEnumerable <Candlestick> list, int limit)
        {
            DateTime oldDate = new DateTime();

            Candlestick[]        hmList   = null;
            List <Candlestick[]> dateList = new List <Candlestick[]>();

            foreach (var c in list)
            {
                if (!oldDate.Equals(c.Time.Date))
                {
                    hmList = new Candlestick[48];
                    dateList.Add(hmList);
                    oldDate = c.Time.Date;
                }
                hmList[c.Time.Hour * 2 + (c.Time.Minute == 30 ? 1 : 0)] = c;
            }

            List <Tuple <int[], int[]> > summary = new List <Tuple <int[], int[]> >();

            for (int s1 = 0; s1 < 48 - 12; s1++)
            {
                for (int e1 = s1 + 1; e1 < s1 + 12; e1++)
                {
                    for (int s2 = e1 + 1; s2 < 48 - 12; s2++)
                    {
                        for (int e2 = s2 + 1; e2 < s2 + 12; e2++)
                        {
                            int[] dayResult = new int[12];
                            summary.Add(new Tuple <int[], int[]>(new int[] { s1, e1, s2, e2 }, dayResult));
                            foreach (var hml in dateList)
                            {
                                try {
                                    if (hml[e1].Close == 0 || hml[s1].Open == 0 || hml[s2].Open == 0 || hml[e2].Close == 0)
                                    {
                                        continue;
                                    }
                                    float d1 = hml[e1].Close - hml[s1].Open;
                                    float d2 = hml[e2].Close - hml[s2].Open;

                                    var c1 = Candlestick.Create(hml.Skip(s1).Take(e1 - s1 + 1));
                                    int barType;
                                    if (c1.IsPinbar(true, 0.3f, 0.1f))
                                    {
                                        barType = 1;
                                    }
                                    else if (c1.IsPinbar(false, 0.3f, 0.1f))
                                    {
                                        barType = 2;
                                    }
                                    else
                                    {
                                        barType = 0;
                                    }
                                    dayResult[GetBestTradeTimePinBarResultIndex(d1 >= 0, d2 >= 0, barType)]++;
                                } catch (Exception) {
                                }
                            }
                        }
                    }
                }
            }

            int best = 0;

            foreach (var t in summary.OrderByDescending(t =>
                                                        new int[] { 0, 1, 2 }.Max(barType => t.Item2[GetBestTradeTimePinBarResultIndex(true, false, barType)]) +
                                                        new int[] { 0, 1, 2 }.Max(barType => t.Item2[GetBestTradeTimePinBarResultIndex(false, true, barType)])))
            {
                if (best >= limit)
                {
                    break;
                }
                yield return(t);

                best++;
            }
        }
Пример #16
0
        private IEnumerable <Tuple <int[], int[]> > GetBestTradeWeekTime(IEnumerable <Candlestick> list, int limit)
        {
            DayOfWeek oldDayOfWeek = new DayOfWeek();

            Candlestick[]        hmList   = null;
            List <Candlestick[]> dateList = new List <Candlestick[]>();

            foreach (var c in list)
            {
                if (hmList == null || oldDayOfWeek > c.Time.DayOfWeek)
                {
                    hmList = new Candlestick[48 * 7];
                    dateList.Add(hmList);
                }
                oldDayOfWeek = c.Time.DayOfWeek;
                hmList[(int)c.Time.DayOfWeek * 48 + c.Time.Hour * 2 + (c.Time.Minute == 30 ? 1 : 0)] = c;
            }

            List <Tuple <int[], int[]> > summary = new List <Tuple <int[], int[]> >();

            for (int s1 = 0; s1 < 7 * 48 - 6; s1++)
            {
                for (int e1 = s1 + 1; e1 < s1 + 6; e1++)
                {
                    for (int s2 = e1 + 1; s2 < 7 * 48 - 6; s2++)
                    {
                        for (int e2 = s2 + 1; e2 < s2 + 6; e2++)
                        {
                            int[] dayResult = new int[4];
                            bool  f         = false;
                            foreach (var hml in dateList)
                            {
                                try {
                                    float d1 = hml[e1].Close - hml[s1].Open;
                                    float d2 = hml[e2].Close - hml[s2].Open;
                                    if (hml[e1].Close == 0 || hml[s1].Open == 0 || hml[s2].Open == 0 || hml[e2].Close == 0)
                                    {
                                        continue;
                                    }
                                    dayResult[(d1 >= 0 ? 0 : 2) + (d2 >= 0 ? 0 : 1)]++;
                                    f = true;
                                } catch (Exception) {
                                }
                            }
                            if (f)
                            {
                                summary.Add(new Tuple <int[], int[]>(new int[] { s1, e1, s2, e2 }, dayResult));
                            }
                        }
                    }
                }
            }

            int i = 0;

            foreach (var t in summary.OrderByDescending(t => (float)(t.Item2[1] + t.Item2[2]) /*/ t.Item2.Sum()*/))
            {
                if (i >= limit)
                {
                    break;
                }
                yield return(t);

                i++;
            }
        }