Ejemplo n.º 1
0
        bool CheckFinished()
        {
            string path        = @"c:\inetpub\wwwroot\PK10\InstData\";
            string strExpectNo = "expectNo";
            string strResult   = "record";
            string strForApp   = "expertNoForApp";
            string strtype     = "txt";

            this.FinishedThreads++;
            //if (IsTestBack) return true; //如果是回测,不做处理
            //Log("进程结束", string.Format("目标{1},现有{0}",this.FinishedThreads,this.RunThreads));

            if (FinishedThreads == RunThreads)
            {
                ThreadPools = new List <Thread>();
                if (IsTestBack)
                {
                    return(true);            //如果是回测,不做处理
                }
                Log("写入标志文件", "供web程序读取!");
                string NewNo = string.Format("{0}|{1}", long.Parse(Program.AllServiceConfig.LastDataSector.LastData.Expect) + 1, Program.AllServiceConfig.LastDataSector.LastData.OpenTime);
                new LogInfo().WriteFile(NewNo, path, strExpectNo, strtype, true, true);

                //保存策略
                GlobalClass.SaveStragList(StragClass.getXmlByObjectList <StragClass>(Program.AllServiceConfig.AllStrags.Values.ToList <StragClass>()));
                Log("保存策略清单", "保存成功");
            }
            return(true);
        }
Ejemplo n.º 2
0
 public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
 {
     if (provider != null)
     {
         this.editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
     }
     if (this.editorService != null)
     {
         AllList  = StragClass.getObjectListByXml <StragClass>(GlobalClass.ReReadStragList());
         AllPlans = StragRunPlanClass.getObjectListByXml <StragRunPlanClass>(GlobalClass.getStragRunningPlan(true)).ToDictionary(t => t.GUID, t => t);
         List <StragClass> list = AllList.Where(t => AllPlans.ContainsKey(t.GUID) == false).ToList <StragClass>();
         picker = new StragPicker(AllList);//支持一组合对多相同策略
         editorService.ShowDialog(picker);
         if (picker.SelectedStrag == null)
         {
             value = null;
         }
         else
         {
             value = picker.SelectedStrag;
             //picker.Hide();
         }
         ////if (value == null)
         ////{
         ////    picker.Show();
         ////    picker.Visible = true;
         ////}
         ////else
         ////{
         ////}
     }
     return(value);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 记录交易,可以可视化查看交易结果及查看资金曲线,同时复利策略的计划必须绑定资产单元
        /// </summary>
        /// <param name="list"></param>
        /// <param name="newList"></param>
        /// <returns></returns>
        bool ExChange(List <ChanceClass> list)
        {
            ////List<ChanceClass> list = new List<ChanceClass>();
            ////Oldlist.Values.ToList<ChanceClass>().ForEach(p => list.Add(p));
            ////newList.ForEach(p => list.Add(p));
            for (int i = 0; i < list.Count; i++)
            {
                ChanceClass cc = list[i];
                StragClass  sc = UseStrags[cc.StragId];
                if (sc.AssetUnitId == null)
                {
                    continue; //所属计划未指定资产单元,不记录交易信息
                }
                if (!UseAssetUnits.ContainsKey(sc.AssetUnitId))
                {
                    continue;//所属分类无记录的资产单元,不记录信息
                }
                AssetUnitClass uu = UseAssetUnits[sc.AssetUnitId];
                ExchangeChance ec = new ExchangeChance(uu.ExchangeServer, sc, cc.ExpectCode, cc);

                ec.ExchangeAmount = cc.UnitCost;
                ec.ExchangeRate   = cc.UnitCost / uu.ExchangeServer.summary;
                if (uu.ExchangeServer.Push(ref ec))
                {
                    AllExchance.Add(ec);
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
 void RefrshStragAndPlan()
 {
     try
     {
         if (UseSetting == null)
         {
             return;
         }
         DataTable dt_strag = StragClass.ToTable <StragClass>(UseSetting.AllStrags.Values.ToList <StragClass>());
         if (dt_strag != null)
         {
             SetDataGridDataTable(dg_StragList, dt_strag);
         }
         DataTable dt_plans = StragRunPlanClass.ToTable <StragRunPlanClass>(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass>());
         if (dt_plans != null)
         {
             SetDataGridDataTable(dg_stragStatus, dt_plans);
         }
         DataTable dt_grps = CalcStragGroupClass.ToTable <CalcStragGroupClass>(UseSetting.AllRunningPlanGrps.Values.ToList <CalcStragGroupClass>());
         if (dt_grps != null)
         {
             SetDataGridDataTable(dg_PlanGrps, dt_grps);
         }
         DataTable dt_assetunit = AssetUnitClass.ToTable <AssetUnitClass>(UseSetting.AllAssetUnits.Values.ToList <AssetUnitClass>());
         if (dt_assetunit != null)
         {
             SetDataGridDataTable(dg_AssetUnits, dt_assetunit);
         }
         refresh_AssetChart();
     }
     catch (Exception e)
     {
     }
 }
Ejemplo n.º 5
0
 public ExchangeChance(ExchangeService _es, StragClass sc, string CurrExpectNo, ChanceClass cc)
 {
     _cc       = cc;
     _sc       = sc;
     _InExpect = CurrExpectNo;
     es        = _es;
 }
Ejemplo n.º 6
0
        bool Update(bool DeleteOperate)
        {
            StragClass sc = this.propertyGrid1.SelectedObject as StragClass;

            if (sc == null)
            {
                MessageBox.Show("请先双击选择要操作的策略!");
                return(false);
            }
            if (!this.AllList.ContainsKey(sc.GUID))
            {
                MessageBox.Show("选择的策略不存在!");
                return(false);
            }
            this.AllList[sc.GUID] = sc;
            if (DeleteOperate)
            {
                if (!this.AllList.Remove(sc.GUID))
                {
                    MessageBox.Show("无法删除选择策略!");
                    return(false);
                }
                this.propertyGrid1.SelectedObject = null;
            }
            SaveList();
            RefreshGrid();
            MessageBox.Show(string.Format("{0}成功!", DeleteOperate?"删除":"保存"));
            return(true);
        }
Ejemplo n.º 7
0
        void SaveList()
        {
            List <StragClass> CurrList = AllList.Values.ToList <StragClass>();

            //Program.AllGlobalSetting.gc.setStragXml(StragClass.getXmlByObjectList<StragClass>(CurrList));
            GlobalClass.SaveStragList(StragClass.getXmlByObjectList <StragClass>(CurrList));
            Program.AllGlobalSetting.AllStrags = AllList;
        }
Ejemplo n.º 8
0
        private void frm_NewStrag_Load(object sender, EventArgs e)
        {
            //this.propertyGrid1.SelectedObject = new StragClass();
            DataTable dt = StragClass.getAllStrags();
            DataView  dv = new DataView(dt);

            this.ddl_StragObjects.DataSource    = dt;
            this.ddl_StragObjects.DisplayMember = "text";
            this.ddl_StragObjects.ValueMember   = "value";
            //this.ddl_StragObjects.Tag = dt;
            DllLoaded = true;
        }
Ejemplo n.º 9
0
        private void btn_Save_Click(object sender, EventArgs e)
        {
            StragClass sc = this.propertyGrid1.SelectedObject as StragClass;

            if (sc == null)
            {
                return;
            }
            //sc.CommSetting.SetGlobalSetting(Program.gc);
            RetJson = sc;
            Saved   = true;
            this.Close();
        }
Ejemplo n.º 10
0
        void RefreshGrid()
        {
            List <StragClass> CurrList = AllList.Values.ToList <StragClass>();

            if (CurrList == null)
            {
                return;
            }
            DataTable dt = StragClass.GetTableByStragList(CurrList);

            this.dg_Strags.DataSource = dt;
            this.dg_Strags.Refresh();
            this.dg_Strags.Tag = CurrList;
        }
Ejemplo n.º 11
0
 private void tmi_Edit_Click(object sender, EventArgs e)
 {
     try
     {
         ToolStripMenuItem menu = sender as ToolStripMenuItem;
         DataGridView      dg   = (menu.Owner as ContextMenuStrip).SourceControl as DataGridView;
         if (dg == null)
         {
             return;
         }
         if (dg.Equals(this.dg_StragList))
         {
             StragClass strag = getGridAfterMouseUp <StragClass>(dg, null) as StragClass;
             if (strag == null)
             {
                 MessageBox.Show("策略为空");
                 return;
             }
             if (MessageBox.Show(string.Format("确定要修改策略:{0}", strag.StragScript), "修改策略", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
             {
                 return;
             }
             frm_StragManager frm = new frm_StragManager();
             frm.SpecList   = UseSetting.AllStrags as Dictionary <string, BaseStragClass <TimeSerialData> >;
             frm.SpecObject = strag;
             frm.Show();
         }
         else if (dg.Equals(this.dg_stragStatus))
         {
             StragRunPlanClass <T> strag = getGridAfterMouseUp <StragRunPlanClass <T> >(dg, null) as StragRunPlanClass <T>;
             if (strag == null)
             {
                 MessageBox.Show("计划为空");
                 return;
             }
             if (MessageBox.Show(string.Format("确定要修改计划:{0}", strag.StragName), "修改计划", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
             {
                 return;
             }
             frm_StragPlanSetting frm = new frm_StragPlanSetting();
             frm.SpecList   = UseSetting.AllRunPlannings as Dictionary <string, StragRunPlanClass <TimeSerialData> >;
             frm.SpecObject = strag as StragRunPlanClass <TimeSerialData>;
             frm.Show();
         }
     }
     catch (Exception ce)
     {
         MessageBox.Show(string.Format("{0}:{1}", ce.Message, ce.StackTrace));
     }
 }
Ejemplo n.º 12
0
        private void dg_StragList_MouseUp(object sender, MouseEventArgs e)
        {
            DisableAllMenus();
            StragClass strag = getGridAfterMouseUp <StragClass>(this.dg_StragList, e) as StragClass;

            tmi_refreshPlans.Enabled = true;
            if (strag == null)
            {
                return;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.tmi_Edit.Enabled = true;
            }
        }
Ejemplo n.º 13
0
 private void ddl_StragObjects_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (!DllLoaded)
     {
         return;
     }
     if (this.ddl_StragObjects.SelectedValue != null)
     {
         ////string strName = (this.ddl_StragObjects.SelectedValue as DataRowView).Row.ItemArray[1].ToString();
         ////this.propertyGrid1.SelectedObject = StragClass.getStragByName(strName);
         //this.propertyGrid1.SelectedObject = StragClass.getStragByName(strName);
         string     sc    = this.ddl_StragObjects.SelectedValue.ToString();
         StragClass scobj = StragClass.getStragByName(sc);
         scobj.CommSetting.SetGlobalSetting(Program.AllGlobalSetting.gc);
         this.propertyGrid1.SelectedObject = scobj;
         this.propertyGrid1.Refresh();
     }
 }
Ejemplo n.º 14
0
        public static Dictionary <string, AssetUnitClass> Init_AssetUnits()
        {
            string strUnits = GlobalClass.ReReadAssetUnitList();
            Dictionary <string, AssetUnitClass> AllStragList = new Dictionary <string, AssetUnitClass>();

            if (strUnits == null || strUnits.Trim().Length == 0)
            {
                ToLog("资产单元列表", "为空!");
                return(AllStragList);
            }
            List <AssetUnitClass> list = StragClass.getObjectListByXml <AssetUnitClass>(strUnits); //StragClass.getStragsByXml(stragList);

            if (list == null)
            {
                return(AllStragList);
            }
            AllStragList = list.ToDictionary(t => t.UnitId, v => v);

            ToLog("资产单元列表", AllStragList.Count.ToString());
            return(AllStragList);
        }
Ejemplo n.º 15
0
        public static Dictionary <string, StragClass> Init_StragList()
        {
            string stragList = GlobalClass.ReReadStragList();

            Dictionary <string, StragClass> AllStragList = new Dictionary <string, StragClass>();

            if (stragList == null || stragList.Trim().Length == 0)
            {
                ToLog("策略列表", "为空!");
                return(AllStragList);
            }
            List <StragClass> list = StragClass.getObjectListByXml <StragClass>(stragList); //StragClass.getStragsByXml(stragList);

            if (list == null)
            {
                return(AllStragList);
            }
            AllStragList = list.ToDictionary(t => t.GUID, v => v);

            ToLog("策略列表", AllStragList.Count.ToString());
            return(AllStragList);
        }
Ejemplo n.º 16
0
        private void btn_Save_Click(object sender, EventArgs e)
        {
            StragRunPlanClass srp = this.propertyGrid1.SelectedObject as StragRunPlanClass;

            if (srp == null)
            {
                MessageBox.Show("请先双击列表选择需要修改的计划或点击新增按钮新建计划!");
                return;
            }
            if (Program.AllGlobalSetting.AllStrags.ContainsKey(srp.GUID))
            {
                StragClass sc = Program.AllGlobalSetting.AllStrags[srp.GUID];
                ////srp.StragName = sc.StragClassName;
                ////srp.StragDescript = sc.StragScript;
            }
            if (!Program.AllGlobalSetting.AllRunPlannings.ContainsKey(srp.GUID))
            {
                Program.AllGlobalSetting.AllRunPlannings.Add(srp.GUID, srp);
            }
            else
            {
                if (NewAPlan)
                {
                    MessageBox.Show("该计划已新建,标志错误!");
                    return;
                }
                Program.AllGlobalSetting.AllRunPlannings[srp.GUID] = srp;
            }
            if (!SaveData())
            {
                return;
            }
            refreshGrid(Program.AllGlobalSetting.AllRunPlannings);
            this.propertyGrid1.SelectedObject = null;
            this.propertyGrid1.Refresh();
            MessageBox.Show("保存成功!");
        }
Ejemplo n.º 17
0
        private void tsmi_refreshPlans_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = sender as ToolStripMenuItem;

            try
            {
                // menu.Owner
                DataGridView dg = (menu.Owner as ContextMenuStrip).SourceControl as DataGridView;
                if (dg == null)
                {
                    return;
                }
                if (UseSetting == null)
                {
                    return;
                }
                DataTable dt = null;

                if (dg.Equals(this.dg_stragStatus))
                {
                    dt = StragRunPlanClass.ToTable <StragRunPlanClass>(UseSetting.AllRunPlannings.Values.ToList <StragRunPlanClass>());
                }
                else if (dg.Equals(this.dg_StragList))
                {
                    dt = StragClass.ToTable <StragClass>(UseSetting.AllStrags.Values.ToList <StragClass>());
                }
                if (dt == null)
                {
                    return;
                }
                SetDataGridDataTable(dg, dt);
            }
            catch (Exception ce)
            {
            }
        }
Ejemplo n.º 18
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>());                            //执行交易提供可视化
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 滚动测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_roundTest_Click(object sender, EventArgs e)
        {
            if (this.runPlanPicker1.Plans.Length == 0)
            {
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            SettingClass setting = new SettingClass();

            setting.GrownMaxVal = int.Parse(this.txt_GrownMaxVal.Text);
            setting.GrownMinVal = int.Parse(this.txt_GrownMinVal.Text);
            setting.DispRows    = int.Parse(this.txt_MinCols.Text);
            setting.minColTimes = new int[10];
            for (int i = 0; i < 9; i++)
            {
                TextBox tb = this.Controls.Find(string.Format("txt_minColTimes{0}", i + 1), true)[0] as TextBox;
                setting.minColTimes[i] = int.Parse(tb.Text);
            }
            btc = new BackTestClass(long.Parse(txt_begExpNo.Text), long.Parse(txt_LoopCnt.Text), setting);
            Assembly asmb = typeof(StragClass).Assembly;
            //////Type sct = asmb.GetType(ddl_StragName.SelectedValue.ToString());
            //////StragClass sc = Activator.CreateInstance(sct) as StragClass;
            StragClass sc = this.runPlanPicker1.Plans[0].PlanStrag;

            sc.CommSetting     = setting;
            sc.ChipCount       = int.Parse(this.txt_ChipCnt.Text);
            sc.FixChipCnt      = (this.txt_FixChipCnt.Text.Trim() == "0") ? false : true;
            sc.ReviewExpectCnt = int.Parse(this.txt_reviewExpCnt.Text);
            sc.InputMinTimes   = int.Parse(this.txt_minInputTimes.Text);
            sc.InputMaxTimes   = int.Parse(this.txt_maxInputTimes.Text);
            sc.ExcludeBS       = this.chkb_exclueBS.Checked;
            sc.ExcludeSD       = this.chkb_exclueSD.Checked;
            sc.BySer           = this.chkb_bySer.Checked;
            sc.OnlyBS          = this.chkb_onlyBS.Checked;
            sc.OnlySD          = this.chkb_onlySD.Checked;
            this.runPlanPicker1.Plans[0].PlanStrag = sc;
            RoundBackTestReturnClass rbtr = null;

            try
            {
                int cycLong  = int.Parse(txt_RoundCycLong.Text);
                int stepLong = int.Parse(txt_RoundStepLong.Text);
                rbtr = btc.RunRound(sc, cycLong, stepLong);
            }
            catch (Exception ce)
            {
                rbtr      = new RoundBackTestReturnClass();
                rbtr.Msg  = ce.Message;
                rbtr.succ = false;
            }
            if (!rbtr.succ)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(rbtr.Msg);
                return;
            }
            listView3.Items.Clear();
            List <float> wins = rbtr.RoundWinRate;

            for (int i = 0; i < rbtr.RoundData.Count; i++)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = (i + 1).ToString();
                lvi.SubItems.Add(rbtr.RoundData[i].LoopCnt.ToString());
                lvi.SubItems.Add(rbtr.RoundData[i].ChanceList.Count.ToString());
                lvi.SubItems.Add(rbtr.RoundData[i].HoldCntDic[1].ToString());
                lvi.SubItems.Add(wins[i].ToString());
                listView3.Items.Add(lvi);
            }
            this.tabPage3.Show();
            this.Cursor = Cursors.Default;
            MessageBox.Show(rbtr.Msg);
        }
Ejemplo n.º 20
0
        private void DoSomething(BackgroundWorker worker, DoWorkEventArgs e)
        {
            SettingClass setting = new SettingClass();

            setting.GrownMaxVal = int.Parse(this.txt_GrownMaxVal.Text);
            setting.GrownMinVal = int.Parse(this.txt_GrownMinVal.Text);
            setting.DispRows    = int.Parse(this.txt_MinCols.Text);
            setting.minColTimes = new int[10];
            for (int i = 0; i < 9; i++)
            {
                TextBox tb = this.Controls.Find(string.Format("txt_minColTimes{0}", i + 1), true)[0] as TextBox;
                setting.minColTimes[i] = int.Parse(tb.Text);
            }
            #region 老的调用逻辑改为和正式运行一样的通过注入选择的计划列表,运行。除了调试不写入表,其他处理一样。其实在正常处理里面也可以使用调试模式,那会导致停止服务后无法保留交易数据
            //////////////////Assembly asmb = typeof(StragClass).Assembly;// Assembly.LoadFrom("EnterpriseServerBase.dll");
            //////////////////btc = new BackTestClass(long.Parse(txt_begExpNo.Text), long.Parse(txt_LoopCnt.Text), setting);
            //////////////////Type sct = asmb.GetType(ddl_StragName.SelectedValue.ToString());
            //////////////////StragClass sc = Activator.CreateInstance(sct) as StragClass;
            //////////////////sc.CommSetting = setting;
            //////////////////sc.ChipCount = int.Parse(this.txt_ChipCnt.Text);
            //////////////////sc.FixChipCnt = (this.txt_FixChipCnt.Text.Trim() == "0") ? false : true;
            //////////////////sc.ReviewExpectCnt = int.Parse(this.txt_reviewExpCnt.Text);
            //////////////////sc.InputMinTimes = int.Parse(this.txt_minInputTimes.Text);
            //////////////////sc.InputMaxTimes = int.Parse(this.txt_maxInputTimes.Text);
            //////////////////sc.ExcludeBS = this.chkb_exclueBS.Checked;
            //////////////////sc.ExcludeSD = this.chkb_exclueSD.Checked;
            //////////////////sc.BySer = this.chkb_bySer.Checked;
            //////////////////sc.OnlyBS = this.chkb_onlyBS.Checked;
            //////////////////sc.OnlySD = this.chkb_onlySD.Checked;
            //////////////////sc.GetRev = this.chkb_getRev.Checked;
            //////////////////if (sc is IProbCheckClass)
            //////////////////{
            //////////////////    (sc as IProbCheckClass).StdvCnt = double.Parse(this.txt_StdvCnt.Text);
            //////////////////}
            //////////////////sc.MinWinRate = (double)double.Parse(this.txt_Odds.Text) / double.Parse(this.txt_ChipCnt.Text) / double.Parse(this.txt_minRate.Text);
            ////////////////////凯利公式 (p*b-q)/q
            ////////////////////////double p = 1 / sc.MinWinRate;
            ////////////////////////double b = double.Parse(this.txt_Odds.Text);
            ////////////////////////double q = 1 - p;
            ////////////////////////sc.StagSetting = sc.getInitStagSetting();
            ////////////////////////sc.StagSetting.BaseType.ChipRate = (p * b - q) / q;
            ////////////////////////if (MessageBox.Show(sc.StagSetting.BaseType.ChipRate.ToString(), "胜率", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel) ;
            ////////////////////////{
            ////////////////////////    return;
            ////////////////////////}
            #endregion
            btc = new BackTestClass(long.Parse(txt_begExpNo.Text), long.Parse(txt_LoopCnt.Text), setting);
            this.listView1.Items.Clear();
            this.listView2.Items.Clear();
            this.listView3.Items.Clear();
            StragRunPlanClass[] plans = this.runPlanPicker1.Plans;
            if (plans.Length == 0)
            {
                return;
            }
            SCList = plans.ToList();
            SCList.ForEach(p => p.PlanStrag.CommSetting = setting);
            SCList.ForEach(p => p.PlanStrag.ChipCount   = int.Parse(this.txt_ChipCnt.Text));
            SCList.ForEach(p => p.Running     = true);
            SCList.ForEach(p => p.AutoRunning = true);
            SCList.ForEach(p => p.FixAmt      = 1);
            SCList.ForEach(p => p.FixRate     = 0.01);
            sc                      = SCList[0].PlanStrag;
            this.Cursor             = Cursors.WaitCursor;
            timer_Tip.Tick         += new EventHandler(RefreshList);
            this.timer_Tip.Interval = int.Parse(txt_Timer_Interval.Text) * 1000;
            this.timer_Tip.Enabled  = true;
            Thread thrd = null;
            try
            {
                btc.FinishedProcess = new SuccEvent(Finished);
                btc.teststrag       = sc;
                thrd = new Thread(new ThreadStart(btc.Run));
                thrd.Start();
            }
            catch (Exception ce)
            {
                ret            = new BackTestReturnClass();
                ret.ChanceList = new List <ChanceClass>();
                ret.Msg        = ce.Message;
                ret.succ       = false;
                MessageBox.Show(ce.Message);
            }


            //this.RunVirExchange = true;


            this.Cursor = Cursors.Default;
            ////if (!ret.succ)
            ////    MessageBox.Show(ret.Msg);
            ////RefreshList(null, null);
            ////while(true)
            ////{
            ////    Thread.Sleep(1000);
            ////    RefreshList(null, null);
            ////    if (thrd.ThreadState == ThreadState.Stopped)
            ////    {
            ////        break;
            ////    }
            ////}
            ////this.timer_Tip.Enabled = false;
            ////RefreshList();
        }
Ejemplo n.º 21
0
        public Dictionary <string, ChanceClass> CloseTheChances(bool IsTestBack)
        {
            List <ChanceClass> cl       = new List <ChanceClass>();
            DateTime           currTime = DateTime.Now;
            Dictionary <string, ChanceClass> CloseList = new Dictionary <string, ChanceClass>();

            if (IsTestBack)//如果回测,使用内存数据
            {
                cl = Program.AllServiceConfig.AllNoClosedChanceList.Values.ToList <ChanceClass>();
            }
            else//非回测,使用数据库数据
            {
                DbChanceList dcl = new PK10ExpectReader().getNoCloseChances(null);
                cl = dcl.Values.ToList <ChanceClass>();
            }

            Dictionary <string, ChanceClass> rl = new Dictionary <string, ChanceClass>();

            if (cl.Count > 0)
            {
                Log("未关闭机会列表数量为", string.Format("{0}", cl.Count));
            }
            for (int i = 0; i < cl.Count; i++)
            {
                string sGUId = cl[i].StragId;
                if (!Program.AllServiceConfig.AllStrags.ContainsKey(sGUId)) //如果策略已注销,立即关闭机会
                {
                    if (cl[i].Tracerable)                                   //如果是自我跟踪机会,不理会,让它自己去跟踪
                    {
                    }
                    else
                    {
                        CloseList.Add(cl[i].GUID, cl[i]);
                        //cl.Remove(cl[i].ChanceIndex);
                        Log("强制结束机会", string.Format("该机会所属策略已经注销,并且该机会是非跟踪机会!{0}", cl[i].ChanceCode));
                        continue;
                    }
                }

                List <string> AllUsePans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Select(t => t.PlanStrag.GUID)).ToList <string>();
                if (!AllUsePans.Contains(sGUId))//不在执行的计划内
                {
                    if (cl[i].Tracerable)
                    {
                    }
                    else
                    {
                        CloseList.Add(cl[i].GUID, cl[i]);
                        Log("强制结束机会", string.Format("不存在绑定该机会所属策略的计划,并且该机会是非跟踪机会!{0}", cl[i].ChanceCode));
                        continue;
                    }
                }
                //如果策略已经超出时间
                List <string> StopedPlans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Where(t => (InitServerClass.JudgeInRunTime(currTime, t) == false))).Select(a => a.GUID).ToList <string>();
                if (!IsTestBack)
                {
                    if (StopedPlans.Contains(sGUId))//停止的计划产生的机会
                    {
                        if (cl[i].Tracerable)
                        {
                        }
                        else
                        {
                            Log("强制结束机会", string.Format("该机会所属策略超出计划运行时间!{0}", cl[i].ChanceCode));
                            CloseList.Add(cl[i].GUID, cl[i]);
                            continue;
                        }
                    }
                }
                StopedPlans = Program.AllServiceConfig.AllRunningPlanGrps.SelectMany(a => a.Value.UseSPlans.Where(t => (t.Running == false))).Select(a => a.GUID).ToList <string>();
                if (StopedPlans.Contains(sGUId))//停止的计划产生的机会
                {
                    if (cl[i].Tracerable)
                    {
                    }
                    else
                    {
                        Log("强制结束机会", string.Format("该机会所属策略的计划状态为停止!{0}", cl[i].ChanceCode));
                        CloseList.Add(cl[i].GUID, cl[i]);
                        continue;
                    }
                }
                //如果策略已经停止
                //获得策略
                StragClass sc      = Program.AllServiceConfig.AllStrags[sGUId];
                int        mcnt    = 0;
                bool       Matched = cl[i].Matched(CurrDataList, out mcnt);
                cl[i].MatchChips += mcnt;
                if (sc is ITraceChance)//优先关闭程序跟踪
                {
                    cl[i].Closed = (sc as ITraceChance).CheckNeedEndTheChance(cl[i], Matched);
                }
                else
                {
                    cl[i].Closed = Matched;
                }
                if (cl[i].Tracerable && Matched)//如果是策略自我跟踪,无论其策略是否是跟踪策略,先关了。
                {
                    cl[i].Closed = true;
                }
                //////if (cl[i].MaxHoldTimeCnt > 0 && cl[i].HoldTimeCnt == cl[i].MaxHoldTimeCnt)
                //////{
                //////    cl[i].Closed = true;
                //////}
                if (cl[i].Closed)//如果已经关闭
                {
                    CloseList.Add(cl[i].GUID, cl[i]);
                    continue;
                }
                rl.Add(cl[i].GUID, cl[i]);
            }
            //Log("结束机会", "所有非法,以及命中并确认需要结束的机会");
            if (!IsTestBack) //如果非回测,保存交易记录
            {
                DbChanceList dbsavelist = new DbChanceList();
                CloseList.Values.ToList <ChanceClass>().ForEach(p => dbsavelist.Add(p.ChanceIndex, p));
                CloseChanceInDBAndExchangeService(dbsavelist);
            }
            return(rl);
        }
Ejemplo n.º 22
0
        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);
        }
Ejemplo n.º 23
0
        public RoundBackTestReturnClass RunRound(StragClass teststrag, long TestLong, long StepLong)//滚动获取
        {
            long begNo = BegExpect;

            ExpectReader                     er             = new ExpectReader();
            ExpectList                       el             = null;
            long                             cnt            = 0;
            RoundBackTestReturnClass         ret            = new RoundBackTestReturnClass();
            ExpectList                       AllData        = new ExpectList();
            long                             testIndex      = teststrag.ReviewExpectCnt - 1;
            ExpectList                       testData       = null;
            Dictionary <string, ChanceClass> NoCloseChances = new Dictionary <string, ChanceClass>();
            Dictionary <string, ChanceClass> tmpChances     = new Dictionary <string, ChanceClass>();

            long roundId = 0;
            int  currRid = 0;
            List <Dictionary <string, ChanceClass> > roundNoMatchedChances = new List <Dictionary <string, ChanceClass> >();
            List <long> roundBegIds = new List <long>();

            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}条记录!", testIndex);
                    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]);
                    }
                    if (roundBegIds.Count > 0 && testIndex == roundBegIds[currRid] + TestLong)//切换当前滚动id
                    {
                        //取出当前未完成队列,
                        Dictionary <string, ChanceClass> NoMatchDic = roundNoMatchedChances[currRid];
                        BackTestReturnClass brc = ret.RoundData[currRid];
                        foreach (string key in NoMatchDic.Keys)//结束所有未玩成的结果
                        {
                            ChanceClass cc       = NoMatchDic[key];
                            int         matchcnt = 0;
                            if (cc.Matched(testData.LastData, out matchcnt, teststrag.GetRev))
                            {
                                cc.HoldTimeCnt = (int)(testData.LastData.ExpectIndex - cc.InputExpect.ExpectIndex);
                            }
                            else
                            {
                                cc.HoldTimeCnt = 99;//还未开出但是已经结束了的
                            }
                            cc.MatchChips  = matchcnt;
                            cc.Closed      = true;
                            cc.EndExpectNo = testData.LastData.Expect;
                            cc.Closed      = true;
                            cc.EndExpectNo = testData.LastData.Expect;
                            cc.UpdateTime  = testData.LastData.OpenTime;
                            brc.ChanceList.Add(cc);
                            if (brc.HoldCntDic.ContainsKey(cc.HoldTimeCnt))
                            {
                                brc.HoldCntDic[cc.HoldTimeCnt] = brc.HoldCntDic[cc.HoldTimeCnt] + 1;
                            }
                            else
                            {
                                brc.HoldCntDic.Add(cc.HoldTimeCnt, 1);
                            }
                        }
                        currRid++;
                    }
                    if ((testIndex + 1 - teststrag.ReviewExpectCnt) % (StepLong) == 0)     //每到周期整数倍,滚动开始id队列增加当前id
                    {
                        roundBegIds.Add(testIndex);                                        //加入队列
                        roundNoMatchedChances.Add(new Dictionary <string, ChanceClass>()); //加入未完成的机会表到指定队列中
                        BackTestReturnClass brc = new BackTestReturnClass();
                        ret.RoundData.Add(brc);
                        roundId++;
                    }
                    for (int i = currRid; i < roundBegIds.Count; i++)//处理每个滚动周期的
                    {
                        ExchanceClass cc = new ExchanceClass();
                        Dictionary <string, ChanceClass> NoMatchDic = roundNoMatchedChances[i];
                        BackTestReturnClass brc = ret.RoundData[i];
                        cc.Run(testData, teststrag, ref brc.ChanceList, ref NoMatchDic, ref brc.HoldCntDic);
                        roundNoMatchedChances[i] = NoMatchDic;
                        brc.LoopCnt      = testIndex - roundBegIds[i] + 1;
                        ret.RoundData[i] = brc;
                    }
                    testIndex++;
                }
            }

            return(ret);
        }
Ejemplo n.º 24
0
 public ExchanceReuslt Run(ExpectList testData, StragClass teststrag)
 {
     return(null);
 }
Ejemplo n.º 25
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]);
                }
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// 初始化计划,将数据类型和运算类型相同的计划分组
        /// </summary>
        public static Dictionary <string, CalcStragGroupClass> InitCalcStrags(ref Dictionary <string, CalcStragGroupClass> AllStatusStrags, Dictionary <string, StragClass> AllStrags, Dictionary <string, StragRunPlanClass> list, Dictionary <string, AssetUnitClass> AssetUnits, bool StartTheAuto, bool IsBackTest)
        {
            if (AllStatusStrags == null)
            {
                AllStatusStrags = new Dictionary <string, CalcStragGroupClass>();
            }
            foreach (string key in list.Keys) //按相同类型+视图分类,相同类分在一组
            {
                StragRunPlanClass spc = list[key];
                if (AllStatusStrags.SelectMany(t => t.Value.UseSPlans.Select(a => a.GUID)).Contains(key))//支持后续加入计划,只要状态合适都可以加入
                {
                    continue;
                }
                if (!AllStrags.ContainsKey(spc.PlanStrag.GUID))
                {
                    ToLog("计划非法", "计划所对应策略已注销");
                    continue;
                }
                StragClass sc = AllStrags[spc.PlanStrag.GUID];
                spc.PlanStrag = sc;
                if (spc.AssetUnitInfo != null)//分配资产单元号
                {
                    sc.AssetUnitId = spc.AssetUnitInfo.UnitId;
                }
                string strModel = "Type_{0}_ViewBySerial_{1}";//按类和数据视图分类
                string strKey   = string.Format(strModel, sc.GetType().Name, sc.BySer);
                if (!IsBackTest)
                {
                    if (spc.ExpiredTime < DateTime.Now)
                    {
                        ToLog("计划时间不匹配", "超过计划过期时间");
                        continue;
                    }
                    if (!JudgeInRunTime(DateTime.Now, spc))//没在指定运行时间内,跳过
                    {
                        ToLog("计划时间不匹配", "未在指定时间区间内");
                        continue;
                    }
                }
                if (StartTheAuto)//如果第一次运行,将此标志设为真,将自动启动策略
                {
                    spc.Running = spc.AutoRunning;
                }
                if (!spc.Running)
                {
                    continue;
                }

                CalcStragGroupClass csg = null;
                if (!AllStatusStrags.ContainsKey(strKey))
                {
                    csg = new CalcStragGroupClass();
                    AllStatusStrags.Add(strKey, csg);
                }
                csg = AllStatusStrags[strKey];
                if (spc.AssetUnitInfo != null)
                {
                    string uid = spc.AssetUnitInfo.UnitId;
                    if (AssetUnits.ContainsKey(uid))
                    {
                        if (!csg.UseAssetUnits.ContainsKey(uid))
                        {
                            csg.UseAssetUnits.Add(uid, AssetUnits[uid]); //注意,一定是要用全局设置的单元资产,才有动态的金额等信息
                        }
                    }
                    else
                    {
                        ToLog("计划资产单元不匹配", "计划所属资产单元不存在");
                    }
                }
                //csg = AllStatusStrags[strKey];
                csg.UseSPlans.Add(spc);
                ToLog("加入计划", spc.StragDescript);
                csg.UseStrags.Add(sc.GUID, sc);
                csg.UseSerial = sc.BySer;
                //ToLog("初始化待计算的计划", string.Format("准备运行的计划数为:{0};", AllStatusStrags.Count));
            }
            return(AllStatusStrags);
        }