Exemplo n.º 1
0
        //public void ConvertHpDataPeriod(string symbol, string periodSsrc, string periodDest)
        //{
        //    string hpFileName = TestParameters.GetBaseFilePath(string.Format("{0}_{1}_hpdata.txt", symbol, periodSsrc));
        //    var hpData = GetHpDateFromTxt(hpFileName, TestParameters2.nTpsl);

        //    string hpFileName2 = TestParameters.GetBaseFilePath(string.Format("{0}_{1}_hpdata.txt", symbol, periodDest));

        //    int m = TestParameters.TpSlMaxCount / TestParameters2.nTpsl;
        //    DateTime date = TestParameters2.TrainStartTime;
        //    DateTime maxDate = TestParameters2.TrainEndTime;
        //    using(StreamWriter sw = new StreamWriter(hpFileName2))
        //    {
        //        while (true)
        //        {
        //            if (!TestParameters2.RealTimeMode)
        //            {
        //                Console.WriteLine(date.ToString(Parameters.DateTimeFormat));
        //            }

        //            StringBuilder sb = new StringBuilder();
        //            {
        //                if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
        //                {
        //                    date = date.AddHours(mainPeriodOfHour);
        //                    continue;
        //                }

        //                bool isComplete = true;


        //                if ((TestParameters2.UsePartialHpData || isComplete))
        //                {
        //                    sb.Append(date.ToString(Parameters.DateTimeFormat));
        //                    sb.Append(", ");

        //                    int[, , ,] hps = new int[2, m, m, 2];
        //                    long[, ,] hpTimes = new long[2, m, m];
        //                    for (int j = 0; j < hps.GetLength(0); ++j)
        //                        for (int k = 0; k < hps.GetLength(1); ++k)
        //                            for (int l = 0; l < hps.GetLength(2); ++l)
        //                            {
        //                                hps[j, k, l, 0] = hps[j, k, l, 1] = -1;
        //                                hpTimes[j, k, l] = -1;
        //                            }

        //                    List<DateTime> dts = new List<DateTime>();
        //                    foreach (DateTime dt in dts)
        //                    {
        //                        if (!hpData.ContainsKey(dt))
        //                            continue;

        //                        sbyte?[, ,] hp = hpData[dt].Item1;
        //                        long[, ,] hpTime = hpData[dt].Item2;

        //                        WekaUtils.DebugAssert(hp.GetLength(0) == 2, "");
        //                        WekaUtils.DebugAssert(hp.GetLength(1) % m == 0, "");
        //                        WekaUtils.DebugAssert(hp.GetLength(2) % m == 0, "");
        //                        WekaUtils.DebugAssert(hpTime.GetLength(0) == 2, "");

        //                        for (int j = 0; j < hp.GetLength(0); ++j)
        //                            for (int k = 0; k < hp.GetLength(1); ++k)
        //                                for (int l = 0; l < hp.GetLength(2); ++l)
        //                                {
        //                                    if (k % n != n - 1 || l % n != n - 1)
        //                                        continue;
        //                                    if (!hp[j, k, l].HasValue || hp[j, k, l] == -1)
        //                                        continue;

        //                                    //if (k / n >= TestParameters2.tpCount)
        //                                    //    continue;
        //                                    //if (l / n >= TestParameters2.slCount)
        //                                    //    continue;

        //                                    if (hps[j, k / n, l / n, 0] == -1)
        //                                        hps[j, k / n, l / n, 0] = 0;
        //                                    if (hps[j, k / n, l / n, 1] == -1)
        //                                        hps[j, k / n, l / n, 1] = 0;

        //                                    if (hp[j, k, l] == 1)
        //                                        hps[j, k / n, l / n, 0]++;
        //                                    else if (hp[j, k, l] == 0)
        //                                        hps[j, k / n, l / n, 1]++;
        //                                    else
        //                                        throw new AssertException("hp should be 0 or 1.");

        //                                    hpTimes[j, k / n, l / n] = Math.Max(hpTimes[j, k / n, l / n], hpTime[j, k, l].Value);
        //                                }

        //                    }

        //                    for (int j = 0; j < hps.GetLength(0); ++j)
        //                        for (int k = 0; k < hps.GetLength(1); ++k)
        //                            for (int l = 0; l < hps.GetLength(2); ++l)
        //                            {
        //                                if (isComplete)
        //                                {
        //                                    if (hps[j, k, l, 0] == -1 || hps[j, k, l, 1] == -1)
        //                                    {
        //                                        throw new AssertException("hps should not be -1.");
        //                                    }
        //                                }
        //                                if (hps[j, k, l, 0] == -1)
        //                                    hps[j, k, l, 0] = 0;
        //                                if (hps[j, k, l, 1] == -1)
        //                                    hps[j, k, l, 1] = 0;
        //                                sb.Append(hps[j, k, l, 0] + ", " + hps[j, k, l, 1] + ", ");
        //                            }

        //                    long maxHpTime = 0;
        //                    for (int j = 0; j < hpTimes.GetLength(0); ++j)
        //                        for (int k = 0; k < hpTimes.GetLength(1); ++k)
        //                            for (int l = 0; l < hpTimes.GetLength(2); ++l)
        //                            {
        //                                if (isComplete)
        //                                {
        //                                    if (hpTimes[j, k, l] == -1)
        //                                    {
        //                                        throw new AssertException("hpTimes should not be -1.");
        //                                    }
        //                                }
        //                                if (hpTimes[j, k, l] == -1)
        //                                    hpTimes[j, k, l] = Parameters.MaxTime;
        //                                sb.Append(hpTimes[j, k, l] + ", ");
        //                                maxHpTime = Math.Max(hpTimes[j, k, l], maxHpTime);
        //                            }

        //                    sb.AppendLine();

        //                    sw.Write(sb.ToString());
        //                }
        //                DateTime nextDate = date.AddMinutes(WekaUtils.GetMinuteofPeriod(periodDest));

        //                date = nextDate;
        //                if (date >= maxDate)
        //                    break;
        //            }
        //        }
        //    }
        //}

        public static Dictionary <long, int> GetHpDateTimesCountFromInstance(string fileName)
        {
            Dictionary <long, int> dictTimes = new Dictionary <long, int>();
            int dealCount = 0;

            var instances = WekaUtils.LoadInstances(TestParameters.GetBaseFilePath(fileName));

            for (int i = 0; i < instances.numInstances(); ++i)
            {
                var date   = WekaUtils.GetDateValueFromInstances(instances, 0, i);
                var hpdate = WekaUtils.GetDateValueFromInstances(instances, 1, i);


                var hptime = WekaUtils.GetTimeFromDate(hpdate);
                if (dictTimes.ContainsKey(hptime))
                {
                    dictTimes[hptime]++;
                }
                else
                {
                    dictTimes[hptime] = 1;
                }
                dealCount++;

                Console.WriteLine(date.ToString(Parameters.DateTimeFormat) + ", " + dictTimes.Count + ", " + dealCount);
            }
            return(dictTimes);
        }
Exemplo n.º 2
0
        public string BuildCandlePatternDeals()
        {
            WekaUtils.Instance.WriteLog("Now BuildCandlePatternDeals");

            var    cp         = TestParameters2.CandidateParameter;
            string resultFile = TestParameters.GetBaseFilePath(string.Format("IncrementTest_{0}_{1}_{2}.txt",
                                                                             cp.MainSymbol, "CandlePattern", cp.MainPeriod));

            if (File.Exists(resultFile))
            {
                return(string.Empty);
            }

            string txtFileName = TestParameters.GetBaseFilePath(string.Format("{0}_{1}_{2}.txt",
                                                                              cp.MainSymbol, "CandlePattern", cp.MainPeriod));

            System.IO.File.Delete(txtFileName);
            if (!File.Exists(txtFileName))
            {
                bool ret = GenerateCandlePatterns(txtFileName);
                if (!ret)
                {
                    return(string.Empty);
                }
            }

            string arffFileName = TestParameters.GetBaseFilePath(string.Format("{0}_{1}_{2}.arff",
                                                                               cp.MainSymbol, "CandlePattern", cp.MainPeriod));

            if (!System.IO.File.Exists(arffFileName))
            {
                GenerateArff(arffFileName, txtFileName);
            }

            weka.core.Instances allInstances = WekaUtils.LoadInstances(arffFileName);

            //FilterInstances(allInstances);
            WekaUtils.SaveInstances(allInstances, arffFileName);

            int n = (int)(24 / TestParameters2.MainPeriodOfHour);

            n = TestParameters2.nPeriod;
            return(TestManager.IncrementTest(allInstances, () =>
            {
                return WekaUtils.CreateClassifier(typeof(MinDistanceClassifier));
                //return WekaUtils.CreateClassifier(typeof(weka.classifiers.lazy.IBk));
            }, "1,2,3,4", resultFile, n));
        }