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); } }
public Dictionary <string, ChanceClass <T> > CloseTheChances(bool IsTestBack, bool ForceCloseAllData = false) { List <ChanceClass <T> > cl = new List <ChanceClass <T> >(); DateTime currTime = DateTime.Now; Dictionary <string, ChanceClass <T> > CloseList = new Dictionary <string, ChanceClass <T> >(); if (IsTestBack)//如果回测,使用内存数据 { cl = Program.AllServiceConfig.AllNoClosedChanceList.Values.Select(a => a).ToList() as List <ChanceClass <T> >; } else//非回测,使用数据库数据ReadDataTableName { //DbChanceList<T> dcl = new PK10ExpectReader().getNoCloseChances<T>(null); DbChanceList <T> dcl = DataReaderBuild.CreateReader(DataPoint.DataType, ReadDataTableName, Codes).getNoCloseChances <T>(null); //必须强制按数据库中存储的策略id,获取到策略的机会类型 foreach (var dc in dcl.Values) { if (!Program.AllServiceConfig.AllStrags.ContainsKey(dc.StragId))//策略未找到数据库中存储的机会所产生的策略 { continue; } if (DataPoint.DataType == "PK10") { cl.Add(dc); } else { BaseStragClass <TimeSerialData> sc = Program.AllServiceConfig.AllStrags[dc.StragId]; Type ct = sc.getTheChanceType(); object tmp = Activator.CreateInstance(ct); if (DataPoint.IsXxY == 1) { (tmp as iXxYClass).AllNums = DataPoint.AllNums; (tmp as iXxYClass).SelectNums = DataPoint.SelectNums; (tmp as iXxYClass).strAllTypeBaseOdds = DataPoint.strAllTypeOdds; (tmp as iXxYClass).strCombinTypeBaseOdds = DataPoint.strCombinTypeOdds; (tmp as iXxYClass).strPermutTypeBaseOdds = DataPoint.strPermutTypeOdds; } Log("转换前机会类型", string.Format("{0}", tmp.GetType())); dc.FillTo(ref tmp, true); //获取所有属性 ChanceClass <T> cc = tmp as ChanceClass <T>; Log("转换后机会类型", string.Format("{0}:{1}:{2}", cc.GetType(), cc.StragId, cc.ChanceCode)); // cc.ToDetailString())); cl.Add(cc); } //cl = dcl.Values.ToList(); } } Dictionary <string, ChanceClass <T> > rl = new Dictionary <string, ChanceClass <T> >(); if (cl.Count > 0) { Log("未关闭机会列表数量为", string.Format("{0}", cl.Count)); } //2019/4/22日出现错过732497,732496两期记录错过,但是732498却收到的情况,同时,正好在732498多次重复策略正好开出结束,因错过2期导致一直未归零, //一直长时间追号开出近60期 //为避免出现这种情况 //判断是否错过了期数,如果错过期数,将所有追踪策略归零,不再追号,也不再执行选号程序, //是否要连续停几期?执行完后,在接收策略里面发现前10期有不连续的情况,直接跳过,只接收数据不执行选号。 this.CurrData.UseType = this.DataPoint; if (this.CurrData.MissExpectCount() > 1) { if (!IsTestBack) //如果非回测,保存交易记录 { DbChanceList <T> dbsavelist = new DbChanceList <T>(); cl.ForEach(p => dbsavelist.Add(p.ChanceIndex, p)); CloseChanceInDBAndExchangeService(dbsavelist, true);//强制关闭,就算命中也不算其盈利 } return(rl); } for (int i = 0; i < cl.Count; i++) { string sGUId = cl[i].StragId; if (sGUId == null || !Program.AllServiceConfig.AllStrags.ContainsKey(sGUId)) //如果策略已注销,立即关闭机会 { if (cl[i].Tracerable) //如果是自我跟踪机会,不理会,让它自己去跟踪 { } else { CloseList.Add(cl[i].GUID, cl[i]); //cl.Remove(cl[i].ChanceIndex); Log("强制结束机会", string.Format("该机会所属策略已经注销,并且该机会是非跟踪机会!{0}", cl[i].ChanceCode)); continue; } } List <string> AllUsePans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Select(t => t.PlanStrag.GUID)).ToList <string>(); if (!AllUsePans.Contains(sGUId))//不在执行的计划内 { if (cl[i].Tracerable) { } else { CloseList.Add(cl[i].GUID, cl[i]); Log("强制结束机会", string.Format("不存在绑定该机会所属策略的计划,并且该机会是非跟踪机会!{0}", cl[i].ChanceCode)); continue; } } //如果策略已经超出时间 List <string> StopedPlans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Where(t => (InitServerClass.JudgeInRunTime(currTime, t) == false))).Select(a => a.GUID).ToList <string>(); if (!IsTestBack) { if (StopedPlans.Contains(sGUId))//停止的计划产生的机会 { if (cl[i].Tracerable) { } else { Log("强制结束机会", string.Format("该机会所属策略超出计划运行时间!{0}", cl[i].ChanceCode)); CloseList.Add(cl[i].GUID, cl[i]); continue; } } } StopedPlans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Where(t => (t.Running == false))).Select(a => a.GUID).ToList <string>(); if (StopedPlans.Contains(sGUId))//停止的计划产生的机会 { if (cl[i].Tracerable) { } else { Log("强制结束机会", string.Format("该机会所属策略的计划状态为停止!{0}", cl[i].ChanceCode)); CloseList.Add(cl[i].GUID, cl[i]); continue; } } //如果策略已经停止 //获得策略 BaseStragClass <T> sc = Program.AllServiceConfig.AllStrags[sGUId] as BaseStragClass <T>; int mcnt = 0; bool Matched = cl[i].Matched(CurrData, out mcnt); cl[i].MatchChips += mcnt; if (sc is ITraceChance)//优先关闭程序跟踪 { cl[i].Closed = (sc as ITraceChance).CheckNeedEndTheChance(cl[i], Matched); } else { cl[i].Closed = Matched; } if (cl[i].Tracerable && Matched)//如果是策略自我跟踪,无论其策略是否是跟踪策略,先关了。 { cl[i].Closed = true; } //////if (cl[i].MaxHoldTimeCnt > 0 && cl[i].HoldTimeCnt == cl[i].MaxHoldTimeCnt) //////{ ////// cl[i].Closed = true; //////} if (cl[i].Closed)//如果已经关闭 { CloseList.Add(cl[i].GUID, cl[i]); continue; } rl.Add(cl[i].GUID, cl[i]); } //Log("结束机会", "所有非法,以及命中并确认需要结束的机会"); if (!IsTestBack) //如果非回测,保存交易记录 { DbChanceList <T> dbsavelist = new DbChanceList <T>(); CloseList.Values.ToList <ChanceClass <T> >().ForEach(p => { if (p != null) { dbsavelist.Add(p.ChanceIndex, p); } }); CloseChanceInDBAndExchangeService(dbsavelist); } return(rl); }
public Dictionary <string, ChanceClass> CloseTheChances(bool IsTestBack) { List <ChanceClass> cl = new List <ChanceClass>(); DateTime currTime = DateTime.Now; Dictionary <string, ChanceClass> CloseList = new Dictionary <string, ChanceClass>(); if (IsTestBack)//如果回测,使用内存数据 { cl = Program.AllServiceConfig.AllNoClosedChanceList.Values.ToList <ChanceClass>(); } else//非回测,使用数据库数据 { DbChanceList dcl = new PK10ExpectReader().getNoCloseChances(null); cl = dcl.Values.ToList <ChanceClass>(); } Dictionary <string, ChanceClass> rl = new Dictionary <string, ChanceClass>(); if (cl.Count > 0) { Log("未关闭机会列表数量为", string.Format("{0}", cl.Count)); } for (int i = 0; i < cl.Count; i++) { string sGUId = cl[i].StragId; if (!Program.AllServiceConfig.AllStrags.ContainsKey(sGUId)) //如果策略已注销,立即关闭机会 { if (cl[i].Tracerable) //如果是自我跟踪机会,不理会,让它自己去跟踪 { } else { CloseList.Add(cl[i].GUID, cl[i]); //cl.Remove(cl[i].ChanceIndex); Log("强制结束机会", string.Format("该机会所属策略已经注销,并且该机会是非跟踪机会!{0}", cl[i].ChanceCode)); continue; } } List <string> AllUsePans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Select(t => t.PlanStrag.GUID)).ToList <string>(); if (!AllUsePans.Contains(sGUId))//不在执行的计划内 { if (cl[i].Tracerable) { } else { CloseList.Add(cl[i].GUID, cl[i]); Log("强制结束机会", string.Format("不存在绑定该机会所属策略的计划,并且该机会是非跟踪机会!{0}", cl[i].ChanceCode)); continue; } } //如果策略已经超出时间 List <string> StopedPlans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Where(t => (InitServerClass.JudgeInRunTime(currTime, t) == false))).Select(a => a.GUID).ToList <string>(); if (!IsTestBack) { if (StopedPlans.Contains(sGUId))//停止的计划产生的机会 { if (cl[i].Tracerable) { } else { Log("强制结束机会", string.Format("该机会所属策略超出计划运行时间!{0}", cl[i].ChanceCode)); CloseList.Add(cl[i].GUID, cl[i]); continue; } } } StopedPlans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Where(t => (t.Running == false))).Select(a => a.GUID).ToList <string>(); if (StopedPlans.Contains(sGUId))//停止的计划产生的机会 { if (cl[i].Tracerable) { } else { Log("强制结束机会", string.Format("该机会所属策略的计划状态为停止!{0}", cl[i].ChanceCode)); CloseList.Add(cl[i].GUID, cl[i]); continue; } } //如果策略已经停止 //获得策略 StragClass sc = Program.AllServiceConfig.AllStrags[sGUId]; int mcnt = 0; bool Matched = cl[i].Matched(CurrDataList, out mcnt); cl[i].MatchChips += mcnt; if (sc is ITraceChance)//优先关闭程序跟踪 { cl[i].Closed = (sc as ITraceChance).CheckNeedEndTheChance(cl[i], Matched); } else { cl[i].Closed = Matched; } if (cl[i].Tracerable && Matched)//如果是策略自我跟踪,无论其策略是否是跟踪策略,先关了。 { cl[i].Closed = true; } //////if (cl[i].MaxHoldTimeCnt > 0 && cl[i].HoldTimeCnt == cl[i].MaxHoldTimeCnt) //////{ ////// cl[i].Closed = true; //////} if (cl[i].Closed)//如果已经关闭 { CloseList.Add(cl[i].GUID, cl[i]); continue; } rl.Add(cl[i].GUID, cl[i]); } //Log("结束机会", "所有非法,以及命中并确认需要结束的机会"); if (!IsTestBack) //如果非回测,保存交易记录 { DbChanceList dbsavelist = new DbChanceList(); CloseList.Values.ToList <ChanceClass>().ForEach(p => dbsavelist.Add(p.ChanceIndex, p)); CloseChanceInDBAndExchangeService(dbsavelist); } return(rl); }