void RefrshStragAndPlan() { try { if (UseSetting == null) { return; } DataTable dt_strag = StragClass.ToTable <StragClass>(UseSetting.AllStrags.Values.ToList <StragClass>()); if (dt_strag != null) { SetDataGridDataTable(dg_StragList, dt_strag); } DataTable dt_plans = StragRunPlanClass.ToTable <StragRunPlanClass>(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass>()); if (dt_plans != null) { SetDataGridDataTable(dg_stragStatus, dt_plans); } DataTable dt_grps = CalcStragGroupClass.ToTable <CalcStragGroupClass>(UseSetting.AllRunningPlanGrps.Values.ToList <CalcStragGroupClass>()); if (dt_grps != null) { SetDataGridDataTable(dg_PlanGrps, dt_grps); } DataTable dt_assetunit = AssetUnitClass.ToTable <AssetUnitClass>(UseSetting.AllAssetUnits.Values.ToList <AssetUnitClass>()); if (dt_assetunit != null) { SetDataGridDataTable(dg_AssetUnits, dt_assetunit); } refresh_AssetChart(); } catch (Exception e) { } }
StragRunPlanClass <TimeSerialData> getPlanAfterMouseUp(MouseEventArgs e) { if (this.dg_stragStatus.SelectedRows.Count <= 0) { return(null); } if (this.dg_stragStatus.SelectedRows[0].Index < 0) { return(null); } int index = this.dg_stragStatus.SelectedRows[0].Index; DataTable dt = this.dg_stragStatus.Tag as DataTable; if (dt == null) { return(null); } if (dt.Rows.Count < index + 1) { return(null); } DataRow dr = dt.Rows[index]; if (dr["GUID"] == null) { return(null); } string pguid = dr["GUID"].ToString(); StragRunPlanClass <TimeSerialData> plan = getServicePlan(pguid); return(plan); }
void refreshGrid(Dictionary <string, StragRunPlanClass> alllist) { List <StragRunPlanClass> list = alllist.Values.ToList <StragRunPlanClass>(); DataTable dt = StragRunPlanClass.ToTable <StragRunPlanClass>(list, true, true); this.dataGridView1.DataSource = dt; this.dataGridView1.Refresh(); }
bool SaveData() { bool suc = GlobalClass.setStragRunningPlan(StragRunPlanClass.getXmlByObjectList <StragRunPlanClass>(Program.AllGlobalSetting.AllRunPlannings.Values.ToList <StragRunPlanClass>())); if (!suc) { MessageBox.Show("保存数据错误,点击确定后将回复较早前的数据!"); refreshGrid(Program.AllGlobalSetting.AllRunPlannings); return(suc); } return(true); }
/// <summary> /// 判断是否超期运行 /// </summary> /// <param name="CurrTime"></param> /// <param name="spc"></param> /// <returns></returns> public static bool JudgeInRunTime(DateTime CurrTime, StragRunPlanClass spc) { string strToday = CurrTime.ToShortDateString(); DateTime setBegTime = DateTime.Parse(string.Format("{0} {1}", strToday, spc.DailyStartTime)); DateTime setEndTime = DateTime.Parse(string.Format("{0} {1}", strToday, spc.DailyEndTime)); if (CurrTime < setBegTime || CurrTime > setEndTime) { //ToLog("策略超出执行时间", spc.ToString()); return(false); } return(true); }
private void btn_Add_Click(object sender, EventArgs e) { NewAPlan = true; StragRunPlanClass spc = new StragRunPlanClass();// //spc.GUID = "请选择需要配置的策略"; spc.AutoRunning = true; spc.DailyStartTime = "09:07:00"; spc.DailyEndTime = "23:59:58"; spc.ExpiredTime = DateTime.Parse("2049/12/31"); spc.Creator = "群主"; spc.CreateTime = DateTime.Now; this.propertyGrid1.SelectedObject = spc; }
private void tmi_Edit_Click(object sender, EventArgs e) { try { ToolStripMenuItem menu = sender as ToolStripMenuItem; DataGridView dg = (menu.Owner as ContextMenuStrip).SourceControl as DataGridView; if (dg == null) { return; } if (dg.Equals(this.dg_StragList)) { StragClass strag = getGridAfterMouseUp <StragClass>(dg, null) as StragClass; if (strag == null) { MessageBox.Show("策略为空"); return; } if (MessageBox.Show(string.Format("确定要修改策略:{0}", strag.StragScript), "修改策略", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { return; } frm_StragManager frm = new frm_StragManager(); frm.SpecList = UseSetting.AllStrags as Dictionary <string, BaseStragClass <TimeSerialData> >; frm.SpecObject = strag; frm.Show(); } else if (dg.Equals(this.dg_stragStatus)) { StragRunPlanClass <T> strag = getGridAfterMouseUp <StragRunPlanClass <T> >(dg, null) as StragRunPlanClass <T>; if (strag == null) { MessageBox.Show("计划为空"); return; } if (MessageBox.Show(string.Format("确定要修改计划:{0}", strag.StragName), "修改计划", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { return; } frm_StragPlanSetting frm = new frm_StragPlanSetting(); frm.SpecList = UseSetting.AllRunPlannings as Dictionary <string, StragRunPlanClass <TimeSerialData> >; frm.SpecObject = strag as StragRunPlanClass <TimeSerialData>; frm.Show(); } } catch (Exception ce) { MessageBox.Show(string.Format("{0}:{1}", ce.Message, ce.StackTrace)); } }
/// <summary> /// 判断是否超期运行 /// </summary> /// <param name="CurrTime"></param> /// <param name="spc"></param> /// <returns></returns></T> public static bool JudgeInRunTime <T>(DateTime CurrTime, StragRunPlanClass <T> spc) where T : TimeSerialData { int diffHours = GlobalClass.TypeDataPoints.First().Value.DiffHours; int diffMinutes = GlobalClass.TypeDataPoints.First().Value.DiffMinutes; DateTime cprTime = CurrTime.AddHours(diffHours).AddMinutes(diffMinutes); string strToday = CurrTime.AddHours(diffHours).ToShortDateString(); DateTime setBegTime = DateTime.Parse(string.Format("{0} {1}", strToday, spc.DailyStartTime)); DateTime setEndTime = DateTime.Parse(string.Format("{0} {1}", strToday, spc.DailyEndTime)); if (cprTime < setBegTime || cprTime > setEndTime) { //ToLog("策略超出执行时间", spc.ToString()); return(false); } return(true); }
bool SetPlanStatus(bool Start, EventArgs e) { StragRunPlanClass <TimeSerialData> plan = getPlanAfterMouseUp(e as MouseEventArgs); if (plan == null) { return(false); } if (!Start) { if (plan.PlanStrag is ITraceChance) { if (MessageBox.Show("该计划执行的策略为策略级跟踪策略,选择出的组合无法自我跟踪,当停止执行该计划后,该计划所选出来的组合在下期将全部关闭,如果跟踪中途产生的浮动亏损将全部实现!确定要继续停止执行该计划?", "继续停止执行计划", MessageBoxButtons.YesNo) == DialogResult.No) { return(false); } } else { if (MessageBox.Show("该计划执行的策略为组合级跟踪策略,选择出的组合将自我跟踪,直至原有停止逻辑满足,当停止执行该计划后,只会停止寻找新的组合,已选组合将继续运行!确定要继续停止执行该计划?", "继续停止执行计划", MessageBoxButtons.YesNo) == DialogResult.No) { return(false); } } } try { bool ret = UseSetting.SetPlanStatus(plan.GUID, Start); if (ret == true) { DataTable dt_plans = StragRunPlanClass <T> .ToTable <StragRunPlanClass <TimeSerialData> >(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass <TimeSerialData> >()); if (dt_plans == null) { return(ret); } SetDataGridDataTable(dg_stragStatus, dt_plans); } return(ret); } catch (Exception ce) { string msg = ce.Message; } return(false); }
private void dg_stragStatus_MouseUp(object sender, MouseEventArgs e) { DisableAllMenus(); StragRunPlanClass <T> strag = getGridAfterMouseUp <StragRunPlanClass <T> >(this.dg_stragStatus, e) as StragRunPlanClass <T>; ////this.tmi_StartPlan.Enabled = false; ////this.tmi_StopPlan.Enabled = false; ////this.tmi_Edit.Enabled = false; tmi_refreshPlans.Enabled = true; if (strag == null) { return; } if (e.Button == System.Windows.Forms.MouseButtons.Right) { this.tmi_Edit.Enabled = true; this.tmi_StopPlan.Enabled = strag.Running; this.tmi_StartPlan.Enabled = !strag.Running; } }
public static Dictionary <string, StragRunPlanClass <T> > Init_StragPlans <T>() where T : TimeSerialData { string stragList = GlobalClass.getStragRunningPlan(true); Dictionary <string, StragRunPlanClass <T> > AllRunPlans = new Dictionary <string, StragRunPlanClass <T> >(); if (stragList == null || stragList.Trim().Length == 0) { ToLog("策略运行计划列表", "为空!"); return(AllRunPlans); } List <StragRunPlanClass <T> > list = StragRunPlanClass <T> .getObjectListByXml <StragRunPlanClass <T> >(stragList); if (list == null) { return(AllRunPlans); } AllRunPlans = list.ToDictionary(t => t.GUID, v => v); ToLog("策略列表", AllRunPlans.Count.ToString()); return(AllRunPlans); }
private void btn_delete_Click(object sender, EventArgs e) { StragRunPlanClass srp = this.propertyGrid1.SelectedObject as StragRunPlanClass; if (NewAPlan || srp == null) { MessageBox.Show("请先双击列表选择需要删除计划!"); return; } if (!Program.AllGlobalSetting.AllRunPlannings.ContainsKey(srp.GUID)) { MessageBox.Show("计划已不存在,点击确定后将更新视图"); refreshGrid(Program.AllGlobalSetting.AllRunPlannings); return; } Program.AllGlobalSetting.AllRunPlannings.Remove(srp.GUID); if (!SaveData()) { return; } refreshGrid(Program.AllGlobalSetting.AllRunPlannings); MessageBox.Show("成功删除!"); }
private void btn_Save_Click(object sender, EventArgs e) { StragRunPlanClass srp = this.propertyGrid1.SelectedObject as StragRunPlanClass; if (srp == null) { MessageBox.Show("请先双击列表选择需要修改的计划或点击新增按钮新建计划!"); return; } if (Program.AllGlobalSetting.AllStrags.ContainsKey(srp.GUID)) { StragClass sc = Program.AllGlobalSetting.AllStrags[srp.GUID]; ////srp.StragName = sc.StragClassName; ////srp.StragDescript = sc.StragScript; } if (!Program.AllGlobalSetting.AllRunPlannings.ContainsKey(srp.GUID)) { Program.AllGlobalSetting.AllRunPlannings.Add(srp.GUID, srp); } else { if (NewAPlan) { MessageBox.Show("该计划已新建,标志错误!"); return; } Program.AllGlobalSetting.AllRunPlannings[srp.GUID] = srp; } if (!SaveData()) { return; } refreshGrid(Program.AllGlobalSetting.AllRunPlannings); this.propertyGrid1.SelectedObject = null; this.propertyGrid1.Refresh(); MessageBox.Show("保存成功!"); }
private void tsmi_refreshPlans_Click(object sender, EventArgs e) { ToolStripMenuItem menu = sender as ToolStripMenuItem; try { // menu.Owner DataGridView dg = (menu.Owner as ContextMenuStrip).SourceControl as DataGridView; if (dg == null) { return; } if (UseSetting == null) { return; } DataTable dt = null; if (dg.Equals(this.dg_stragStatus)) { dt = StragRunPlanClass.ToTable <StragRunPlanClass>(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass>()); } else if (dg.Equals(this.dg_StragList)) { dt = StragClass.ToTable <StragClass>(UseSetting.AllStrags.Values.ToList <StragClass>()); } if (dt == null) { return; } SetDataGridDataTable(dg, dt); } catch (Exception ce) { } }
/// <summary> /// 初始化计划,将数据类型和运算类型相同的计划分组 /// </summary> public static Dictionary <string, CalcStragGroupClass <T> > InitCalcStrags <T>(DataTypePoint dpt, ref Dictionary <string, CalcStragGroupClass <T> > AllStatusStrags, Dictionary <string, BaseStragClass <T> > AllStrags, Dictionary <string, StragRunPlanClass <T> > list, Dictionary <string, AssetUnitClass> AssetUnits, bool StartTheAuto, bool IsBackTest) where T : TimeSerialData { if (AllStatusStrags == null) { AllStatusStrags = new Dictionary <string, CalcStragGroupClass <T> >(); } foreach (string key in list.Keys) //按相同类型+视图分类,相同类分在一组 { StragRunPlanClass <T> spc = list[key]; if (AllStatusStrags.SelectMany(t => t.Value.UseSPlans.Select(a => a.GUID)).Contains(key))//支持后续加入计划,只要状态合适都可以加入 { continue; } if (!AllStrags.ContainsKey(spc.PlanStrag.GUID)) { ToLog(string.Format("计划{0}非法", spc.PlanStrag.StragScript), "计划所对应策略已注销"); continue; } BaseStragClass <T> sc = AllStrags[spc.PlanStrag.GUID]; spc.PlanStrag = sc; if (spc.AssetUnitInfo != null)//分配资产单元号 { sc.AssetUnitId = spc.AssetUnitInfo.UnitId; } ///更新为首先按数据源分类,然后按类和视图分类 //string strModel = "Type_{0}_ViewBySerial_{1}";//按类和数据视图分类 //string strKey = string.Format(strModel, sc.GetType().Name, sc.BySer); string strModel = "DS_{2}Type_{0}_ViewBySerial_{1}";//更新为首先按数据源分类,然后按类和视图分类 string strKey = string.Format(strModel, sc.GetType().Name, sc.BySer, spc.UseDataSource); if (!IsBackTest) { if (spc.ExpiredTime < DateTime.Now) { ToLog("计划时间不匹配", "超过计划过期时间"); continue; } if (!JudgeInRunTime(DateTime.Now, spc))//没在指定运行时间内,跳过 { ToLog("计划时间不匹配", "未在指定时间区间内"); continue; } } if (StartTheAuto)//如果第一次运行,将此标志设为真,将自动启动策略 { spc.Running = spc.AutoRunning; } if (!spc.Running && !IsBackTest) { continue; } if (spc.UseDataSource != dpt.DataType && !IsBackTest) { ToLog("计划不属于使用的数据源", strKey); continue; } CalcStragGroupClass <T> csg = null; if (!AllStatusStrags.ContainsKey(strKey) || IsBackTest) { if (GlobalClass.TypeDataPoints.ContainsKey(spc.UseDataSource) || IsBackTest)//如果计划不属于数据源,不加载 { csg = new CalcStragGroupClass <T>(GlobalClass.TypeDataPoints[spc.UseDataSource]); if (!AllStatusStrags.ContainsKey(strKey)) { AllStatusStrags.Add(strKey, csg); } } } if (!AllStatusStrags.ContainsKey(strKey)) { ToLog("计划未登记!", strKey); continue; } csg = AllStatusStrags[strKey]; if (spc.AssetUnitInfo != null)//必须加资产单元信息 { string uid = spc.AssetUnitInfo.UnitId; if (AssetUnits.ContainsKey(uid)) { if (!csg.UseAssetUnits.ContainsKey(uid)) { csg.UseAssetUnits.Add(uid, AssetUnits[uid]); //注意,一定是要用全局设置的单元资产,才有动态的金额等信息 } } else { ToLog("计划资产单元不匹配", "计划所属资产单元不存在"); } } //csg = AllStatusStrags[strKey]; csg.UseSPlans.Add(spc); ToLog("加入计划", spc.StragDescript); csg.UseStrags.Add(sc.GUID, sc); csg.UseSerial = sc.BySer; //ToLog("初始化待计算的计划", string.Format("准备运行的计划数为:{0};", AllStatusStrags.Count)); } return(AllStatusStrags); }
void RefrshStragAndPlan(bool ForceRefresh = false) { try { if (UseSetting == null) { return; } DataTable dt_strag = BaseStragClass <TimeSerialData> .ToTable <BaseStragClass <TimeSerialData> >(UseSetting.AllStrags.Values.ToList <BaseStragClass <TimeSerialData> >()); if (dt_strag != null) { SetDataGridDataTable(dg_StragList, dt_strag); } DataTable dt_plans = StragRunPlanClass <TimeSerialData> .ToTable <StragRunPlanClass <TimeSerialData> >(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass <TimeSerialData> >()); if (dt_plans != null) { SetDataGridDataTable(dg_stragStatus, dt_plans); } DataTable dt_grps = CalcStragGroupClass <TimeSerialData> .ToTable <CalcStragGroupClass <TimeSerialData> >(UseSetting.AllRunningPlanGrps.Values.ToList <CalcStragGroupClass <TimeSerialData> >()); if (dt_grps != null) { SetDataGridDataTable(dg_PlanGrps, dt_grps); } DataTable dt_assetunit = AssetUnitClass.ToTable <AssetUnitClass>(UseSetting.AllAssetUnits.Values.ToList <AssetUnitClass>()); if (dt_assetunit != null) { SetDataGridDataTable(dg_AssetUnits, dt_assetunit); } DataTable dt_exchange = null; foreach (AssetUnitClass auc in UseSetting.AllAssetUnits.Values) { ExchangeService ed = auc.getCurrExchangeServer(); if (ed == null) { //MessageBox.Show(string.Format("{0}资产单元交易服务为空!", auc.UnitName)); continue; } DataTable assetdt = ed.ExchangeDetail; if (assetdt == null) { //MessageBox.Show(string.Format("{0}资产单元交易记录为空!", auc.UnitName)); continue; } //MessageBox.Show(string.Format("{0}有新交易记录{1}条!列数:{2}", auc.UnitName, assetdt.Rows.Count,assetdt.Columns.Count)); if (assetdt != null) { if (dt_exchange == null) { dt_exchange = assetdt.Clone(); } for (int i = 0; i < assetdt.Rows.Count; i++) { dt_exchange.Rows.Add(assetdt.Rows[i].ItemArray); } } } //MessageBox.Show(dt_exchange.Rows.Count.ToString()); if (dt_exchange != null) { SetDataGridDataTable(dg_ExchangeList, dt_exchange, "createtime desc"); } if (UseSetting.AllAssetUnits != null) { lock (UseSetting.AllAssetUnits) { refresh_AssetChart(); } } } catch (Exception e) { string msg = string.Format("{0}:{1}", e.Message, e.StackTrace); if (ForceRefresh) { MessageBox.Show(msg); } } }
/// <summary> /// 初始化计划,将数据类型和运算类型相同的计划分组 /// </summary> public static Dictionary <string, CalcStragGroupClass> InitCalcStrags(ref Dictionary <string, CalcStragGroupClass> AllStatusStrags, Dictionary <string, StragClass> AllStrags, Dictionary <string, StragRunPlanClass> list, Dictionary <string, AssetUnitClass> AssetUnits, bool StartTheAuto, bool IsBackTest) { if (AllStatusStrags == null) { AllStatusStrags = new Dictionary <string, CalcStragGroupClass>(); } foreach (string key in list.Keys) //按相同类型+视图分类,相同类分在一组 { StragRunPlanClass spc = list[key]; if (AllStatusStrags.SelectMany(t => t.Value.UseSPlans.Select(a => a.GUID)).Contains(key))//支持后续加入计划,只要状态合适都可以加入 { continue; } if (!AllStrags.ContainsKey(spc.PlanStrag.GUID)) { ToLog("计划非法", "计划所对应策略已注销"); continue; } StragClass sc = AllStrags[spc.PlanStrag.GUID]; spc.PlanStrag = sc; if (spc.AssetUnitInfo != null)//分配资产单元号 { sc.AssetUnitId = spc.AssetUnitInfo.UnitId; } string strModel = "Type_{0}_ViewBySerial_{1}";//按类和数据视图分类 string strKey = string.Format(strModel, sc.GetType().Name, sc.BySer); if (!IsBackTest) { if (spc.ExpiredTime < DateTime.Now) { ToLog("计划时间不匹配", "超过计划过期时间"); continue; } if (!JudgeInRunTime(DateTime.Now, spc))//没在指定运行时间内,跳过 { ToLog("计划时间不匹配", "未在指定时间区间内"); continue; } } if (StartTheAuto)//如果第一次运行,将此标志设为真,将自动启动策略 { spc.Running = spc.AutoRunning; } if (!spc.Running) { continue; } CalcStragGroupClass csg = null; if (!AllStatusStrags.ContainsKey(strKey)) { csg = new CalcStragGroupClass(); AllStatusStrags.Add(strKey, csg); } csg = AllStatusStrags[strKey]; if (spc.AssetUnitInfo != null) { string uid = spc.AssetUnitInfo.UnitId; if (AssetUnits.ContainsKey(uid)) { if (!csg.UseAssetUnits.ContainsKey(uid)) { csg.UseAssetUnits.Add(uid, AssetUnits[uid]); //注意,一定是要用全局设置的单元资产,才有动态的金额等信息 } } else { ToLog("计划资产单元不匹配", "计划所属资产单元不存在"); } } //csg = AllStatusStrags[strKey]; csg.UseSPlans.Add(spc); ToLog("加入计划", spc.StragDescript); csg.UseStrags.Add(sc.GUID, sc); csg.UseSerial = sc.BySer; //ToLog("初始化待计算的计划", string.Format("准备运行的计划数为:{0};", AllStatusStrags.Count)); } return(AllStatusStrags); }
private void tsmi_refreshPlans_Click(object sender, EventArgs e) { ToolStripMenuItem menu = sender as ToolStripMenuItem; string sort = null; try { // menu.Owner DataGridView dg = (menu.Owner as ContextMenuStrip).SourceControl as DataGridView; if (dg == null) { return; } if (UseSetting == null) { return; } DataTable dt = null; if (dg.Equals(this.dg_stragStatus)) { dt = StragRunPlanClass <TimeSerialData> .ToTable <StragRunPlanClass <TimeSerialData> >(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass <TimeSerialData> >()); } else if (dg.Equals(this.dg_NoCloseChances)) { //DbChanceList<T> dc = new PK10ExpectReader().getNoCloseChances<T>(null); DbChanceList <T> dc = exread.getNoCloseChances <T>(null); if (dc == null) { return; } dt = dc.Table; } else if (dg.Equals(this.dg_CloseChances)) { //DbChanceList<T> dc = new PK10ExpectReader().getNoCloseChances<T>(null); DbChanceList <T> dc = exread.getClosedChances <T>(null, 2); if (dc == null) { return; } dt = dc.Table; sort = "chanceindex desc"; } else if (dg.Equals(this.dg_baseData)) { RefreshPK10Data(); } else if (dg.Equals(this.dg_ExchangeList)) { RefrshStragAndPlan(true); } else if (dg.Equals(this.dg_LoginList)) { RefreshLogData(true); } else if (dg.Equals(this.dg_StragList)) { dt = BaseStragClass <TimeSerialData> .ToTable <BaseStragClass <TimeSerialData> >(UseSetting.AllStrags.Values.ToList <BaseStragClass <TimeSerialData> >()); } if (dt == null) { return; } SetDataGridDataTable(dg, dt, sort); } catch (Exception ce) { string msg = ce.Message; } }
public BackTestReturnClass <T> VirExchange_oldLogic(ExchangeService es, StragRunPlanClass <T>[] teststragplans) { long begNo = BegExpect; ExpectReader er = new ExpectReader(); 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> >(); long 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 <T> > NewExchangeRecord = new Dictionary <Int64, ExchangeChance <T> >(); int AllCnt = 0; while (el == null || el.Count > 0) //如果取到的数据长度大于0 { 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); 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]); } for (int i = 0; i < teststrags.Length; i++)//专门针对需要程序话关闭机会,且关闭时需要知道当前数据策略使用 { teststrags[i].SetLastUserData(testData); } tmpChances = new Dictionary <string, StragChance <T> >(); //关闭所有交易 foreach (int id in NewExchangeRecord.Keys) { ExchangeChance <T> ec = NewExchangeRecord[id]; int matchcnt = 0; ec.OwnerChance.Matched(testData.LastData, out matchcnt, false); ec.MatchChips = matchcnt; es.Update(ec); ec = null; } NewExchangeRecord = new Dictionary <Int64, ExchangeChance <T> >(); foreach (string key in NoCloseChances.Keys) { StragChance <T> scc = NoCloseChances[key]; ChanceClass <T> cc = scc.Chance; if (cc.Closed == false) { int matchcnt = 0; //////if (teststrag.GetRev)//如果求相反组合 //////{ ////// if (cc.Matched(testData.LastData, out matchcnt, true))//不关闭 ////// { ////// if (cc.HoldTimeCnt < 0) ////// { ////// cc.HoldTimeCnt = (int)(testData.LastData.ExpectIndex - cc.InputExpect.ExpectIndex); ////// } ////// } //////} bool Matched = cc.Matched(testData.LastData, out matchcnt, false); if (cc.NeedConditionEnd) //需要策略自定义条件结束 { cc.MatchChips += matchcnt; if (Matched) //匹配到了 { int LastMatchId = cc.LastMatchTimesId; //最后一次匹配次序号 int maxHoldCnt = cc.AllowMaxHoldTimeCnt; if (cc.HoldTimeCnt - cc.LastMatchTimesId > maxHoldCnt) { cc.AllowMaxHoldTimeCnt = cc.HoldTimeCnt - cc.LastMatchTimesId; } cc.LastMatchTimesId = cc.HoldTimeCnt; } if (CurrExpectClose == 1)//如果当期已关闭,后面所有机会均关闭 { cc.Closed = true; } else if (CurrExpectClose == -1) { cc.Closed = Matched; } else { cc.Closed = cc.OnCheckTheChance(cc, Matched); if (teststrags[0].StagSetting.IsLongTermCalc) //如果是长期计算,设置当期是否关闭 { if (!Matched && cc.Closed) //匹配和状态相背,一定是状态已关闭 { CurrExpectClose = 1; } if (!Matched && !cc.Closed)//第一次非匹配状态能判断出当期是否关闭 { CurrExpectClose = -1; } } } if (cc.Closed) { cc.EndExpectNo = testData.LastData.Expect; cc.UpdateTime = testData.LastData.OpenTime; } else { cc.HoldTimeCnt++; tmpChances.Add(key, scc); } } else { if (Matched || cc.HoldTimeCnt == cc.AllowMaxHoldTimeCnt)//关闭 { cc.Closed = true; cc.EndExpectNo = testData.LastData.Expect; cc.MatchChips = matchcnt; //////if (!teststrag.GetRev)//只有不求相反值的情况下,才赋持有是次数 //////{ cc.HoldTimeCnt = (int)(testData.LastData.ExpectIndex - cc.InputExpect.ExpectIndex); //////} //////else //////{ ////// if (cc.HoldTimeCnt < 0) ////// { ////// cc.HoldTimeCnt = 999; ////// } //////} cc.UpdateTime = testData.LastData.OpenTime; ret.ChanceList.Add(cc); } else { cc.HoldTimeCnt++; tmpChances.Add(key, scc); } } #region //////////if (cc.Closed) //////////{ ////////// int HCnt = 1; ////////// if (cc.NeedConditionEnd) ////////// { ////////// if (ret.HoldCntDic.ContainsKey(cc.MaxHoldTimeCnt)) ////////// { ////////// HCnt = ret.HoldCntDic[cc.MaxHoldTimeCnt]; ////////// HCnt++; ////////// ret.HoldCntDic[cc.MaxHoldTimeCnt] = HCnt; ////////// ret.HoldWinCntDic[cc.MaxHoldTimeCnt] = ret.HoldWinCntDic[cc.MaxHoldTimeCnt] + matchcnt; ////////// ret.InChipsDic[cc.MaxHoldTimeCnt] = ret.InChipsDic[cc.MaxHoldTimeCnt] + cc.ChipCount * cc.HoldTimeCnt; ////////// ret.WinChipsDic[cc.MaxHoldTimeCnt] = ret.WinChipsDic[cc.MaxHoldTimeCnt] + cc.MatchChips; ////////// } ////////// else ////////// { ////////// ret.HoldCntDic.Add(cc.MaxHoldTimeCnt, 1); ////////// ret.HoldWinCntDic.Add(cc.MaxHoldTimeCnt, matchcnt); ////////// ret.InChipsDic.Add(cc.MaxHoldTimeCnt, cc.ChipCount * cc.HoldTimeCnt); ////////// ret.WinChipsDic.Add(cc.MaxHoldTimeCnt, cc.MatchChips); ////////// } ////////// } ////////// else ////////// { ////////// if (ret.HoldCntDic.ContainsKey(cc.HoldTimeCnt)) ////////// { ////////// HCnt = ret.HoldCntDic[cc.HoldTimeCnt]; ////////// HCnt++; ////////// ret.HoldCntDic[cc.HoldTimeCnt] = HCnt; ////////// ret.HoldWinCntDic[cc.HoldTimeCnt] = ret.HoldWinCntDic[cc.HoldTimeCnt] + matchcnt; ////////// ret.InChipsDic[cc.HoldTimeCnt] = ret.InChipsDic[cc.HoldTimeCnt] + cc.ChipCount; ////////// ret.WinChipsDic[cc.HoldTimeCnt] = ret.WinChipsDic[cc.HoldTimeCnt] + cc.MatchChips; ////////// } ////////// else ////////// { ////////// ret.HoldCntDic.Add(cc.HoldTimeCnt, 1); ////////// ret.HoldWinCntDic.Add(cc.HoldTimeCnt, matchcnt); ////////// ret.InChipsDic.Add(cc.HoldTimeCnt, cc.ChipCount); ////////// ret.WinChipsDic.Add(cc.HoldTimeCnt, cc.MatchChips); ////////// } ////////// } //////////} #endregion } } List <StragChance <T> > cs = new List <StragChance <T> >(); for (int i = 0; i < teststrags.Length; i++) { BaseCollection <T> sc = new ExpectListProcessBuilder <T>(dtp, testData).getProcess().getSerialData(teststrags[i].ReviewExpectCnt, teststrags[i].BySer); if (testData.Count == 0) { break; } List <ChanceClass <T> > scs = teststrags[i].getChances(sc, testData.LastData);//获取所有机会 for (int j = 0; j < scs.Count; j++) { ChanceClass <T> CurrCc = scs[j]; ////scs[j].IncrementType = teststragplans[i].IncreamType; ////scs[j].FixAmt = teststragplans[i].FixAmt; ////scs[j].FixRate = teststragplans[i].FixRate; StragRunPlanClass <T> currPlan = teststragplans[i]; BaseStragClass <T> currStrag = currPlan.PlanStrag; CurrCc.HoldTimeCnt = 1; CurrCc.Cost = CurrCc.ChipCount * CurrCc.UnitCost; CurrCc.Gained = 0; CurrCc.Profit = 0; CurrCc.ExecDate = DateTime.Today; CurrCc.CreateTime = el.LastData.OpenTime; CurrCc.UpdateTime = CurrCc.CreateTime; CurrCc.StragId = currStrag.GUID; CurrCc.ExpectCode = el.LastData.Expect; CurrCc.AllowMaxHoldTimeCnt = currPlan.AllowMaxHoldTimeCnt; CurrCc.FixAmt = currPlan.FixAmt; CurrCc.FixRate = currPlan.FixRate; CurrCc.IncrementType = currPlan.IncreamType; cs.Add(new StragChance <T>(teststrags[i], CurrCc)); } } if (ret.ChanceList == null) { ret.ChanceList = new List <ChanceClass <T> >(); } //ret.ChanceList.AddRange(cs); NoCloseChances = new Dictionary <string, StragChance <T> >(); foreach (string key in tmpChances.Keys) { StragChance <T> scc = tmpChances[key]; ChanceClass <T> cc = scc.Chance; NoCloseChances.Add(key, scc); //////ProbWaveSelectStragClass组合改为统一交易 ////if ((scc.Strag is ProbWaveSelectStragClass) == false) ////{ //// ExchangeChance<T> ec = new ExchangeChance(scc.Strag, testData.LastData.Expect, cc); //// bool Suc = es.Push(ref ec); //// if (Suc) //// NewExchangeRecord.Add(ec.Id, ec); ////} } tmpChances = null; //如果设置了最大持仓,必须按照最大持仓限制下注。 for (int i = 0; i < Math.Min(cs.Count, teststrags[0].CommSetting.MaxHoldingCnt - NoCloseChances.Count); i++) { //string key = string.Format("{0}_{1}", cs[i].SignExpectNo, cs[i].ChanceCode); string key = string.Format("{0}", cs[i].Chance.ChanceCode); if (NoCloseChances.ContainsKey(key)) { //////if (teststrag.AllowRepeat) //////{ ////// string test = key; ////// //NoCloseChances.Add(key, cs[i]); //////} } else { cs[i].Chance.BaseAmount = es.summary < es.InitCash?1:es.summary / es.InitCash; NoCloseChances.Add(key, cs[i]); ////////ProbWaveSelectStragClass组合改为统一交易 //////if ((cs[i].Strag is ProbWaveSelectStragClass)==false) //////{ ////// ExchangeChance<T> ec = new ExchangeChance(cs[i].Strag, testData.LastData.Expect, cs[i].Chance);//交易 ////// bool Suc = es.Push(ref ec); ////// if (Suc) ////// NewExchangeRecord.Add(ec.Id, ec); //////} } } //if ((cs[0].Strag is ProbWaveSelectStragClass) == false) //{ foreach (string key in NoCloseChances.Keys) { ExchangeChance <T> ec = new ExchangeChance <T>(es, NoCloseChances[key].Strag, NoCloseChances[key].Chance.ExpectCode, testData.LastData.Expect, NoCloseChances[key].Chance); //交易 if (ec.OccurStrag is ProbWaveSelectStragClass) //对于ProbWaveSelectStragClass,一开始就计算好了Amount { ProbWaveSelectStragClass strag = ec.OccurStrag as ProbWaveSelectStragClass; if (!strag.UseAmountList().ContainsKey(testData.LastData.Expect)) { Int64 AllAmt = (ec.OccurStrag as BaseObjectsLib.ISpecAmount).getChipAmount(es.summary, ec.OwnerChance, ec.OccurStrag.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials); Int64 ChipAmt = (Int64)Math.Floor((double)AllAmt / NoCloseChances.Count); ec.ExchangeAmount = ChipAmt; ec.ExchangeRate = ChipAmt / es.summary; if (!strag.UseAmountList().ContainsKey(testData.LastData.Expect)) { strag.UseAmountList().Add(testData.LastData.Expect, ChipAmt); } } else { ec.ExchangeAmount = strag.UseAmountList()[testData.LastData.Expect]; } } bool Suc = es.Push(ref ec); if (Suc) { NewExchangeRecord.Add(ec.Id, ec); } } //} testIndex++; } } return(ret); }
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); } }