Ejemplo n.º 1
0
        private void AssertText(string code, int start, int end, string fileName)
        {
            List <string>      list_OnBar  = new List <string>();
            IDataPackage_Code  datapackage = DataCenter.Default.DataPackageFactory.CreateDataPackage_Code(code, start, end, 0, 0);
            IKLineData_Extend  klineData   = datapackage.GetKLineData(KLinePeriod.KLinePeriod_1Minute);
            ITickData          tickData    = datapackage.GetTickData(start);
            KLineToTickIndeier indeier     = new KLineToTickIndeier(tickData, klineData);

            int lastBarPos;

            for (int i = 0; i < tickData.Length; i++)
            {
                list_OnBar.Add("tick:" + tickData.GetBar(i));
                Console.WriteLine("tick:" + tickData.GetBar(i));
                int pos = indeier.GetKLineBarPosIfFinished(i, out lastBarPos);
                if (pos >= 0)
                {
                    list_OnBar.Add(klineData.Period + ":" + klineData.GetBar(pos));
                    Console.WriteLine(klineData.Period + ":" + klineData.GetBar(pos));
                    for (int m = pos + 1; m <= lastBarPos; m++)
                    {
                        list_OnBar.Add(klineData.Period + ":" + klineData.GetBar(m));
                        Console.WriteLine(klineData.Period + ":" + klineData.GetBar(m));
                        //lastBarPos = 1;
                    }
                }
            }
            AssertUtils.AssertEqual_List(fileName, GetType(), list_OnBar);
        }
Ejemplo n.º 2
0
 public StrategyDrawer(IDataPackage_Code dataPackage, StrategyReferedPeriods referPeriods)
 {
     this.dataPackage = dataPackage;
     foreach (KLinePeriod period in referPeriods.UsedKLinePeriods)
     {
         IKLineData_Extend klineData = dataPackage.GetKLineData(period);
         dic_KLinePeriod_Start.Add(period, klineData.BarPos);
         dic_KLinePeriod_DataKey.Add(period, new KLineKey(dataPackage.Code, dataPackage.StartDate, dataPackage.EndDate, period));
     }
 }
Ejemplo n.º 3
0
        private void AssertKLineToKLine(string code, int start, int end, List <string> list_OnBar)
        {
            IDataPackage_Code         datapackage = DataCenter.Default.DataPackageFactory.CreateDataPackage_Code(code, start, end);
            IKLineData_Extend         mainKLine   = datapackage.GetKLineData(KLinePeriod.KLinePeriod_1Minute);
            IList <IKLineData_Extend> indexKLines = new List <IKLineData_Extend>();

            indexKLines.Add(datapackage.GetKLineData(KLinePeriod.KLinePeriod_5Minute));
            indexKLines.Add(datapackage.GetKLineData(KLinePeriod.KLinePeriod_15Minute));
            indexKLines.Add(datapackage.GetKLineData(KLinePeriod.KLinePeriod_1Hour));
            indexKLines.Add(datapackage.GetKLineData(KLinePeriod.KLinePeriod_1Day));
            KLineToKLineIndeier indeier = new KLineToKLineIndeier(mainKLine, indexKLines);

            int startMainPos = mainKLine.BarPos;
            int endMainPos   = mainKLine.Length - 1;

            for (int i = startMainPos; i <= endMainPos; i++)
            {
                Console.WriteLine(mainKLine.Period + ":" + mainKLine.GetBar(i));
                list_OnBar.Add(mainKLine.Period + ":" + mainKLine.GetBar(i));
                PrintKLines(i, indeier, indexKLines, list_OnBar);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 运行策略,并且将策略运行结果绑定到该
        /// </summary>
        public void Run()
        {
            if (this.strategyData == null || this.strategyData.Strategy == null)
            {
                return;
            }
            IStrategy strategy = this.strategyData.Strategy;
            StrategyReferedPeriods referedPeriods = strategy.GetReferedPeriods();

            if (referedPeriods == null)
            {
                referedPeriods = new StrategyReferedPeriods();
                KLinePeriod currentPeriod = this.compChart.Controller.ChartComponentData.KlinePeriod;
                referedPeriods.UsedKLinePeriods.Add(currentPeriod);
                if (strategy is StrategyAbstract)
                {
                    ((StrategyAbstract)strategy).MainKLinePeriod = currentPeriod;
                }
            }
            KLinePeriod           mainPeriod    = referedPeriods.MainPeriod;
            StrategyForwardPeriod forwardPeriod = new StrategyForwardPeriod(referedPeriods.UseTickData, mainPeriod);

            IStrategyExecutorFactory executorFactory = StrategyCenter.Default.GetStrategyExecutorFactory();
            IDataPackage_Code        dataPackage     = this.compChart.Controller.CurrentNavigater.DataPackage;

            Dictionary <KLinePeriod, int> dic_KLinePeriod_StartPos = new Dictionary <KLinePeriod, int>();

            for (int i = 0; i < referedPeriods.UsedKLinePeriods.Count; i++)
            {
                KLinePeriod period = referedPeriods.UsedKLinePeriods[i];
                dic_KLinePeriod_StartPos.Add(period, dataPackage.GetKLineData(period).BarPos);
            }

            drawOperator = new ChartComponentStrategyDrawer(this.compChart.Drawer, dic_KLinePeriod_StartPos, 0, 0);
            StrategyArguments_DataPackage strategyDataPackage = new StrategyArguments_DataPackage(dataPackage, referedPeriods, forwardPeriod);;
            StrategyHelper strategyOperator = new StrategyHelper();

            strategyOperator.Drawer = drawOperator;
            strategyExecutor        = executorFactory.CreateExecutor_History(strategyDataPackage);

            strategyExecutor.Strategy = strategy;
            //strategyExecutor.Run();
            //strategyExecutor.ExecuteFinished += StrategyExecutor_ExecuteFinished;
            strategyExecutor.Execute();
        }
Ejemplo n.º 5
0
        public void TestTimeLineToKLine()
        {
            string code  = "RB1710";
            int    start = 20170601;
            int    end   = 20170605;

            IDataPackage_Code datapackage = DataCenter.Default.DataPackageFactory.CreateDataPackage_Code(code, start, end, 0, 0);
            IKLineData_Extend klineData   = datapackage.GetKLineData(KLinePeriod.KLinePeriod_1Minute);
            //IKLineData_Extend klineData = datapackage.GetKLineData(KLinePeriod.KLinePeriod_5Minute);
            IList <int>            tradingDays  = datapackage.GetTradingDays();
            ITimeLineData          timeLineData = datapackage.GetTimeLineData(tradingDays[0]);
            TimeLineToKLineIndeier indeier      = new TimeLineToKLineIndeier(klineData, timeLineData);

            int  tradingDayIndex = 0;
            bool isFirst         = true;

            for (int i = klineData.BarPos; i < klineData.Length; i++)
            {
                if (!isFirst && klineData.IsDayStart(i))
                {
                    tradingDayIndex++;
                    if (tradingDayIndex >= tradingDays.Count)
                    {
                        return;
                    }
                    timeLineData = datapackage.GetTimeLineData(tradingDays[tradingDayIndex]);
                    indeier.ChangeTradingDay(timeLineData);
                }
                isFirst          = false;
                klineData.BarPos = i;
                Console.WriteLine(klineData.Period + ":" + klineData.GetBar(i));
                int barPos = indeier.GetTimeLineBarPosIfFinished(i);
                if (barPos >= 0)
                {
                    timeLineData.BarPos = barPos;
                    Console.WriteLine("分时线:" + timeLineData.GetBar(barPos));
                }
                Assert.AreEqual(klineData.Time, timeLineData.Time);
                Assert.AreEqual(klineData.End, timeLineData.Price);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 运行策略,并且将策略运行结果绑定到该
        /// </summary>
        public void Run()
        {
            Strategy_MultiMa strategy = new Strategy_MultiMa();

            strategy.MainKLinePeriod = this.compChart.Controller.CompData.KlinePeriod;
            this.strategy            = strategy;
            if (this.strategy == null)
            {
                return;
            }
            StrategyReferedPeriods referedPeriods = this.strategy.GetReferedPeriods();

            if (referedPeriods == null)
            {
                referedPeriods = new StrategyReferedPeriods();
                KLinePeriod currentPeriod = this.compChart.Controller.CompData.KlinePeriod;
                referedPeriods.UsedKLinePeriods.Add(currentPeriod);
            }
            KLinePeriod           mainPeriod    = referedPeriods.MainPeriod;
            StrategyForwardPeriod forwardPeriod = new StrategyForwardPeriod(referedPeriods.UseTickData, mainPeriod);

            IStrategyExecutorFactory_History executorFactory = StrategyCenter.Default.GetStrategyExecutorFactory_History();
            IDataPackage_Code dataPackage = this.compChart.Controller.CurrentNavigater.DataPackage;

            Dictionary <KLinePeriod, int> dic_KLinePeriod_StartPos = new Dictionary <KLinePeriod, int>();

            for (int i = 0; i < referedPeriods.UsedKLinePeriods.Count; i++)
            {
                KLinePeriod period = referedPeriods.UsedKLinePeriods[i];
                dic_KLinePeriod_StartPos.Add(period, dataPackage.GetKLineData(period).BarPos);
            }

            drawOperator = new StrategyDrawOperator(this.compChart.Drawer, dic_KLinePeriod_StartPos, 0, 0);
            IStrategyOperator strategyOperator = new StrategyOperator(drawOperator);
            IStrategyExecutor executor         = executorFactory.CreateExecutorByDataPackage(dataPackage, referedPeriods, forwardPeriod, strategyOperator);

            executor.SetStrategy(strategy);
            executor.Run();
        }
Ejemplo n.º 7
0
        public RealTimeReader_Strategy(IDataPackage_Code dataPackage, ForwardReferedPeriods referedPeriods, ForwardPeriod forwardPeriod)
        {
            this.dataPackage    = dataPackage;
            this.code           = dataPackage.Code;
            this.startDate      = dataPackage.StartDate;
            this.endDate        = dataPackage.EndDate;
            this.referedPeriods = referedPeriods;
            this.forwardPeriod  = forwardPeriod;

            this.allKLineData = new Dictionary <KLinePeriod, KLineData_RealTime>();
            for (int i = 0; i < referedPeriods.UsedKLinePeriods.Count; i++)
            {
                KLinePeriod        period             = referedPeriods.UsedKLinePeriods[i];
                IKLineData         klineData          = dataPackage.GetKLineData(period);
                KLineData_RealTime klineData_RealTime = new KLineData_RealTime(klineData);
                allKLineData.Add(period, klineData_RealTime);
            }

            IList <int> allTradingDays = dataPackage.GetTradingDays();

            if (forwardPeriod.IsTickForward)
            {
                this.klineDataForward = new HistoryDataForward_Code_TickPeriod(dataPackage, allKLineData, allTradingDays, forwardPeriod.KlineForwardPeriod);
            }
            else
            {
                KLinePeriod        mainPeriod    = forwardPeriod.KlineForwardPeriod;
                KLineData_RealTime mainKLineData = allKLineData[mainPeriod];
                this.klineDataForward = new HistoryDataForward_Code_KLinePeriod(code, mainKLineData, allKLineData);
            }

            this.klineDataForward.OnRealTimeChanged += KlineDataForward_OnRealTimeChanged;
            this.klineDataForward.OnTick            += KlineDataForward_OnTick;
            this.klineDataForward.OnBar             += KlineDataForward_OnBar;

            //this.klineDataForward = HistoryDataForwardFactory.CreateHistoryDataForward_Code(dataPackage, referedPeriods, forwardPeriod);
            //this.klineDataForward.OnTick += KlineDataForward_OnTick;
            //this.klineDataForward.OnBar += KlineDataForward_OnBar;
        }
Ejemplo n.º 8
0
        public void TestExecuteStrategy_MA()
        {
            string code  = "rb1710";
            int    start = 20170601;
            int    end   = 20170603;
            IStrategyExecutor_Single executor = StrategyTestUtils.CreateExecutor_CodePeriod(code, start, end);
            MockStrategy_Ma          strategy = new MockStrategy_Ma();

            executor.Strategy = strategy;
            executor.Run();

            List <float>      floats      = strategy.MAList;
            IDataPackage_Code dataPackage = DataCenter.Default.DataPackageFactory.CreateDataPackage_Code(code, start, end);
            IKLineData_Extend klineData   = dataPackage.GetKLineData(KLinePeriod.KLinePeriod_1Minute);
            List <float>      floats2     = KLineDataUtils.MaList(klineData, klineData.BarPos, klineData.Length - 1, 5);

            for (int i = 0; i < floats.Count; i++)
            {
                Console.WriteLine(floats[i] + "," + floats2[i]);
            }
            AssertUtils.AssertEqual_List(floats2, floats);
        }
Ejemplo n.º 9
0
        public void TestKLineToTick_Days()
        {
            string code  = "RB1710";
            int    start = 20170601;
            int    end   = 20170605;

            List <string>     list_OnBar  = new List <string>();
            IDataPackage_Code datapackage = DataCenter.Default.DataPackageFactory.CreateDataPackage_Code(code, start, end, 0, 0);
            IKLineData_Extend klineData   = datapackage.GetKLineData(KLinePeriod.KLinePeriod_1Minute);

            IList <int>        tradingDays = datapackage.GetTradingDays();
            ITickData          tickData    = datapackage.GetTickData(tradingDays[0]);
            KLineToTickIndeier indeier     = new KLineToTickIndeier(tickData, klineData);

            DoIndex(list_OnBar, klineData, tickData, indeier);

            for (int i = 1; i < tradingDays.Count; i++)
            {
                tickData = datapackage.GetTickData(tradingDays[i]);
                indeier.ChangeTradingDay(tickData);
                DoIndex(list_OnBar, klineData, tickData, indeier);
            }
        }
Ejemplo n.º 10
0
 private KLineData_RealTime GetKLineData_RealTime(int date, double time)
 {
     this.tickData  = dataPackage.GetTickData(date);
     this.klineData = dataPackage.GetKLineData(klinePeriod);
     return(GetKLineData_RealTime(klineData, tickData, time));
 }