public override ExpectList <T> getXmlData <T>(string strXml) { ExpectList <T> ret = new ExpectList <T>(); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(strXml); XmlNodeList rows = doc.SelectNodes("/xml/row"); if (rows.Count == 0) { return(ret); } for (int i = rows.Count - 1; i >= 0; i--) { ExpectData <T> ed = new ExpectData <T>(); ed.Expect = rows[i].Attributes["expect"].Value; ed.OpenCode = rows[i].Attributes["opencode"].Value; ed.OpenTime = DateTime.Parse(rows[i].Attributes["opentime"].Value); if (!ed.IsValidData()) { continue; } ret.Add(ed as ExpectData <T>); } } catch (Exception e) { LogLib.LogableClass.ToLog(string.Format("非正常的xml数据,[{0}]:[{1}]", e.Message, e.StackTrace), strXml); } return(ret); }
public override ExpectList <T> ReadHistory <T>(string begt, string endt) { DateTime dt = DateTime.Parse(begt); MongoDataDictionary <ExchangeMongoData> res = GetAllCodeDateSerialDataList <ExchangeMongoData>(begt, endt, true); Dictionary <string, MongoReturnDataList <ExchangeMongoData> > data = res; ExpectList <ExchangeMongoData> ret = new ExpectList <ExchangeMongoData>(data, true); ExpectList <T> retlist = new ExpectList <T>(); ret.DataList.ForEach(a => { ExpectData <T> ed = new ExpectData <T>(); ed.CurrTime = null; ed.Key = "Security"; foreach (string code in a.Keys) { if (ed.CurrTime == null) { ed.CurrTime = a[code].date; } ed.Expect = MongoDateTime.StampToDate(a[code].date_stamp).Ticks.ToString(); ed.Add(code, a[code] as T); } retlist.Add(ed); }); return(retlist); }
public override ExpectList <T> getJsonData <T>(string strXml) { Web52CP_KL12_DataClass dc = new Web52CP_KL12_DataClass(); dc = dc.FromJson(strXml); ExpectList <T> ret = new ExpectList <T>(); if (dc.result != null) { for (int i = dc.result.Count - 1; i >= 0; i--) { ExpectData <T> data = new ExpectData <T>(); Web52CP_Lotty_KL12_DataClass obj = dc.result[i]; data.Expect = obj.expect; data.OpenCode = obj.num; if (string.IsNullOrEmpty(obj.num)) { if (string.IsNullOrEmpty(obj.num10)) { data.OpenCode = string.Format("{0},{1},{2},{3},{4}", obj.num1, obj.num2, obj.num3, obj.num4, obj.num5); } else { data.OpenCode = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", obj.num1, obj.num2, obj.num3, obj.num4, obj.num5, obj.num6, obj.num7, obj.num8, obj.num9, obj.num10); } } data.OpenTime = new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(obj.opentime); data.ExpectSerialByType = 2; data.ExpectSerialLong = 3; ret.Add(data); } } return(ret); }
public override ExpectList <T> getNewestData <T>(ExpectList <T> NewestData, ExpectList <T> ExistData) { DataTable dt = null; ExpectList <T> ret = new ExpectList <T>(dt); if (NewestData == null) { return(ret); } if (ExistData == null) { ExistData = new ExpectList <T>(); } HashSet <string> existDic = new HashSet <string>(); for (int i = 0; i < ExistData.Count; i++) { existDic.Add(ExistData[i].Expect); } for (int i = NewestData.Count - 1; i >= 0; i--) { if (existDic.Contains(NewestData[i].Expect)) { continue; } ret.Add(NewestData[i]); } return(ret); }
public override ExpectList <T> getHisData <T>(string strHtml) { return(getData <T>(strHtml)); ExpectList <T> ret = new ExpectList <T>(); string strBeg = "<table cellspacing=\"0\" cellpadding=\"0\" class=\"dt caipiao mbm\""; string strEnd = "</table>"; int ibeg = strHtml.IndexOf(strBeg); strHtml = strHtml.Substring(ibeg); int iend = strHtml.IndexOf(strEnd) + strEnd.Length; if (ibeg == 0) { return(ret); } if (iend <= 0) { return(ret); } string txtTable = strHtml.Substring(0, iend); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(txtTable); XmlNodeList nodelist = doc.SelectNodes("table/tr"); for (int i = 0; i < nodelist.Count; i++) { if (i == 0) { continue; } XmlNodeList tdlist = nodelist[i].SelectNodes("td"); if (tdlist.Count == 0) { continue; } ExpectData <T> ed = new ExpectData <T>(); string strExpect = tdlist[0].InnerText; string strIndex = tdlist[1].InnerText; string strOpenCodes = string.Join(",", tdlist[2].InnerText.Substring(0, 5).ToCharArray()); string[] strTimes = strExpect.Split(' '); DateTime dt = DateTime.ParseExact(strExpect, "yyyyMMdd HH:mm", CultureInfo.InvariantCulture); DateTime bdt = DateTime.Parse(dt.ToShortDateString()); string strRindex = "0000" + dt.Subtract(bdt).TotalMinutes.ToString(); ed.Expect = string.Format("{0}{1}", strTimes[0], strRindex.Substring(strRindex.Length - 4)); ed.OpenCode = strOpenCodes; ed.OpenTime = dt; ret.Add(ed); } } catch (Exception ce) { } return(ret); }
public override ExpectList <T> getData <T>(string strHtml) { ExpectList <T> ret = new ExpectList <T>(); string strBeg = "<table"; string strEnd = "</table>"; int ibeg = strHtml.IndexOf(strBeg); int iend = strHtml.IndexOf(strEnd) + strEnd.Length; if (ibeg == 0) { return(ret); } if (iend <= ibeg) { return(ret); } string txtTable = strHtml.Substring(ibeg, iend - ibeg); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(txtTable); XmlNodeList nodelist = doc.SelectNodes("table/tr"); for (int i = 0; i < nodelist.Count; i++) { XmlNodeList tdlist = nodelist[i].SelectNodes("td"); if (tdlist.Count == 0) { continue; } ExpectData <T> ed = new ExpectData <T>(); ed.Expect = tdlist[1].InnerText.Replace("-", ""); ed.OpenCode = tdlist[4].InnerText; ed.OpenTime = DateTime.Parse(tdlist[2].InnerText); ret.Add(ed); } } catch (Exception ce) { } return(ret); }
public override ExpectList <T> getData <T>(string strHtml) { ExpectList <T> ret = new ExpectList <T>(); string startStr = "lg-history-table\">"; string endStr = "</table>"; string strXml = strHtml.Substring(strHtml.IndexOf(startStr) + startStr.Length); int endPos = strXml.IndexOf(endStr) + endStr.Length; strXml = strXml.Substring(0, endPos); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(strXml); XmlNodeList rows = doc.SelectNodes("/table/tbody/tr"); if (rows.Count == 0) { return(ret); } for (int i = rows.Count - 1; i >= 0; i--) { XmlNodeList tdNodes = rows[i].SelectNodes("td"); XmlNodeList td2Nodes = tdNodes[2].SelectNodes("div"); ExpectData <T> ed = new ExpectData <T>(); ed.Expect = tdNodes[0].InnerText; string strCode = string.Join(",", td2Nodes[0].InnerText.Replace("10", "0").ToCharArray()); ed.OpenCode = ChanceCodes(strCode); ed.OpenTime = DateTime.Now.Date.Add(DateTime.Parse(tdNodes[1].InnerText).TimeOfDay); if (!ed.IsValidData()) { continue; } ret.Add(ed); } } catch { } return(ret); }
public RoundBackTestReturnClass <T> RunRound(BaseStragClass <T> teststrag, long TestLong, long StepLong)//滚动获取 { long begNo = BegExpect; ExpectReader er = new ExpectReader(); ExpectList <T> el = null; long cnt = 0; RoundBackTestReturnClass <T> ret = new RoundBackTestReturnClass <T>(); ExpectList <T> AllData = new ExpectList <T>(); long testIndex = teststrag.ReviewExpectCnt - 1; ExpectList <T> testData = null; Dictionary <string, ChanceClass <T> > NoCloseChances = new Dictionary <string, ChanceClass <T> >(); Dictionary <string, ChanceClass <T> > tmpChances = new Dictionary <string, ChanceClass <T> >(); long roundId = 0; int currRid = 0; List <Dictionary <string, ChanceClass <T> > > roundNoMatchedChances = new List <Dictionary <string, ChanceClass <T> > >(); List <long> roundBegIds = new List <long>(); 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}条记录!", testIndex); break; } AllData = ExpectList <T> .Concat(AllData, el); begNo = el.LastData.LExpectNo + 1; cnt++; //Todo: while (testIndex < AllData.Count) { if (testData == null) { testData = AllData.getSubArray(0, teststrag.ReviewExpectCnt); } 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]); } if (roundBegIds.Count > 0 && testIndex == roundBegIds[currRid] + TestLong)//切换当前滚动id { //取出当前未完成队列, Dictionary <string, ChanceClass <T> > NoMatchDic = roundNoMatchedChances[currRid]; BackTestReturnClass <T> brc = ret.RoundData[currRid]; foreach (string key in NoMatchDic.Keys)//结束所有未玩成的结果 { ChanceClass <T> cc = NoMatchDic[key]; int matchcnt = 0; if (cc.Matched(testData.LastData, out matchcnt, teststrag.GetRev)) { cc.HoldTimeCnt = (int)(testData.LastData.ExpectIndex - cc.InputExpect.ExpectIndex); } else { cc.HoldTimeCnt = 99;//还未开出但是已经结束了的 } cc.MatchChips = matchcnt; cc.Closed = true; cc.EndExpectNo = testData.LastData.Expect; cc.Closed = true; cc.EndExpectNo = testData.LastData.Expect; cc.UpdateTime = testData.LastData.OpenTime; brc.ChanceList.Add(cc); if (brc.HoldCntDic.ContainsKey(cc.HoldTimeCnt)) { brc.HoldCntDic[cc.HoldTimeCnt] = brc.HoldCntDic[cc.HoldTimeCnt] + 1; } else { brc.HoldCntDic.Add(cc.HoldTimeCnt, 1); } } currRid++; } if ((testIndex + 1 - teststrag.ReviewExpectCnt) % (StepLong) == 0) //每到周期整数倍,滚动开始id队列增加当前id { roundBegIds.Add(testIndex); //加入队列 roundNoMatchedChances.Add(new Dictionary <string, ChanceClass <T> >()); //加入未完成的机会表到指定队列中 BackTestReturnClass <T> brc = new BackTestReturnClass <T>(); ret.RoundData.Add(brc); roundId++; } for (int i = currRid; i < roundBegIds.Count; i++)//处理每个滚动周期的 { ExchanceClass <T> cc = new ExchanceClass <T>(); Dictionary <string, ChanceClass <T> > NoMatchDic = roundNoMatchedChances[i]; BackTestReturnClass <T> brc = ret.RoundData[i]; cc.Run(dtp, testData, teststrag, ref brc.ChanceList, ref NoMatchDic, ref brc.HoldCntDic); roundNoMatchedChances[i] = NoMatchDic; brc.LoopCnt = testIndex - roundBegIds[i] + 1; ret.RoundData[i] = brc; } testIndex++; } } return(ret); }
public void Run() { //LoopCnt = 0; testIndex = 0; ret = new BackTestReturnClass <T>(); long begNo = BegExpect; //ExpectReader er = new ExpectReader(); DataReader er = DataReaderBuild.CreateReader(dtp.DataType, "", null); ExpectList <T> el = null; long cnt = 0; 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>(); testIndex = teststrag.ReviewExpectCnt - 1; ExpectList <T> testData = null; Dictionary <string, ChanceClass <T> > NoCloseChances = new Dictionary <string, ChanceClass <T> >(); Dictionary <string, ChanceClass <T> > tmpChances = new Dictionary <string, ChanceClass <T> >(); while (el == null || el.Count > 0) //如果取到的数据长度大于0 { el = er.ReadHistory <T>(begNo, LoopCnt); if (el == null) { ret.LoopCnt = (cnt + 1) * 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: int pastCnt = 0; while (testIndex < AllData.Count) { ret.LoopCnt = pastCnt + testIndex; if (testData == null) { testData = AllData.getSubArray(0, teststrag.ReviewExpectCnt); } else { if (dtp.DataType == "PK10") { 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]); } tmpChances = new Dictionary <string, ChanceClass <T> >(); foreach (string key in NoCloseChances.Keys) { ChanceClass <T> cc = NoCloseChances[key]; 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); } } } if (dtp.IsXxY == 1) { (cc as iXxYClass).AllNums = dtp.AllNums; (cc as iXxYClass).SelectNums = dtp.SelectNums; (cc as iXxYClass).strAllTypeBaseOdds = dtp.strAllTypeOdds; (cc as iXxYClass).strCombinTypeBaseOdds = dtp.strCombinTypeOdds; (cc as iXxYClass).strPermutTypeBaseOdds = dtp.strPermutTypeOdds; } 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 (teststrag is ITraceChance) { ITraceChance its = teststrag as ITraceChance; if (its == null) { cc.Closed = cc.OnCheckTheChance(cc, Matched); } else { cc.Closed = its.CheckNeedEndTheChance(cc, Matched); } } else { cc.Closed = cc.OnCheckTheChance(cc, Matched); } if (cc.Closed) { cc.EndExpectNo = testData.LastData.Expect; cc.UpdateTime = testData.LastData.OpenTime; } else { cc.HoldTimeCnt++; tmpChances.Add(key, cc); } } else { if (Matched || (!Matched && cc.HoldTimeCnt > 0 && cc.HoldTimeCnt == cc.AllowMaxHoldTimeCnt))//关闭 { cc.Closed = true; cc.EndExpectNo = testData.LastData.Expect; cc.MatchChips = matchcnt; if (!teststrag.GetRev)//只有不求相反值的情况下,才赋持有是次数 { if (dtp.DataType == "PK10") { 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, cc); } } if (cc.Closed && cc.MatchChips > 0) { int HCnt = 1; 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); } } } } BaseCollection <T> sc = new ExpectListProcessBuilder <T>(dtp, testData).getProcess().getSerialData(teststrag.ReviewExpectCnt, teststrag.BySer); if (testData.Count == 0) { break; } teststrag.SetLastUserData(testData); teststrag.setDataTypePoint(dtp); List <ChanceClass <T> > cs = teststrag.getChances(sc, testData.LastData);//获取所有机会 if (ret.ChanceList == null) { ret.ChanceList = new List <ChanceClass <T> >(); } //ret.ChanceList.AddRange(cs); NoCloseChances = new Dictionary <string, ChanceClass <T> >(); foreach (string key in tmpChances.Keys) { ChanceClass <T> cc = tmpChances[key]; cc.AllowMaxHoldTimeCnt = int.MaxValue; NoCloseChances.Add(key, cc); } for (int i = 0; i < cs.Count; i++) { //string key = string.Format("{0}_{1}", cs[i].SignExpectNo, cs[i].ChanceCode); string key = string.Format("{0}", cs[i].ChanceCode); if (NoCloseChances.ContainsKey(key)) { if (teststrag.AllowRepeat) { string test = key; //NoCloseChances.Add(key, cs[i]); } } else { cs[i].AllowMaxHoldTimeCnt = int.MaxValue; NoCloseChances.Add(key, cs[i]); } } testIndex++; } pastCnt += el.Count; } FinishedProcess(); //return ret; }
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); }
/// <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); }
public override ExpectList <T> getData <T>(string strHtml) { DateTime Now = DateTime.Now; ExpectList <T> ret = new ExpectList <T>(); string classTable = @"class=""list"""; string regtxt = @"<table class=""list"">.*(<tr>.*?</tr>)</tbody></table>"; Regex regTr = new Regex(@"(?is)(?<=<table[^>]*?" + classTable + "[^>]*?>(?:(?!</?table).)*)(?is)<tr[^>]*?>(?:\\s*<td[^>]*>(.*?)</td>)*\\s*</tr>"); //MatchCollection mc = Regex.Matches(strHtml, regtxt); MatchCollection mc = regTr.Matches(strHtml); HashSet <string> AllEds = new HashSet <string>(); for (int ci = mc.Count - 1; ci >= 0; ci--) { Match m = mc[ci]; Regex regTd = new Regex(@"(?is)(?<=<td(\s+align=[^>]+)?>).*?(?=\s*</td)"); MatchCollection mtd = regTd.Matches(m.Value); ExpectData <T> ed = new ExpectData <T>(); ed.Expect = mtd[0].Value; string[] strs = mtd[2].Value.Split('=')[0].Replace(" ", "").Split('+'); for (int i = 0; i < strs.Length; i++) { strs[i] = ((strs[i] == "0")?"1":"0") + strs[i]; } ed.OpenCode = string.Join(",", strs); DateTime currVal = DateTime.Parse(string.Format("{0}-{1}", Now.Year, mtd[1].Value)); if (currVal > Now) //如果匹配出的时间大于当前时间 { currVal = DateTime.Parse(string.Format("{0}-{1}", Now.Year - 1, mtd[1].Value)); } ed.OpenTime = currVal; if (AllEds.Contains(ed.Expect)) { continue; } AllEds.Add(ed.Expect); ret.Add(ed); } return(ret); string strBeg = "<table"; string strEnd = "</table>"; int ibeg = strHtml.IndexOf(strBeg); int iend = strHtml.IndexOf(strEnd) + strEnd.Length; if (ibeg == 0) { return(ret); } if (iend <= ibeg) { return(ret); } string txtTable = strHtml.Substring(ibeg, iend - ibeg); XmlDocument doc = new XmlDocument(); try { doc.LoadXml(txtTable); XmlNodeList nodelist = doc.SelectNodes("table/tr"); for (int i = 0; i < nodelist.Count; i++) { XmlNodeList tdlist = nodelist[i].SelectNodes("td"); if (tdlist.Count == 0) { continue; } ExpectData <T> ed = new ExpectData <T>(); ed.Expect = tdlist[1].InnerText.Replace("-", ""); ed.OpenCode = tdlist[4].InnerText; ed.OpenTime = DateTime.Parse(tdlist[2].InnerText); ret.Add(ed); } } catch (Exception ce) { } return(ret); }
public ExpectList <T> getFileData <T>(string filename) where T : TimeSerialData { ExpectList <T> ret = new ExpectList <T>(); FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader str = new StreamReader(file, Encoding.Default); try { //用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0, //表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符. Decoder d = Encoding.Default.GetDecoder(); int lcnt = 0; string txtline; while ((txtline = str.ReadLine()) != null) { lcnt++; if (lcnt <= 1) { continue;//标题跳过 } if (txtline.Trim().Length == 0) { continue; } txtline = txtline.Replace(" ", ","); string[] items = txtline.Split(','); if (items.Length != 2) { throw new Exception(string.Format("{0}第{1}行数据异常!", filename, lcnt)); } ExpectData <T> ed = new ExpectData <T>(); ed.OpenCode = string.Join(",", items[1].ToCharArray()); string strOrg = items[0]; string[] strOrgs = strOrg.Split('-'); if (strOrgs.Length > 1) { string strDate = strOrgs[0]; string strId = strOrgs[1]; string DstrDate = string.Format("{0}-{1}-{2}", strDate.Substring(0, 4), strDate.Substring(4, 2), strDate.Substring(6)); DateTime etime = DateTime.Parse(DstrDate); ed.OpenTime = etime.AddMinutes(int.Parse(strId)).AddSeconds(6); ed.Expect = ed.OpenTime.ToString("yyyy-MM-dd HH-mm-SS").Replace("-", "").Replace("/", "").Replace(":", "").Replace(" ", "").Substring(0, 12); } else { ed.Expect = strOrg; string strDate = strOrg.Substring(0, 8); string strId = ed.Expect.Substring(8); string DstrDate = string.Format("{0}-{1}-{2} {3}:{4}:{5}", strDate.Substring(0, 4), strDate.Substring(4, 2), strDate.Substring(6), strId.Substring(0, 2), strId.Substring(2), "06"); DateTime etime = DateTime.Parse(DstrDate); ed.OpenTime = etime; } //ed.Expect = items[0].Replace("-", ""); //if (ed.OpenCode == "0,0,0,0,0") ret.Add(ed); } str.Close(); //file.Close(); } catch (IOException e) { str.Close(); //file.Close(); throw e; } return(ret); }
/// <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); }