Esempio n. 1
0
        /// <summary>
        /// 新逻辑调用服务的计算类,注入策略运行计划清单和数据
        /// </summary>
        /// <param name="es"></param>
        /// <param name="teststragplans"></param>
        /// <returns></returns>
        public BackTestReturnClass <T> VirExchange(ServiceSetting <T> sc, ref ExchangeService es, StragRunPlanClass <T>[] teststragplans)
        {
            //LoopCnt = 0;
            testIndex = 0;
            //调用计算服务进行计算
            if (!teststragplans[0].AssetUnitInfo.Running) //如果资产单元没有启动,启动资产单元
            {
                teststragplans[0].AssetUnitInfo.Run(false);
            }
            es = teststragplans[0].AssetUnitInfo.getCurrExchangeServer(); //设置资产单元的模拟交易器
            CalcService <T> cs = new CalcService <T>(true, sc, teststragplans.ToDictionary(t => t.GUID, t => t));

            cs.DataPoint = dtp;
            if (dtp.IsSecurityData == 1)
            {
                cs.ReadDataTableName = dtp.NewestTable;
                cs.Codes             = null;
            }
            cs.IsTestBack = true;

            long begNo = BegExpect;
            //ExpectReader er = new ExpectReader();
            DataReader              er  = DataReaderBuild.CreateReader(dtp.DataType, dtp.HistoryTable, dtp.RuntimeInfo.SecurityCodes); //支持所有数据
            ExpectList <T>          el  = null;
            long                    cnt = 0;
            BackTestReturnClass <T> ret = new BackTestReturnClass <T>();

            ret.HoldCntDic    = new Dictionary <int, int>();
            ret.HoldWinCntDic = new Dictionary <int, int>();
            ret.InChipsDic    = new Dictionary <int, int>();
            ret.WinChipsDic   = new Dictionary <int, int>();

            ExpectList <T> AllData = new ExpectList <T>();

            //long testIndex = teststrag.ReviewExpectCnt - 1;
            BaseStragClass <T>[] teststrags = teststragplans.Select(p => p.PlanStrag).ToArray <BaseStragClass <T> >();
            testIndex = teststrags.Max <BaseStragClass <T> >(s => s.ReviewExpectCnt);//取所有策略中回览期最大的开始,之前的数据不看
            long           InitIndex = testIndex;
            ExpectList <T> testData  = null;
            ////Dictionary<string, StragChance<T>> NoCloseChances = new Dictionary<string, StragChance<T>>();
            ////Dictionary<string, StragChance<T>> tmpChances = new Dictionary<string, StragChance<T>>();
            ////Dictionary<Int64, ExchangeChance> NewExchangeRecord = new Dictionary<Int64, ExchangeChance>();
            int AllCnt = 0;

            while (el == null || el.Count > 0) //如果取到的数据长度大于0
            {
                if (dtp.IsSecurityData == 1)
                {
                    el = er.ReadHistory <T>(begNo, LoopCnt);
                }
                else
                {
                    el = er.ReadHistory <T>(begNo, LoopCnt);
                }
                if (el == null)
                {
                    ret.LoopCnt = cnt * LoopCnt;
                    ret.succ    = false;
                    ret.Msg     = "读取历史数据错误!";
                    break;
                }
                if (el.Count == 0)
                {
                    ret.LoopCnt = testIndex;
                    ret.succ    = true;
                    //ret.Msg = string.Format("成功遍历{0}条记录!共发现机会{1}次!其中,{2}.", testIndex, ret.ChanceList.Count, ret.HoldInfo);
                    break;
                }
                AllData = ExpectList <T> .Concat(AllData, el);

                if (dtp.IsSecurityData == 0)
                {
                    //begNo = el.LastData.LExpectNo + 1;//加一期
                    begNo = long.Parse(DataReader.getNextExpectNo(el.LastData.Expect, dtp));
                }
                else
                {
                    DateTime dt = new DateTime(el.LastData.LExpectNo);
                    begNo = dt.AddDays(1).Ticks;//加一个周期,如果要回测其他周期,AddDays许更换为其他时间周期
                }
                cnt++;
                //Todo:

                while (testIndex < AllData.Count)
                {
                    int CurrExpectClose = 0;
                    AllCnt++;
                    es.UpdateExpectCnt(AllCnt);
                    if (testData == null)
                    {
                        //testData = AllData.getSubArray(0, teststrag.ReviewExpectCnt);
                        testData = AllData.getSubArray(0, (int)InitIndex + 1);
                    }
                    else
                    {
                        if (dtp.IsSecurityData == 0)//如果非证券,判断两个期号之间是否连续
                        {
                            ////if (AllData[(int)testIndex].Expect != testData.LastData.ExpectIndex + 1)
                            ////{
                            ////    if (dtp.DataType == "PK10")
                            ////    {
                            ////        throw new Exception(string.Format("{1}第{0}期后出现数据遗漏,请补充数据后继续测试!", testData.LastData.Expect, testData.LastData.OpenTime));
                            ////    }
                            ////}
                        }
                        testData.RemoveAt(0);
                        testData.Add(AllData[(int)testIndex]);
                    }
                    //只是取数据的逻辑一样,以后均调用CalcService
                    //ToAdd:以下是内容
                    cs.CurrData        = testData;
                    cs.OnFinishedCalc += OnCalcFinished;
                    cs.setGlobalClass(Program.gc);
                    cs.Calc();
                    while (!cs.CalcFinished)
                    {
                        Thread.Sleep(1 * 100);
                    }
                    this.SystemStdDevs = cs.getSystemStdDevList();
                    //testIndex++;
                    testIndex++;
                }
            }
            FinishedProcess();
            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// 新逻辑调用服务的计算类,注入策略运行计划清单和数据
        /// </summary>
        /// <param name="es"></param>
        /// <param name="teststragplans"></param>
        /// <returns></returns>
        public BackTestReturnClass VirExchange(ServiceSetting sc, ref ExchangeService es, StragRunPlanClass[] teststragplans)
        {
            LoopCnt   = 0;
            testIndex = 0;
            //调用计算服务进行计算
            if (!teststragplans[0].AssetUnitInfo.Running) //如果资产单元没有启动,启动资产单元
            {
                teststragplans[0].AssetUnitInfo.Run();
            }
            es = teststragplans[0].AssetUnitInfo.ExchangeServer; //设置资产单元的模拟交易器
            CalcService cs = new CalcService(true, sc, teststragplans.ToDictionary(t => t.GUID, t => t));

            cs.IsTestBack = true;

            long                begNo = BegExpect;
            ExpectReader        er    = new ExpectReader();
            ExpectList          el    = null;
            long                cnt   = 0;
            BackTestReturnClass ret   = new BackTestReturnClass();

            ret.HoldCntDic    = new Dictionary <int, int>();
            ret.HoldWinCntDic = new Dictionary <int, int>();
            ret.InChipsDic    = new Dictionary <int, int>();
            ret.WinChipsDic   = new Dictionary <int, int>();

            ExpectList AllData = new ExpectList();

            //long testIndex = teststrag.ReviewExpectCnt - 1;
            StragClass[] teststrags = teststragplans.Select(p => p.PlanStrag).ToArray <StragClass>();
            testIndex = teststrags.Max <StragClass>(s => s.ReviewExpectCnt);//取所有策略中回览期最大的开始,之前的数据不看
            long       InitIndex = testIndex;
            ExpectList testData  = null;
            ////Dictionary<string, StragChance> NoCloseChances = new Dictionary<string, StragChance>();
            ////Dictionary<string, StragChance> tmpChances = new Dictionary<string, StragChance>();
            ////Dictionary<Int64, ExchangeChance> NewExchangeRecord = new Dictionary<Int64, ExchangeChance>();
            int AllCnt = 0;

            while (el == null || el.Count > 0) //如果取到的数据长度大于0
            {
                el = er.ReadHistory(begNo, LoopCnt);
                if (el == null)
                {
                    ret.LoopCnt = cnt * LoopCnt;
                    ret.succ    = false;
                    ret.Msg     = "读取历史数据错误!";
                    break;
                }
                if (el.Count == 0)
                {
                    ret.LoopCnt = testIndex;
                    ret.succ    = true;
                    //ret.Msg = string.Format("成功遍历{0}条记录!共发现机会{1}次!其中,{2}.", testIndex, ret.ChanceList.Count, ret.HoldInfo);
                    break;
                }
                AllData = ExpectList.Concat(AllData, el);
                begNo   = el.LastData.LExpectNo + 1;

                cnt++;
                //Todo:

                while (testIndex < AllData.Count)
                {
                    int CurrExpectClose = 0;
                    AllCnt++;
                    es.UpdateExpectCnt(AllCnt);
                    if (testData == null)
                    {
                        //testData = AllData.getSubArray(0, teststrag.ReviewExpectCnt);
                        testData = AllData.getSubArray(0, (int)InitIndex + 1);
                    }
                    else
                    {
                        if (AllData[(int)testIndex].ExpectIndex != testData.LastData.ExpectIndex + 1)
                        {
                            throw new Exception(string.Format("{1}第{0}期后出现数据遗漏,请补充数据后继续测试!", testData.LastData.Expect, testData.LastData.OpenTime));
                        }
                        testData.RemoveAt(0);
                        testData.Add(AllData[(int)testIndex]);
                    }
                    //只是取数据的逻辑一样,以后均调用CalcService
                    //ToAdd:以下是内容
                    cs.CurrData = testData;
                    cs.Calc();
                    while (!cs.CalcFinished)
                    {
                        Thread.Sleep(100);
                    }
                    this.SystemStdDevs = cs.getSystemStdDevList();
                    testIndex++;
                }
            }
            FinishedProcess();
            return(ret);
        }