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); }
private void timer_Tip_Tick(object sender, EventArgs e) { try { DataTable dt = btc.SystemStdDevs; if (dt.Rows.Count > 0) { string strExpect = dt.Rows[dt.Rows.Count - 1]["Expect"].ToString(); Int64 MinExpect = Int64.Parse(strExpect) - 180; string sql = string.Format("Expect>={0}", MinExpect); DataView dv_stddev = new DataView(dt); dv_stddev.RowFilter = sql; if (this.chart_ForSystemStdDev.Series.Count < dt.Columns.Count - 1) { this.chart_ForSystemStdDev.Series.Clear(); for (int i = 1; i < dt.Columns.Count; i++) { string strName = dt.Columns[i].ColumnName; Series sr = new Series(strName); sr.ChartType = SeriesChartType.Line; if (strName == "StdDev" || strName == "StdMa20" || strName == "StdMa5") { sr.BorderWidth = 3; } //sr.IsValueShownAsLabel = true; this.chart_ForSystemStdDev.Series.Add(sr); } this.chart_ForSystemStdDev.GetToolTipText += new EventHandler <ToolTipEventArgs>(chart_ForSystemStdDev_GetToolTipText); } for (int i = 1; i < dt.Columns.Count; i++) { this.chart_ForSystemStdDev.Series[i - 1].Points.DataBindXY(dv_stddev, dt.Columns[0].ColumnName, dv_stddev, dt.Columns[i].ColumnName); } //this.chart_ForSystemStdDev.DataSource = dv_stddev; //this.chart_ForSystemStdDev.Series[0].Points.DataBindXY(dv_stddev, "Expect", dv_stddev, "StdDev"); //this.chart_ForSystemStdDev.Series[1].Points.DataBindXY(dv_stddev, "Expect", dv_stddev, "StdMa20"); //this.chart_ForSystemStdDev.Series[2].Points.DataBindXY(dv_stddev, "Expect", dv_stddev, "StdMa5"); this.chart_ForSystemStdDev.ChartAreas[0].AxisY.Maximum = 0.6; this.chart_ForSystemStdDev.ChartAreas[0].AxisY.Minimum = 0.1; this.chart_ForSystemStdDev.Show(); this.dg_forStdDev.DataSource = dv_stddev; this.dg_forStdDev.Tag = dt; this.dg_forStdDev.Refresh(); } lock (es.MoneyIncreamLine) { this.toolStripStatusLabel1.Text = string.Format("第{2}次,{0}/{1}", es.CurrIndex.ToString(), es.ExpectCnt, btc.testIndex); this.toolStripStatusLabel2.Text = string.Format("{0}% 最大值:[{1}%] 最小值:[{2}%] ", es.GainedRate.ToString(), es.MaxRate, es.MinRate); DataView moneyLines = new DataView(es.MoneyIncreamLine); if (this.chart1.Series.Count == 0) { Series ss = new Series(); ss.ChartType = SeriesChartType.Line; this.chart1.Series.Add(ss); } this.chart1.Series[0].Points.DataBindXY(moneyLines, "id", moneyLines, "val"); } lock (es.ExchangeDetail) { DataView vExchangeDetail = new DataView(es.ExchangeDetail); this.dataGridView_ExchangeDetail.DataSource = vExchangeDetail; this.dataGridView_ExchangeDetail.Refresh(); if (SCList.Count > 0) { if (SCList[0].PlanStrag.StagSetting.BaseType.traceType == TraceType.WaveTrace) { ProbWaveSelectStragClass pss = SCList[0].PlanStrag as ProbWaveSelectStragClass; if (pss.BaseWaves().Tables.Count == 0) { return; } GuideResult gs = pss.BaseWaves().Tables[0] as GuideResult; GuideResultSet gss = pss.GuideWaves(); Int32 rcnt = gs.Rows.Count; if (!checkBox_UseBuffRsult.Checked)//如果不用本地数据 { if (rcnt > 0 && (rcnt % 100) < 20 && rcnt > lastSaveCnt + 50) { //保存一次数据 retData = gs; lastSaveCnt = rcnt; new Thread(SaveWaveTable).Start(); } } DataView dvWv = new DataView(gs); //this.chart_ForProb.ChartAreas[0].AxisY.MaximumAutoSize = false; this.chart_ForProb.ChartAreas[0].AxisY.Maximum = gs.High; this.chart_ForProb.ChartAreas[0].AxisY.Minimum = gs.Low; this.chart_ForProb.Series[0].Points.DataBindXY(dvWv, "Id", dvWv, "val"); if (this.chart_ForProb.Series.Count < gss.Tables.Count + 1) { for (int i = this.chart_ForProb.Series.Count; i <= gss.Tables.Count; i++) { Series si = new Series(string.Format("指标{0}", i)); si.ChartType = SeriesChartType.Line; this.chart_ForProb.Series.Add(si); } } for (int i = 0; i < gss.Tables.Count; i++) { DataView dv = new DataView(gss.Tables[i]); this.chart_ForProb.Series[1 + i].Points.DataBindXY(dv, "Id", dv, "val"); } this.dataGridView_ProbData.DataSource = dvWv; } } } } catch (Exception ce) { } }