InstrumentCommission.mq5 MQL5
//+------------------------------------------------------------------+ //| 期货交易CTP_例子Example10_合约手续费InstrumentCommision.mq5 | //| 1145412@qq.com | //| www.wewin28.com | //+------------------------------------------------------------------+ #property copyright "www.wewin28.com" #property link "www.wewin28.com" #property version "1.00" //引用mt5ctp.dll头文件,该DLL封装了全部CTP接口,EA通过该DLL调用CTP接口交易期货合约 #include <mt5ctp.mqh> //引用CTP手续费回调类头文件 #include <CThostFtdcInstrumentCommissionRateField.mqh> //如果当前图表不是主力连续合约例如rb2701,可以直接使用MQL Symbol()属性获得当前图表的合约 //注意但如果当前图表是主力连续合约例如rb9999,则需要通过MQL SYMBOL_ISIN属性获得当前的主力合约是哪个 //主力连续合约例如rb9999是不能直接进行交易的,因为交易所没有这个合约 //如果EA跑在举例rb9999图表上,则在报单的时候需要通过MQL SYMBOL_ISIN属性获得rb9999当前是哪个合约(例如rb2610),然后用rb2610合约去报单 string chartSymbol=SymbolInfoString(Symbol(), SYMBOL_ISIN); ushort splitCommma=StringGetCharacter(",",0); //开仓手续费率 double openRatioByMoney=0; //开仓手续费 double openRatioByVolume=0; //平仓手续费率 double closeRatioByMoney=0; //平仓手续费 double closeRatioByVolume=0; //平今手续费率 double closeTodayRatioByMoney=0; //平今手续费 double closeTodayRatioByVolume=0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //-- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //检查是否已经连接交易所 if(getIsConnectCTP()==true && getIsLoginCTP()==true && getIsConfirmSettlementInfoCTP()==true) { //getInstrumentCommissionRateCTP()函数查询查询合约手续费,不同于外汇, //期货交易中查询合约手续费后交易所不是马上返回数据,需要等待交易所异步返回数据 int res=getInstrumentCommissionRateCTP(chartSymbol); //返回0 代表本地报单成功 //返回-1 表示网络连接失败 //返回-2,表示未处理请求超过许可数 //返回-3,表示每秒发送请求数超过许可数 //如果报单错误打印错误 if(res!=0) { printf("getInstrumentCommissionRateCTP error %d", 1); } //交易所对我们有流量控制,规定我们在1秒钟内,最多只能发起几次查询请求(比如2-3次) //如果我们查询请求得太快、太频繁超出了流量控制,交易所会直接拒绝我们的请求 //所以查询权益后需要暂停1秒 Sleep(1000); //通过SYMBOL_TRADE_CONTRACT_SIZE属性获得合约每手乘数 double contractSize=SymbolInfoDouble(chartSymbol, SYMBOL_TRADE_CONTRACT_SIZE); //例子计算开仓5手的手续费 double volumn=1; MqlTick mqlTick; //取得当前最新价格的结构体 if(SymbolInfoTick(chartSymbol, mqlTick)) { /* 单笔开仓手续费 = (当前价格 × 每手乘数 × OpenRatioByMoney) + (开仓手数 × OpenRatioByVolume) 其中的OpenRatioByMoney是按合约价值计算手续费的比例,例如0.0001 (即手续费万分之1) 其中的OpenRatioByVolume是按每1手收取的手续费 例如5(即手续费每手5元) 对于同一笔交易,OpenRatioByMoney/OpenRatioByVolume通常只有一个会被设置有值 例如OpenRatioByMoney = 0.0001 (即手续费万分之1),OpenRatioByVolume = 0 或者OpenRatioByMoney = 0,OpenRatioByVolume = 5 (即手续费每手5元) 如果发现两个都有值,将二者相加是总手续费 */ double commission=(mqlTick.last*contractSize*openRatioByMoney)+(volumn*openRatioByVolume); printf("%s 开仓手续费commission %.2f", chartSymbol, commission); //计算平仓手续费 double closeCommission=(mqlTick.last*contractSize*closeRatioByMoney)+(volumn*closeRatioByVolume); printf("%s 平仓手续费commission %.2f", chartSymbol, commission); //计算平今仓手续费 double closeCommissionToday=(mqlTick.last*contractSize*closeTodayRatioByMoney)+(volumn*closeTodayRatioByVolume); printf("%s 平今仓手续费commission %.2f", chartSymbol, commission); } else { printf("获取报价失败 %d", GetLastError()); } } } //不同于外汇,期货交易中查询手续费后交易所不是马上返回数据,需要等待交易所异步返回数据 //OnChartEvent函数是MQL中的一个预定义事件处理函数,当用户在图表上进行操作(如点击、按键、移动鼠标等)会触发该函数 //本软件可以通过 OnChartEvent函数得到交易所回调,这些回调代号的id都是3000,根据该代号可以区分哪些是交易所回调而非用户在图表上进行操作产生的事件 void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- //只处理MT5 CTP的事件 if(id==3000) { //处理回调 string event = processChartEventMsg(sparam); //printf(event); string chartEvents[]; //按逗号将交易所回调字符串拆成数组chartEvents int n=StringSplit(event,splitCommma,chartEvents); if(n>0) { string eventType=chartEvents[0]; //数组第一个元素是回调类型,共有以下这几种回调: //报单回调 OnRtnOrder //成交回调 OnRtnTrade //报单错误回调 OnErrRtnOrderInsert //撤单错误回调 OnErrRtnOrderAction //查询仓位回调 OnRspQryInvestorPosition //查询资金回调 OnRspQryTradingAccount //查询合约手续费回调 OnRspQryInstrumentCommissionRate //查询合约保证金回调 OnRspQryInstrumentMarginRate if(eventType=="OnRspQryInstrumentCommissionRate") { //调用函数getInstrumentCommissionRateCTP后交易所会推送OnRspQryInstrumentCommissionRate回调给我们,格式如下: /* OnRspQryInstrumentCommissionRate,投资者范围,经纪公司代码,投资者代码,开仓手续费率,开仓手续费, 平仓手续费率,平仓手续费,平今手续费率,平今手续费,交易所代码,业务类型,投资单元代码,合约代码 */ //把手续费回调转变为类CThostFtdcInstrumentCommissionRateField的属性 CThostFtdcInstrumentCommissionRateField cThostFtdcInstrumentCommissionRateField(event); //取得合约 string eventSymbol=cThostFtdcInstrumentCommissionRateField.getInstrumentID(); //取得开仓手续费率 openRatioByMoney=cThostFtdcInstrumentCommissionRateField.getOpenRatioByMoney(); //取得开仓手续费 openRatioByVolume=cThostFtdcInstrumentCommissionRateField.getOpenRatioByVolume(); //取得平仓手续费率 closeRatioByMoney=cThostFtdcInstrumentCommissionRateField.getCloseRatioByMoney(); //取得平仓手续费 closeRatioByVolume=cThostFtdcInstrumentCommissionRateField.getCloseRatioByVolume(); //取得平今手续费率 closeTodayRatioByMoney=cThostFtdcInstrumentCommissionRateField.getCloseTodayRatioByMoney(); //取得平今手续费 closeTodayRatioByVolume=cThostFtdcInstrumentCommissionRateField.getCloseTodayRatioByVolume(); } } } } //+------------------------------------------------------------------+
✅ 代码已复制到剪贴板