public static List <ITimeLineData> ConvertTimeLineDataList(IKLineData data, float lastEndPrice, ITradingDayReader openDateReader)
        {
            List <SplitterResult> splitResult = DaySplitter.Split(data, openDateReader);

            List <ITimeLineData> realdataList = new List <ITimeLineData>(splitResult.Count);

            for (int i = 0; i < splitResult.Count; i++)
            {
                SplitterResult split           = splitResult[i];
                int            date            = split.Date;
                int            todayStartIndex = split.Index;
                int            todayEndIndex;
                if (i == splitResult.Count - 1)
                {
                    todayEndIndex = data.Length - 1;
                }
                else
                {
                    todayEndIndex = splitResult[i + 1].Index;
                }
                int          len = todayEndIndex - todayStartIndex + 1;
                TimeLineData r   = new TimeLineData(lastEndPrice, len);
                r.Code = data.Code;
                Convert2RealData(data, todayStartIndex, todayEndIndex, r);
                realdataList.Add(r);
            }

            return(realdataList);
        }
        public static void FindBestRaterWeights()
        {
            using (StreamWriter w = new StreamWriter("rater-weights.csv", true))
            {
                w.WriteLine("down,up,view,dl,rmse,solved,total,precision,rank");
            }

            var once    = new User(0, "");
            var options = new List <dynamic>();

            for (double up = 0.0; up < 5.0; up += 1)
            {
                for (double down = -5.0; down <= 0.0; down += 1)
                {
                    for (double dl = 0.0; dl < 5.0; dl += 1)
                    {
                        for (double view = 0.0; view < 5.0; view += 1)
                        {
                            options.Add(new { up, down, dl, view });
                        }
                    }
                }
            }

            var dbp = new UserBehaviorDatabaseParser();
            var db  = dbp.LoadUserBehaviorDatabase("UserBehaviour.txt");
            var sp  = new DaySplitter(db, 3);
            var cp  = new CorrelationUserComparer();

            Parallel.ForEach(options, set =>
            {
                try
                {
                    var rate = new LinearRater(set.down, set.up, set.view, set.dl);
                    var mfr  = new MatrixFactorizationRecommender(20, rate);
                    //var mfr = new UserCollaborativeFilterRecommender(cp, rate, set.features);

                    mfr.Train(sp.TrainingDB);

                    var score   = mfr.Score(sp.TestingDB, rate);
                    var results = mfr.Test(sp.TestingDB, 100);

                    lock (once)
                    {
                        using (StreamWriter w = new StreamWriter("rater-weights.csv", true))
                        {
                            w.WriteLine(set.down + "," + set.up + "," + set.view + "," + set.dl + "," + score.RootMeanSquareDifference + "," + results.UsersSolved + "," + results.TotalUsers + "," + results.AveragePrecision + "," + results.AverageRecall);
                        }
                    }
                }
                catch (Exception ex)
                {
                    File.WriteAllText("errors.txt", ex.ToString());
                }
            });
        }
        public static void TestAllRecommenders()
        {
            UserBehaviorDatabaseParser dbp = new UserBehaviorDatabaseParser();
            UserBehaviorDatabase       db  = dbp.LoadUserBehaviorDatabase("UserBehaviour.txt");

            //var ubt = new UserBehaviorTransformer(db);
            //var uart = ubt.GetUserArticleRatingsTable();
            //uart.SaveSparcityVisual("sparcity.bmp");
            //uart.SaveUserRatingDistribution("distrib.csv");
            //uart.SaveArticleRatingDistribution("distriba.csv");

            var rate = new LinearRater();
            var sp   = new DaySplitter(db, 5);
            var uc   = new CorrelationUserComparer();

            //var rr = new RandomRecommender();
            //rr.Train(sp.TrainingDB);
            //ScoreResults scores5 = rr.Score(sp.TestingDB, rate);
            //TestResults results5 = rr.Test(sp.TestingDB, 30);

            var ubc = new UserCollaborativeFilterRecommender(uc, rate, 30);
            var mfr = new MatrixFactorizationRecommender(30, rate);
            var icf = new ItemCollaborativeFilterRecommender(uc, rate, 30);
            var hbr = new HybridRecommender(ubc, mfr, icf);

            hbr.Train(sp.TrainingDB);
            ScoreResults scores1  = hbr.Score(sp.TestingDB, rate);
            TestResults  results1 = hbr.Test(sp.TestingDB, 30);

            ubc = new UserCollaborativeFilterRecommender(uc, rate, 30);
            mfr = new MatrixFactorizationRecommender(30, rate);
            icf = new ItemCollaborativeFilterRecommender(uc, rate, 30);

            ubc.Train(sp.TrainingDB);
            ScoreResults scores2  = ubc.Score(sp.TestingDB, rate);
            TestResults  results2 = ubc.Test(sp.TestingDB, 30);

            mfr.Train(sp.TrainingDB);
            ScoreResults scores3  = mfr.Score(sp.TestingDB, rate);
            TestResults  results3 = mfr.Test(sp.TestingDB, 30);

            icf.Train(sp.TrainingDB);
            ScoreResults scores4  = icf.Score(sp.TestingDB, rate);
            TestResults  results4 = icf.Test(sp.TestingDB, 30);

            using (StreamWriter w = new StreamWriter("results.csv"))
            {
                w.WriteLine("model,rmse,users,user-solved,precision,recall");
                w.WriteLine("UCF," + scores2.RootMeanSquareDifference + "," + results2.TotalUsers + "," + results2.UsersSolved + "," + results2.AveragePrecision + "," + results2.AverageRecall);
                w.WriteLine("SVD," + scores3.RootMeanSquareDifference + "," + results3.TotalUsers + "," + results3.UsersSolved + "," + results3.AveragePrecision + "," + results3.AverageRecall);
                w.WriteLine("ICF," + scores4.RootMeanSquareDifference + "," + results4.TotalUsers + "," + results4.UsersSolved + "," + results4.AveragePrecision + "," + results4.AverageRecall);
                w.WriteLine("HR," + scores1.RootMeanSquareDifference + "," + results1.TotalUsers + "," + results1.UsersSolved + "," + results1.AveragePrecision + "," + results1.AverageRecall);
            }
        }
Beispiel #4
0
        public int GetOpenDate(double time)
        {
            int date = (int)time;

            if (!DaySplitter.IsNight(time))
            {
                return(date);
            }

            return(openDateCache.GetNextOpenDate(date));
        }
Beispiel #5
0
        /// <summary>
        /// 将k线转换成日线
        /// </summary>
        /// <param name="data"></param>
        /// <param name="targetPeriod"></param>
        /// <param name="timeSplit"></param>
        /// <returns></returns>
        public static IKLineData Transfer_Day(IKLineData data, KLinePeriod targetPeriod)
        {
            List<SplitterResult> results = DaySplitter.Split(data);

            List<KLineBar> charts = new List<KLineBar>(results.Count);
            for (int i = 0; i < results.Count; i++)
            {
                int startIndex = results[i].Index;
                int endIndex = (i == results.Count - 1) ? data.Length - 1 : results[i + 1].Index - 1;
                charts.Add(GetChart_Day(data, startIndex, endIndex));
            }         

            KLineData klineData = GetKLineData(charts);
            klineData.Period = targetPeriod;
            return klineData;
        }
        public void ChangeTime(double time)
        {
            if (currentTime == time)
            {
                return;
            }
            //比如 20150531140500
            //1分钟线可以正常显示
            //日线需要将

            int date = DaySplitter.GetTimeDate(time, dataCache_Code.GetOpenDateReader());

            if (currentDate != date)
            {
                //TODO 将以前生成的klineChartBuilder_1Minute cache下来
                this.currentDataCache_CodeDate = dataCache_Code.GetCache_CodeDate(date);
                this.klineChartBuilder_1Minute = new RealTimeDataBuilder_DayData(currentDataCache_CodeDate, time);
                this.currentDate = date;
            }

            int    index = klineData.IndexOfTime(time);
            double t     = klineData.Arr_Time[index];

            IKLineData todayMinuteKLineData = klineChartBuilder_1Minute.MinuteKlineData;
            KLineBar   currentMinuteChart   = klineChartBuilder_1Minute.GetCurrentChart();

            //TODO 这里最多只支持到日线,两日线或以上不支持
            int startIndex;

            if (t == (int)t)
            {
                startIndex = 0;
            }
            else
            {
                startIndex = todayMinuteKLineData.IndexOfTime(t);
            }
            int endIndex = todayMinuteKLineData.IndexOfTime(currentMinuteChart.Time);

            IKLineBar currentChart = todayMinuteKLineData.GetAggrKLineBar(startIndex, endIndex - 1);

            KLineBarMerge.MergeTo((KLineBar)currentChart, currentMinuteChart);
            this.currentChart = currentChart;
            this.currentTime  = time;
        }
Beispiel #7
0
        public void DaySplitterTest(int days, int items, int shouldItemsPerDay)
        {
            //Arrange
            var splitter       = new DaySplitter();
            var testCollection = TestEntityGenerator
                                 .Generate(TimeRange.Create(DateTime.Now.Date.AddHours(12).AddDays(-days), DateTime.Now.Date.AddHours(12)), items);

            //Act
            var splitted = splitter.Split(testCollection).ToList();

            //Assert
            var timeRanges   = splitted.Select(x => x.Key);
            var itemsPerDay  = splitted.First().Value.Count();
            var daysAreEqual = splitted.All(x => x.Value.Count() == itemsPerDay);

            Assert.That(timeRanges.Count(), Is.EqualTo(days));
            Assert.That(itemsPerDay, Is.EqualTo(shouldItemsPerDay));
            Assert.That(daysAreEqual, Is.True);
        }
Beispiel #8
0
        public void DoIndex()
        {
            if (!File.Exists(path))
            {
                return;
            }
            String indexPath = path + ".index";
            KLineDataStore_File_Single store = new KLineDataStore_File_Single(path);
            IKLineData            klineData  = store.LoadAll();
            List <SplitterResult> results    = DaySplitter.Split(klineData);

            List <String> indeies = new List <string>(results.Count);

            for (int i = 0; i < results.Count; i++)
            {
                indeies.Add(results[i].ToString());
            }

            File.WriteAllLines(indexPath, indeies.ToArray());

            //FileStream file = new FileStream(path, FileMode.Open);
            //try
            //{
            //indeies.Add(((int)time).ToString() + "," + currentIndex);
            //File.WriteAllLines(indexPath, indeies.ToArray());

            //double lastTime = GetTimeByIndex(file, 0);
            //double time = GetTimeByIndex(file, 1);
            ////KLinePeriod period = KLineData.GetPeriod(lastTime, time);

            ////算法

            //int len = GetLength(file);
            //int currentIndex = 0;
            //bool hasNight = false;
            //for (int index = 1; index < len; index++)
            //{
            //    time = GetTimeByIndex(file, index);

            //    int date = (int)time;
            //    int lastDate = (int)lastTime;

            //    //夜盘开始,则一定是新的一天开始
            //    if (IsNightStart(time, lastTime))
            //    {
            //        indeies.Add(((int)lastTime).ToString() + "," + currentIndex.ToString());
            //        currentIndex = index;
            //        hasNight = true;
            //    }
            //    else if (hasNight)
            //    {
            //        //对于夜盘来说,如果到了第二天,则说明夜盘结束了,此时不算新的一天开始
            //        if (date != lastDate)
            //            hasNight = false;
            //    }
            //    //只要过了夜都算第二天的
            //    else if (date != lastDate)
            //    {
            //        indeies.Add(((int)lastTime).ToString() + "," + currentIndex.ToString());
            //        currentIndex = index;
            //    }

            //    lastTime = time;
            //}

            //}
            //finally
            //{
            //    //file.Close();
            //}
        }
Beispiel #9
0
 public KLineData_DaySplitter(IKLineData klineData, ITradingSessionReader_Instrument tradingSessionReader)
 {
     this.results = DaySplitter.Split(klineData, tradingSessionReader);
 }
Beispiel #10
0
 public KLineData_DaySplitter(IKLineData klineData, ITradingTimeReader_Code tradingSessionReader)
 {
     this.results = DaySplitter.Split(klineData, tradingSessionReader);
 }