AccountEquity.mq5 MQL5
/* 该例子的源代码在"EA交易" -> "Examples"下 本例子讲解了EA如何获得账号权益 不同于外汇,期货交易中查询账号权益后交易所不是马上返回数据,需要等待交易所异步返回数据 交易所对我们有流量控制,规定我们在1秒钟内,最多只能发起几次查询请求(比如2-3次) 如果我们查询请求得太快、太频繁超出了流量控制,交易所会直接拒绝我们的请求 所以查询权益后需要暂停1秒 */ #property copyright "www.wewin28.com" #property link "http://www.wewin28.com" #property version "1.00" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //引用mt5ctp.dll头文件,该DLL封装了全部CTP接口,EA通过该DLL调用CTP接口交易期货合约 #include <mt5ctp.mqh> //引用CTP权益回调类CThostFtdcTradingAccountField头文件 #include <CThostFtdcTradingAccountField.mqh> //逗号 ushort splitCommma=StringGetCharacter(",",0); //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //收到交易所推送的逐笔行情不断触发OnTick事件 void OnTick() { //--- //检查是否已经连接交易所 if(getIsConnectCTP()==true && getIsLoginCTP()==true && getIsConfirmSettlementInfoCTP()==true) { //调用getAccountCTP()函数查询账号权益,不同于外汇,期货交易中查询账号权益后交易所不是马上返回数据,需要等待交易所异步返回数据 int res=getAccountCTP(); //交易所对我们有流量控制,规定我们在1秒钟内,最多只能发起几次查询请求(比如2-3次) //如果我们查询请求得太快、太频繁超出了流量控制,交易所会直接拒绝我们的请求 //所以查询权益后需要暂停1秒 Sleep(1000); } } //+------------------------------------------------------------------+ //不同于外汇,期货交易中查询账号权益后交易所不是马上返回数据,需要等待交易所异步返回数据 //OnChartEvent函数是MQL中的一个预定义事件处理函数,当用户在图表上进行操作(如点击、按键、移动鼠标等)会触发该函数 //本软件可以通过 OnChartEvent函数得到交易所回调,这些回调代号的id都是3000,根据该代号可以区分哪些是交易所回调而非用户在图表上进行操作产生的事件 void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id==3000) //只处理MT5 CTP的事件 { //处理回调 string event = processChartEventMsg(sparam); string chartEvents[]; //按逗号将交易所回调字符串拆成数组chartEvents int n=StringSplit(event,splitCommma,chartEvents); if(n>0) { printf(event); string eventType=chartEvents[0]; //调用函数getAccountCTP后交易所会推送OnRspQryTradingAccount回调给我们,格式如下: /* OnRspQryTradingAccount,经纪公司代码,投资者帐号,上次质押金额,上次信用额度,上次存款额,上次结算准备金,上次占用的保证金,利息基数, 利息收入,入金金额,出金金额,冻结的保证金,冻结的资金,冻结的手续费,当前保证金总额,资金差额,手续费,平仓盈亏,持仓盈亏,期货结算准备金, 可用资金,可取资金,基本准备金,交易日,结算编号,信用额度,质押金额,交易所保证金,投资者交割保证金,交易所交割保证金,保底期货结算准备金, 币种代码,上次货币质入金额,上次货币质出金额,货币质入金额,货币质出金额,货币质押余额,可质押货币金额,特殊产品占用保证金,特殊产品冻结保证金, 特殊产品手续费,特殊产品冻结手续费,特殊产品持仓盈亏,特殊产品平仓盈亏,根据持仓盈亏算法计算的特殊产品持仓盈亏,特殊产品交易所保证金, 业务类型,延时换汇冻结金额,剩余换汇额度 */ //查询账号权益回调 if(eventType=="OnRspQryTradingAccount") { //把账号权益回调转变为类CThostFtdcOrderField的属性 CThostFtdcTradingAccountField cThostFtdcTradingAccountField(event); //获得经纪公司代码 string brokerId=cThostFtdcTradingAccountField.getBrokerID(); //获得投资者帐号 string accountId=cThostFtdcTradingAccountField.getAccountID(); //获得静态权益 double prevBalance=cThostFtdcTradingAccountField.getPreBalance(); //获得盯市持仓浮盈 double positionProfit=cThostFtdcTradingAccountField.getPositionProfit(); //获得平仓盈亏 double closeProfit=cThostFtdcTradingAccountField.getCloseProfit(); //获得占用保证金 double currentMargin=cThostFtdcTradingAccountField.getCurrMargin(); //获得手续费 double commission=cThostFtdcTradingAccountField.getCommission(); //通过计算取得动态权益=静态权益+盯市持仓浮盈+平仓盈亏-手续费 double equity=prevBalance+positionProfit+closeProfit-commission; //获得可用资金 double available=cThostFtdcTradingAccountField.getAvailable(); printf("静态权益:%.2f 盯市持仓浮盈:%.2f 平仓盈亏:%.2f 占用保证金:%.2f 手续费:%.2f 动态权益:%.2f 可用资金:%.2f", prevBalance, positionProfit, closeProfit, currentMargin, commission, equity, available); } } } } //+------------------------------------------------------------------+
✅ 代码已复制到剪贴板