Пример #1
0
        public static double AverageCommMembers(CommCollection cc)
        {
            double sum = 0;
            for (int i = 0; i < cc.CommOverlap.Rows; i++)
                sum += cc.CommOverlap[i, i];

            return (double)sum / (double)cc.CommOverlap.Rows;
        }
Пример #2
0
        public static double AverageBlockSize(CommCollection cc)
        {
            int sum = 0;
            for (int i = 0; i < cc.Count; ++i)
                sum += cc[i].Size;

            return (double)sum / (double)cc.Count;
        }
Пример #3
0
        // new code to get GOI for communities
        // above function is not correct
        /*
        public static double ComGOI(Matrix commDensity)
        {
            double GOI = 0.0;

            return GOI;
        }
        */
        public static Pair<double, double> COC(CommCollection cc)
        {
            double simple_sum = 0, complex_sum = 0;

            Matrix CBCO = cc.CommByCommOverlap;
            Vector v = CBCO as Vector;

            if (CBCO.Cols == 1)
                return new Pair<double, double>(0.0, 0.0);

            double denominator = (double)(((double)CBCO.Cols * ((double)CBCO.Cols - 1)) / 2.0);

            if (v != null)
            {
                Vector w = new Vector(v.Size);
                w.Clear();
                for (int i = 0; i < v.Size; ++i)
                {
                    for (int j = i + 1; j < v.Size; ++j)
                        w[j] += cc.GetCommByCommOverlap(i, j);
                }

                for (int j = 1; j < v.Size; ++j)
                {
                    double sum = w[j];
                    simple_sum += (sum / v[j]);
                    if (v[j] > 1)
                        complex_sum += (sum / (v[j] - 1));
                }
            }
            else
            {
                for (int j = 1; j < CBCO.Rows; ++j)
                {
                    double sum = 0.0;

                    for (int i = 0; i < j; ++i)
                        sum += CBCO[i, j];

                    simple_sum += sum / CBCO[j, j];
                    if (CBCO[j, j] > 1)
                        complex_sum += sum / (CBCO[j, j] - 1);
                }

            }

            return new Pair<double, double>(simple_sum / denominator, complex_sum / denominator);
        }
Пример #4
0
        public static double CMOI(CommCollection cc)
        {
            Matrix O = cc.CommOverlap;
            double total_sum = 0;
            for (int i = 0; i < O.Rows; i++)
                for (int j = i + 1; j < O.Rows; j++)
                {
                    if (O[j, j] != 0)
                        total_sum += (double)(O[i, j]) / O[j, j];
                }

            double coi = (double)(O.Rows * O.Rows - O.Rows);
            if (coi != 0)
                coi = (total_sum * 2.0) / coi;
            return coi;
        }
Пример #5
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();

            if (sc == null || sc.Table == null || sc.Table.Rows.Count < this.ReviewExpectCnt)
            {
                return(ret);
            }
            DataTableEx dt = sc.CarTable;

            if (dt == null)
            {
                throw new Exception("无法获得概率分布表!");
            }
            //Log("最后一期数据", string.Join(",", dt.Rows[0].ItemArray));
            //Log("最后二期数据", string.Join(",", dt.Rows[1].ItemArray));
            string     val = dt.Rows[dt.Rows.Count - 1][string.Format("{0}", InputMinTimes)].ToString();
            OnceChance cc  = new OnceChance();

            cc.SignExpectNo = ed.Expect;
            cc.ChanceType   = 1;
            string code_model = "{0}/{1}";

            HoldCnt++;
            cc.ChanceCode          = BySer ? string.Format(code_model, InputMaxTimes, val) : string.Format(code_model, val, InputMaxTimes);
            cc.ChipCount           = 1;//码数必须是实际码数,使用的金额队列必须是1码的队列
            cc.InputTimes          = HoldCnt;
            cc.strInputTimes       = string.Format("{0}", cc.InputTimes);
            cc.AllowMaxHoldTimeCnt = 1;
            cc.InputExpect         = ed;
            cc.StragId             = this.GUID;
            cc.NeedConditionEnd    = true;//这是必须的,才能在整体回测中触发CheckNeedEndTheChance
            //cc.MinWinRate = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
            cc.IsTracer = 0;
            cc.Odds     = this.CommSetting.GetGlobalSetting().Odds;
            //cc.UnitCost = this.getChipAmount(0,cc,);
            ret.Add(cc);
            return(ret);
        }
Пример #6
0
        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);
            }
        }
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();
            DataTableEx        dt  = null;

            if (this.BySer)
            {
                dt = sc.SerialDistributionTable;
            }
            else
            {
                dt = sc.CarDistributionTable;
            }
            if (dt == null)
            {
                throw new Exception("无法获得概率分布表!");
            }
            string strCodes   = "";
            int    AllChipCnt = 0;

            for (int i = 0; i < 10; i++)
            {
                //获得各项的最小的
                List <double> coldata = null;
                string        strCol  = string.Format("{0}", (i + 1) % 10);
                dt.getColumnData(strCol, ref coldata);
                double    avgval  = coldata.Average();
                double    stdval  = ProbMath.CalculateStdDev(coldata);
                string    strSql  = string.Format("[{0}]<{1}", strCol, avgval - this.StdvCnt * stdval);
                string    strSort = string.Format("[{0}] asc", "Id");
                DataRow[] drs     = dt.Select(strSql, strSort);
                if (drs.Length < this.ChipCount)
                {
                    continue;
                }

                string        strCode = "";
                StringBuilder sb      = new StringBuilder();
                bool          Matched = false;
                for (int j = 0; j < drs.Length; j++)
                {
                    string strId  = drs[j]["Id"].ToString();
                    int    RowCnt = sc.FindLastDataExistCount(this.InputMinTimes, strCol, strId);
                    if (RowCnt > 0)//任何一个不匹配最近5期内出现,不满足条件
                    {
                        Matched = false;
                        break;
                    }
                    sb.Append(drs[j]["Id"].ToString());
                    Matched = true;
                }
                if (!Matched)
                {
                    continue;
                }
                AllChipCnt += drs.Length;
                if (BySer)
                {
                    strCode = string.Format("{0}/{1}", strCol, sb.ToString());
                }
                else
                {
                    strCode = string.Format("{0}/{1}", sb.ToString(), strCol);
                }
                if (strCode.Length > 0)
                {
                    strCodes = string.Format("{0}{1}{2}", strCodes, strCodes.Length > 0?"+":"", strCode);
                }
            }
            if (strCodes.Length < 2 * (this.ChipCount + 2))
            {
                return(ret);
            }
            ChanceClass cc = new ChanceClass();

            cc.SignExpectNo        = ed.Expect;
            cc.ChanceType          = 3;
            cc.InputTimes          = 1;
            cc.strInputTimes       = "1";
            cc.AllowMaxHoldTimeCnt = 1;
            cc.InputExpect         = ed;
            cc.ChipCount           = AllChipCnt;
            cc.ChanceCode          = strCodes;
            cc.CreateTime          = ed.OpenTime;
            cc.Closed = false;
            ret.Add(cc);
            return(ret);
        }
Пример #8
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();

            if (sc == null || sc.Table == null || sc.Table.Rows.Count < this.ReviewExpectCnt)
            {
                return(ret);
            }
            if (this.getAllStdDev() == null)
            {
                this.setAllStdDev(new Dictionary <string, List <double> >());
            }
            double        stdval = 0;
            List <double> list   = sc.getAllDistrStdDev(this.ReviewExpectCnt, this.InputMaxTimes);

            if (!this.getAllStdDev().ContainsKey(ed.Expect))
            {
                stdval = list[10];//最后一位
                this.getAllStdDev().Add(ed.Expect, list);
            }
            if (getAllStdDev().Count < 20)
            {
                return(ret);
            }
            MA ma20 = new MA(getAllStdDev().Values.Select(p => p[10]).ToArray(), 20);
            MA ma5  = new MA(getAllStdDev().Values.Select(p => p[10]).ToArray(), 5);

            if (!IsTracing)
            {
                //if (stdval > 0.2) return ret;
                if (ma20.IsDownCross())//下穿均线,开始跟踪
                {
                    IsTracing = true;
                }
                else
                {
                    return(ret);
                }
            }
            else
            {
                ////if (stdval > 0.2)
                ////{
                ////    IsTracing = false;
                ////    return ret;
                ////}
                if (ma5.IsUpCross())//上穿均线,停止跟踪
                {
                    IsTracing = false;
                    return(ret);
                }
            }
            strag_CommOldClass strag = new strag_CommOldClass();

            strag.BySer         = this.BySer;
            strag.ChipCount     = this.ChipCount;
            strag.InputMinTimes = this.InputMinTimes;
            List <ChanceClass>          tmp = strag.getChances(sc, ed);
            Dictionary <string, double> dic = new Dictionary <string, double>();

            for (int i = 0; i < 10; i++)
            {
                if (list[i] < stdval)//小于整体的才有效
                {
                    string strcol = string.Format("{0}", (i + 1) % 10);
                    dic.Add(strcol, list[i]);
                }
            }
            for (int i = 0; i < tmp.Count; i++)
            {
                if (dic.ContainsKey(tmp[i].GetCodeKey(this.BySer)))
                {
                    ret.Add(tmp[i]);
                }
            }
            return(ret);
        }
Пример #9
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();

            //ExpectListProcess elp = new ExpectListProcess(Data);
            //CommCollection sc = elp.getSerialData(ReviewExpectCnt,BySer);
            for (int i = 0; i < 10; i++)
            {
                Dictionary <int, string> SerStrs = sc.Data[i];
                int ishift = sc.Data[0].Count - 1;
                while (ishift >= this.InputMinTimes)
                {
                    if (SerStrs.ContainsKey(ishift))
                    {
                        if (SerStrs[ishift].Trim().Length == (10 - this.ChipCount))
                        {
                            if (this.ExcludeBS && ChanceClass.isBS(SerStrs[ishift].Trim()))
                            {
                                break;
                            }
                            if (this.ExcludeSD && ChanceClass.isSD(SerStrs[ishift].Trim()))
                            {
                                break;
                            }
                            if (this.OnlyBS && !ChanceClass.isBS(SerStrs[ishift].Trim()))
                            {
                                break;
                            }
                            if (this.OnlySD && !ChanceClass.isSD(SerStrs[ishift].Trim()))
                            {
                                break;
                            }
                            ChanceClass cc = new ChanceClass();
                            cc.SignExpectNo = ed.Expect;
                            cc.ChanceType   = 2;
                            cc.InputTimes   = ishift;
                            cc.InputExpect  = ed;
                            string strCode = "";
                            if (BySer)
                            {
                                strCode = string.Format("{0}/{1}", (i + 1) % 10, SerStrs[ishift].Trim());
                            }
                            else
                            {
                                strCode = string.Format("{0}/{1}", SerStrs[ishift].Trim(), i);
                            }
                            cc.ChanceCode = ChanceClass.getRevChance(strCode);
                            cc.CreateTime = ed.OpenTime;
                            cc.Closed     = false;
                            ret.Add(cc);
                            break;
                        }
                        else
                        {
                            ishift--;
                        }
                    }
                }
            }
            return(ret);
        }
Пример #10
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();

            return(ret);
        }
Пример #11
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();

            CurrChancesCnt = 0;//置零,防止后面留存
            if (this.LastUseData().Count < ReviewExpectCnt)
            {
                Log("基础数据数量不足", string.Format("小于回览期数:{0}", ReviewExpectCnt));
                return(ret);
            }
            ExpectList LastDataList = this.LastUseData();
            ExpectData PreData      = LastDataList[LastDataList.Count - 2];
            //Log(string.Format("前期{0}:{1}", PreData.Expect, PreData.OpenCode), string.Format("当期{0}:{1}", LastDataList.LastData.Expect, LastDataList.LastData.OpenCode));
            //Log(string.Format("el数据长度:{0},First:{1};Last{2}", LastDataList.Count,LastDataList.FirstData.Expect,LastDataList.LastData.Expect), string.Format("原始数据长度:{0};First:{1};Last:{2}", sc.orgData.Count,sc.orgData.FirstData.Expect,sc.orgData.LastData.Expect));
            DataTableEx   dt       = sc.getSubTable(sc.orgData.Count - this.ReviewExpectCnt, this.ReviewExpectCnt);
            List <string> strCodes = new List <string>();

            for (int i = 0; i < 10; i++)//遍历每个车号/名次
            {
                List <int> coldata = null;
                string     strCol  = string.Format("{0}", (i + 1) % 10);
                dt.getColumnData(strCol, ref coldata);
                //Log(string.Format("车/次:{0}",strCol), string.Format("取得的数据:{0}",string.Join(",",coldata)));
                int RepeatCnt = 0;
                for (int j = 1; j < ReviewExpectCnt; j++)
                {
                    if (coldata[j] == coldata[j - 1])
                    {
                        RepeatCnt++;
                    }
                }
                if (RepeatCnt < this.ReviewExpectCnt - 1)//如果重复次数小于回顾次数减一,表示重复次数不够,跳过
                {
                    continue;
                }
                string strCode = "";//其实无需比较,对单个车/名次来说,矩阵都一样,策略只需建立一个即可
                strCode = string.Format("{0}/{1}", strCol, coldata[0]);

                //Log(string.Format("车/次:{0}", strCol), strCode);
                strCodes.Add(strCode);

                if (HoldCnt >= 0) //当持有次数超过指定次数后,不再增加
                {
                    HoldCnt++;    //持有次数加1
                }
                //Log("获得机会处理", string.Format("当前持有次数:{0}", HoldCnt));
                RealCnt++;
            }

            if (strCodes.Count == 0)//机会数为0
            {
                return(ret);
            }
            if (!GetRev)
            {
                OnceChance cc = new OnceChance();
                cc.SignExpectNo        = ed.Expect;
                cc.ChanceType          = 1;
                cc.ChanceCode          = string.Join("+", strCodes);
                cc.ChipCount           = strCodes.Count;//码数必须是实际码数,使用的金额队列必须是1码的队列
                cc.InputTimes          = RealCnt;
                cc.strInputTimes       = string.Format("{0}", cc.InputTimes);
                cc.AllowMaxHoldTimeCnt = 1;
                cc.InputExpect         = ed;
                cc.NeedConditionEnd    = true;
                cc.StragId             = this.GUID;
                //cc.MinWinRate = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                cc.IsTracer    = 0;
                cc.Odds        = this.CommSetting.GetGlobalSetting().Odds;
                CurrChancesCnt = strCodes.Count;
                //cc.UnitCost = this.getChipAmount(0,cc,);
                ret.Add(cc);
                //获得所有机会后,统一为本次机会分配资金
                return(ret);
            }
            else
            {
                for (int i = 0; i < strCodes.Count; i++)
                {
                    NolimitTraceChance cc = new NolimitTraceChance();
                    cc.SignExpectNo        = ed.Expect;
                    cc.ChanceType          = 1;
                    cc.ChanceCode          = ChanceClass.getRevChance(strCodes[i]);
                    cc.ChipCount           = 9;//码数必须是实际码数,使用的金额队列必须是1码的队列
                    cc.InputTimes          = RealCnt;
                    cc.strInputTimes       = string.Format("{0}", cc.InputTimes);
                    cc.AllowMaxHoldTimeCnt = 1;
                    cc.InputExpect         = ed;
                    cc.StragId             = this.GUID;
                    //cc.MinWinRate = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                    cc.IsTracer    = 1;
                    cc.Odds        = this.CommSetting.GetGlobalSetting().Odds;
                    CurrChancesCnt = strCodes.Count;
                    //cc.UnitCost = this.getChipAmount(0,cc,);
                    ret.Add(cc);
                }
                return(ret);
            }
        }
Пример #12
0
        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>());                            //执行交易提供可视化
        }
Пример #13
0
        public static double NPOL(CommCollection cc, Matrix data)
        {
            int total_sum = 0;
            for (int i = 0; i < cc.Count; i++)
            {
                total_sum += cc[i].Size * (data.Rows - cc[i].Size);
            }

            double first_term = 4.0 / (double)(cc.Count * data.Rows * data.Rows);

            return first_term * total_sum;
        }
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            InitAllPeaks();//初始化峰值列表
            List <ChanceClass> ret = new List <ChanceClass>();

            if (sc == null || sc.Table == null || sc.Table.Rows.Count < this.ReviewExpectCnt)
            {
                return(ret);
            }
            int AllItemCnt = AllBinomPeaks.Count;
            Dictionary <string, Dictionary <int, int> > AllList = new Dictionary <string, Dictionary <int, int> >();

            for (int i = 0; i < 10; i++)//遍历各车/各名次
            {
                string strCol = string.Format("{0}", (i + 1) % 10);
                //string strVal = ed.ValueList[i];
                Dictionary <int, int> ValCntItems = new Dictionary <int, int>();
                for (int val = 0; val < 10; val++)//对每个数字出现的次数检查
                {
                    ValCntItems.Add(val, 0);
                    string strVal = string.Format("{0}", val);
                    foreach (string key in AllBinomPeaks.Keys)                             //遍历所有峰值清单
                    {
                        int ViewCnt  = int.Parse(key.Split('_')[0]);                       //获得峰值清单对应的实验次数
                        int ExistCnt = sc.FindLastDataExistCount(ViewCnt, strCol, strVal); //获得前N-1次该车次出现的次数
                        if (AllBinomPeaks[key].Contains(ExistCnt + 1))                     //如果该二项分布检查的峰值是7,8,9,值出现的次数是6,7,8,匹配,+1
                        {
                            ValCntItems[val] = ValCntItems[val] + 1;
                        }
                    }
                }
                //满足所有二项分布的才入选
                Dictionary <int, int> ValidItems       = ValCntItems.Where(c => c.Value == AllItemCnt).ToDictionary(a => a.Key, b => b.Value);
                Dictionary <int, int> MatchPoisonItems = new Dictionary <int, int>();
                foreach (int key in ValidItems.Keys)
                {
                    int ViewCnt  = MinFilterCnt - 1;                                           //找出所有值在前100次里出现的次数
                    int ExistCnt = sc.FindLastDataExistCount(ViewCnt, strCol, key.ToString()); //满足最泊松分布最顶峰
                    if (ExistCnt == ViewCnt / 10 - 1)
                    {
                        MatchPoisonItems.Add(key, ValidItems[key]);
                    }
                }
                AllList.Add(strCol, MatchPoisonItems);
            }
            //检查所有满足条件的清单

            Dictionary <string, int> AllCodes = new Dictionary <string, int>();
            List <string>            strCnts  = new List <string>();
            bool RetMutliChances = this.FixChipCnt;

            foreach (string strcol in AllList.Keys)
            {
                if (AllList[strcol].Count == 0)
                {
                    continue;
                }
                int MaxCnt = AllList[strcol].Values.Max();//选最优
                //Dictionary<int,int> strCodeList = AllList[strcol].Where(a => a.Value == MaxCnt).ToDictionary(a => a.Key, b => b.Value);
                Dictionary <int, int> strCodeList = AllList[strcol];
                string StrCode = "";
                string StrVal  = string.Join("", strCodeList.Keys.ToArray());
                string strCnt  = string.Join("_", strCodeList.Values.ToArray());
                strCnts.Add(strCnt);
                if (BySer)
                {
                    StrCode = string.Format("{0}/{1}", strcol, StrVal);
                }
                else
                {
                    StrCode = string.Format("{0}/{1}", StrVal, strcol);
                }
                AllCodes.Add(StrCode, strCodeList.Count);
            }
            Log("所有选出来的记录", string.Join("+", AllCodes.Keys.ToArray()));
            if (AllCodes.Count == 0)
            {
                return(ret);
            }
            if (RetMutliChances)
            {
                foreach (string key in AllCodes.Keys)
                {
                    int Chips = AllCodes[key];
                    if (Chips == 1)
                    {
                        continue;
                    }
                    if (Chips < this.ChipCount)
                    {
                        continue;
                    }
                    ChanceClass cc = new ChanceClass();
                    cc.ChanceCode    = key;
                    cc.SignExpectNo  = ed.Expect;
                    cc.ChanceType    = 2;
                    cc.ChipCount     = AllCodes[key];
                    cc.InputTimes    = 1;
                    cc.strInputTimes = "";// string.Join("_", strCnts.ToArray());
                    //cc.AllowMaxHoldTimeCnt = this.AllowMaxHoldTimeCnt;
                    cc.InputExpect = ed;
                    cc.StragId     = this.GUID;
                    //cc.MinWinRate = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                    cc.IsTracer       = 0;
                    cc.HoldTimeCnt    = 1;
                    cc.Odds           = this.CommSetting.GetGlobalSetting().Odds;
                    cc.ExpectCode     = ed.Expect;
                    cc.CreateTime     = DateTime.Now;
                    cc.UpdateTime     = DateTime.Now;
                    cc.MaxHoldTimeCnt = 1;
                    cc.Closed         = false;
                    ret.Add(cc);
                }
                return(ret);
            }
            ////if (AllCodes.Count == 1 && AllCodes.First().Value == 1)//独此一份
            ////    return ret;
            string strAllCode = string.Join("+", AllCodes.Keys.ToArray());

            if (ChanceClass.getChipsByCode(strAllCode) < this.ChipCount)
            {
                return(ret);
            }
            if (!RetMutliChances)
            {
                ChanceClass cc = new ChanceClass();
                cc.SignExpectNo  = ed.Expect;
                cc.ChanceType    = 2;
                cc.ChipCount     = AllCodes.Sum(p => p.Value);
                cc.InputTimes    = 1;
                cc.strInputTimes = string.Join("_", strCnts.ToArray());
                //cc.AllowMaxHoldTimeCnt = this.AllowMaxHoldTimeCnt;
                cc.InputExpect = ed;
                cc.StragId     = this.GUID;
                //cc.MinWinRate = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                cc.IsTracer       = 0;
                cc.HoldTimeCnt    = 1;
                cc.Odds           = this.CommSetting.GetGlobalSetting().Odds;
                cc.ExpectCode     = ed.Expect;
                cc.ChanceCode     = string.Join("+", AllCodes.Keys.ToArray());
                cc.CreateTime     = DateTime.Now;
                cc.UpdateTime     = DateTime.Now;
                cc.MaxHoldTimeCnt = 1;
                cc.Closed         = false;
                ret.Add(cc);
            }

            return(ret);
        }
Пример #15
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass> ret = new List <ChanceClass>();

            //ExpectListProcess elp = new ExpectListProcess(Data);
            //CommCollection sc = elp.getSerialData(ReviewExpectCnt,BySer);
            //            Log("策略关键参数",string.Format("name:{4};ViewCnt:{0};MinInput:{1};DataCnt:{2};Chipcnt:{3}",this.ReviewExpectCnt,this.InputMinTimes,sc.Data[0].Count,this.ChipCount,this.StragScript));
            for (int i = 0; i < 10; i++)
            {
                Dictionary <int, string> SerStrs = sc.Data[i];
                if (!SerStrs.ContainsKey(this.InputMinTimes - 1))
                {
                    continue;
                }
                if (SerStrs[this.InputMinTimes - 1].Length < this.ChipCount)
                {
                    continue;
                }
                int ishift = this.InputMinTimes - 1;
                //Log("起始位置", ishift.ToString());
                List <string> strCcs = ChanceClass.getAllSubCode(SerStrs[ishift].Trim(), this.ChipCount);
                for (int j = 0; j < strCcs.Count; j++)
                {
                    string strCars = strCcs[j];
                    if (this.ExcludeBS && ChanceClass.isBS(strCars))
                    {
                        continue;
                    }
                    if (this.ExcludeSD && ChanceClass.isSD(strCars))
                    {
                        continue;
                    }
                    if (this.OnlyBS && !ChanceClass.isBS(strCars))
                    {
                        continue;
                    }
                    if (this.OnlySD && !ChanceClass.isSD(strCars))
                    {
                        continue;
                    }
                    NolimitTraceChance cc = new NolimitTraceChance();
                    cc.SignExpectNo  = ed.Expect;
                    cc.ChanceType    = 0;
                    cc.ChipCount     = strCars.Length;
                    cc.InputTimes    = ishift + 1;
                    cc.strInputTimes = cc.InputTimes.ToString();
                    //cc.AllowMaxHoldTimeCnt = this.AllowMaxHoldTimeCnt;
                    cc.InputExpect = ed;
                    cc.StragId     = this.GUID;
                    cc.MinWinRate  = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                    cc.IsTracer    = 1;
                    cc.HoldTimeCnt = 1;
                    cc.Odds        = this.CommSetting.GetGlobalSetting().Odds;
                    cc.ExpectCode  = ed.Expect;
                    string strCode = "";
                    if (BySer)
                    {
                        strCode = string.Format("{0}/{1}", (i + 1) % 10, strCcs[j].Trim());
                    }
                    else
                    {
                        strCode = string.Format("{0}/{1}", strCcs[j].Trim(), i);
                    }
                    cc.ChanceCode = strCode;
                    cc.CreateTime = DateTime.Now;
                    cc.UpdateTime = DateTime.Now;
                    cc.Closed     = false;
                    ret.Add(cc);
                }
                //}
                //}
            }
            return(ret);
        }
Пример #16
0
 public abstract List <ChanceClass> getChances(CommCollection sc, ExpectData ed);
Пример #17
0
        public override List <ChanceClass> getChances(CommCollection sc, ExpectData ed)
        {
            List <ChanceClass>          ret       = new List <ChanceClass>();
            Dictionary <string, string> MatchCols = new Dictionary <string, string>();
            Dictionary <string, int>    MatchCnt  = new Dictionary <string, int>();
            int    MutliMinCnt = this.CommSetting.GetGlobalSetting().SingleColMinTimes;
            int    TwoMinCnt   = (this.CommSetting.GetGlobalSetting().MinTimeForChance(1) + this.CommSetting.GetGlobalSetting().MinTimeForChance(2)) / 2;
            string colmodel    = "{0}_{1}";//列_长度

            for (int i = 0; i < 10; i++)
            {
                int ishift = MutliMinCnt;
                if (!sc.Data[i].ContainsKey(ishift) || sc.Data[i][ishift].Trim().Length == 0) //不及最小值,跳过
                {
                    continue;
                }
                string strCol = string.Format(colmodel, i, sc.Data[i][ishift].Length);
                if (!MatchCols.ContainsKey(strCol))
                {
                    MatchCols.Add(strCol, sc.Data[i][ishift]);
                    MatchCnt.Add(strCol, ishift + 1);//实际数量
                }
                ishift++;
                while (sc.Data[i].ContainsKey(ishift) && sc.Data[i][ishift].Trim().Length > 0) //一直往上找
                {
                    string lastVal = sc.Data[i][ishift];                                       //可能不止一个数字
                    strCol = string.Format(colmodel, i, lastVal.Length);
                    if (!MatchCols.ContainsKey(strCol))
                    {
                        MatchCols.Add(strCol, lastVal);
                        MatchCnt.Add(strCol, ishift + 1);//实际数量
                    }
                    else
                    {
                        MatchCnt[strCol] = MatchCnt[strCol] + 1;
                    }
                    ishift++;
                }
                //ishift--;
            }
            //替换列中数据长度大于1的数据,只留最短的
            for (int i = 0; i < 10; i++)
            {
                string keymodel = "{0}_{1}";
                string key      = string.Format(keymodel, i, 1);
                if (!MatchCols.ContainsKey(key))//如果1个都不存在,跳过
                {
                    continue;
                }
                string strVal = MatchCols[key];
                int    k      = 2;
                key = string.Format(keymodel, i, k);
                while (MatchCols.ContainsKey(key))//逐级替换
                {
                    string tmp = MatchCols[key];
                    MatchCols[key] = MatchCols[key].Replace(strVal, "");
                    strVal         = tmp;
                    k++;
                    key = string.Format(keymodel, i, k);
                }
            }
            if (MatchCols.Count > 1)
            {
                Log("50以上的组合", string.Join("+", MatchCols.Select(p => string.Format("{0}/{1}:{2}", p.Key, p.Value, MatchCnt[p.Key])).ToArray()));
            }
            if (MatchCols.Count < 2)
            {
                return(ret);
            }
            List <string> strCodes  = new List <string>();
            List <string> strCodes2 = new List <string>();
            List <int>    intCnt2   = new List <int>();

            foreach (string key in MatchCnt.Keys)
            {
                string[] keys    = key.Split('_');
                string   strCode = string.Format("{0}/{1}", (int.Parse(keys[0]) + 1) % 10, MatchCols[key]);
                if (MatchCnt[key] > TwoMinCnt)//如果2个数小于58
                {
                    strCodes2.Add(strCode);
                    intCnt2.Add(MatchCnt[key]);
                }
                strCodes.Add(strCode);
            }
            if (strCodes.Count == 2)
            {
                if (strCodes2.Count < 2)
                {
                    return(ret);
                }
            }
            List <string> strCommCodes = new List <string>();
            List <string> strCommCnts  = new List <string>();

            if (strCodes2.Count >= 2) //合成2个的组合
            {
                Log("50以上机会:", string.Join("+", strCodes2.ToArray()));
                for (int i = 0; i < strCodes2.Count; i++)
                {
                    for (int j = i + 1; j < strCodes2.Count; j++)
                    {
                        strCommCodes.Add(string.Format("{0}+{1}", strCodes2[i], strCodes2[j]));
                        strCommCnts.Add(string.Format("{0}_{1}", intCnt2[i], intCnt2[j]));
                    }
                }
            }
            for (int i = 0; i < strCommCodes.Count; i++)
            {
                NolimitTraceChance cc = new NolimitTraceChance();
                cc.SignExpectNo  = ed.Expect;
                cc.ChanceType    = 0;
                cc.ChipCount     = 2;
                cc.InputTimes    = MatchCnt.Values.Min <int>();
                cc.strInputTimes = strCommCnts[i];
                //cc.AllowMaxHoldTimeCnt = this.AllowMaxHoldTimeCnt;
                cc.InputExpect = ed;
                cc.StragId     = this.GUID;
                cc.MinWinRate  = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                cc.IsTracer    = 1;
                cc.HoldTimeCnt = 1;
                cc.Odds        = this.CommSetting.GetGlobalSetting().Odds;
                cc.ExpectCode  = ed.Expect;
                cc.ChanceCode  = strCommCodes[i];
                cc.CreateTime  = DateTime.Now;
                cc.UpdateTime  = DateTime.Now;
                cc.Closed      = false;
                ret.Add(cc);
            }
            if (strCodes.Count > 2)
            {
                string             strAllCodes = string.Join("+", strCodes.ToArray());
                NolimitTraceChance cc          = new NolimitTraceChance();
                cc.SignExpectNo  = ed.Expect;
                cc.ChanceType    = 0;
                cc.ChipCount     = strCodes.Count;
                cc.InputTimes    = MatchCnt.Values.Min <int>();
                cc.strInputTimes = string.Join("_", MatchCnt.Values.ToArray <int>());
                //cc.AllowMaxHoldTimeCnt = this.AllowMaxHoldTimeCnt;
                cc.InputExpect = ed;
                cc.StragId     = this.GUID;
                cc.MinWinRate  = this.CommSetting.GetGlobalSetting().DefaultHoldAmtSerials.MaxRates[cc.ChipCount - 1];
                cc.IsTracer    = 1;
                cc.HoldTimeCnt = 1;
                cc.Odds        = this.CommSetting.GetGlobalSetting().Odds;
                cc.ExpectCode  = ed.Expect;
                cc.ChanceCode  = strAllCodes;
                cc.CreateTime  = DateTime.Now;
                cc.UpdateTime  = DateTime.Now;
                cc.Closed      = false;
                ret.Add(cc);
            }
            return(ret);
        }