コード例 #1
0
        public override List <PK10CorePress.ChanceClass> getChances(PK10CorePress.CommCollection sc, PK10CorePress.ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();
            ExpectList         el  = sc.orgData;
            List <ChanceClass> scs = new List <ChanceClass>();
            strag_CommOldClass coc = new strag_CommOldClass();

            coc.CommSetting = this.CommSetting;
            coc.ChipCount   = this.ChipCount;
            coc.FixChipCnt  = true;
            //coc.AllowMaxHoldTimeCnt = this.AllowMaxHoldTimeCnt;
            coc.BySer = this.BySer;
            //coc.ReviewExpectCnt = ;
            coc.InputMinTimes = this.InputMinTimes;
            coc.StagSetting   = new StagConfigSetting();
            if (!this.IsTracing)//未持仓时才计算过往概率
            {
                double sucrate = double.NaN;
                if (this.CommSetting.UseLocalWaveData)//如果使用本地数据,获取该期本地数据
                {
                    sucrate = this.LocalWaveData[el.LastData.Expect];
                }
                if (double.IsNaN(sucrate))               //如果数据异常,继续去寻找
                {
                    sucrate = getSucRate(el, coc, true); //该类策略全部是一次性机会,可反复下注
                }
                if (this.RateDic == null)
                {
                    this.RateDic = new Dictionary <string, double>();
                }
                if (!this.RateDic.ContainsKey(this.LastUseData().LastData.Expect))//加入胜率队列
                {
                    RateDic.Add(this.LastUseData().LastData.Expect, sucrate);
                }
                if (!this.CheckEnableIn())
                {
                    return(ret);
                }

                this.IsTracing = true;//满足条件,开始持仓
                //this.debug_maxRate = 0;
            }
            CommCollection scc = new ExpectListProcess(el).getSerialData(InputMinTimes, BySer);

            ret = coc.getChances(scc, el.LastData);
            for (int i = 0; i < ret.Count; i++)
            {
                ret[i].NeedConditionEnd  = true;
                ret[i].OnCheckTheChance += CheckNeedEndTheChance;
            }
            return(ret);
        }
コード例 #2
0
        public CommExpectListProcess <T> getProcess()
        {
            CommExpectListProcess <T> ret = null;

            if (dtp.IsSecurityData == 1)
            {
                ret = new SecurityListProcess <T>(data);
            }
            else
            {
                switch (dtp.DataType)
                {
                case "PK10":
                case "XYFT":

                {
                    ret = new ExpectListProcess(new ExpectList(data.Table.Copy())) as CommExpectListProcess <T>;       // ConvertionExtensions.CopyTo<CommExpectListProcess<T>>(new ExpectListProcess(new ExpectList(data.Table)));
                    break;
                }

                case "SCKL12":
                case "NLKL12":
                {
                    ret = new CombinLottery_ExpectListProcess(new ExpectList(data.Table)) as CommExpectListProcess <T>;
                    (ret as CombinLottery_ExpectListProcess).AllNums           = dtp.AllNums;
                    (ret as CombinLottery_ExpectListProcess).SelectNums        = dtp.SelectNums;
                    (ret as CombinLottery_ExpectListProcess).strAllTypeOdds    = dtp.strAllTypeOdds;
                    (ret as CombinLottery_ExpectListProcess).strCombinTypeOdds = dtp.strCombinTypeOdds;
                    (ret as CombinLottery_ExpectListProcess).strPermutTypeOdds = dtp.strPermutTypeOdds;

                    break;
                }

                case "GDKL11":
                {
                    ret = new CombinLottery_ExpectListProcess(new ExpectList(data.Table)) as CommExpectListProcess <T>;
                    (ret as CombinLottery_ExpectListProcess).AllNums           = dtp.AllNums;
                    (ret as CombinLottery_ExpectListProcess).SelectNums        = dtp.SelectNums;
                    (ret as CombinLottery_ExpectListProcess).strAllTypeOdds    = dtp.strAllTypeOdds;
                    (ret as CombinLottery_ExpectListProcess).strCombinTypeOdds = dtp.strCombinTypeOdds;
                    (ret as CombinLottery_ExpectListProcess).strPermutTypeOdds = dtp.strPermutTypeOdds;
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
            return(ret);
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: zhihuba/PK10Projects
        void RefreshSerialData(ListView lv, bool byNo, int minRow)
        {
            ExpectList        el  = ViewDataList;;
            ExpectListProcess elp = new ExpectListProcess(el);
            CommCollection    sc  = elp.getSerialData(180, byNo);

            sc.isByNo = byNo;
            lv.Items.Clear();
            for (int i = minRow - 1; i < sc.Table.Rows.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (i - minRow + 1 + 1).ToString();
                if (byNo)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        lvi.SubItems.Add(sc.Table.Rows[i][j].ToString());
                    }
                }
                else
                {
                    for (int j = 0; j < 10; j++)
                    {
                        lvi.SubItems.Add(sc.Table.Rows[i][string.Format("{0}", (j + 1) % 10)].ToString());
                    }
                }
                for (int j = 1; j < 9; j++)
                {
                    if (i + 1 == this.gobj.MinTimeForChance(j))
                    {
                        lvi.BackColor = Color.Yellow;
                        break;
                    }
                }
                lv.Items.Add(lvi);
            }
        }
コード例 #4
0
        double getSucRate(ExpectList el, strag_CommOldClass coc, bool OnlyOnceChance)
        {
            Dictionary <string, ChanceClass> NoEndChances = new Dictionary <string, ChanceClass>();
            Dictionary <string, ChanceClass> TmpChances   = new Dictionary <string, ChanceClass>();
            int MatchTimes = 0;
            int AllTimes   = 0;

            for (int i = this.InputMaxTimes; i < el.Count; i++)//因为最后一次数据如果下注就不知道结果,所以最后一次排除下注,只做验证上次结果用
            {
                ExpectList useList  = el.getSubArray(i - this.InputMinTimes, this.InputMinTimes);
                ExpectData CurrData = useList.LastData;
                TmpChances = new Dictionary <string, ChanceClass>();
                foreach (string key in NoEndChances.Keys)
                {
                    ChanceClass cc       = NoEndChances[key];
                    int         MatchCnt = 0;
                    bool        Matched  = cc.Matched(CurrData, out MatchCnt, false);
                    if (Matched)
                    {
                        if (OnlyOnceChance)//如果是一次性机会
                        {
                            if (cc.HoldTimeCnt == 1)
                            {
                                MatchTimes++;
                                continue;
                            }
                        }
                        else
                        {
                            MatchTimes++;
                        }
                    }
                    else
                    {
                        if (!TmpChances.ContainsKey(key))
                        {
                            cc.HoldTimeCnt++;
                            cc.Closed = false;
                            TmpChances.Add(key, cc);
                            if (!OnlyOnceChance)//如果不是一次性机会,每次机会都加1
                            {
                                AllTimes++;
                            }
                        }
                    }
                }
                if (i == el.Count - 1)
                {
                    break;//最后一期不找机会
                }
                NoEndChances = new Dictionary <string, ChanceClass>();
                foreach (string key in TmpChances.Keys)//遗留机会
                {
                    NoEndChances.Add(key, TmpChances[key]);
                }
                CommCollection     scc = new ExpectListProcess(useList).getSerialData(InputMinTimes, BySer);
                List <ChanceClass> ccs = coc.getChances(scc, CurrData);
                for (int j = 0; j < ccs.Count; j++)//新增机会
                {
                    if (!NoEndChances.ContainsKey(ccs[j].ChanceCode))
                    {
                        NoEndChances.Add(ccs[j].ChanceCode, ccs[j]);
                        AllTimes++;
                    }
                }
            }
            return((double)100 * MatchTimes / AllTimes);
        }
コード例 #5
0
        public void Run(ExpectList testData, StragClass teststrag, ref List <ChanceClass> ChanceList, ref Dictionary <string, ChanceClass> NoCloseChances, ref Dictionary <int, int> HoldCntDic)
        {
            tmpChances = new Dictionary <string, ChanceClass>();
            if (ChanceList == null)
            {
                ChanceList = new List <ChanceClass>();
            }
            CommCollection sc = new ExpectListProcess(testData).getSerialData(teststrag.ReviewExpectCnt, teststrag.BySer);

            foreach (string key in NoCloseChances.Keys)
            {
                ChanceClass cc = NoCloseChances[key];
                if (cc.Closed == false)
                {
                    int matchcnt = 0;
                    if (teststrag.GetRev)                                      //如果求相反组合
                    {
                        if (cc.Matched(testData.LastData, out matchcnt, true)) //不关闭
                        {
                            cc.HoldTimeCnt = (int)(testData.LastData.ExpectIndex - cc.InputExpect.ExpectIndex);
                        }
                    }
                    if (cc.Matched(testData.LastData, out matchcnt, false))//如果用相反组合,不改变真正关闭
                    {
                        cc.Closed      = true;
                        cc.EndExpectNo = testData.LastData.Expect;
                        if (!teststrag.GetRev)//只有不求相反值的情况下,才赋持有是次数
                        {
                            cc.HoldTimeCnt = (int)(testData.LastData.ExpectIndex - cc.InputExpect.ExpectIndex);
                        }
                        cc.MatchChips = matchcnt;
                        cc.UpdateTime = testData.LastData.OpenTime;
                        ChanceList.Add(cc);
                        int HCnt = 1;
                        if (HoldCntDic == null)
                        {
                            HoldCntDic = new Dictionary <int, int>();
                        }
                        if (HoldCntDic.ContainsKey(cc.HoldTimeCnt))
                        {
                            HCnt = HoldCntDic[cc.HoldTimeCnt];
                            HCnt++;
                            HoldCntDic[cc.HoldTimeCnt] = HCnt;
                        }
                        else
                        {
                            HoldCntDic.Add(cc.HoldTimeCnt, 1);
                        }
                    }
                    else
                    {
                        tmpChances.Add(key, cc);
                    }
                }
            }
            //List<ChanceClass> cs = teststrag.getChances(testData);

            List <ChanceClass> cs = teststrag.getChances(sc, testData.LastData);

            if (ChanceList == null)
            {
                ChanceList = new List <ChanceClass>();
            }
            //ret.ChanceList.AddRange(cs);
            NoCloseChances = new Dictionary <string, ChanceClass>();
            foreach (string key in tmpChances.Keys)
            {
                ChanceClass cc = tmpChances[key];
                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
                {
                    NoCloseChances.Add(key, cs[i]);
                }
            }
        }
コード例 #6
0
ファイル: BackTestClass.cs プロジェクト: zhihuba/PK10Projects
        public BackTestReturnClass VirExchange_oldLogic(ExchangeService es, StragRunPlanClass[] teststragplans)
        {
            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>();
            long         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]);
                    }
                    for (int i = 0; i < teststrags.Length; i++)//专门针对需要程序话关闭机会,且关闭时需要知道当前数据策略使用
                    {
                        teststrags[i].SetLastUserData(testData);
                    }
                    tmpChances = new Dictionary <string, StragChance>();

                    //关闭所有交易
                    foreach (int id in NewExchangeRecord.Keys)
                    {
                        ExchangeChance 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>();

                    foreach (string key in NoCloseChances.Keys)
                    {
                        StragChance scc = NoCloseChances[key];
                        ChanceClass 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.MaxHoldTimeCnt;
                                    if (cc.HoldTimeCnt - cc.LastMatchTimesId > maxHoldCnt)
                                    {
                                        cc.MaxHoldTimeCnt = 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> cs = new List <StragChance>();
                    for (int i = 0; i < teststrags.Length; i++)
                    {
                        CommCollection sc = new ExpectListProcess(testData).getSerialData(teststrags[i].ReviewExpectCnt, teststrags[i].BySer);
                        if (testData.Count == 0)
                        {
                            break;
                        }
                        List <ChanceClass> scs = teststrags[i].getChances(sc, testData.LastData);//获取所有机会
                        for (int j = 0; j < scs.Count; j++)
                        {
                            ChanceClass CurrCc = scs[j];
                            ////scs[j].IncrementType = teststragplans[i].IncreamType;
                            ////scs[j].FixAmt = teststragplans[i].FixAmt;
                            ////scs[j].FixRate = teststragplans[i].FixRate;
                            StragRunPlanClass currPlan  = teststragplans[i];
                            StragClass        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.MaxHoldTimeCnt = currPlan.AllowMaxHoldTimeCnt;
                            CurrCc.FixAmt         = currPlan.FixAmt;
                            CurrCc.FixRate        = currPlan.FixRate;
                            CurrCc.IncrementType  = currPlan.IncreamType;
                            cs.Add(new StragChance(teststrags[i], CurrCc));
                        }
                    }
                    if (ret.ChanceList == null)
                    {
                        ret.ChanceList = new List <ChanceClass>();
                    }
                    //ret.ChanceList.AddRange(cs);
                    NoCloseChances = new Dictionary <string, StragChance>();
                    foreach (string key in tmpChances.Keys)
                    {
                        StragChance scc = tmpChances[key];
                        ChanceClass cc  = scc.Chance;
                        NoCloseChances.Add(key, scc);
                        //////ProbWaveSelectStragClass组合改为统一交易
                        ////if ((scc.Strag is ProbWaveSelectStragClass) == false)
                        ////{
                        ////    ExchangeChance 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 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 ec = new ExchangeChance(es, NoCloseChances[key].Strag, 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 ChanceTraceStragClass).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);
        }
コード例 #7
0
ファイル: BackTestClass.cs プロジェクト: zhihuba/PK10Projects
        public void  Run()
        {
            LoopCnt   = 0;
            testIndex = 0;
            ret       = new BackTestReturnClass();
            long begNo = BegExpect;

            ExpectReader er  = new ExpectReader();
            ExpectList   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 AllData = new ExpectList();

            testIndex = teststrag.ReviewExpectCnt - 1;
            ExpectList testData = null;
            Dictionary <string, ChanceClass> NoCloseChances = new Dictionary <string, ChanceClass>();
            Dictionary <string, ChanceClass> tmpChances     = new Dictionary <string, ChanceClass>();

            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)
                {
                    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]);
                    }
                    tmpChances = new Dictionary <string, ChanceClass>();
                    foreach (string key in NoCloseChances.Keys)
                    {
                        ChanceClass 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);
                                    }
                                }
                            }
                            bool Matched = cc.Matched(testData.LastData, out matchcnt, false);
                            if (cc.NeedConditionEnd)
                            {
                                cc.MatchChips += matchcnt;
                                if (Matched)
                                {
                                    int LastMatchId = cc.LastMatchTimesId;//最后一次匹配次序号
                                    int maxHoldCnt  = cc.MaxHoldTimeCnt;
                                    if (cc.HoldTimeCnt - cc.LastMatchTimesId > maxHoldCnt)
                                    {
                                        cc.MaxHoldTimeCnt = 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 || (cc.HoldTimeCnt > 0 && 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, cc);
                                }
                            }
                            if (cc.Closed)
                            {
                                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);
                                }
                            }
                        }
                    }
                    CommCollection sc = new ExpectListProcess(testData).getSerialData(teststrag.ReviewExpectCnt, teststrag.BySer);
                    if (testData.Count == 0)
                    {
                        break;
                    }
                    teststrag.SetLastUserData(testData);
                    List <ChanceClass> cs = teststrag.getChances(sc, testData.LastData);//获取所有机会
                    if (ret.ChanceList == null)
                    {
                        ret.ChanceList = new List <ChanceClass>();
                    }
                    //ret.ChanceList.AddRange(cs);
                    NoCloseChances = new Dictionary <string, ChanceClass>();
                    foreach (string key in tmpChances.Keys)
                    {
                        ChanceClass cc = tmpChances[key];
                        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
                        {
                            NoCloseChances.Add(key, cs[i]);
                        }
                    }
                    testIndex++;
                }
            }
            FinishedProcess();
            //return ret;
        }
コード例 #8
0
ファイル: PlanGrupClass.cs プロジェクト: zhihuba/PK10Projects
        public void ExecRun(object data)
        {
            ExpectList el = data as ExpectList;

            //Log("计算服务","准备数据", "为每个策略分配数据");
            foreach (string key in UseStrags.Keys)
            {
                UseStrags[key].SetLastUserData(el);
            }
            //准备数据
            CommCollection cc         = null;
            int            maxViewCnt = (int)this.UseStrags.Max(t => t.Value.ReviewExpectCnt);

            //Log("计算服务", "最大回览期数", maxViewCnt.ToString());
            cc = new ExpectListProcess(el).getSerialData(maxViewCnt, this.UseSerial);
            // cc.orgData = el;//必须指定原始数据?
            //Log("计算服务", "中间数据长度",cc.Data.Count.ToString());
            Dictionary <StragClass, List <ChanceClass> > css = new Dictionary <StragClass, List <ChanceClass> >();
            //Log("计算服务", "计算数据", "为每个策略计算最大回顾周期数据");
            //遍历每个策略获得机会
            DbChanceList OldDbList = new DbChanceList();
            Dictionary <string, ChanceClass> OldList = new Dictionary <string, ChanceClass>();
            List <ChanceClass> NewList = new List <ChanceClass>();

            //Log("计算服务", "遍历所有策略", string.Format("策略数量:{0}",this.UseStrags.Count));


            CloseAllExchance(el);//清空所有可视化机会



            #region 获取交易机会
            for (int i = 0; i < this.UseSPlans.Count; i++)
            {
                StragRunPlanClass currPlan = UseSPlans[i];
                if (currPlan.PlanStrag == null)//如果计划所执行的策略为空,只在chance上执行tracer
                {
                    List <ChanceClass> emptycs = CurrExistChanceList.Values.Where(p => p.StragId == null).ToList <ChanceClass>();
                    for (int c = 0; c < emptycs.Count; c++)
                    {
                        ChanceClass CurrCc = emptycs[c];
                        TraceChance tcc    = CurrCc as TraceChance;
                        CurrCc.UnitCost    = tcc.getChipAmount(GlobalClass.DefaultMaxLost, CurrCc, GlobalClass._DefaultHoldAmtSerials.Value);
                        CurrCc.HoldTimeCnt = CurrCc.HoldTimeCnt + 1;
                        CurrCc.Cost       += CurrCc.ChipCount * CurrCc.UnitCost;
                        CurrCc.UpdateTime  = CurrCc.CreateTime;
                        OldList.Add(CurrCc.GUID, CurrCc);
                        if (!IsBackTest)//非回测需要额外保存数据
                        {
                            OldDbList.Add(CurrCc.ChanceIndex, CurrCc);
                        }
                    }
                    continue;
                }
                StragClass currStrag = UseStrags[currPlan.PlanStrag.GUID];
                currStrag.SetLastUserData(el);                                 //必须给策略填充数据
                List <ChanceClass> cs = currStrag.getChances(cc, el.LastData); //获取该策略的机会
                if (currStrag is TotalStdDevTraceStragClass)                   //如果是整体标准差类,记录所有的标准差数据
                {
                    grpTotolStdDic = (currStrag as TotalStdDevTraceStragClass).getAllStdDev();
                }
                if (cs.Count > 0)
                {
                    Log("计算服务", string.Format("策略[{0}/{1}]", currStrag.GUID, currStrag.StragScript), string.Format("取得机会数量为:{0}", cs.Count));
                }
                Dictionary <string, ChanceClass> StragChances = CurrExistChanceList.Where(p => p.Value.StragId == currStrag.GUID).ToDictionary(p => p.Value.ChanceCode, p => p.Value);
                AmoutSerials amts    = GlobalClass.getOptSerials(CurrSetting.Odds, currPlan.InitCash, 1);
                Int64        restAmt = currStrag.CommSetting.GetGlobalSetting().DefMaxLost; //初始资金
                #region 遍历各机会
                for (int j = 0; j < cs.Count; j++)                                          //对每个机会,检查上期遗留的机会是否包括
                {
                    bool        NeedUseOldData = false;
                    ChanceClass CurrCc         = cs[j];
                    CurrCc.HoldTimeCnt         = 1;
                    CurrCc.AllowMaxHoldTimeCnt = currPlan.AllowMaxHoldTimeCnt;
                    CurrCc.IncrementType       = currPlan.IncreamType;
                    if (currPlan.IncreamType == InterestType.CompoundInterest)
                    {
                        CurrCc.FixRate = currPlan.FixRate;
                    }
                    else
                    {
                        CurrCc.FixAmt = currPlan.FixAmt;
                    }
                    //该语句存在机会重复的风险
                    if (StragChances.ContainsKey(CurrCc.ChanceCode))//未关闭的及机会列表中存在该机会
                    {
                        ChanceClass OldCc = StragChances[CurrCc.ChanceCode];
                        //Log("计算服务", "老机会信息", string.Format("idx:{0};holdcnt:{1}", OldCc.ChanceIndex, OldCc.HoldTimeCnt));
                        //Log("计算服务", "老记录", string.Format("上期相同的机会{0}", CurrCc.ChanceCode));
                        //Log("计算服务", "判断是否允许重复", currStrag.AllowRepeat.ToString());
                        if (!currStrag.AllowRepeat)//如果不允许重复
                        {
                            CurrCc             = OldCc;
                            CurrCc.HoldTimeCnt = CurrCc.HoldTimeCnt + 1;
                            NeedUseOldData     = true;
                            Log("计算服务", "相同处理", string.Format("出现相同的机会{0},持有次数增1->{1}", CurrCc.ChanceCode, CurrCc.HoldTimeCnt));
                        }
                    }
                    else
                    {
                        //Log("计算服务", string.Format("上期相同未关闭的机会数{0},{1}", CurrExistChanceList.Count, CurrCc.ChanceCode), "本期未出现");
                    }

                    if (currPlan.AssetUnitInfo != null)
                    {
                        if (this.UseAssetUnits.ContainsKey(currPlan.AssetUnitInfo.UnitId))
                        {
                            AssetUnitClass useUnit = UseAssetUnits[currPlan.AssetUnitInfo.UnitId];
                            if (!useUnit.Running)
                            {
                                useUnit.Run();
                            }
                            restAmt = (long)useUnit.ExchangeServer.summary;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    //Log("计算服务", "再次检查数据", string.Format("出现相同的机会{0},持有次数增1->{1}", CurrCc.ChanceCode, CurrCc.HoldTimeCnt));
                    CurrCc.UnitCost = -1;         //先默认为-1
                    if (currStrag is ISpecAmount) //先从策略级别判断
                    {
                        ISpecAmount testStrag = (currStrag as ISpecAmount);
                        if (testStrag == null)
                        {
                            //等待下一步按机会级别判断
                        }
                        else
                        {
                            CurrCc.UnitCost = testStrag.getChipAmount(restAmt, CurrCc, amts);
                        }
                    }
                    if (CurrCc.UnitCost < 0)      //如果策略级别未改变值
                    {
                        if (CurrCc.IsTracer == 1) //如果是自我追踪机会
                        {
                            Log("计算服务", "自我跟踪机会,当前持有次数", string.Format("HoldTimes:{0}", CurrCc.HoldTimeCnt));
                            TraceChance useCc = Convert.ChangeType(CurrCc, currStrag.getTheChanceType()) as TraceChance;
                            //Log("计算服务", "使用的机会持有次数", string.Format("HoldTimes:{0}", useCc.HoldTimeCnt));
                            if (useCc == null) //获得的类型并非跟踪类型
                            {
                                CurrCc.UnitCost = (currStrag as ChanceTraceStragClass).getChipAmount(restAmt, CurrCc, amts);
                            }
                            else
                            {
                                CurrCc.UnitCost = useCc.getChipAmount(restAmt, CurrCc, amts);
                            }
                        }
                        else//默认为ChanceTraceStragClass,其实是不可能触发的,而且会出错,因为ChanceTraceStragClass本身就是ispaceamount
                        {
                            Log("计算服务", "非跟踪机会,持有次数", string.Format("HoldTimes:{0}", CurrCc.HoldTimeCnt));
                            CurrCc.UnitCost = (currStrag as ChanceTraceStragClass).getChipAmount(restAmt, CurrCc, amts);
                        }
                    }
                    //Log("计算服务", "再二次检查数据", string.Format("出现相同的机会{0},持有次数增1->{1}", CurrCc.ChanceCode, CurrCc.HoldTimeCnt));
                    if (NeedUseOldData)//未关闭的及机会列表中存在该机会
                    {
                        Log("计算服务", "策略不可以出现重复", string.Format("策略编号:{0}", CurrCc.UnitCost));
                        CurrCc.Cost      += CurrCc.UnitCost * CurrCc.ChipCount;
                        CurrCc.UpdateTime = DateTime.Now;
                        OldList.Add(CurrCc.GUID, CurrCc);
                        if (!IsBackTest)
                        {
                            OldDbList.Add(CurrCc.ChanceIndex, CurrCc);
                        }
                        continue;
                    }
                    CurrCc.HoldTimeCnt    = 1;
                    CurrCc.Cost           = CurrCc.ChipCount * CurrCc.UnitCost;
                    CurrCc.Gained         = 0;
                    CurrCc.Profit         = 0;
                    CurrCc.ExecDate       = DateTime.Today;
                    CurrCc.CreateTime     = DateTime.Now;
                    CurrCc.UpdateTime     = CurrCc.CreateTime;
                    CurrCc.StragId        = currStrag.GUID;
                    CurrCc.ExpectCode     = el.LastData.Expect;
                    CurrCc.MaxHoldTimeCnt = currPlan.AllowMaxHoldTimeCnt;
                    CurrCc.ChanceType     = currPlan.OutPutType;
                    NewList.Add(CurrCc);
                }
                #endregion

                #region 未关闭的机会需要自我跟踪
                foreach (string code in StragChances.Keys)
                {
                    ChanceClass CurrCc = StragChances[code];
                    //if (!CurrCc.Tracerable) continue;
                    int cnt = OldList.Values.Where(p => p.ChanceCode.Equals(code)).Count();
                    if (cnt > 0)
                    {
                        continue;
                    }
                    if (currStrag is ISpecAmount)//先从策略级检查
                    {
                        ISpecAmount specStrag = currStrag as ISpecAmount;
                        if (specStrag != null)//如果没有方法,再从机会级检查
                        {
                            CurrCc.HoldTimeCnt++;
                            CurrCc.UnitCost   = specStrag.getChipAmount(restAmt, CurrCc, amts);
                            CurrCc.Cost      += CurrCc.ChipCount * CurrCc.UnitCost;
                            CurrCc.UpdateTime = DateTime.Now;
                            OldList.Add(CurrCc.GUID, CurrCc);
                            if (!IsBackTest)
                            {
                                OldDbList.Add(CurrCc.ChanceIndex, CurrCc);
                            }
                            continue;
                        }
                    }
                    if (CurrCc.Tracerable)//再检查机会级
                    {
                        CurrCc.HoldTimeCnt++;
                        TraceChance testCc = (TraceChance)CurrCc;
                        if (testCc == null)
                        {
                            continue;
                        }
                        CurrCc.UnitCost   = testCc.getChipAmount(restAmt, CurrCc, amts);
                        CurrCc.Cost      += CurrCc.ChipCount * CurrCc.UnitCost;
                        CurrCc.UpdateTime = DateTime.Now;
                        OldList.Add(CurrCc.GUID, CurrCc);
                        if (!IsBackTest)
                        {
                            OldDbList.Add(CurrCc.ChanceIndex, CurrCc);
                        }
                        continue;
                    }
                    else
                    {
                        CurrCc.HoldTimeCnt++;
                        ISpecAmount Strag = (ISpecAmount)currStrag;
                        if (Strag == null)
                        {
                            continue;
                        }
                        CurrCc.UnitCost   = Strag.getChipAmount(restAmt, CurrCc, amts);
                        CurrCc.Cost       = CurrCc.ChipCount * CurrCc.UnitCost;
                        CurrCc.UpdateTime = DateTime.Now;
                        OldList.Add(CurrCc.GUID, CurrCc);
                        if (!IsBackTest)
                        {
                            OldDbList.Add(CurrCc.ChanceIndex, CurrCc);
                        }
                    }
                }
                #endregion
            }
            #endregion

            if (!IsBackTest)//额外保存
            {
                int savecnt = OldDbList.Save(null);
                if (OldList.Count > 0)
                {
                    Log("计算服务", "保存已有机会", string.Format("条数:{0};实际条数:{1}", OldList.Count, savecnt));
                }
                savecnt = new PK10ExpectReader().SaveChances(NewList, null);
                if (NewList.Count > 0)
                {
                    Log("计算服务", "保存新增机会", string.Format("条数:{0};实际条数:{1}", NewList.Count, savecnt));
                }
            }
            //合并到未关闭机会列表中
            NewList.ForEach(p => AllNoClosedChances.Add(p.GUID, p));
            OldList.Values.ToList <ChanceClass>().ForEach(p => AllNoClosedChances.Add(p.GUID, p)); //就算是老记录未有guid,当ToTable时已经生成了guid
            ExChange(AllNoClosedChances.Values.ToList <ChanceClass>());                            //执行交易提供可视化
        }