Пример #1
0
 public override double classifyInstance(weka.core.Instance instance)
 {
     var d = base.classifyInstance(instance);
     if (d == 3)
         return 2;
     return d;
 }
Пример #2
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);
        }
Пример #3
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]++;
                }
            }
        }
Пример #4
0
        public static bool Filter(DateTime nowDate, weka.core.Instance instance = null, weka.core.Instance preInstance = null)
        {
            //if (nowDate.DayOfWeek == DayOfWeek.Friday && nowDate.AddDays(7).Month != nowDate.Month)
            //    return true;
            if (nowDate.DayOfWeek == DayOfWeek.Friday)
                return true;

            //var cp = TestParameters2.CandidateParameter;
            //string symbolPeriod = string.Format("{0}_{1}", cp.MainSymbol, cp.MainPeriod);
            //ForexDataRows hpdv = DbData.Instance.GetDbData(TestParameters2.TrainStartTime, TestParameters2.TrainEndTime, symbolPeriod, 0, true, cp);
            //int idx = hpdv.BinarySearch(0, hpdv.Length, WekaUtils.GetTimeFromDate(nowDate));
            //if (idx < 1)
            //    return true;

            //if ((int)hpdv[idx]["spread"] >= 50)
            //    return true;
            //if (Math.Abs((double)hpdv[idx]["close"] - (double)hpdv[idx-1]["close"]) > 0.0050)
            //    return true;

            if (instance != null)
            {
                if (instance.value(2) > 60)
                    return true;
                if (Math.Abs((double)instance.value(3) - (double)preInstance.value(3)) > 0.0050)
                    return true;
            }
            else
            {
                var dt = Feng.Data.DbHelper.Instance.ExecuteDataTable(string.Format("SELECT TOP 2 * FROM {0}_{1} WHERE TIME <= {2} ORDER BY TIME DESC",
                TestParameters2.CandidateParameter.MainSymbol, TestParameters2.CandidateParameter.MainPeriod,
                WekaUtils.GetTimeFromDate(nowDate)));

                if ((int)dt.Rows[0]["spread"] > 50)
                    return true;

                if ((double)dt.Rows[0]["close"] - (double)dt.Rows[1]["close"] > 0.0050)// && selectedDeal == 0)
                    return true;
                if ((double)dt.Rows[0]["close"] - (double)dt.Rows[1]["close"] < -0.0050)// && selectedDeal == 1)
                    return true;

                //if ((double)dt.Rows[0]["MA_10"] - (double)dt.Rows[1]["MA_10"] < 0 && selectedDeal == 0)
                //    return true;
                //if ((double)dt.Rows[0]["MA_10"] - (double)dt.Rows[1]["MA_10"] > 0 && selectedDeal == 1)
                //    return true;

                //dt = Feng.Data.DbHelper.Instance.ExecuteDataTable(string.Format("SELECT TOP 1 * FROM {0}_{1} WHERE TIME = {2} ORDER BY TIME",
                //    TestParameters2.CandidateParameter.MainSymbol, TestParameters2.CandidateParameter.MainPeriod,
                //    WekaUtils.GetTimeFromDate(nowDate)));
                //if ((double)dt.Rows[0]["MACD_12_26_9_M"] >= 0 && selectedDeal == 1)
                //    return true;
                //if ((double)dt.Rows[0]["MACD_12_26_9_M"] <= 0 && selectedDeal == 0)
                //    return true;
                //if ((double)dt.Rows[0]["RSI_14"] >= 70 && selectedDeal == 0)
                //    return true;
                //if ((double)dt.Rows[0]["RSI_14"] <= 30 && selectedDeal == 1)
                //    return true;
            }
            return false;
        }
Пример #5
0
 //private weka.core.Instances m_instances;
 public void evaluateModel(double[] v, weka.core.Instances instances)
 {
     double[] c = new double[instances.numInstances()];
     for (int i = 0; i < c.Length; ++i)
     {
         c[i] = instances.instance(i).classValue();
     }
     evaluateModel(v, c);
 }
Пример #6
0
 public double GetVolume(weka.core.Instance instance)
 {
     //double v = m_counts[2] * m_tp - m_counts[0] * m_sl + (m_tp - 1.5 * m_sl) * m_counts[1] / 5;
     //if (v < 0)
     //    return 0;
     //v /= 1000;
     double sum = (m_counts[0] + m_counts[1] + m_counts[2]);
     if (sum == 0)
         return 0;
     double v = m_counts[2] / sum;
     return Math.Round(v, 2);
 }
Пример #7
0
        public void Test(weka.core.Instances testInstances = null)
        {
            if (testInstances == null)
            {
                testInstances = CreateCurrentInstances();
            }

            //if (m_cls is MLEA.IBatchClassifier)
            //{
            //    StringBuilder sb = new StringBuilder();
            //    MLEA.IBatchClassifier batchClassifier = m_cls as MLEA.IBatchClassifier;
            //    double[] d = batchClassifier.classifyInstances(testInstances);
            //    for (int i = 0; i < d.Length; ++i)
            //    {
            //        string s = string.Format("{0}, {1}: {2}", testInstances.instance(i).value(0).ToString("N2"), testInstances.instance(i).value(1).ToString("N2"), d[i].ToString("N0"));
            //        sb.AppendLine(s);
            //    }

            //    this.Invoke(new Action(() =>
            //    {
            //        txtEval.Text = sb.ToString();
            //    }));
            //}
            //else
            {
                MLEA.MyEvaluation eval = null;
                if (testInstances.classAttribute().isNominal())
                {
                    var costMatrix = MLEA.TestParameters.CostMatrix;
                    eval = new MLEA.MyEvaluation(costMatrix);

                    eval.evaluateModel(m_cls, testInstances);

                    this.Invoke(new Action(() =>
                    {
                        txtEval.Text = string.Format("TP:{0}, FP:{1}, Cost:{2}", eval.numTruePositives(1), eval.numFalsePositives(1), eval.totalCost().ToString());
                    }));
                }
                else
                {
                    //eval = new MLEA.MyEvaluation(costMatrix);

                    //eval.evaluateModel(m_cls, testInstances);

                    //this.Invoke(new Action(() =>
                    //{
                    //    txtEval.Text = eval.toSummaryString().Replace("\n", System.Environment.NewLine)
                    //        + System.Environment.NewLine;
                    //}));
                }
            }
        }
Пример #8
0
 protected override string instanceToLibsvm(weka.core.Instance inst)
 {
     //StringBuffer.__<clinit>();
     //StringBuffer buffer = new StringBuffer(new StringBuilder().append("").append(inst.classValue()).toString());
     StringBuffer buffer = new StringBuffer(new StringBuilder().append("").append(inst.classValue() - 1).toString());
     for (int i = 0; i < inst.numAttributes(); i++)
     {
         if ((i != inst.classIndex()) && (inst.value(i) != 0f))
         {
             buffer.append(new StringBuilder().append(" ").append((int)(i + 1)).append(":").append(inst.value(i)).toString());
         }
     }
     return buffer.toString();
 }
Пример #9
0
        public double GetVolume(weka.core.Instance instance)
        {
            double sum = m_counts[0] + m_counts[1] + m_counts[2];
            if (sum == 0)
                return 0;

            double kelly_b = -m_tp / m_sl;
            double kelly_p = (double)m_counts[2] / sum;
            double kelly_f = (kelly_p * (kelly_b + 1) - 1) / kelly_b;
            // f=0.2, sl=20 =>0.01
            double v = 2 * (kelly_f / 0.2) / (m_sl / 10);
            if (v <= 0)
                return 0;
            return v;
        }
Пример #10
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]++;
            }
        }
Пример #11
0
        private Type GetEnumType(weka.core.Attribute attribute)
        {
            var attributeName = attribute.name();
            var enumType =_builder.Assembly.GetType(attributeName);
            if (enumType != null) {
                return enumType;
            }

            // Define a public enumeration with the name "MyEnum" and an underlying type of Integer.
            EnumBuilder myEnum = _builder.DefineEnum (attributeName,
                                     TypeAttributes.Public, typeof(int));

            for (int i=0; i<attribute.numValues(); ++i) {
                var name = attribute.value(i);
                myEnum.DefineLiteral (name, i);
            }

            // Create the enum
            return myEnum.CreateType ();
        }
        public weka.core.Instance GenerateFeatures(PokerHand hand, int rIdx, int aIdx, weka.core.Instances data, bool generateClass = true)
        {
            // Check that we are using limit betting.
            Debug.Assert(hand.Context.BettingType == BettingType.FixedLimit);

            var results = new weka.core.Instance(data.numAttributes());
            results.setDataset(data);
            int attIdx = 0;
            foreach (var method in typeof(LimitFeatureGenerator).GetMethods())
            {
                // Get all the features of this class.
                var attributes = method.GetCustomAttributes(typeof(Feature), true);
                if (attributes.Length == 0)
                    continue;

                // Get the feature attribute on this method.
                var attr = ((Feature)attributes[0]);

                // Get the name for this attribute
                string name = attr.Name;
                //Console.WriteLine("Hand: {0} Feature: {1}", hand.Context.ID, name);

                // Get the feature only if it's applicable to this situation.
                object feature = null;
                if(rIdx >= (int)attr.MinRound && rIdx <= (int)attr.MaxRound)
                    feature = method.Invoke(this, new object[] { hand, rIdx, aIdx });

                if (SkipMissingFeatures && (feature == null || feature.ToString() == "?"))
                    continue;

                switch (attr.FType) {
                case FeatureType.Continuous:
                    results.setValue(attIdx, (double)feature);
                    break;
                case FeatureType.Discrete:
                    results.setValue(attIdx, (int)feature);
                    break;
                case FeatureType.Boolean:
                case FeatureType.Nominal:
                case FeatureType.Enum:
                {
                    var attribute = data.attribute(attIdx);
                    var attVal = attribute.indexOfValue(feature.ToString());
                    if(attVal < 0 || attVal > attribute.numValues())
                        throw new Exception(string.Format ("Invalid attribute value: {0} for attribute {1} (idx: {2} total values: {3}", feature.ToString(), name, attVal, attribute.numValues()));
                    results.setValue(attribute, attVal);
                }
                    break;
                case FeatureType.String:
                {
                    var attribute = data.attribute(attIdx);
                    results.setValue(attribute, feature.ToString());
                }
                    break;
                default: throw new Exception("Unspecified feature type for feature: " + method.Name);
                }

                attIdx++;
            }

            if(generateClass)
            {
                var classAttr = data.classAttribute();
                switch (hand.Rounds[rIdx].Actions[aIdx].Type)
                {
                case ActionType.Bet:
                case ActionType.Raise: results.setClassValue(classAttr.indexOfValue("Raise"));
                    break;
                case ActionType.Call:
                case ActionType.Check: results.setClassValue(classAttr.indexOfValue("Call"));
                    break;
                case ActionType.Fold: results.setClassValue(classAttr.indexOfValue("Fold"));;
                    break;
                default:
                    break;
                }
            }

            return results;
        }
Пример #13
0
        public override double classifyInstance(weka.core.Instance instance)
        {
            if (m_instances.numInstances() == 0)
                return 2;

            if (m_instances.numAttributes() != instance.numAttributes())
            {
                throw new AssertException("different attribute.");
            }
            int n = instance.numAttributes();
            List<Tuple<int, int>> dist = new List<Tuple<int, int>>();
            for (int i = 0; i < m_instances.numInstances(); ++i)
            {
                int d1 = 0, d2 = 0;
                weka.core.Instance instanceI = m_instances.instance(i);
                for (int j = 0; j < n; ++j)
                {
                    //d += (int)((instanceI.value(j) - instance.value(j)) * (instanceI.value(j) - instance.value(j)));
                    if (instanceI.value(j) != instance.value(j))
                    {
                        d1++;
                    }
                    if (instance.value(j) != 0)
                    {
                        d2++;
                    }
                }
                int c = (int)instanceI.classValue();

                dist.Add(new Tuple<int, int>(d1, c));
            }
            if (dist.Count == 0)
                return 2;

            dist.Sort(new Comparison<Tuple<int, int>>((x, y) =>
            {
                return x.Item1.CompareTo(y.Item1);
            }));

            int sum = 0, count = 0;
            for (int i = 0; i < dist.Count; ++i)
            {
                if (dist[i].Item1 < 4)
                {
                    sum += dist[i].Item2;
                    count++;
                }
                else
                    break;
            }
            if (count == 0)
                return 2;
            if (count < m_instances.numInstances() / 70)
                return 2;
            return (int)Math.Round((double)sum / count);
        }
        private int[] ReduceByOneR(weka.core.Instances insts)
        {
            List<int> Rank = new List<int>();
            List<cScoreAndClass> ListScoreAndClass = new List<cScoreAndClass>();

            weka.attributeSelection.OneRAttributeEval Attrib = new OneRAttributeEval();
            Attrib.buildEvaluator(insts);
            richTextBoxSupervisedDimRec.AppendText("\n\nScores:");
            int realIdx = 0;
            for (int i = 0; i < CompleteScreening.ListDescriptors.Count; i++)
            {
                if (CompleteScreening.ListDescriptors[i].IsActive() == false) continue;
                double TmpScore = Attrib.evaluateAttribute(realIdx++);
                richTextBoxSupervisedDimRec.AppendText("\n " + CompleteScreening.ListDescriptors[i].GetName() + "\t: " + TmpScore);
                ListScoreAndClass.Add(new cScoreAndClass(realIdx-1, TmpScore));
            }

            ListScoreAndClass.Sort(delegate(cScoreAndClass p1, cScoreAndClass p2) { return p1.Score.CompareTo(p2.Score); });
            for(int i=ListScoreAndClass.Count-1;i>=0;i--)
            {
                Rank.Add(ListScoreAndClass[i].Class);
            }
            return Rank.ToArray();
        }
        private int[] ReduceByPCA(weka.core.Instances insts)
        {
            int[] rang = null;
            PrincipalComponents filter = new PrincipalComponents();  // package weka.filters.supervised.attribute!
            filter.setTransformBackToOriginal(true);
            filter.buildEvaluator(insts);

            Ranker search2 = new Ranker();
             //   search2.setNumToSelect(2);
            search2.setGenerateRanking(true);
            rang = search2.search(filter, insts);

            return rang;
        }
Пример #16
0
 public void Build(weka.core.Instances instances)
 {
 }
        private int[] ReduceByInfoGain(weka.core.Instances insts)
        {
            weka.attributeSelection.InfoGainAttributeEval AttribEvaluator = new weka.attributeSelection.InfoGainAttributeEval();
            AttribEvaluator.buildEvaluator(insts);
            Ranker search2 = new Ranker();
            int[] rang = search2.search(AttribEvaluator, insts);

            return rang;
        }
Пример #18
0
        public Enum GetEnumValue(weka.core.Attribute attribute,
			                       int enumValue)
        {
            var enumType = GetEnumType(attribute);
            return (Enum)Enum.ToObject(enumType, enumValue);
        }
Пример #19
0
 public double GetVolume(weka.core.Instance instance)
 {
     return m_volume;
 }
Пример #20
0
 public override void buildClassifier(weka.core.Instances insts)
 {
     m_instances = insts;
 }
Пример #21
0
 public static void GenerateHpDateSpan(weka.core.Instances allInstances)
 {
     using (StreamWriter sw = new StreamWriter("d:\\a.txt"))
     {
         for (int i = 0; i < allInstances.numInstances(); ++i)
         {
             DateTime nowDate = WekaUtils.GetDateValueFromInstances(allInstances, 0, i);
             DateTime nowHpDate = WekaUtils.GetDateValueFromInstances(allInstances, 1, i);
             sw.WriteLine((nowHpDate - nowDate).TotalMinutes);
         }
     }
 }
Пример #22
0
        public static string IncrementTest(weka.core.Instances allInstancesWithDate,
            Func<weka.classifiers.Classifier> clsCreator, string removeAttributes, string resultFile, int step)
        {
            //if (!(TestParameters2.UsePartialHpDataM1 || TestParameters2.UsePartialHpData))
            //{
            //    HpData.Instance.Clear();
            //}

            int trainMinutes = TestParameters2.MinTrainPeriod * WekaUtils.GetMinuteofPeriod(TestParameters2.CandidateParameter.MainPeriod);

            string ret = string.Empty;

            string sampleFile = null; // resultFile.Replace("Increment", "sample");
            bool useInstanceWeight = false;
            bool enablePerHour = false;
            bool enableDiffClass = false;
            int sameClassCount = -1;// TestParameters2.MaxTrainSize / 3; // allInstances.numClasses();
            bool enableDiffHpTime = false;
            bool enableRemoveLittle = false;
            bool enableRemoveLargeThanMid = true;
            bool enableFilter = false;

            if (!TestParameters2.RealTimeMode && File.Exists(resultFile))
                File.Delete(resultFile);

            weka.core.Instances allInstances;
            var filter = new weka.filters.MultiFilter();
            //filter.setOptions(weka.core.Utils.splitOptions("-F \"weka.filters.unsupervised.attribute.Remove -R 1,4\" -F \"weka.filters.unsupervised.attribute.Discretize -B 10 -M -1.0 -R first-last\""));
            filter.setOptions(weka.core.Utils.splitOptions(string.Format("-F \"weka.filters.unsupervised.attribute.Remove -R {0} \"", removeAttributes)));
            filter.setInputFormat(allInstancesWithDate);
            allInstances = weka.filters.Filter.useFilter(allInstancesWithDate, filter);

            long[] jHpTimes = new long[allInstancesWithDate.numInstances()];
            DateTime[] jDates = new DateTime[allInstancesWithDate.numInstances()];
            DateTime[] jHpDates = new DateTime[allInstancesWithDate.numInstances()];
            int[] jHps = new int[allInstancesWithDate.numInstances()];

            for (int j = 0; j < jDates.Length; ++j)
            {
                jDates[j] = WekaUtils.GetDateValueFromInstances(allInstancesWithDate, 0, j);
                jHpTimes[j] = WekaUtils.GetTimeValueFromInstances(allInstancesWithDate, 1, j);
                jHpDates[j] = WekaUtils.GetDateFromTime(jHpTimes[j]);
                jHps[j] = (int)allInstances.instance(j).classValue();
            }

            #region "action"
            Func<Tuple<int, int>, Tuple<weka.core.Instances, weka.core.Instances, long, int, int>> action = (tuple) =>
            {
                var ai = tuple.Item1;

                DateTime nowDate = WekaUtils.GetDateValueFromInstances(allInstancesWithDate, 0, ai);
                if (nowDate < TestParameters2.TrainStartTime || nowDate > TestParameters2.TrainEndTime)
                    return null;

                DateTime nowHpDate = WekaUtils.GetDateValueFromInstances(allInstancesWithDate, 1, ai);
                double nowClass = allInstancesWithDate.instance(ai).classValue();
                double preJClass = -1;

                List<weka.core.Instance> listTrainInstances = new List<weka.core.Instance>(ai / 2);
                List<weka.core.Instance> listTrainInstancesWithDate = new List<weka.core.Instance>(ai / 2);

                int[] counts = new int[allInstancesWithDate.numClasses()];

                long maxjHpTime = -1;
                int maxjHpTimeHp = 2;
                int maxjHpTimeCount = 0;
                bool enoughTrainMinutes = false;
                DateTime firstDate = nowDate.AddMinutes(-trainMinutes);
                int classIdxWithDate = allInstancesWithDate.classIndex();
                int classIdx = allInstances.classIndex();
                for (int j = ai - 1; j >= 0; --j)
                {
                    long jHpTime = jHpTimes[j]; // WekaUtils.GetTimeValueFromInstances(allInstancesWithDate, 1, j);
                    DateTime jHpDate = jHpDates[j];// WekaUtils.GetDateFromTime(jHpTime);
                    DateTime jDate = jDates[j];// WekaUtils.GetDateValueFromInstances(allInstancesWithDate, 0, j);
                    int jHp = jHps[j]; // (int)allInstancesWithDate.instance(j).value(classIdxWithDate);

                    if (enablePerHour)
                    {
                        if (nowDate.Hour != jDate.Hour)
                            continue;
                    }

                    weka.core.Instance instInsert = null;
                    weka.core.Instance instInsertWithDate = null;
                    if (jHpDate <= nowDate)
                    {
                        if (jHpTime > maxjHpTime)
                        {
                            maxjHpTime = jHpTime;
                            maxjHpTimeHp = jHp;
                            maxjHpTimeCount = 1;
                        }
                        else if (jHpTime == maxjHpTime)
                        {
                            maxjHpTimeCount++;
                        }

                        instInsert = new weka.core.DenseInstance(allInstances.instance(j));
                        //instInsert.setDataset(trainInstances);

                        instInsertWithDate = new weka.core.DenseInstance(allInstancesWithDate.instance(j));
                        //instInsertWithDate.setDataset(trainInstancesWithDate);
                    }
                    else
                    {
                        if (TestParameters2.UsePartialHpDataM1 || TestParameters2.UsePartialHpData)
                        {
                            Tuple<int, long> hp = null;
                            if (TestParameters2.UsePartialHpDataM1)
                            {
                                hp = HpData.Instance.GetHpSumByM1(TestParameters2.CandidateParameter.MainSymbol, TestParameters2.CandidateParameter.MainPeriod,
                                    WekaUtils.GetTimeFromDate(nowDate), WekaUtils.GetTimeFromDate(jDate));
                                if (hp.Item2 == 0)
                                    hp = null;
                            }
                            else if (TestParameters2.UsePartialHpData)
                            {
                                var hps = HpData.Instance.GetHpSum(TestParameters2.CandidateParameter.MainSymbol, TestParameters2.CandidateParameter.MainPeriod,
                                    WekaUtils.GetTimeFromDate(nowDate), WekaUtils.GetTimeFromDate(jDate));

                                if (hps.ContainsKey(jDate))
                                {
                                    hp = hps[jDate];
                                }
                            }

                            if (hp != null)
                            {
                                if (WekaUtils.GetDateFromTime(hp.Item2) > nowDate)
                                {
                                    throw new AssertException("hpdate should less than now");
                                }
                                jHp = hp.Item1;
                                jHpTime = hp.Item2;

                                if (jHpTime > maxjHpTime)
                                {
                                    maxjHpTime = jHpTime;
                                    maxjHpTimeHp = jHp;
                                    maxjHpTimeCount = 0;
                                }
                                else if (jHpTime == maxjHpTime)
                                {
                                    maxjHpTimeCount++;
                                }

                                instInsert = new weka.core.DenseInstance(allInstances.instance(j));
                                //instInsert.setDataset(trainInstances);
                                //instInsert.setClassValue(jHp);
                                instInsert.setValue(classIdx, jHp);

                                instInsertWithDate = new weka.core.DenseInstance(allInstancesWithDate.instance(j));
                                //instInsertWithDate.setDataset(trainInstancesWithDate);
                                //instInsertWithDate.setClassValue(jHp);
                                instInsertWithDate.setValue(classIdxWithDate, jHp);
                                instInsertWithDate.setValue(1, jHpTime * 1000);
                            }
                        }

                    }
                    if (instInsert == null)
                        continue;

                    double jClass = jHp;
                    if (enableDiffClass && jClass == preJClass)
                        continue;
                    if (sameClassCount > 0)
                    {
                        if (counts[(int)jClass] >= sameClassCount)
                            continue;
                        counts[(int)jClass]++;
                    }
                    if (enableFilter && j > 0 && Filter(jDate, allInstancesWithDate.instance(j), allInstancesWithDate.instance(j - 1)))
                        continue;
                    if (useInstanceWeight)
                        instInsert.setWeight((nowDate - jDate).TotalMinutes);

                    listTrainInstances.Add(instInsert);
                    listTrainInstancesWithDate.Add(instInsertWithDate);

                    preJClass = jClass;

                    if (jDate <= firstDate)
                    {
                        enoughTrainMinutes = true;
                        break;
                    }
                }

                //weka.core.Instances trainInstances2 = new weka.core.Instances(allInstancesNoDate, 0);
                //for (int x = trainInstances.numInstances() - 1; x >= 0; --x)
                //{
                //    weka.core.Instance inst = new weka.core.DenseInstance(trainInstances.instance(x));
                //    trainInstances2.add(inst);
                //}
                //WekaUtils.SaveInstances(trainInstances2, "d:\\a.arff");

                //if (trainInstances.numInstances() >= trainLength)
                //    break;

                if (!enoughTrainMinutes)
                {
                    Console.WriteLine(string.Format("{0}, not enough trainMinutes",
                        nowDate.ToString(Parameters.DateTimeFormat)));
                    return null;
                }
                if (listTrainInstances.Count < TestParameters2.MinTrainSize)
                {
                    Console.WriteLine(string.Format("{0}, numInstances {1} < minTrainSize {2}",
                        nowDate.ToString(Parameters.DateTimeFormat), listTrainInstances.Count, TestParameters2.MinTrainSize));
                    return null;
                }
                //else if (listTrainInstances.Count == 1)
                //{
                //    lock (WekaUtils.Instance)
                //    {
                //        WekaUtils.Instance.WriteLog("trainInstances.numInstances() == 1, nowDate = " + nowDate.ToString());
                //        if (!System.IO.File.Exists("d:\\a.arff"))
                //        {
                //            WekaUtils.SaveInstances(trainInstances, "d:\\a.arff");
                //        }
                //    }
                //}

                weka.core.Instances trainInstances = new weka.core.Instances(allInstances, listTrainInstances.Count);
                weka.core.Instances trainInstancesWithDate = new weka.core.Instances(allInstancesWithDate, listTrainInstancesWithDate.Count);
                WekaUtils.AddInstanceQuickly(trainInstances, listTrainInstances);
                WekaUtils.AddInstanceQuickly(trainInstancesWithDate, listTrainInstancesWithDate);

                if (enableRemoveLittle)
                {
                    double preClass = 2;
                    for (int ii = 0; ii < trainInstances.numInstances(); ++ii)
                    {
                        var iiClass = trainInstances.instance(ii).classValue();
                        if (iiClass == 2)
                            continue;

                        int jj = ii + 1;
                        while (jj < trainInstances.numInstances())
                        {
                            if (trainInstances.instance(jj).classValue() == iiClass)
                                jj++;
                            else
                                break;
                        }
                        int count = jj - ii;
                        if (count < 5)
                        {
                            for (jj = 0; jj < count; ++jj)
                            {
                                trainInstances.instance(ii + jj).setClassValue(preClass);
                            }
                        }
                        else
                        {
                            preClass = iiClass;
                            ii += count;
                        }
                    }
                }

                if (enableDiffHpTime)
                {
                    Dictionary<long, int> jDictHpTimes = new Dictionary<long, int>();

                    int n = trainInstances.numInstances();
                    List<weka.core.Instance> list1 = new List<weka.core.Instance>(n);
                    List<weka.core.Instance> list2 = new List<weka.core.Instance>(n);

                    //java.util.LinkedList deleteList = new java.util.LinkedList();
                    for (int j = 0; j < n; ++j)
                    {
                        long jHpTime = WekaUtils.GetTimeValueFromInstances(trainInstancesWithDate, 1, j);
                        if (jDictHpTimes.ContainsKey(jHpTime))
                        {
                            continue;
                        }
                        else
                        {
                            jDictHpTimes[jHpTime] = list1.Count;
                            list1.Add(trainInstances.instance(j));
                            list2.Add(trainInstancesWithDate.instance(j));
                        }
                    }
                    weka.core.Instances newTrainInstances = new weka.core.Instances(trainInstances, list1.Count);
                    weka.core.Instances newTrainInstancesWithDate = new weka.core.Instances(trainInstancesWithDate, list2.Count);
                    WekaUtils.AddInstanceQuickly(newTrainInstances, list1);
                    WekaUtils.AddInstanceQuickly(newTrainInstancesWithDate, list2);

                    trainInstances = newTrainInstances;
                    trainInstancesWithDate = newTrainInstancesWithDate;
                }

                if (enableRemoveLargeThanMid)
                {
                    int n = trainInstances.numInstances();
                    long[] lastTimes = new long[n];
                    for (int j = 0; j < n; ++j)
                    {
                        long openTime = WekaUtils.GetTimeValueFromInstances(trainInstancesWithDate, 0, j);
                        long closeTime = WekaUtils.GetTimeValueFromInstances(trainInstancesWithDate, 1, j);

                        lastTimes[j] = (long)(closeTime - openTime);
                    }
                    Array.Sort(lastTimes);
                    long midLastTime = lastTimes[lastTimes.Count() / 2];

                    List<weka.core.Instance> list1 = new List<weka.core.Instance>(n);
                    List<weka.core.Instance> list2 = new List<weka.core.Instance>(n);
                    //java.util.LinkedList deleteList = new java.util.LinkedList();
                    for (int j = 0; j < n; ++j)
                    {
                        if (lastTimes[j] > midLastTime)
                        {
                            //deleteList.add(trainInstances.instance(j));
                        }
                        else
                        {
                            list1.Add(trainInstances.instance(j));
                            list2.Add(trainInstancesWithDate.instance(j));
                        }
                    }
                    weka.core.Instances newTrainInstances = new weka.core.Instances(trainInstances, list1.Count);
                    weka.core.Instances newTrainInstancesWithDate = new weka.core.Instances(trainInstancesWithDate, list2.Count);
                    WekaUtils.AddInstanceQuickly(newTrainInstances, list1);
                    WekaUtils.AddInstanceQuickly(newTrainInstancesWithDate, list2);

                    trainInstances = newTrainInstances;
                    trainInstancesWithDate = newTrainInstancesWithDate;
                    //trainInstances.removeAll(deleteList);
                }

                if (!string.IsNullOrEmpty(sampleFile))
                {
                    lock (sampleFile)
                    {
                        if (!System.IO.File.Exists(sampleFile))
                        {
                            WekaUtils.SaveInstances(trainInstancesWithDate, sampleFile);
                        }
                    }
                }

                //using (StreamWriter sw = new StreamWriter("d:\\p.txt", true))
                //{
                //    sw.Write("{0},{1},", nowDate.ToString(Parameters.DateTimeFormat), nowHpDate.ToString(Parameters.DateTimeFormat));
                //}

                return new Tuple<weka.core.Instances, weka.core.Instances, long, int, int>(trainInstances, trainInstancesWithDate, maxjHpTime, maxjHpTimeHp, maxjHpTimeCount);
            };

            #endregion

            //allInstancesNoDate = allInstances;
            if (!TestParameters2.RealTimeMode)
            {
                int tpb = 0, fpb = 0, tps = 0, fps = 0;
                int db = 0, ds = 0, dn = 0;

                int parallelStep = 1;
                if (TestParameters.EnableMultiThread)
                {
                    parallelStep = 100;
                }
                int startIdx = TestParameters2.MinTrainPeriod * 2 / 3;
                for (int i0 = startIdx; i0 < allInstancesWithDate.numInstances() - step; i0 += step * parallelStep)
                {
                    List<Tuple<int, int>> toTest = new List<Tuple<int, int>>();
                    for (int i = i0; i < Math.Min(i0 + step * parallelStep, allInstancesWithDate.numInstances() - step); i += step)
                    {
                        toTest.Add(new Tuple<int, int>(i, toTest.Count));
                    }

                    var toRet0 = new Tuple<weka.core.Instances, weka.core.Instances, long, int, int>[toTest.Count];
                    if (TestParameters.EnableMultiThread)
                    {
                        Parallel.ForEach(toTest, (tuple) =>
                            {
                                int i = tuple.Item1 - i0;
                                toRet0[i] = action(tuple);
                            });
                    }
                    else
                    {
                        for (int i = 0; i < toTest.Count; ++i)
                        {
                            var r = action(toTest[i]);
                            toRet0[i] = r;
                        }
                    }

                    object[, ,] toRet = new object[toTest.Count, step, 9];
                    bool[] toRet1 = new bool[toTest.Count];
                    for (int i = 0; i < toTest.Count; ++i)
                    {
                        for (int j = 0; j < step; ++j)
                            toRet[i, j, 0] = 2;

                        if (toRet0[i] != null)
                        {
                            var maxjHpTime = toRet0[i].Item3;
                            var maxjHpTimeHp = toRet0[i].Item4;
                            var maxjHpTimeCount = toRet0[i].Item5;

                            toRet1[i] = IncrementTestTrainCheck(resultFile, maxjHpTime, maxjHpTimeHp, maxjHpTimeCount, toTest[i]);
                        }
                    }

                    if (TestParameters.EnableMultiThread)
                    {
                        Parallel.ForEach(toTest, (tuple) =>
                            {
                                int i = tuple.Item1 - i0;

                                if (toRet0[i] != null)
                                {
                                    var trainInstances = toRet0[i].Item1;
                                    var trainInstancesWithDate = toRet0[i].Item2;
                                    var maxjHpTime = toRet0[i].Item3;
                                    var maxjHpTimeHp = toRet0[i].Item4;
                                    var r = IncrementTestTrain(toRet1[i], clsCreator, resultFile, step, trainInstances, trainInstancesWithDate, allInstances, allInstancesWithDate,
                                        maxjHpTime, maxjHpTimeHp, toTest[i]);

                                    if (r != null)
                                    {
                                        for (int j = 0; j < toRet.GetLength(1); ++j)
                                            for (int k = 0; k < toRet.GetLength(2); ++k)
                                                toRet[i, j, k] = r[j, k];
                                    }
                                }
                            });
                    }
                    else
                    {
                        for (int i = 0; i < toTest.Count; ++i)
                        {
                            if (toRet0[i] != null)
                            {
                                var trainInstances = toRet0[i].Item1;
                                var trainInstancesWithDate = toRet0[i].Item2;
                                var maxjHpTime = toRet0[i].Item3;
                                var maxjHpTimeHp = toRet0[i].Item4;
                                var r = IncrementTestTrain(toRet1[i], clsCreator, resultFile, step, trainInstances, trainInstancesWithDate, allInstances, allInstancesWithDate,
                                    maxjHpTime, maxjHpTimeHp, toTest[i]);

                                if (r != null)
                                {
                                    for (int j = 0; j < toRet.GetLength(1); ++j)
                                        for (int k = 0; k < toRet.GetLength(2); ++k)
                                            toRet[i, j, k] = r[j, k];
                                }
                            }
                        }
                    }

                    for (int i = 0; i < toTest.Count; ++i)
                    {
                        //action(toTest[i]);
                        for (int j = 0; j < step; ++j)
                        {
                            int d = (int)toRet[i, j, 0];
                            if (d != 0 && d != 1 && d != 2)
                                throw new AssertException("d should be -1, 0, 1 or 2. but it's" + d.ToString());

                            if (toRet[i, j, 6] == null)
                            {
                                //Console.WriteLine("toRet[i, j, 6] == null");
                                continue;
                            }
                            long maxJHpTime = (long)toRet[i, j, 6];
                            //if (lastMaxJHpTime >= (long)toRet[i, j, 6])
                            //    continue;
                            //lastMaxJHpTime = (long)toRet[i, j, 6];

                            if (d == 0)
                                db++;
                            else if (d == 1)
                                ds++;
                            else
                                dn++;

                            int v = (int)toRet[i, j, 1];

                            if (d != 2)
                            {
                                if (v == 3 || d == v)
                                {
                                    if (d == 0)
                                    {
                                        tpb++;
                                    }
                                    else if (d == 1)
                                    {
                                        tps++;
                                    }
                                }
                                else
                                {
                                    if (d == 0)
                                    {
                                        fpb++;
                                    }
                                    else if (d == 1)
                                    {
                                        fps++;
                                    }
                                }
                            }

                            DateTime nowDate = WekaUtils.GetDateFromTime((long)toRet[i, j, 3]);
                            DateTime nextDate = WekaUtils.GetDateFromTime((long)toRet[i, j, 2]);

                            //nextDate = WekaUtils.GetDateFromTime(maxJHpTime);

                            //if (j == 0)
                            {
                                if (tpb + fpb + tps + fps != 0)
                                {
                                    ret = (string.Format("{0}, tn={1},tc={2}, db={3},ds={4},dn={5}, tpb={6},fpb={7},tps={8},fps={9},p={10}",
                                            nextDate.ToString(Parameters.DateTimeFormat), toRet[i, j, 4], toRet[i, j, 5],
                                            db, ds, dn,
                                            tpb, fpb, tps, fps, ((double)(tpb+tps) / (tpb + fpb + tps + fps)).ToString("F2")));
                                }
                                else
                                {
                                    ret = (string.Format("{0}, tn={1},tc={2}, db={3},ds={4},dn={5}, tpb={6},fpb={7},tps={8},fps={9},p={10}",
                                            nextDate.ToString(Parameters.DateTimeFormat), toRet[i, j, 4], toRet[i, j, 5],
                                            db, ds, dn,
                                            tpb, fpb, tps, fps, 0));
                                }
                                WekaUtils.Instance.WriteLog(ret);
                            }

                            //if (d != 2)
                            {
                                using (StreamWriter sw = new StreamWriter(resultFile, true))
                                {
                                    sw.WriteLine(string.Format("{1}, {2}, {6}, {3}, {4}, {5}", nowDate,
                                        nextDate.ToString(Parameters.DateTimeFormat),
                                        d, v, (double)toRet[i, j, 7],
                                        WekaUtils.GetDateFromTime(maxJHpTime).ToString(Parameters.DateTimeFormat),
                                        toRet[i, j, 8]));
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                for (int i0 = allInstancesWithDate.numInstances() - 1; i0 < allInstancesWithDate.numInstances() && i0 >= 0; i0++)
                {
                    List<Tuple<int, int>> toTest = new List<Tuple<int, int>>();
                    toTest.Add(new Tuple<int, int>(i0, toTest.Count));

                    var toRet0 = new Tuple<weka.core.Instances, weka.core.Instances, long, int, int>[toTest.Count];

                    for (int i = 0; i < toTest.Count; ++i)
                    {
                        var r = action(toTest[i]);
                        toRet0[i] = r;
                    }

                    object[, ,] toRet = new object[toTest.Count, step, 9];
                    for (int i = 0; i < toTest.Count; ++i)
                    {
                        for (int j = 0; j < toRet.GetLength(1); ++j)
                            toRet[i, j, 0] = 2;

                        if (toRet0[i] != null)
                        {
                            var trainInstances = toRet0[i].Item1;
                            var trainInstancesWithDate = toRet0[i].Item2;
                            var maxjHpTime = toRet0[i].Item3;
                            var maxjHpTimeHp = toRet0[i].Item4;
                            var maxjHpTimeCount = toRet0[i].Item5;

                            bool b = IncrementTestTrainCheck(resultFile, maxjHpTime, maxjHpTimeHp, maxjHpTimeCount, toTest[i]);
                            var r = IncrementTestTrain(b, clsCreator, resultFile, step, trainInstances, trainInstancesWithDate, allInstances, allInstancesWithDate,
                                maxjHpTime, maxjHpTimeHp, toTest[i]);

                            if (r != null)
                            {
                                for (int j = 0; j < toRet.GetLength(1); ++j)
                                    for (int k = 0; k < toRet.GetLength(2); ++k)
                                        toRet[i, j, k] = r[j, k];
                            }
                        }
                    }

                    for (int j = 0; j < step; ++j)
                    {
                        int d = (int)toRet[0, j, 0];
                        if (d != 0 && d != 1 && d != 2)
                            throw new AssertException("d should be -1, 0, 1 or 2.");

                        if (toRet[0, j, 6] == null)
                            continue;
                        long maxJHpTime = (long)toRet[0, j, 6];
                        //if (lastMaxJHpTime >= (long)toRet[i, j, 6])
                        //    continue;
                        //lastMaxJHpTime = (long)toRet[i, j, 6];

                        int v = (int)toRet[0, j, 1];

                        DateTime nowDate = WekaUtils.GetDateFromTime((long)toRet[0, j, 3]);
                        DateTime nextDate = WekaUtils.GetDateFromTime((long)toRet[0, j, 2]);

                        //nextDate = WekaUtils.GetDateFromTime(maxJHpTime);
                        //if (d != 2)
                        {
                            using (StreamWriter sw = new StreamWriter(resultFile, true))
                            {
                                sw.WriteLine(string.Format("{1}, {2}, {6}, {3}, {4}, {5}", nowDate,
                                    nextDate.ToString(Parameters.DateTimeFormat),
                                    d, v, (double)toRet[0, j, 7],
                                    WekaUtils.GetDateFromTime(maxJHpTime).ToString(Parameters.DateTimeFormat),
                                    toRet[0, j, 8]));
                            }
                        }
                    }
                }
            }

            return ret;
        }
Пример #23
0
        private static object[,] IncrementTestTrain(bool newjHpTime, Func<weka.classifiers.Classifier> clsCreator, string resultFile, int step,
            weka.core.Instances trainInstances, weka.core.Instances trainInstancesWithDate,
            weka.core.Instances allInstances, weka.core.Instances allInstancesWithDate,
            long maxjHpTime, int maxjHpTimeHp,
            Tuple<int, int> tuple)
        {
            if (!newjHpTime)
                return null;

            int timeAfter = 0;

            object[,] toRet = new object[step, 9];
            for (int j = 0; j < step; ++j)
                toRet[j, 0] = 2;

            var cls = clsCreator();
            bool b = true;
            b = WekaUtils.TrainInstances(cls, trainInstances);
            if (!b)
            {
                WekaUtils.Instance.WriteLog(string.Format("TrainInstance error!"));
                return null;
            }

            long totolCost = 0;
            //var eval = WekaUtils.TestInstances(trainInstances, cls, null);
            //totolCost = (long)eval.totalCost();

            // get hp last time
            long[] maxLastTimes = new long[trainInstances.numClasses()];
            long[] minLastTimes = new long[trainInstances.numClasses()];
            for (int j = 0; j < trainInstances.numClasses(); ++j)
            {
                maxLastTimes[j] = 0;
                minLastTimes[j] = long.MaxValue;
            }
            for (int j = 0; j < trainInstances.numInstances(); ++j)
            {
                DateTime openTime = WekaUtils.GetDateValueFromInstances(trainInstancesWithDate, 0, j);
                DateTime closeTime = WekaUtils.GetDateValueFromInstances(trainInstancesWithDate, 1, j);

                int v = (int)trainInstances.instance(j).classValue();

                long lastTime = (long)(closeTime - openTime).TotalSeconds;
                if (lastTime > maxLastTimes[v])
                    maxLastTimes[v] = lastTime;
                if (lastTime < minLastTimes[v])
                    minLastTimes[v] = lastTime;
            }

            var ai = tuple.Item1;
            DateTime nowDate = WekaUtils.GetDateValueFromInstances(allInstancesWithDate, 0, ai);
            //var i2 = tuple.Item2;
            for (int j = 0; j < step; ++j)
            {
                int idx = ai + timeAfter + j;
                DateTime nextDate = WekaUtils.GetDateValueFromInstances(allInstancesWithDate, 0, idx);

                double d = 2;
                d = cls.classifyInstance(allInstances.instance(idx));
                double[] v = cls.distributionForInstance(allInstances.instance(idx));
                //if (d == 2)
                //{
                //    WekaUtils.Instance.WriteLog(string.Format("{0} classifyInstance result = 2", nowDate.ToString(Parameters.DateTimeFormat)));
                //    continue;
                //}

                //if ((d == 1 && maxjHpTimeHp == 1)
                //    || (d == 0 && maxjHpTimeHp == 0))
                //    d = 2;
                //if ((d == 1 && maxjHpTimeHp == 0)
                //    || (d == 0 && maxjHpTimeHp == 1))
                //    d = 2;

                toRet[j, 0] = (int)d;
                toRet[j, 1] = (int)allInstances.instance(idx).classValue();
                toRet[j, 2] = WekaUtils.GetTimeFromDate(nextDate);
                toRet[j, 3] = WekaUtils.GetTimeFromDate(nowDate);
                toRet[j, 4] = trainInstances.numInstances();
                toRet[j, 5] = totolCost;
                toRet[j, 6] = maxjHpTime;
                toRet[j, 7] = v[(int)d];
                if (d != 2 && (maxLastTimes[(int)d] == 0
                    || maxLastTimes[(int)d] == long.MaxValue))
                {
                    maxLastTimes[(int)d] = maxLastTimes[3];
                }
                toRet[j, 8] = maxLastTimes[(int)d];

                //using (StreamWriter sw = new StreamWriter("d:\\p.txt", true))
                //{
                //    sw.WriteLine(string.Format("{0}", (int)allInstancesNoDate.instance(idx).classValue()));
                //}
            }

            return toRet;
        }
Пример #24
0
        //public void SetTimeDays(DateTime trainStart, int trainDays, int testDays)
        //{
        //    SetTimeMinutesFromTrainStart(trainStart, trainDays * 60 * 24, testDays * 60 * 24);
        //}
        public void GenerateEaOrder(CandidateClassifier bestClassifierInfo, weka.core.Instances testInstances)
        {
            if (bestClassifierInfo == null)
                return;

            //int j = m_bestClassifierInfo.DealType == m_dealType[0] ? 0 : 1;

            string eaorderFileName = string.Format("{0}\\ea_order.txt", TestParameters.BaseDir);
            //Instances testInstances = bestClassifierInfo.CurrentTestInstances;

            //Instances testInstancesNew = null;
            //Instances testInstances = null;
            //if (m_saveDataFile)
            //{
            //    SetTraining(false);
            //    string testFileName = GetArffFileName(m_dealType[j], m_newFileAppend);
            //    WekaUtils.Instance.WriteLog("Load instance of " + testFileName);

            //    testInstancesNew = WekaUtils.LoadInstances(testFileName);

            //    testInstances = WekaUtils.LoadInstances(GetArffFileName(m_dealType[j]));
            //}
            //else
            //{
            //    testInstancesNew = m_testInstancesNew[j];
            //    testInstances = m_testInstances[j];
            //}

            System.Data.DataTable dt = null;
            if (!testInstances.attribute(0).isDate())
            {
                dt = Feng.Data.DbHelper.Instance.ExecuteDataTable(string.Format("SELECT Date FROM {0} WHERE Date >= '{1}' AND Date < '{2}' {4}",
                    "EURUSD" + "_" + "M1",
                    WekaData.m_testTimeStart.ToString(Parameters.DateTimeFormat), WekaData.m_testTimeEnd.ToString(Parameters.DateTimeFormat),
                    "ORDER BY Time"));

                if (dt.Rows.Count != testInstances.numInstances())
                {
                    WekaUtils.Instance.WriteLog("different count!");
                    return;
                }
            }
            //else
            //{
            //    if (testInstancesNew.numInstances() != testInstances.numInstances())
            //    {
            //        WekaUtils.Instance.WriteLog("different count!");
            //        return;
            //    }
            //}

            // todo: save info in file
            //for (int j = 0; j < m_dealType.Length; ++j)
            //{
            //    if (m_saveDataFile)
            //    {
            //        string modelFileName = GetModelFileName(m_dealType[j]);

            //        WriteLog("Load model of " + modelFileName);

            //        //ObjectInputStream ois = new ObjectInputStream(new FileInputStream(modelFileName));
            //        //Classifier cls = (Classifier)ois.readObject();
            //        //ois.close();
            //        m_bestClassifier[m_dealType[j]] = (Classifier)weka.core.SerializationHelper.read(modelFileName);
            //    }
            //}

            //foreach (string testFileName in System.IO.Directory.GetFiles(m_baseDir, NormalizeFileName(string.Format("{0}_{1}_{2}_*.arff", m_symbol, dt1.ToString(m_dateTimeFormat), dt2.ToString(m_dateTimeFormat)))))
            //{
            //    string[] ss2 = System.IO.Path.GetFileNameWithoutExtension(testFileName).Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            //    if (ss2.Length != 5)
            //        continue;

            //    try
            //    {
            //        m_trainTimeStart = ConvertFileNameToDateTime(ss2[1]);
            //        m_trainTimeEnd = ConvertFileNameToDateTime(ss2[2]);
            //        m_testTimeStart = ConvertFileNameToDateTime(ss2[3]);
            //        m_testTimeEnd = ConvertFileNameToDateTime(ss2[4]);
            //    }
            //    catch (Exception)
            //    {
            //        continue;
            //    }

            double[] tr = bestClassifierInfo.CurrentTestRet;
            double[] cv = bestClassifierInfo.CurrentClassValue;

            using (StreamWriter sw = new StreamWriter(eaorderFileName, true))
            {
                for (int i = 0; i < testInstances.numInstances(); i++)
                {
                    string clsLabels;
                    double v = tr[i];

                    if (v != double.NaN)
                    {
                        clsLabels = testInstances.classAttribute().value((int)v);// CalcAction(v);
                    }
                    else
                    {
                        clsLabels = "0";
                    }

                    string clsLabel;
                    if (clsLabels == "2")
                    {
                        clsLabel = bestClassifierInfo.DealType == 'B' ? "1" : "-1";
                    }
                    else
                    {
                        clsLabel = "0";
                    }
                    //if (clsLabels.Length >= 2)
                    //{
                    //    if (clsLabels[0] == "1" && clsLabels[1] == "0")
                    //        clsLabel = "1";
                    //    else if (clsLabels[0] == "0" && clsLabels[1] == "1")
                    //        clsLabel = "-1";
                    //    else if (clsLabels[0] == "1" && clsLabels[1] == "1")
                    //        clsLabel = "2";
                    //}
                    //else if (clsLabels.Length == 1)
                    //{
                    //    if (clsLabels[0] == "1")
                    //    {
                    //        if (m_dealType[0] == "B")
                    //            clsLabel = "1";
                    //        else if (m_dealType[0] == "S")
                    //            clsLabel = "-1";
                    //    }
                    //}
                    //else
                    //{
                    //    throw new AssertException("invalid clsLabels.Length");
                    //}
                    string date;
                    string dateClose = string.Empty;
                    if (testInstances.attribute(0).isDate())
                    {
                        date = WekaUtils.GetDateValueFromInstances(testInstances, 0, i).ToString(Parameters.DateTimeFormat);
                        dateClose = WekaUtils.GetDateValueFromInstances(testInstances, 1, i).ToString(Parameters.DateTimeFormat);
                    }
                    else
                    {
                        date = ((DateTime)dt.Rows[i]["Date"]).ToString(Parameters.DateTimeFormat);
                    }

                    sw.WriteLine(string.Format("{0}, {1}, {2}, {3}, 0, 0, {4}, {5}",
                        m_actions[clsLabel], date, bestClassifierInfo.Tp, bestClassifierInfo.Sl, dateClose, cv[i] == v ? "Right" : "Wrong"));

                    //ret[ConvertFileNameToDateTime(date)] = clsLabel;
                }
            }

            //// save labeled data
            //BufferedWriter writer = new BufferedWriter(new FileWriter(string.Format("c:\\eurusd_m1_arff.result")));
            //writer.write(labeled.toString());
            //writer.newLine();
            //writer.flush();
            //writer.close();
        }
Пример #25
0
        public override double classifyInstance(weka.core.Instance instance)
        {
            if (m_instances.numInstances() == 0)
                return 2;

            if (m_instances.numAttributes() != instance.numAttributes())
            {
                throw new AssertException("different attribute.");
            }
            int n = (instance.numAttributes() - 1) / 2;
            List<Tuple<int, int>> dist = new List<Tuple<int, int>>();
            for (int i = 0; i < m_instances.numInstances(); ++i)
            {
                int d1 = 0, d2 = 0;
                weka.core.Instance instanceI = m_instances.instance(i);
                for (int j = 0; j < n; ++j)
                {
                    //d += (int)((instanceI.value(j) - instance.value(j)) * (instanceI.value(j) - instance.value(j)));
                    if (instanceI.value(j) != instance.value(j))
                    {
                        if (instance.value(j) == 2 || instanceI.value(j) == 2)
                            d1++;
                        else
                            d1 += 4;
                    }
                }
                for (int j = n; j < 2*n; ++j)
                {
                    //d += (int)((instanceI.value(j) - instance.value(j)) * (instanceI.value(j) - instance.value(j)));
                    if (instanceI.value(j) != instance.value(j))
                        if (instance.value(j) == 2 || instanceI.value(j) == 2)
                            d2++;
                        else
                            d2 += 4;
                }
                int c = (int)instanceI.classValue();
                //if (c == 0)
                //{
                //    if (d1 < n / 4 && d1 < d2)
                //    {
                //        dist.Add(new Tuple<int, int>(d1, c));
                //    }
                //}
                //else if (c == 1)
                //{
                //    if (d2 < n / 4 && d2 < d1)
                //    {
                //        dist.Add(new Tuple<int, int>(d2, c));
                //    }
                //}
                //else
                //{
                //    throw new AssertException("");
                //}
                dist.Add(new Tuple<int, int>(d1 + d2, c));
            }
            if (dist.Count == 0)
                return 2;

            dist.Sort(new Comparison<Tuple<int, int>>((x, y) =>
            {
                return x.Item1.CompareTo(y.Item1);
            }));

            int sum = 0, count = 0;
            for (int i = 0; i < dist.Count; ++i)
            {
                if (dist[i].Item1 < n / 4 * 2 * 4)
                {
                    if (dist[i].Item2 != 2 && dist[i].Item2 != 3)
                    {
                        sum += dist[i].Item2;
                        count++;
                    }
                    else
                    {
                    }
                }
                else
                    break;
            }
            if (count == 0)
                return 2;
            if (count < m_instances.numInstances() / 30)
                return 2;
            return (int)Math.Round((double)sum / count);
        }
Пример #26
0
 public override void buildClassifier(weka.core.Instances value)
 {
 }
Пример #27
0
 public override double classifyInstance(weka.core.Instance instance)
 {
     return 2;
 }
        private int[] ReduceByGreedySupervised(weka.core.Instances insts)
        {
            int[] rang = null;

            AttributeSelection filter = new AttributeSelection();  // package weka.filters.supervised.attribute!
            CfsSubsetEval eval = new CfsSubsetEval();
            GreedyStepwise search = new GreedyStepwise();
            search.setSearchBackwards(true);
            filter.setEvaluator(eval);
            filter.setSearch(search);
            filter.SelectAttributes(insts);
            rang = filter.selectedAttributes();

            return rang;
        }
Пример #29
0
 private static void writeFile(weka.core.Instances features, string filename)
 {
     if(FILE_FORMAT == "csv")
     {
         using (TextWriter writer = new StreamWriter(filename))
         {
             if(features.numInstances() == 0)
                 return;
             for(int i = 0; i < features.numAttributes(); i++)
             {
                 writer.Write(features.attribute(i));
                 if(i < features.numAttributes() - 1)
                     writer.Write(DELIMITER);
             }
             writer.WriteLine();
             for(int i = 0; i < features.numInstances(); i++)
             {
                 for(int j = 0; j < features.numAttributes(); j++)
                 {
                     writer.Write(features.instance(i).value(j));
                     if(j < features.numAttributes() - 1)
                         writer.Write(DELIMITER);
                 }
                 writer.WriteLine();
             }
         }
     } else if (FILE_FORMAT == "arff")
     {
         using (TextWriter writer = new StreamWriter(filename))
         {
             writer.WriteLine(features.toString());
         }
     }
 }
Пример #30
0
        public void FilterArff(weka.filters.Filter filter, string append = Parameters.NewFileAppend)
        {
            if (TestParameters.UseFilter)
            {
                Instances origInstances = m_trainInstances;
                filter.setInputFormat(origInstances);
                if (origInstances != null && origInstances.numInstances() > 0)
                {
                    m_trainInstancesNew = weka.filters.Filter.useFilter(origInstances, filter);
                }

                origInstances = m_testInstances;
                if (origInstances != null && origInstances.numInstances() > 0)
                {
                    m_testInstancesNew = weka.filters.Filter.useFilter(origInstances, filter);
                }

                if (TestParameters.SaveDataFile)
                {
                    string newFileName = GetArffFileName(true, m_currentDealType.ToString(), append);
                    string testNewFileName = GetArffFileName(false, m_currentDealType.ToString(), append);

                    if (!System.IO.File.Exists(newFileName)
                        && m_trainInstancesNew != null && m_trainInstancesNew.numInstances() > 0)
                    {
                        WekaUtils.SaveInstances(m_trainInstancesNew, newFileName);
                    }
                    if (!System.IO.File.Exists(testNewFileName)
                        && m_testInstancesNew != null && m_testInstancesNew.numInstances() > 0)
                    {
                        WekaUtils.SaveInstances(m_testInstancesNew, testNewFileName);
                    }
                }
            }
            else
            {
                m_trainInstancesNew = m_trainInstances;
                m_testInstancesNew = m_testInstances;
            }
        }