以下风控功能从磊金量化平台开发。
程序执行界面:集成交易客户端、多账号和量化模型
开发步骤
一、策略参数编辑
开发第一步先处理策略参数
拷贝策略模板,改名
我们做一个通用的风控策略,核心功能是间隔时间查询权益,达到预警间隔声音提示、达止损清仓撤开仓、达止盈清仓撤开仓
2.完整策略逻辑:其中CStgyBase是基类,封装了基础属性和函数
//风控策略publicclassCStgyFund:CStgyBase{publicCStgyFund(T_Strategysetting):base(setting){UpdateSetting(setting);_bChkAllInsts=true;}/*------------------------指标与参数------------------------*///指标值publicdouble_预警=0;//预警值publicdouble_止损=0;//止损值publicdouble_止盈=0;//止盈值//publicdouble_PoLoss=0;//单笔持仓亏损提醒//publicdouble_PoWin=0;//单笔持仓亏损提醒publicint_自动止盈=0;//是否自动止盈publicint_自动止损=0;//是否自动止损publicint_查询频率=60;//资金查询频率秒/*-----------------------对界面输入参数进行解析-------------------------*/publicoverrideintUpdateParamList(refstringparam_list){intret=0;JObjectdictParam=null;stringname=string.Empty,val=string.Empty;if(!string.IsNullOrEmpty(param_list)){dictParam=JObject.Parse(param_list);if(dictParam!=null){ret=ParseBasicParam(dictParam);}}if(dictParam!=null){foreach(varitemindictParam){name=item.Key;val=DataUtil.toString(item.Value);if(name=="_预警"){if(val.LastIndexOf("万")==val.Length-1){val=val.Substring(0,val.Length-2);_预警=DataUtil.toDouble(val,0)*.0;}else{_预警=DataUtil.toDouble(val,0);}ret++;}elseif(name=="_止损"){if(val.LastIndexOf("万")==val.Length-1){val=val.Substring(0,val.Length-2);_止损=DataUtil.toDouble(val,0)*.0;}else{_止损=DataUtil.toDouble(val,0);}ret++;}elseif(name=="_止盈"){if(val.LastIndexOf("万")==val.Length-1){val=val.Substring(0,val.Length-2);_止盈=DataUtil.toDouble(val,0)*.0;}else{_止盈=DataUtil.toDouble(val,0);}ret++;}elseif(name=="_查询频率"){_查询频率=DataUtil.toInt(item.Value,0);ret++;}elseif(name=="_自动止损"){_自动止损=DataUtil.toInt(item.Value,0);ret++;}elseif(name=="_自动止盈"){_自动止盈=DataUtil.toInt(item.Value,0);ret++;}}}else{dictParam=newJObject();}dictParam["_预警"]=_预警.ToString();dictParam["_止损"]=_止损.ToString();dictParam["_止盈"]=_止盈.ToString();dictParam["_自动止损"]=_自动止损.ToString();dictParam["_自动止盈"]=_自动止盈.ToString();dictParam["_查询频率"]=_查询频率.ToString();param_list=dictParam.ToString().Replace("\r\n",string.Empty).Replace("\"",string.Empty).Replace("",string.Empty);returnret;}/*------------------------策略开始执行时,读取K线和历史信号--------*/publicoverrideintInitRealRun(boolbLoadData=false,intbarfrom=0,intsignfrom=0){vart=Task.Factory.StartNew(()={_bInProcessing=false;_Bars.Clear();_DBar=null;if(bLoadData){ClearData();LoadData(barfrom,signfrom,LoadTypeEnum.None,LoadTypeEnum.None);}_dtQry=DateTime.MinValue;_dtDone=DateTime.MinValue;ServiceManager.Instance.SetStgyStatus(_stgyid,StrategyStatus.running);});return0;}privateint_tick_num=0;DateTime_dtQry=DateTime.MinValue;DateTime_dtDone=DateTime.MinValue;//执行处理的时间T_Amount_Amount=null;/*---------------------策略执行入口---------------------*/publicoverridevoidOnTick(MdTickmdTick){_bInProcessing=true;_tick_num++;_cParam.Clear();if(_Td==null
_Td.is_login=0){return;}if(ServiceManager.Instance._TickTime!=TTIME_ENUM.行情中){return;}if(_Amount==null){_Amount=ServiceManager.Instance.GetAmount(_Td.account_id);}if(_Amount==null){ServiceManager.Instance.ReqAmount(_Td);_dtQry=DateTime.Now;return;}if((DateTime.Now-_dtQry).TotalSeconds_查询频率){ServiceManager.Instance.ReqAmount(_Td);_dtQry=DateTime.Now;}varhm=_dtQry.Hour+_dtQry.Minute*0.01+_dtQry.Second*0.;//查询时间if(hm=9.0hm8){return;}elseif(hm=21.0hm20)//{return;}if(_Amount.update_time_dtQry)//查询后{return;}if(_Amount.position_profit==0)//没有持仓{return;}_Amount.update_time=_dtQry.AddSeconds(-1);//保证查询一次、执行一次doubleamount=_Amount.amount/.0;_cParam.SetParm("当前权益",Math.Round(amount,2));if(_止损0amount=_止损){_cParam.SetParm("达止损线",_止损);//提示音Task.Factory.StartNew(()={PlaySound.Play(SoundEnum.红警);});//执行动作if(_自动止损0){ClearPositions();}_查询频率=5;}elseif(_预警0amount=_预警){_cParam.SetParm("达告警线",_预警);//提示音PlaySound.Play(SoundEnum.*警);_查询频率=5;}elseif(_止盈0amount=_止盈){_cParam.SetParm("达止盈线",_止盈);//提示音Task.Factory.StartNew(()={PlaySound.Play(SoundEnum.提示);});//执行动作if(_自动止盈0){ClearPositions();}}_cParam.SetParm("查询时间",(_Amount.update_time.Hour+_Amount.update_time.Minute*0.01+_Amount.update_time.Second*0.).ToString());MsgManager.Instance.SendStgyInData(_stgyid,_cParam.ToView());}/*---------------------风控清仓---------------------*/privatevoidClearPositions(){//撤开仓varlstOrder=ServiceManager.Instance._Orders.Where(x=x.account_id==_Td.account_idx.status==OrderStatusEnum.submitx.kp==(int)KPEnum.open).ToList();foreach(varorderinlstOrder){ServiceManager.Instance.CancelOrder(order,0,"止损清仓");}//对报单进行撤单重报设置lstOrder=ServiceManager.Instance._Orders.Where(x=x.account_id==_Td.account_idx.status==OrderStatusEnum.submitx.ref_id!=StgyHelper.KEEP_PRICEx.kp!=(int)KPEnum.open).ToList();foreach(varorderinlstOrder){order.cancel_slop=0;order.re_slop=order.cancel_slop;order.ref_id=StgyHelper.KEEP_PRICE;}//要避免非交易时间合约反复平仓varpositons=ServiceManager.Instance._Positions.Where(x=x.account_id==_Td.account_idx.vol!=0).ToList();ServiceManager.Instance.ClearTdPositions(_Td.account_id,0,StgyHelper.KEEP_PRICE,positons);}}
3.策略注册:方便界面知道有这个策略存在,并根据名称创建策略
4.策略使用:在策略列表(从顶部菜单调出)中新增策略
策略使用编辑界面,左边是所有策略的固定参数,右边是策略的个性化参数
磊金量化平台:
1.源码级量化平台,C#开发语言,理论上能实现任何策略模型;
2.量化执行平台,多账号实盘自动化交易;
3.同时是一个交易客户端,集成账号、成交、持仓、报单、条件单、划线下单、图表K线管理
4.策略源码安全
预览时标签不可点收录于话题#个上一篇下一篇