Example #1
0
        /// <summary>
        /// 模拟回归
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_VirExchange_Click(object sender, EventArgs e)
        {
            StragRunPlanClass[] plans = this.runPlanPicker1.Plans;
            if (plans == null || plans.Length == 0)
            {
                return;
            }
            SCList = plans.ToList();
            if (!RunVirExchange)
            {
                if (MessageBox.Show("确定开始模拟成交?", "确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
            }
            if (RunVirExchange)
            {
                if (MessageBox.Show("确定停止模拟成交?", "确认", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
                try
                {
                    if (th.ThreadState == ThreadState.Running)
                    {
                        th.Abort();
                    }
                }
                catch
                {
                }
                th = null;
                this.timer_Tip.Enabled = false;
                RunVirExchange         = false;
                return;
            }
            this.RunVirExchange = true;
            //this.btn_VirExchange.Text = "停止";
            //this.Cursor = Cursors.WaitCursor;
            //es = new ExchangeService(int.Parse(this.txt_InitCash.Text),double.Parse(this.txt_Odds.Text));
            SettingClass setting = new SettingClass();

            setting.SetGlobalSetting(this.globalSetting);
            setting.GrownMaxVal      = int.Parse(this.txt_GrownMaxVal.Text);
            setting.GrownMinVal      = int.Parse(this.txt_GrownMinVal.Text);
            setting.DispRows         = int.Parse(this.txt_MinCols.Text);
            setting.minColTimes      = new int[10];
            setting.UseLocalWaveData = this.checkBox_UseBuffRsult.Checked;
            AssetUnitClass auc = new AssetUnitClass();

            if (Program.AllSettings.AllAssetUnits.Count > 0)//如果存在资产管理单元,所有计划均绑定到第一个上进行测试
            {
                auc = Program.AllSettings.AllAssetUnits.Values.First();
            }
            es = auc.ExchangeServer;
            if (es != null)
            {
                es.Reset();
            }
            //设置所有默认值
            SCList.ForEach(p => p.PlanStrag.CommSetting = setting);
            SCList.ForEach(p => p.AssetUnitInfo         = auc);
            SCList.ForEach(p => p.Running = true);
            //if (SCList.Count == 1)
            //{
            //    //SCList[0].PlanStrag = sc;
            SCList.ForEach(p => p.IncreamType = this.checkBox_CreamModel.Checked ? InterestType.CompoundInterest : InterestType.SimpleInterest);
            SCList.ForEach(p => p.AutoRunning = true);
            ////SCList.ForEach(p=>p.FixAmt = 1);
            ////SCList.ForEach(p=>p.FixRate = 0.01);
            SCList.ForEach(p => p.AllowMaxHoldTimeCnt = int.Parse(txt_AllowMaxHoldTimeCnt.Text));
            if (SCList.Count == 1)
            {
                SCList.ForEach(p => p.PlanStrag.ChipCount = int.Parse(this.txt_ChipCnt.Text));
            }
            //}
            #region 用新的计算进行回测
            ////////////////for (int i = 0; i < 9; i++)
            ////////////////{
            ////////////////    TextBox tb = this.Controls.Find(string.Format("txt_minColTimes{0}", i + 1), true)[0] as TextBox;
            ////////////////    setting.minColTimes[i] = int.Parse(tb.Text);
            ////////////////}
            ////////////////setting.MaxHoldingCnt = int.Parse(this.txt_MaxHoldChanceCnt.Text);
            ////////////////setting.Odds = double.Parse(this.txt_Odds.Text);
            ////////////////setting.InitCash = int.Parse(this.txt_InitCash.Text);
            ////////////////Assembly asmb = typeof(StragClass).Assembly;// Assembly.LoadFrom("EnterpriseServerBase.dll");
            ////////////////btc = new BackTestClass(long.Parse(txt_begExpNo.Text), long.Parse(txt_LoopCnt.Text), setting);
            ////////////////////Type sct = asmb.GetType(ddl_StragName.SelectedValue.ToString());


            ////////////////if (!checkBox_MixAll.Checked)
            ////////////////{
            ////////////////    ////sc = Activator.CreateInstance(sct) as StragClass;
            ////////////////    sc.CommSetting = setting;
            ////////////////    sc.ChipCount = int.Parse(this.txt_ChipCnt.Text);
            ////////////////    sc.FixChipCnt = (this.txt_FixChipCnt.Text.Trim() == "0") ? false : true;
            ////////////////    sc.ReviewExpectCnt = int.Parse(this.txt_reviewExpCnt.Text);
            ////////////////    sc.InputMinTimes = int.Parse(this.txt_minInputTimes.Text);
            ////////////////    if (sc.StagSetting.IsLongTermCalc)
            ////////////////    {
            ////////////////        sc.ReviewExpectCnt = sc.ReviewExpectCnt + sc.InputMinTimes;
            ////////////////    }
            ////////////////    sc.InputMaxTimes = int.Parse(this.txt_maxInputTimes.Text);
            ////////////////    sc.ExcludeBS = this.chkb_exclueBS.Checked;
            ////////////////    sc.ExcludeSD = this.chkb_exclueSD.Checked;
            ////////////////    sc.BySer = this.chkb_bySer.Checked;
            ////////////////    sc.OnlyBS = this.chkb_onlyBS.Checked;
            ////////////////    sc.OnlySD = this.chkb_onlySD.Checked;
            ////////////////    sc.GetRev = this.chkb_getRev.Checked;
            ////////////////    if (sc is IProbCheckClass)
            ////////////////    {
            ////////////////        (sc as IProbCheckClass).StdvCnt = double.Parse(this.txt_StdvCnt.Text);
            ////////////////    }
            ////////////////    //sc.AllowMaxHoldTimeCnt = int.Parse(this.txt_AllowMaxHoldTimeCnt.Text);
            ////////////////    sc.MinWinRate = double.Parse(this.txt_minRate.Text);
            ////////////////    ////double p = double.Parse(this.txt_ChipCnt.Text) / setting.Odds;
            ////////////////    ////double Normal_p = double.Parse(this.txt_ChipCnt.Text) / 10;
            ////////////////    ////double b = double.Parse(this.txt_Odds.Text);
            ////////////////    ////double q = 1 - p;
            ////////////////    sc.StagSetting = sc.getInitStagSetting();
            ////////////////    ////sc.StagSetting.BaseType.ChipRate = (p * b - q) / b;
            ////////////////    //sc.StagSetting.BaseType.IncrementType = this.checkBox_CreamModel.Checked ? InterestType.CompoundInterest : InterestType.SimpleInterest;

            ////////////////    if (setting.UseLocalWaveData && sc is ProbWaveSelectStragClass)//是长期概率分布类
            ////////////////    {
            ////////////////        DataTable dt = new PK10ProbWaveDataInterface().GetProWaveResult(long.Parse(txt_begExpNo.Text));
            ////////////////        (sc as ProbWaveSelectStragClass).LocalWaveData = new GuideResult(dt);
            ////////////////    }
            ////////////////    StragRunPlanClass sr = new StragRunPlanClass();
            ////////////////    sr.PlanStrag = sc;
            ////////////////    sr.IncreamType = this.checkBox_CreamModel.Checked ? InterestType.CompoundInterest : InterestType.SimpleInterest;
            ////////////////    sr.AutoRunning = true;
            ////////////////    sr.FixAmt = 1;
            ////////////////    sr.FixRate = 0.01;
            ////////////////    SCList.Add(sr);
            ////////////////}
            ////////////////else
            ////////////////{
            ////////////////    for (int i = 2; i < 9; i++)
            ////////////////    {
            ////////////////        for (int j = 0; j < 2; j++)
            ////////////////        {
            ////////////////            StragClass sobj = Activator.CreateInstance(sct) as StragClass;
            ////////////////            sobj.CommSetting = setting;
            ////////////////            sobj.ChipCount = i;
            ////////////////            sobj.FixChipCnt = (this.txt_FixChipCnt.Text.Trim() == "0") ? false : true;
            ////////////////            sobj.ReviewExpectCnt = sobj.CommSetting.minColTimes[i - 1] + 1;
            ////////////////            sobj.BySer = j==1;
            ////////////////            sobj.MinWinRate = double.Parse(this.txt_minRate.Text);
            ////////////////            //sobj.AllowMaxHoldTimeCnt = 100;
            ////////////////            sobj.StagSetting = sobj.getInitStagSetting();
            ////////////////            //sobj.StagSetting.BaseType.IncrementType = this.checkBox_CreamModel.Checked ? InterestType.CompoundInterest : InterestType.SimpleInterest;
            ////////////////            StragRunPlanClass sr = new StragRunPlanClass();
            ////////////////            sr.PlanStrag = sobj;
            ////////////////            sr.IncreamType = this.checkBox_CreamModel.Checked ? InterestType.CompoundInterest : InterestType.SimpleInterest;
            ////////////////            sr.AutoRunning = true;
            ////////////////            sr.FixAmt = 1;
            ////////////////            sr.FixRate = 0.01;
            ////////////////            SCList.Add(sr);
            ////////////////        }
            ////////////////    }
            ////////////////}
            //////////////////sc.MinWinRate = double.Parse(this.txt_Odds.Text) *double.Parse(this.txt_minRate.Text);
            //////////////////凯利公式 (p*b-q)/b
            //////////////////凯利公式 (p*b-q)/b
            //////////////////////DialogResult rs = MessageBox.Show(sc.StagSetting.BaseType.ChipRate.ToString(), "胜率", MessageBoxButtons.OKCancel) ;
            //////////////////////if (rs == DialogResult.Cancel)
            //////////////////////{
            //////////////////////    return;
            //////////////////////}
            #endregion
            //this.Cursor = Cursors.WaitCursor;
            this.chart1.Series.Clear();
            //this.chart1.DataSource = null;
            this.timer_Tip.Interval = int.Parse(txt_Timer_Interval.Text) * 1000;
            timer_Tip.Tick         += new EventHandler(timer_Tip_Tick);
            //this.timer_Tip.Enabled = true;
            this.timer_Tip.Enabled = true;

            //this.timer_Tip_Tick(null, null);
            //////try
            //////{
            if (btc == null)
            {
                btc = btc = new BackTestClass(long.Parse(txt_begExpNo.Text), long.Parse(txt_LoopCnt.Text), setting);
            }
            th = new Thread(RunVirtual);
            th.Start();
            return;
            //////}
            //////catch (Exception ce)
            //////{
            //////    ret = new BackTestReturnClass();
            //////    ret.ChanceList = new List<ChanceClass>();
            //////    ret.Msg = ce.Message;
            //////    ret.succ = false;
            //////}
        }
Example #2
0
        public void Calc()
        {
            try
            {
                ////if (!AllowCalc) return;
                DateTime currTime = DateTime.Now;
                ////int MaxViewCnt = Program.AllServiceConfig.AllRunningPlanGrps.Max(t=>t.Value.UseSPlans.ForEach(a=>Program.AllServiceConfig.AllStrags[a.GUID]))));
                ExpectList <T> el = CurrData;                            // Program.AllServiceConfig.LastDataSector;
                                                                         //Log("数据源", string.Format("数据条数:{0};最后数据:{1}", el.Count, el.LastData.ToString()));
                                                                         //if (CurrDataList.Count < MaxViewCnt)
                                                                         //{
                                                                         //    el = new ExpectReader().ReadNewestData(MaxViewCnt);
                                                                         //}
                                                                         //Log("计算准备", "获取未关闭的机会");
                Dictionary <string, ChanceClass <T> > NoClosedChances = new Dictionary <string, ChanceClass <T> >();
                NoClosedChances      = CloseTheChances(this.IsTestBack); //关闭机会
                this.FinishedThreads = 0;                                //完成数量置0
                RunThreads           = 0;

                Dictionary <string, CalcStragGroupClass <T> > testGrps = Program.AllServiceConfig.AllRunningPlanGrps as Dictionary <string, CalcStragGroupClass <T> >;
                if (!IsTestBack)
                {
                    foreach (string key in testGrps.Keys)
                    {
                        ///修改:分组不用备份,每次策略都要获取前次的状态
                        ///先剔除状态异常的计划,然后再增加新的计划
                        ///CalcStragGroupClass<T> csc = Program.AllServiceConfig.AllRunningPlanGrps[key].Copy();//只用备份,允许窗体启动曾经停止的计划
                        CalcStragGroupClass <T> csc = Program.AllServiceConfig.AllRunningPlanGrps[key] as CalcStragGroupClass <T>;
                        for (int i = csc.UseSPlans.Count - 1; i >= 0; i--)
                        {
                            //修改为检查计划的状态,前台程序修改的是计划表的状态
                            StragRunPlanClass <T> UsePlan = csc.UseSPlans[i];
                            if (!Program.AllServiceConfig.AllRunPlannings.ContainsKey(UsePlan.GUID))
                            {
                                Log("计划表中已注销", UsePlan.Plan_Name);
                                csc.UseSPlans.RemoveAt(i);
                                csc.UseStrags.Remove(UsePlan.PlanStrag.GUID);
                                continue;
                            }
                            StragRunPlanClass <T> spc = Program.AllServiceConfig.AllRunPlannings[UsePlan.GUID] as StragRunPlanClass <T>;
                            if (!spc.Running)
                            {
                                Log("计划未启动", csc.UseSPlans[i].Plan_Name);
                                csc.UseSPlans.RemoveAt(i);
                                csc.UseStrags.Remove(UsePlan.PlanStrag.GUID);
                                continue;
                            }
                            if (!InitServerClass.JudgeInRunTime(currTime, spc))
                            {
                                Log("计划超时", csc.UseSPlans[i].Plan_Name);
                                csc.UseSPlans.RemoveAt(i);
                                csc.UseStrags.Remove(UsePlan.PlanStrag.GUID); //只清除计划,不清除策略?????
                                continue;
                            }
                        }
                    }
                    Dictionary <string, CalcStragGroupClass <TimeSerialData> > allGrps = Program.AllServiceConfig.AllRunningPlanGrps as Dictionary <string, CalcStragGroupClass <TimeSerialData> >;
                    //加入后续启动的计划
                    Program.AllServiceConfig.AllRunningPlanGrps = InitServerClass.InitCalcStrags <TimeSerialData>(DataPoint, ref allGrps, Program.AllServiceConfig.AllStrags as Dictionary <string, BaseStragClass <TimeSerialData> >, Program.AllServiceConfig.AllRunPlannings, Program.AllServiceConfig.AllAssetUnits, false, this.IsTestBack);
                }
                foreach (string key in Program.AllServiceConfig.AllRunningPlanGrps.Keys)//再次为计划组分配资源,保证策略和计划一直在内存。
                {
                    CalcStragGroupClass <T> csc = Program.AllServiceConfig.AllRunningPlanGrps[key] as CalcStragGroupClass <T>;
                    //增加新的计划

                    SettingClass comSetting = new SettingClass();
                    comSetting.SetGlobalSetting(Program.AllServiceConfig.gc);
                    List <ChanceClass <T> > NoClsList = NoClosedChances.Values.ToList();
                    //NoClsList.ForEach(a=>Log(a.ChanceCode,a.ToDetailString()));
                    //Log("未关闭机会数量", NoClosedChances.Count.ToString());
                    //NoClsList.ForEach(a => Log("策略:", a.StragId));
                    if (!csc.initRunningData(comSetting, NoClsList))
                    {
                        Log("为计划组分配通用参数", "配置计划组的通用参数失败,该组计划暂时不参与运算!");
                        continue;
                    }
                    RunThreads++;
                    csc.Finished           = new CalcStragGroupDelegate(CheckFinished);
                    csc.GetNoClosedChances = new ReturnChances <T>(FillNoClosedChances);
                    csc.GetAllStdDevList   = new ReturnStdDevList(FillAllStdDev);
                    //Program.AllServiceConfig.AllRunningPlanGrps[key] = csc;//要加吗?不知道,应该要加
                }
                //分配完未关闭的数据后,将全局内存中所有未关闭机会列表清除
                Program.AllServiceConfig.AllNoClosedChanceList = new Dictionary <string, ChanceClass <T> >() as Dictionary <string, ChanceClass <TimeSerialData> >;
                this.FinishedThreads = 0;
                foreach (string key in Program.AllServiceConfig.AllRunningPlanGrps.Keys)//再次为计划组分配资源,保证策略和计划一直在内存。
                {
                    //Log("计算组执行处理", key, Program.gc.NormalNoticeFlag);
                    CalcStragGroupClass <T> csc = Program.AllServiceConfig.AllRunningPlanGrps[key] as CalcStragGroupClass <T>;
                    //if (!IsTestBack &&  !csc.Running)
                    //    continue;
                    csc.IsBackTest = IsTestBack;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(csc.Run), el);
                }
            }
            catch (Exception ce)
            {
                Log(ce.Message, ce.StackTrace, true);
            }
        }