MarginCalculation.mq5 MQL5
/* 该例子的源代码在"EA交易" -> "Examples"下 本例子讲解了EA如何计算合约保证金 */ #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 <CThostFtdcInstrumentMarginRateField.mqh> ushort splitCommma=StringGetCharacter(",",0); string marginPriceTypeCTP="0"; //如果当前图表不是主力连续合约例如rb2701,可以直接使用MQL Symbol()属性获得当前图表的合约 //注意但如果当前图表是主力连续合约例如rb9999,则需要通过MQL SYMBOL_ISIN属性获得当前的主力合约是哪个 //主力连续合约例如rb9999是不能直接进行交易的,因为交易所没有这个合约 //如果EA跑在举例rb9999图表上,则在报单的时候需要通过MQL SYMBOL_ISIN属性获得rb9999当前是哪个合约(例如rb2610),然后用rb2610合约去报单 string chartSymbol=SymbolInfoString(Symbol(), SYMBOL_ISIN); //防止保证金计算为0 double longMarginRatioByMoney=100000000; double longMarginRatioByVolume=100000000; double shortMarginRatioByMoney=100000000; double shortMarginRatioByVolume=100000000; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- //获得期货公司是用什么价格计算保证金 marginPriceTypeCTP=getMarginPriceTypeCTP(); //getInstrumentMarginRateCTP()函数查询保证金,不同于外汇,期货交易中查询合约手续费后交易所不是马上返回数据,需要等待交易所异步返回数据 getInstrumentMarginRateCTP(chartSymbol); //交易所对我们有流量控制,规定我们在1秒钟内,最多只能发起几次查询请求(比如2-3次) //如果我们查询请求得太快、太频繁超出了流量控制,交易所会直接拒绝我们的请求 //所以查询权益后需要暂停1秒 Sleep(1000); double marginPrice=100000000; //期货公司有几种不同的计算保证金方式 //1 期货公司用结算价计算保证金 if(marginPriceTypeCTP==(string)ENUM_CTP_MPT_PRESETTLEMENTPRICE) { //通过SYMBOL_SESSION_PRICE_SETTLEMENT属性获得结算价 double settlementPrice=SymbolInfoDouble(chartSymbol, SYMBOL_SESSION_PRICE_SETTLEMENT); marginPrice=settlementPrice; } //2 期货公司用最新价计算保证金 if(marginPriceTypeCTP==(string)ENUM_CTP_MPT_LAST) { MqlTick mqlTick; //取得当前最新价格的结构体 if(SymbolInfoTick(chartSymbol, mqlTick)) { marginPrice=mqlTick.last; } else { printf("获取报价失败 %d", GetLastError()); } } //3 期货公司用成交均价计算保证金 if(marginPriceTypeCTP==(string)ENUM_CTP_MPT_AVERAGEPRICE) { /*暂不支持 dont support*/ } //4 期货公司用开仓价计算保证金 if(marginPriceTypeCTP==(string)ENUM_CTP_MPT_OPENPRICE) { MqlTick mqlTick; //取得当前最新价格的结构体 if(SymbolInfoTick(chartSymbol, mqlTick)) { marginPrice=mqlTick.last; } else { printf("获取报价失败 %d", GetLastError()); } } //通过SYMBOL_TRADE_CONTRACT_SIZE属性获得合约每手乘数 double contractSize=SymbolInfoDouble(chartSymbol, SYMBOL_TRADE_CONTRACT_SIZE); //计算开仓1手保证金 int volume=1; if(contractSize>0) { //开多保证金 long margin double marginLong=(longMarginRatioByVolume+shortMarginRatioByMoney*marginPrice*contractSize)*volume; //开空保证金 short margin double marginShort=(shortMarginRatioByVolume+shortMarginRatioByMoney*marginPrice*contractSize)*volume; printf("%s 开多保证金 long margin %.2f 开空保证金 short margin %.2f", chartSymbol, marginLong, marginShort); } } //不同于外汇,期货交易中查询保证金后交易所不是马上返回数据,需要等待交易所异步返回数据 //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) { //数组第一个元素是回调类型,共有以下这几种回调: //报单回调 OnRtnOrder //成交回调 OnRtnTrade //报单错误回调 OnErrRtnOrderInsert //撤单错误回调 OnErrRtnOrderAction //查询仓位回调 OnRspQryInvestorPosition //查询资金回调 OnRspQryTradingAccount //查询合约手续费回调 OnRspQryInstrumentCommissionRate //查询合约保证金回调 OnRspQryInstrumentMarginRate string eventType=chartEvents[0]; if(eventType=="OnRspQryInstrumentMarginRate") { //调用函数getInstrumentMarginRateCTP后交易所会推送OnRspQryInstrumentMarginRate回调给我们,格式如下: /* OnRspQryInstrumentMarginRate,投资者范围,经纪公司代码,投资者代码,投机套保标志,多头保证金率,多头保证金费, 空头保证金率,空头保证金费,是否相对交易所收取,交易所代码,投资单元代码,合约代码 */ //把保证金回调转变为类CThostFtdcInstrumentMarginRateField的属性 CThostFtdcInstrumentMarginRateField cThostFtdcInstrumentMarginRateField(event); //取得合约 string symbol=cThostFtdcInstrumentMarginRateField.getInstrumentID(); if(symbol==chartSymbol) { //取得多头保证金率 longMarginRatioByMoney=cThostFtdcInstrumentMarginRateField.getLongMarginRatioByMoney(); //取得多头保证金费 longMarginRatioByVolume=cThostFtdcInstrumentMarginRateField.getLongMarginRatioByVolume(); //空头保证金率 shortMarginRatioByMoney=cThostFtdcInstrumentMarginRateField.getShortMarginRatioByMoney(); //空头保证金费 shortMarginRatioByVolume=cThostFtdcInstrumentMarginRateField.getShortMarginRatioByVolume(); } } } } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+
✅ 代码已复制到剪贴板