Beispiel #1
0
        public void Now(DateTime nowTime, double?nowPrice)
        {
            m_nowTime = nowTime;
            if (nowPrice.HasValue)
            {
                m_nowPrice.close = nowPrice.Value;
                m_nowPrice.time  = WekaUtils.GetTimeFromDate(nowTime);
            }

            if (!m_isLastForPeriod)
            {
                if (m_firstLastDate == null)
                {
                    m_firstLastDate = nowTime.AddMinutes(m_lastMinutes);
                }
                if (m_firstLastDate <= m_nowTime)
                {
                    m_isLastForPeriod = true;
                }
            }
            else
            {
                RemoveOldDeals();
            }

            //CloseAll();

            CalculateTotal();
            CalculateNow();
        }
Beispiel #2
0
        public void Build(weka.core.Instances instances)
        {
            WekaUtils.DebugAssert(instances.numClasses() == 3, "instance's numClasses should be 3.");
            for (int i = 0; i < m_counts.Length; i++)
            {
                m_counts[i] = 0;
            }

            double c = m_tp / m_sl;

            foreach (weka.core.Instance instance in instances)
            {
                int v = (int)instance.classValue();
                if (v == 2)
                {
                    m_counts[2] += c;
                }
                else if (v == 0)
                {
                    m_counts[0]++;
                }
                else
                {
                    m_counts[1]++;
                }
            }
        }
Beispiel #3
0
        public void SetData(string str, string scv, byte[] dealsInfo, byte[] dealsData)
        {
            this.CurrentTestRet    = WekaUtils.StringToDoubleArray(str);
            this.CurrentClassValue = WekaUtils.StringToDoubleArray(scv);

            var deals = Feng.Windows.Utils.SerializeHelper.Deserialize <DealsInfo>(dealsInfo);

            if (dealsData != null)
            {
                System.IO.MemoryStream outStream = new System.IO.MemoryStream();
                var inStream = new System.IO.MemoryStream(dealsData);
                System.IO.Compression.GZipStream zipStream = new System.IO.Compression.GZipStream(inStream, System.IO.Compression.CompressionMode.Decompress);
                zipStream.CopyTo(outStream);

                deals.Deals = Feng.Windows.Utils.SerializeHelper.Deserialize <List <DealInfo> >(outStream.ToArray());
            }
            else
            {
                deals.Deals = new List <DealInfo>();
            }
            if (deals != null)
            {
                this.Deals  = deals;
                Initialized = true;
            }
        }
Beispiel #4
0
        ///
        ///             <summary> * Generates the classifier.
        ///             * </summary>
        ///             * <param name="instances"> set of instances serving as training data  </param>
        ///             * <exception cref="Exception"> if the classifier has not been generated successfully </exception>
        ///
        public override void buildClassifier(Instances instances)
        {
            // can classifier handle the data?
            getCapabilities().testWithFail(instances);

            // remove instances with missing class
            var trainInstances = new Instances(instances);

            trainInstances.deleteWithMissingClass();

            WekaUtils.DebugAssert(instances.numClasses() == 3, "instance's numClasses should be 3.");
            m_counts = new double[instances.numClasses()];
            for (int i = 0; i < m_counts.Length; i++)
            {
                m_counts[i] = 0;
            }

            //double c = m_tp / m_sl;
            foreach (Instance instance in instances)
            {
                int v = (int)instance.classValue();
                m_counts[v]  += 1;
                sumOfWeights += 1;
            }
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public static void FilterInstances(weka.core.Instances allInstances)
        {
            DateTime nextHpDate = DateTime.MinValue;

            java.util.LinkedList deleteList = new java.util.LinkedList();
            for (int i = 0; i < allInstances.numInstances(); ++i)
            {
                DateTime nowDate = WekaUtils.GetDateValueFromInstances(allInstances, 0, i);

                if (TestParameters2.RealTimeMode && i == allInstances.numInstances() - 1)
                {
                    allInstances.instance(i).setClassValue(0);
                    allInstances.instance(i).setValue(1, WekaUtils.GetTimeFromDate(Parameters.MaxDate) * 1000);
                }
                else
                {
                    if (nowDate < nextHpDate)
                    {
                        deleteList.Add(allInstances.instance(i));
                    }
                    else
                    {
                        DateTime hpDate = WekaUtils.GetDateValueFromInstances(allInstances, 1, i);
                        nextHpDate = hpDate;
                    }
                }
            }
            allInstances.removeAll(deleteList);
        }
Beispiel #7
0
        private void CheckCandlePatternResult(Core.RetCode ret, int outBegIdx, int outNBElement, int[] outInteger, string candlePatternFile)
        {
            //if (!m_minOutNBElements.ContainsKey(m_currentPeriod))
            //{
            //    m_minOutNBElements[m_currentPeriod] = outNBElement - (minOutBegIdx - outBegIdx);
            //}
            //int minOutNBElement = m_minOutNBElements[m_currentPeriod];
            int minOutNBElement = outInteger.Length - minOutBegIdx;

            WekaUtils.DebugAssert(ret == Core.RetCode.Success, "ret == Core.RetCode.Success");
            WekaUtils.DebugAssert(outBegIdx <= minOutBegIdx, "outBegIdx <= minOutBegIdx");
            WekaUtils.DebugAssert(outNBElement >= minOutNBElement + (minOutBegIdx - outBegIdx), "outNBElement >= minOutNBElement + (minOutBegIdx - outBegIdx)");

            using (StreamWriter sw = new StreamWriter(candlePatternFile, true))
            {
                for (int i = 0; i < minOutNBElement; ++i)
                {
                    WekaUtils.DebugAssert(outInteger[minOutBegIdx - outBegIdx + i] != -1, "outInteger[minOutBegIdx - outBegIdx + i] != -1");

                    sw.Write(outInteger[minOutBegIdx - outBegIdx + i]);
                    sw.Write(",");
                }
                sw.WriteLine();
            }
            //WekaUtils.DebugAssert(outInteger[minOutBegIdx - outBegIdx + minOutNBElement - 1 + 1] == -1);
            for (int i = 0; i < outInteger.Length; ++i)
            {
                outInteger[i] = -1;
            }
        }
Beispiel #8
0
 private void RemoveOldDeals()
 {
     m_deals.RemoveAll(new System.Predicate <DealInfo>(i =>
     {
         WekaUtils.DebugAssert(i.CloseTime.HasValue, "Deal should has closeTime.");
         if (i.CloseTime.HasValue)
         {
             if (i.CloseTime < m_nowTime)
             {
                 if (i.Cost < 0)
                 {
                     m_nowTp++;
                 }
                 else
                 {
                     m_nowFp++;
                 }
                 m_nowCost   += i.Cost * i.Volume;
                 m_nowVolume += i.Volume;
                 return(true);
             }
         }
         else
         {
         }
         return(false);
     }));
 }
Beispiel #9
0
 private void AddIndicatorValues(ForexDataRows hpdv, Dictionary <long, double> indValues, Core.RetCode ret, int outBegIdx, int outNBElement, double[] outDouble)
 {
     WekaUtils.DebugAssert(ret == Core.RetCode.Success, "TaRet should Success.");
     for (int i = 0; i < outNBElement; ++i)
     {
         indValues[hpdv[i + outBegIdx].Time] = outDouble[i];
     }
 }
Beispiel #10
0
 private Tuple <long, long> GetDbDateFilter(DateTime dt1, DateTime dt2, int tableType)
 {
     if (tableType == 0)
     {
         return(new Tuple <long, long>(WekaUtils.GetTimeFromDate(dt1.AddDays(-1)), WekaUtils.GetTimeFromDate(dt2)));
     }
     else
     {
         return(new Tuple <long, long>(WekaUtils.GetTimeFromDate(dt1.AddDays(-3)), WekaUtils.GetTimeFromDate(dt2)));
     }
 }
Beispiel #11
0
        public static void SaveCCScoresToDb(DateTime nowDate, CandidateParameter cp, ParameterdCandidateStrategy pcs)
        {
            string tableName = string.Format("{0}_CCSCORE_{1}", cp.MainSymbol, cp.DealInfoLastMinutes / (7 * 24 * 12 * 5));
            long   nowTime   = WekaUtils.GetTimeFromDate(nowDate);

            try
            {
                Feng.Data.DbHelper.Instance.ExecuteNonQuery(string.Format("SELECT TOP 1 * FROM {0} WHERE TIME = -1", tableName));
            }
            catch (Exception)
            {
                string sqlCreate = string.Format(@"CREATE TABLE {0}(
	[Time] [bigint] NOT NULL,
	[scores] [varbinary](max) NOT NULL,
	[nda] [varbinary](max) NOT NULL,
	[nds] [varbinary](max) NOT NULL,
 CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED 
(
	[Time] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]", tableName);
                Feng.Data.DbHelper.Instance.ExecuteNonQuery(sqlCreate);
            }

            long[, ,] ndas        = new long[Parameters.AllDealTypes.Length, cp.BatchTps.Length, cp.BatchSls.Length];
            long[, ,] ndss        = new long[Parameters.AllDealTypes.Length, cp.BatchTps.Length, cp.BatchSls.Length];
            double[, ,] nowScores = new double[Parameters.AllDealTypes.Length, cp.BatchTps.Length, cp.BatchSls.Length];

            pcs.IterateClassifierInfos((k, ii, jj, h) =>
            {
                nowScores[k, ii, jj] = pcs.m_classifierInfoIdxs[k, ii, jj, h].Deals.NowScore;
                ndas[k, ii, jj]      = pcs.m_classifierInfoIdxs[k, ii, jj, h].Deals.DealLastTimeAvg;
                ndss[k, ii, jj]      = pcs.m_classifierInfoIdxs[k, ii, jj, h].Deals.DealLastTimeStd;
            });

            string sql = string.Format("INSERT INTO [{0}] ([Time],[scores],[nda],[nds]) VALUES (@Time, @score, @nda, @nds)", tableName);
            var    cmd = new System.Data.SqlClient.SqlCommand(sql);

            cmd.Parameters.AddWithValue("@Time", nowTime);
            cmd.Parameters.AddWithValue("@score", CCScoreData.SerializeScores(nowScores));
            cmd.Parameters.AddWithValue("@nda", CCScoreData.SerializeScoreTimes(ndas));
            cmd.Parameters.AddWithValue("@nds", CCScoreData.SerializeScoreTimes(ndss));

            try
            {
                Feng.Data.DbHelper.Instance.ExecuteNonQuery(string.Format("DELETE FROM {0} WHERE TIME = '{1}'", tableName, WekaUtils.GetTimeFromDate(nowDate)));

                Feng.Data.DbHelper.Instance.ExecuteNonQuery(cmd);
            }
            catch (Exception)
            {
            }
        }
Beispiel #12
0
        public void evaluateModel(weka.classifiers.Classifier classifier, weka.core.Instances instances)
        {
            double[] v = WekaUtils.ClassifyInstances(instances, classifier);

            //weka.classifiers.Evaluation eval = new weka.classifiers.Evaluation(instances, m_costMatrix);
            //v = eval.evaluateModel(classifier, instances);
            evaluateModel(v, instances);

            //int n = 0;
            //for (int i = 0; i < v.Length; ++i)
            //    if (v[i] != 0)
            //        n++;
        }
Beispiel #13
0
        private string GetDbDateFilter4Db(DateTime dt1, DateTime dt2, int tableType)
        {
            string filter;

            if (tableType == 0)
            {
                filter = string.Format("Time >= {0} AND Time < {1}", WekaUtils.GetTimeFromDate(dt1.AddDays(-1)), WekaUtils.GetTimeFromDate(dt2));
            }
            else
            {
                filter = string.Format("Time >= {0} AND Time < {1}", WekaUtils.GetTimeFromDate(dt1.AddDays(-3)), WekaUtils.GetTimeFromDate(dt2));
            }
            return(filter);
        }
Beispiel #14
0
        public DateTime?GetNewestTestData()
        {
            string sql = string.Format("SELECT MAX(Time) FROM {0}", m_testDataTableName);
            var    r   = Feng.Data.DbHelper.Instance.ExecuteScalar(sql);

            if (r == System.DBNull.Value)
            {
                return(null);
            }
            else
            {
                return(WekaUtils.GetDateFromTime(Convert.ToInt64(r)));
            }
        }
Beispiel #15
0
        public void Now(DateTime nowTime, double?nowPrice)
        {
            m_nowTime = nowTime;
            if (nowPrice.HasValue)
            {
                m_nowPrice.close = nowPrice.Value;
            }
            m_nowPrice.time = WekaUtils.GetTimeFromDate(nowTime);

            RemoveOldDeals();

            //CloseAll();

            CalculateCurrent();
        }
Beispiel #16
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));
        }
Beispiel #17
0
        public void Build(weka.core.Instances instances)
        {
            WekaUtils.DebugAssert(instances.numClasses() == 3, "instance's numClasses should be 3.");
            m_counts = new int[instances.numClasses()];

            for (int i = 0; i < m_counts.Length; i++)
            {
                m_counts[i] = 0;
            }

            foreach (Instance instance in instances)
            {
                int v = (int)instance.classValue();
                m_counts[v]++;
            }
        }
Beispiel #18
0
        public static int GetNonbusinessMinutes(DateTime dateStart, DateTime dateEnd, int stepMinutes = 1)
        {
            int  r  = 0;
            long t1 = WekaUtils.GetTimeFromDate(dateStart);
            long t2 = WekaUtils.GetTimeFromDate(dateEnd);

            for (long i = t1; i < t2; i += stepMinutes * 60)
            {
                DateTime d = WekaUtils.GetDateFromTime(i);
                if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
                {
                    r++;
                }
            }
            return(r);
        }
Beispiel #19
0
        ///
        ///             <summary> * Generates the classifier.
        ///             * </summary>
        ///             * <param name="instances"> set of instances serving as training data  </param>
        ///             * <exception cref="Exception"> if the classifier has not been generated successfully </exception>
        ///
        public override void buildClassifier(Instances instances)
        {
            // can classifier handle the data?
            getCapabilities().testWithFail(instances);

            // remove instances with missing class
            instances = new Instances(instances);
            instances.deleteWithMissingClass();

            double sumOfWeights = 0;

            WekaUtils.DebugAssert(instances.numClasses() == 3, "instance's numClasses should be 3.");
            m_counts       = new double[instances.numClasses()];
            m_normalCounts = new double[instances.numClasses()];
            for (int i = 0; i < m_counts.Length; i++)
            {
                m_counts[i]       = 0;
                m_normalCounts[i] = 0;
            }

            double c = m_tp / m_sl;

            foreach (Instance instance in instances)
            {
                int v = (int)instance.classValue();
                if (v == 2)
                {
                    m_counts[v]  += instance.weight() * c;
                    sumOfWeights += instance.weight() * c;
                }
                else
                {
                    m_counts[v]  += instance.weight();
                    sumOfWeights += instance.weight();
                }
            }

            double start = 0;

            for (int i = 0; i < m_counts.Length; ++i)
            {
                m_normalCounts[i] = (double)m_counts[i] / sumOfWeights + start;
                start             = m_normalCounts[i];
            }
        }
        public TpSlM1SimulateStrategy(string symbol, double tp, double sl, SimulationData simulationData)
        {
            m_simulationData = simulationData;

            m_symbol = symbol;
            if (WekaUtils.GetSymbolPoint(symbol) == 1)
            {
                point = 0.001;
            }
            else
            {
                point = 0.00001;
            }
            eps = point * 0.01;

            m_tp = tp * point;
            m_sl = sl * point;
        }
        public void UpdateUnclosedDeals(DateTime nowDate)
        {
            Dictionary <DateTime, System.Data.DataRow> dictHp = new Dictionary <DateTime, System.Data.DataRow>();

            foreach (var kvp in m_classifierInfos)
            {
                foreach (var i in kvp.Value.Deals.Deals)
                {
                    if (i.CloseTime.HasValue)
                    {
                        continue;
                    }

                    if (!dictHp.ContainsKey(i.OpenTime))
                    {
                        var sql = string.Format("SELECT * FROM {0}_HP WHERE TIME = {1}", this.CandidateParameter.MainSymbol, WekaUtils.GetTimeFromDate(i.OpenTime));
                        var dt  = Feng.Data.DbHelper.Instance.ExecuteDataTable(sql);
                        if (dt.Rows.Count > 0)
                        {
                            dictHp[i.OpenTime] = dt.Rows[0];
                        }
                    }
                    if (!dictHp.ContainsKey(i.OpenTime))
                    {
                        continue;
                    }
                    var row = dictHp[i.OpenTime];

                    int tpMinDelta = TestParameters.GetTpSlMinDelta(this.CandidateParameter.MainSymbol);
                    int slMinDelta = TestParameters.GetTpSlMinDelta(this.CandidateParameter.MainSymbol);

                    int d = i.DealType == 'B' ? 0 : 1;
                    sbyte?[, ,] hps    = HpData.DeserializeHp((byte[])row["hp"]);
                    long?[, ,] hpsTime = HpData.DeserializeHpTimes((byte[])row["hp_date"]);
                    var hp = hps[d, kvp.Value.Tp / tpMinDelta - 1, kvp.Value.Sl / slMinDelta - 1];
                    if (hp.HasValue &&
                        hp.Value != -1)
                    {
                        i.CloseTime = WekaUtils.GetDateFromTime(hpsTime[d, kvp.Value.Tp / tpMinDelta - 1, kvp.Value.Sl / slMinDelta - 1].Value);
                        i.Cost      = hp.Value == 1 ? -kvp.Value.Tp : kvp.Value.Sl;
                    }
                }
            }
        }
Beispiel #22
0
        public static void GenerateTrainTest()
        {
            using (StreamWriter sw = new StreamWriter("d:\\test.arff"))
            {
                sw.WriteLine("@relation 'Captcha'");
                for (int i = 0; i < 150; ++i)
                {
                    sw.WriteLine(string.Format("@attribute p{0}", i.ToString()) + " {0, 1}");
                }
                sw.WriteLine("@attribute prop {0,1}");
                sw.WriteLine("@data");
                sw.WriteLine();
                using (StreamReader sr = new StreamReader("d:\\downloads\\reference\\train_EntWeb_1a.txt"))
                {
                    while (!sr.EndOfStream)
                    {
                        string s1 = sr.ReadLine();
                        string s2 = sr.ReadLine();

                        string[] sp = s1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        WekaUtils.DebugAssert(sp.Length == 150, "");
                        for (int i = 0; i < sp.Length; ++i)
                        {
                            sw.Write(sp[i].Trim() + " ");
                        }

                        if (s2 == "1 0")
                        {
                            sw.WriteLine("1");
                        }
                        else if (s2 == "0 1")
                        {
                            sw.WriteLine("0");
                        }
                        else
                        {
                            WekaUtils.DebugAssert(false, "");
                        }
                    }
                }
            }
        }
        private void OutputRealDealSummary(DateTime nowDate)
        {
            string realDealSummary = "TR:TTP={0},TFP={1},NC={2},NTP={3},NFP={4},NV={5},CP={6},CV={7},CD={8},TC={9},TV={10}";

            if (!string.IsNullOrEmpty(this.CandidateParameter.Name))
            {
                realDealSummary = this.CandidateParameter.Name + "-" + realDealSummary;
            }
            var testInstancesTemplate = WekaData.GetTestInstancesTemplate(this.CandidateParameter.Name);

            if (testInstancesTemplate != null && testInstancesTemplate.numInstances() > 0)
            {
                m_realDealsInfo.Now(nowDate, WekaUtils.GetValueFromInstance(testInstancesTemplate, "mainClose", 0));
            }
            else
            {
                m_realDealsInfo.Now(nowDate, null);
            }
            //currentSummary = string.Format(realDealSummary,
            //    (int)eval.numTruePositives(1), (int)eval.numFalsePositives(1),
            //    m_realDealsInfo.NowCost.ToString(Parameters.DoubleFormatString), m_realDealsInfo.NowTp, m_realDealsInfo.NowFp, m_realDealsInfo.NowVolume.ToString("N2"),
            //    m_realDealsInfo.CurrentProfit.ToString(Parameters.DoubleFormatString), m_realDealsInfo.CurrentVolume.ToString("N2"), m_realDealsInfo.CurrentDeal,
            //    m_realDealsInfo.TotalCost.ToString(Parameters.DoubleFormatString), m_realDealsInfo.TotalVolume.ToString("N2"));

            currentSummary = string.Format(realDealSummary,
                                           0, 0,
                                           m_realDealsInfo.NowCost.ToString(Parameters.DoubleFormatString), m_realDealsInfo.NowTp, m_realDealsInfo.NowFp, m_realDealsInfo.NowVolume.ToString("N2"),
                                           m_realDealsInfo.CurrentProfit.ToString(Parameters.DoubleFormatString), m_realDealsInfo.CurrentVolume.ToString("N2"), m_realDealsInfo.CurrentDeal,
                                           m_realDealsInfo.TotalCost.ToString(Parameters.DoubleFormatString), m_realDealsInfo.TotalVolume.ToString("N2"));

            //if (m_enableStoreResultInDb)
            //{
            //System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT TestResult (TestName, Date, Content, TotalCost) VALUES (@TestName, @Date, @Content, @TotalCost)");
            //cmd.Parameters.AddWithValue("@TestName", TestParameters.TestName);
            //cmd.Parameters.AddWithValue("@Date", m_testTimeEnd);
            //cmd.Parameters.AddWithValue("@Content", currentSummary);
            //cmd.Parameters.AddWithValue("@TotalCost", realDealsInfo.NowCost);

            //Feng.Data.DbHelper.Instance.ExecuteNonQuery(cmd);
            //}
        }
Beispiel #24
0
        public ForexData(ForexDataTable dt, System.Data.DataRow row, bool isHpData)
        {
            m_isHpData        = isHpData;
            m_parentDataTable = dt;
            m_values          = new object[row.Table.Columns.Count - 1];
            if (!isHpData)
            {
                for (int i = 0; i < m_values.Length; ++i)
                {
                    m_values[i] = row[i + 1];
                }
            }
            else
            {
                WekaUtils.DebugAssert(m_values.Length == 2, "hp data length is 2");

                m_values[0] = row[1];
                m_values[1] = row[2];
            }
            this.Time = (long)row[0];
        }
Beispiel #25
0
        private bool?Do(DateTime openDate, double openPrice, out DateTime?closeDate, int dealType)
        {
            closeDate = DateTime.MinValue;
            var dt = Feng.Data.DbHelper.Instance.ExecuteDataTable(string.Format("SELECT * FROM {0}_HP WHERE Time = {1}", m_symbol, WekaUtils.GetTimeFromDate(openDate)));

            if (dt.Rows.Count == 0)
            {
                return(null);
            }
            sbyte?[, ,] hp    = HpData.DeserializeHp((byte[])dt.Rows[0]["hp"]);
            long?[, ,] hpTime = HpData.DeserializeHpTimes((byte[])dt.Rows[0]["hp_date"]);

            int ix = (int)m_tp / TestParameters.GetTpSlMinDelta(m_symbol) - 1;
            int iy = (int)m_sl / TestParameters.GetTpSlMinDelta(m_symbol) - 1;

            if (hp[dealType, ix, iy].HasValue)
            {
                closeDate = WekaUtils.GetDateFromTime(hpTime[dealType, ix, iy].Value);
                return(hp[dealType, ix, iy].Value == 1);
            }
            return(null);
        }
Beispiel #26
0
        public Tuple <int[, , , ], long[, , ]> GetHpData(DateTime date)
        {
            int n = m_buffer.Count - 5 * 15 * 20000;

            if (n > 0)
            {
                List <long> delete = new List <long>();
                foreach (var kvp in m_buffer)
                {
                    delete.Add(kvp.Key);
                    if (delete.Count >= n)
                    {
                        break;
                    }
                }
                foreach (var i in delete)
                {
                    m_buffer.Remove(i);
                }
            }

            Tuple <int[, , , ], long[, , ]> ret = null;
            long time = WekaUtils.GetTimeFromDate(date);

            if (!m_buffer.ContainsKey(time))
            {
                var sql = string.Format("SELECT * FROM {0}_HP WHERE TIME = {1}", m_symbol, time);
                var dt  = Feng.Data.DbHelper.Instance.ExecuteDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    ret = HpData.SumHp(dt.Select());
                }
                m_buffer[time] = ret;
            }

            return(m_buffer[time]);
        }
Beispiel #27
0
        public static void InitParameters(string symbol = "EURUSD", string period = "D1", int ntpsl = 1)
        {
            //TestParameters.TpSlMaxCount = 30;
            //nTpsl = 2;
            nTpsl = ntpsl;

            if (tpCount == -1)
            {
                tpCount = TestParameters.TpMaxCount / nTpsl;
            }
            if (slCount == -1)
            {
                slCount = TestParameters.SlMaxCount / nTpsl;
            }

            //tpStart = slStart = 0;
            //tpCount = slCount = 10;

            var cp = CandidateParameter;

            cp.AllSymbols  = new string[] { symbol };
            cp.SymbolStart = 0;
            cp.SymbolCount = cp.AllSymbols.Length;

            cp.AllPeriods  = new string[] { period };
            cp.PeriodStart = 0;
            cp.PeriodCount = cp.AllPeriods.Length;

            cp.PrevTimeCount = PreLength;

            //int delta = TestParameters.GetTpSlMinDelta(symbol) * TestParameters2.nTpsl;
            //cp.InitTpsls(TestParameters2.tpStart, delta, TestParameters2.tpCount, TestParameters2.slStart, delta, TestParameters2.slCount);

            MainPeriodOfHour = WekaUtils.GetMinuteofPeriod(cp.MainPeriod) / 60.0;   // 24=D

            cp.InitIndicators();
        }
Beispiel #28
0
        public void GenerateArff(string arffFileName, string candlePatternFile)
        {
            int preLength = TestParameters2.PreLength;

            int tpStart = TestParameters2.tpStart;
            int slStart = TestParameters2.slStart;
            int tpCount = TestParameters2.tpCount;
            int slCount = TestParameters2.slCount;

            var dataDates = TestManager.GetDataDateRange();

            int minOutBegIdx = TaLibTest.minOutBegIdx;

            var cp = TestParameters2.CandidateParameter;

            int[, , ,] cds = null;

            ForexDataRows[,] hpdvs = new ForexDataRows[cp.SymbolCount, cp.PeriodCount];

            for (int s = 0; s < cp.SymbolCount; ++s)
            {
                for (int p = 0; p < cp.PeriodCount; ++p)
                {
                    string symbol       = cp.AllSymbols[s + cp.SymbolStart];
                    string period       = cp.AllPeriods[p + cp.PeriodStart];
                    string symbolPeriod = string.Format("{0}_{1}", symbol, period);
                    hpdvs[s, p] = DbData.Instance.GetDbData(dataDates[0], dataDates[1], symbolPeriod, 0, true, cp);

                    m_currentPeriod = period;
                    //int minOutNBElement = m_minOutNBElements[m_currentPeriod];
                    int minOutNBElement = hpdvs[s, p].Length - minOutBegIdx;

                    if (cds == null)
                    {
                        CreateCds(ref cds, cp, minOutNBElement + 100);
                    }

                    using (StreamReader sr = new StreamReader(candlePatternFile))
                    {
                        int n = 0;
                        while (!sr.EndOfStream)
                        {
                            string   line = sr.ReadLine();
                            string[] ss   = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                            WekaUtils.DebugAssert(ss.Length == minOutNBElement, "ss.Length == minOutNBElement");
                            for (int i = 0; i < minOutNBElement; ++i)
                            {
                                cds[s, p, i, n] = Convert.ToInt32(ss[i]);
                            }
                            n++;
                        }
                    }
                }
            }

            if (File.Exists(arffFileName))
            {
                System.IO.File.Delete(arffFileName);
            }

            string wekaFileName = string.Format(arffFileName);

            using (StreamWriter sw = new StreamWriter(wekaFileName))
            {
                sw.WriteLine("@relation 'candlePatterns'");
                sw.WriteLine("@attribute timestamp date \"yyyy-MM-dd\'T\'HH:mm:ss\"");
                sw.WriteLine("@attribute hpdate date \"yyyy-MM-dd\'T\'HH:mm:ss\"");
                sw.WriteLine("@attribute spread numeric");
                sw.WriteLine("@attribute mainClose numeric");

                for (int pre = 0; pre < preLength; ++pre)
                {
                    for (int s = 0; s < cp.SymbolCount; ++s)
                    {
                        for (int p = 0; p < cp.PeriodCount; ++p)
                        {
                            for (int i = 0; i < 61; ++i)
                            {
                                sw.WriteLine(string.Format("@attribute {0}_{1}_{2}_p{3} {4}", cp.AllSymbols[s + cp.SymbolStart],
                                                           cp.AllPeriods[p + cp.PeriodStart], i.ToString(), pre.ToString(), "{0,100,200,-100,-200}"));
                            }
                        }
                    }
                }
                sw.WriteLine("@attribute prop " + " {0,1,2,3}");
                sw.WriteLine("@data");
                sw.WriteLine();

                var hps  = HpData.Instance.GetHpSum(cp.MainSymbol, cp.MainPeriod);
                var hpdv = hpdvs[0, 0];
                for (int i = minOutBegIdx + preLength - 1; i < hpdv.Length; ++i)
                {
                    DateTime nowDate = WekaUtils.GetDateFromTime((long)hpdv[i].Time);
                    //if (nowDate.Hour % TestParameters2.MainPeriodOfHour != 0)
                    //    continue;

                    if (!hps.ContainsKey(nowDate) && !(TestParameters2.RealTimeMode && i == hpdv.Length - 1))
                    {
                        continue;
                    }

                    long hpTime = WekaUtils.GetTimeFromDate(Parameters.MaxDate);
                    int  hp     = 0;
                    if (!(TestParameters2.RealTimeMode && i == hpdv.Length - 1))
                    {
                        hpTime = hps[nowDate].Item2;
                        hp     = hps[nowDate].Item1;
                    }

                    sw.Write(nowDate.ToString(Parameters.DateTimeFormat));
                    sw.Write(",");

                    // hp
                    sw.Write(WekaUtils.GetDateFromTime(hpTime).ToString(Parameters.DateTimeFormat));
                    sw.Write(",");

                    sw.Write((int)hpdv[i]["spread"]);
                    sw.Write(",");

                    sw.Write(((double)hpdv[i]["close"]).ToString());
                    sw.Write(",");

                    for (int pre = 0; pre < preLength; ++pre)
                    {
                        for (int s = 0; s < cp.SymbolCount; ++s)
                        {
                            for (int p = 0; p < cp.PeriodCount; ++p)
                            {
                                for (int j = 0; j < 61; ++j)
                                {
                                    int candlePattern = cds[s, p, i - minOutBegIdx - preLength + pre + 1, j];
                                    if (candlePattern == -1)
                                    {
                                        throw new AssertException(string.Format("candle pattern should not be -1.idx={0}", i - minOutBegIdx - preLength + pre + 1));
                                    }
                                    sw.Write(candlePattern);
                                    sw.Write(",");
                                }
                            }
                        }
                    }

                    sw.WriteLine(hp.ToString());
                }
            }
        }
Beispiel #29
0
        public void InitIndicators()
        {
            // 1: 0~100: (v - 50) * 2 * 0.01
            // 2: (-price):-0.01~0.01:(v - price) * 100
            // 3: 0~0.01: (v * 100 - 0.5) * 2
            // 4: -0.01~0.01: v * 100
            // 5: -500~500: v * 0.002
            // 6: 0~1: (v - 0.5) * 2
            // 7: -100~0: (v + 50) * 2 * 0.01
            // 8: -1~1: nothing
            // 9: 0~0.01: v * 100

            AllIndNames2["close"] = 2;
            AllIndNames2["open"]  = 2;
            AllIndNames2["high"]  = 2;
            AllIndNames2["low"]   = 2;

            AllIndNames["ADXWilder_14"]   = -1; // 3line
            AllIndNames["ADXWilder_14_P"] = -1;
            AllIndNames["ADXWilder_14_M"] = -1;
            AllIndNames["ADX_14"]         = -1; // 3line
            AllIndNames["ADX_14_P"]       = -1;
            AllIndNames["ADX_14_M"]       = -1;
            AllIndNames["AMA_9_2_30"]     = 2;
            AllIndNames["ATR_14"]         = 9;
            AllIndNames["Bands_20_2"]     = 2; //3line
            AllIndNames["BearsPower_13"]  = -4;
            AllIndNames["BullsPower_13"]  = -4;
            AllIndNames["CCI_14"]         = -5;
            AllIndNames["Demarker_14"]    = -6;
            AllIndNames["DEMA_14"]        = -2;
            AllIndNames["FrAMA_14"]       = -2;
            AllIndNames["MACD_12_26_9_M"] = 4;
            AllIndNames["MACD_12_26_9_S"] = 4;
            AllIndNames["MA_10"]          = 2;
            //inds["Momentum_14"] = 0.01;  //97-103?    // ind25
            //inds["OsMA_12_26_9"] = double.MinValue; // MACD - SIGNAL  // ind26
            AllIndNames["RSI_14"]   = 1;
            AllIndNames["RVI_10_M"] = -8;         // 2Line
            AllIndNames["RVI_10_S"] = -8;
            //AllIndNames["SAR_002_02"] = 2;
            //AllIndNames["StdDev_20"] = -3;
            AllIndNames["Stochastic_5_3_3_M"] = -1;
            AllIndNames["Stochastic_5_3_3_S"] = -1;
            AllIndNames["TEMA_14"]            = -2;
            AllIndNames["Trix_14"]            = -4;
            AllIndNames["VIDyA_9_12"]         = -2;
            AllIndNames["WPR_14"]             = -7;
            WekaUtils.DebugAssert(AllIndNames.Count == 27, "AllIndNames.Count == 27");

            ////All
            //AllIndNames2["open"] = 2;
            //AllIndNames2["close"] = 2;
            //AllIndNames2["high"] = 2;
            //AllIndNames2["low"] = 2;

            //AllIndNames["ADXWilder_14"] = 1;   // 3line
            //AllIndNames["ADXWilder_14_P"] = 1;
            //AllIndNames["ADXWilder_14_M"] = 1;
            //AllIndNames["ADX_14"] = 1;         // 3line
            //AllIndNames["ADX_14_P"] = 1;
            //AllIndNames["ADX_14_M"] = 1;
            //AllIndNames["AMA_9_2_30"] = 2;
            //AllIndNames["ATR_14"] = 9;
            //AllIndNames["Bands_20_2"] = 2;     //3line
            //AllIndNames["BearsPower_13"] = 4;
            //AllIndNames["BullsPower_13"] = 4;
            //AllIndNames["CCI_14"] = 5;
            //AllIndNames["Demarker_14"] = 6;
            //AllIndNames["DEMA_14"] = 2;
            //AllIndNames["FrAMA_14"] = 2;
            //AllIndNames["MACD_12_26_9_M"] = 4;
            //AllIndNames["MACD_12_26_9_S"] = 4;
            //AllIndNames["MA_10"] = 2;
            ////inds["Momentum_14"] = 0.01;  //97-103?    // ind25
            ////inds["OsMA_12_26_9"] = double.MinValue; // MACD - SIGNAL  // ind26
            //AllIndNames["RSI_14"] = 1;
            //AllIndNames["RVI_10_M"] = 8;         // 2Line
            //AllIndNames["RVI_10_S"] = 8;
            ////AllIndNames["SAR_002_02"] = 2;
            //AllIndNames["StdDev_20"] = -3;
            //AllIndNames["Stochastic_5_3_3_M"] = 1;
            //AllIndNames["Stochastic_5_3_3_S"] = 1;
            //AllIndNames["TEMA_14"] = 2;
            //AllIndNames["Trix_14"] = 4;
            //AllIndNames["VIDyA_9_12"] = 2;
            //AllIndNames["WPR_14"] = 7;

            if (this.MainPeriod == "M1")
            {
                List <string> allIndNames = new List <string>();
                foreach (var kvp in AllIndNames)
                {
                    allIndNames.Add(kvp.Key);
                }
                foreach (var s in allIndNames)
                {
                    AllIndNames[s] = -1;
                }
            }
        }
Beispiel #30
0
        static void Main(string[] args)
        {
            System.Console.Write("File Date: ");
            System.Console.WriteLine(System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location));

            try
            {
                Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(new Microsoft.Practices.ServiceLocation.ServiceLocatorProvider(
                                                                                          delegate()
                {
                    return(new EmptyServiceProvider());
                }));

                //Feng.Utils.SecurityHelper.SelectAutomaticServer();

                java.lang.System.setOut(new PrintStream(new ByteArrayOutputStream()));
                java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("GMT"));

                string cmdSymbol = "EURUSD";
                if (args.Length == 2)
                {
                    if (args[0] == "-s")
                    {
                        cmdSymbol = args[1];
                    }
                }
                //if (args.Length > 0)
                //{
                //    int n = ea.ReadConfig(args);
                //    if (n == 1)
                //        return;
                //}

                //DbUtils.CompensateMissingDateAll();
                //DbUtils.CheckDataIntegrityAll();
                //DbUtils.CheckDateIntegrityOfCount();
                //DbUtils.CheckDbZeroValues();
                //DbUtils.ConvertHpdataFromSaturday2Monday();
                //TestTool.CheckMtData("EURUSD_M1");
                //TestTool.CheckMtHpData("EURUSD");
                //DbUtils.ImportToDbAll("EURUSD", "M30");
                //DbUtils.CalculateUSDX();
                //DbUtils.GeneratePeriodData("USDX", "D1", 0);
                //DbUtils.GenerateIndicators("USDX", "D1");
                //return;
                //DbUtils.GenerateRandomData("EURUSD");

                //TestParameters2.InitParameters("EURUSD", "M15", 1);
                //string updateHpWhere = string.Format("TIME >= {0} AND TIME < {1} AND Time % 1800 = 0",
                //    WekaUtils.GetTimeFromDate(new DateTime(2000, 1, 1)), WekaUtils.GetTimeFromDate(new DateTime(2013, 1, 1)));
                //DbUtils.UpdateAllHp3("EURUSD", updateHpWhere);

                //WekaEA.WekaEA2 wekaEA2 = new WekaEA.WekaEA2();
                //wekaEA2.Init("EURUSD");
                //wekaEA2.RunTool("ImportDB");
                //WekaEA.WekaEA2.SimulateMt("EURUSD");
                //return;

                //SimulateIncrementTest();

                //TestTool.GenerateConsoleCCs();

                //TestTool.CheckHpData();
                //TestTool.GetResultCost("f:\\Forex\\console.2000-2012.USDCHF.w8.txt", "d:\\result8.txt");
                //TestTool.ParseMtReport4Consecutive();
                //TestTool.TestMultiClassifierResult();

                //DbUtils.CheckDbCountToSame("D1");

                //TestTool.ReorderEaOrderTxt(string.Format("f:\\forex\\TestDebug\\ea_order_{0}.txt", TestParameters2.CandidateParameter.MainSymbol),
                //    "f:\\forex\\TestDebug\\ea_order.txt", 1);
                //TestTool.GenerateHpAccordCloseTime("USDCHF");
                //TestTool.ConvertHpAccordCloseTime();
                //DbUtils.GeneratePeriodData("IF9999", "D1", TestParameters2.HourAhead);
                //DbUtils.GenerateIndicators("AUDUSD", "D1");
                //TestTool.ParseDetailDealLog(string.Format("{0}_ccScores_w{1}", TestParameters2.CandidateParameter.MainSymbol, TestParameters2.lastWeek));

                //string symbol = cmdSymbol;

                TestTool.ParseTotalResult("e:\\total_result.txt", "e:\\ff", true);
                //foreach (var s in Parameters.AllSymbolsFull)
                //{
                //    string fileName = string.Format("\\\\192.168.0.10\\f$\\Forex\\Test_{0}\\total_result.txt", s);
                //    if (System.IO.File.Exists(fileName))
                //        TestTool.ParseTotalResult(fileName, s);
                //}

                TaLibTest   taLibTest = new TaLibTest();
                TestManager tm        = new TestManager();
                TxtTest     txtTest   = new TxtTest();

                foreach (string s in new string[] { "EURUSD" })  //"GBPUSD", "EURUSD", "USDCHF","AUDUSD", "USDJPY", "USDCAD"
                {
                    foreach (string p in new string[] { "M15" }) //"M5", "M15", "M30", "H1", "H4", "D1" })
                    {
                        TestParameters2.InitParameters(s, p, 1);
                        TestParameters.TestName = "Test_" + s;

                        System.IO.File.Delete(TestParameters.GetBaseFilePath("console.txt"));
                        foreach (string s1 in Directory.GetFiles(TestParameters.BaseDir, "ea_order*.txt"))
                        {
                            System.IO.File.Delete(s1);
                        }

                        //TxtTest.SimulateEaOrders();

                        HpData.Instance.GenerateHpDataToTxt(TestParameters2.CandidateParameter.MainSymbol, TestParameters2.CandidateParameter.MainPeriod);

                        bool[,] selectedTpSl = new bool[20, 20];
                        for (int i = 0; i < 20; i += 1)
                        {
                            for (int j = 0; j < 20; j += 1)
                            {
                                selectedTpSl[i, j] = false;
                            }
                        }

                        bool onlyOneTime = false;

                        for (int t = 0; t < 1; ++t)
                        {
                            //TestParameters2.TrainStartTime = (new DateTime(2010, 1, 1)).AddMonths(12 * t);
                            //TestParameters2.TrainEndTime = (new DateTime(2010, 1, 1)).AddMonths(12 * (t + 1));
                            //if (TestParameters2.TrainStartTime > new DateTime(2012, 8, 1))
                            //    break;

                            string resultFileName = TestParameters.GetBaseFilePath("total_result.txt");
                            var    alreadyResults = TestTool.ParseTotalResult(resultFileName, null);

                            for (int i = 9; i >= 2; i -= 1)
                            {
                                for (int j = 19; j >= 2; j -= 1)
                                {
                                    if (alreadyResults[i, j] != -1)
                                    {
                                        continue;
                                    }

                                    if (!onlyOneTime)
                                    {
                                        TestParameters2.slStart = i;
                                        TestParameters2.slCount = i + 1;
                                        TestParameters2.tpStart = j;
                                        TestParameters2.tpCount = j + 1;
                                    }

                                    using (StreamWriter sw = new StreamWriter(resultFileName, true))
                                    {
                                        sw.WriteLine(string.Format("{0},{1},{2},{3}",
                                                                   TestParameters2.slStart, TestParameters2.slCount,
                                                                   TestParameters2.tpStart, TestParameters2.tpCount));

                                        if (!(args.Length >= 1 && args[0] == "-t"))
                                        {
                                            foreach (string s1 in Directory.GetFiles(TestParameters.BaseDir, "IncrementTest_*.txt"))
                                            {
                                                System.IO.File.Delete(s1);
                                            }
                                            foreach (string s1 in Directory.GetFiles(TestParameters.BaseDir, "*.arff"))
                                            {
                                                System.IO.File.Delete(s1);
                                            }
                                        }

                                        try
                                        {
                                            TestParameters2.OutputParameters();

                                            var s0 = tm.BuildHpProbDeals();
                                            sw.WriteLine(s0);

                                            var s1 = tm.BuildPricePatternDeals();
                                            sw.WriteLine(s1);

                                            var s2 = taLibTest.BuildCandlePatternDeals();
                                            sw.WriteLine(s2);

                                            //foreach (string w in new string[] { "1" })
                                            //{
                                            //    TestParameters2.lastWeek = w;
                                            //    try
                                            //    {
                                            //        CCScoreData.Instance.GenerateData(s, Convert.ToInt32(w));
                                            //    }
                                            //    catch (Exception ex)
                                            //    {
                                            //        WekaUtils.Instance.WriteLog(ex.Message);
                                            //    }

                                            //    CCScoreData.Instance.GenerateDataToTxt(TestParameters2.CandidateParameter.MainSymbol, TestParameters2.lastWeek);
                                            //    txtTest.BuildCCScoreDeals();
                                            //}

                                            WekaUtils.Instance.DeInit();

                                            string result;
                                            if (selectedTpSl[i, j] || onlyOneTime)
                                            {
                                                if (selectedTpSl[i, j])
                                                {
                                                    sw.WriteLine(string.Format("{0} select {1},{2}",
                                                                               TestParameters2.TrainStartTime.ToString(Parameters.DateTimeFormat), i, j));
                                                }
                                                result = txtTest.SimulateAccordDealLog(true);
                                            }
                                            else
                                            {
                                                result = txtTest.SimulateAccordDealLog(false);
                                            }
                                            sw.WriteLine(result);
                                            sw.Flush();

                                            string profitString = WekaUtils.GetSubstring(result, "ProfitFactor = ");
                                            double profitFactor = 0;
                                            if (!string.IsNullOrEmpty(profitString))
                                            {
                                                profitFactor = Convert.ToDouble(profitString);
                                            }
                                            if (profitFactor > 1.1)
                                            {
                                                selectedTpSl[i, j] = true;
                                            }
                                            else
                                            {
                                                selectedTpSl[i, j] = false;
                                            }

                                            if (!onlyOneTime)
                                            {
                                                foreach (string is1 in Directory.GetFiles(TestParameters.BaseDir, "IncrementTest_*.txt"))
                                                {
                                                    string is2 = string.Format("{0}_{1}_{2}.txt", is1.Replace(".txt", "").Replace("IncrementTest_", "IncrementTest2_"), i, j);
                                                    System.IO.File.Delete(is2);
                                                    System.IO.File.Move(is1, is2);
                                                }

                                                WekaUtils.Instance.DeInit();
                                                foreach (string is1 in Directory.GetFiles(TestParameters.BaseDir, "console.txt"))
                                                {
                                                    string is2 = string.Format("{0}_{1}_{2}.txt", is1.Replace(".txt", ""), i, j);
                                                    System.IO.File.Delete(is2);
                                                    System.IO.File.Move(is1, is2);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            WekaUtils.Instance.WriteLog(ex.Message);
                                            WekaUtils.Instance.WriteLog(ex.StackTrace);
                                        }

                                        TestManager.Clear();

                                        if (onlyOneTime)
                                        {
                                            break;
                                        }
                                    }
                                    if (onlyOneTime)
                                    {
                                        break;
                                    }
                                }

                                HpData.Instance.Clear();
                                DbData.Instance.Clear();
                                taLibTest.Clear();
                                WekaData.ClearTemplates();
                                CCScoreData.Instance.Clear();
                            }
                        }
                        TxtTest.SortEaOrders();
                    }
                }

                //foreach (string s in new string[] { "GBPUSD", "AUDUSD", "USDJPY", "USDCAD", "USDCHF", "EURUSD" })
                //{
                //    TestParameters.TestName = "Test_" + s;
                //    TestParameters2.InitParameters(s, "M5", 1);

                //    foreach (string w in new string[] { "1" })
                //    {
                //        TestParameters2.lastWeek = w;
                //        try
                //        {
                //            CCScoreData.Instance.GenerateData(s, Convert.ToInt32(w));
                //        }
                //        catch (Exception ex)
                //        {
                //            WekaUtils.Instance.WriteLog(ex.Message);
                //        }
                //    }

                //    foreach (string p in new string[] { "M5", "M15", "M30", "H1", "H4", "D1" })
                //    {
                //        TestParameters.TestName = "Test_" + s;

                //        if (p == "M5")
                //            TestParameters2.MaxTrainSize = TestParameters2.MinTrainSize = 5;
                //        else
                //            TestParameters2.MaxTrainSize = TestParameters2.MinTrainSize = 2;

                //        TestParameters2.InitParameters(s, p, 1);
                //        //TestParameters2.tpCount = TestParameters2.slCount = 20;

                //        //TestParameters2.InitParameters(symbol, "D1", 4);
                //        //TestParameters2.tpCount = TestParameters2.slCount = 10;
                //        //TestParameters2.tpStart = TestParameters2.slStart = 9;

                //        TestParameters2.OutputParameters();

                //        foreach (string w in new string[] { "1" })
                //        {
                //            TestParameters2.lastWeek = w;

                //            CCScoreData.Instance.GenerateDataToTxt(TestParameters2.CandidateParameter.MainSymbol, TestParameters2.lastWeek);
                //            txtTest.BuildCCScoreDeals();
                //        }
                //    }
                //}

                ///////////////////////////////////////////////////////////////////////////////////
                WaitForThreads();
                //Feng.Utils.SecurityHelper.DeselectAutomaticServer();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
                System.Console.ReadLine();
            }

            System.Console.WriteLine("End");
            System.Console.Beep();

            ReadLine(60000);
        }