MT4插件开发 之 通过managerAPI获取实时行情数据

提示:如有疑问可在下方留言或私信说明,博主每天抽空回复


前言

之前做crm需要把MT4行情实时显示到CRM中,又或者你需要把MT4的行情实时推送到自己的平台中,可以有多种方式去实现,可以连接数据源获取,可以通过serverAPI的方式获取也可以通过managerAPI获取,这里就不一一讲解了,本文讲解如果使用managerAPI获取。


提示:如有疑问可在下方留言或私信说明,博主每天抽空回复

一、实现原理

在服务器上运行我们编写的插件,CRM可以通过websocket的方式请求数据,插件实时推送行情给CRM。插件的核心是使用managerAPI的PumpingSwitch方法来订阅MT4的行情,这样当MT4有行情信息PumpingSwitch就会收到MT4返回的行情数据。
在这里插入图片描述

二、PumpingSwitch介绍

virtual int __stdcall PumpingSwitch(MTAPI_NOTIFY_FUNC pfnFunc,const HWND destwnd,const UINT eventmsg,const int flags)=0;
Pumping
Pumping is a saving and quick mode of uploading data from the server. When transferring to pumping mode, manager interface requests the server for symbol settings, groups, account databases, orders, and trading requests, after that the server sends only updated data to the connected manager. After having connected to the server, the manager interface is transferred to the pumping mode with the PumpingSwitch() function・ The pointer to the callback function to be used by the manager interface to notify about the data updating, or window handle and identifier of the user message to which the notification about the data updating will be sent, must be passed as a parameter of this function.
After the PumpingSwitch() has been called, the manager interface will send the following codes as a parameter of the callback function or as WPARAM of the user message:
■PUMP_START_PUMPING — manager interface has successfully changed for the pumping mode;
■PUMP_UPDATE_SYMBOLS — symbol settings have been 叩dated, the 叩dated symbols can be obtained with functions named SymbolsGetAIIO, SymbolGetO, or SymbolInfoGet();
■PUMP_UPDATE_GROUPS — group settings have been 叩dated, the 叩dated group settings can be obtained with functions named GroupsGet() or GroupRecordGet();
■PUMP_UPDATE_USERS — the account list has been 叩dated, the 叩dated account list can be obtained with functions named UsersGet() or UserRecordGet();
■PUMP_UPDATE_ONLINE — the list of connected clients has been 叩dated, the 叩dated list can be obtained with functions named OnlineGet() or OnlineRecordGet();
■PUMP_UPDATE_BIDASK — a new quote has income,叩dated quotes can be obtained with TickInfoLast() function, the updated prices can be obtained with one or several calls of the SymbolInfoUpdated() function;
■PUMP_UPDATE_NEWS — a news has income, the headings of the income news can be obtained with functions named NewsGet(), NewsTotal()z and NewsTopicGet(); after the news has income it will be possible to request for its body with the NewsBodyRequest() function;
■PUMP_UPDATE_NEWS_BODY — the news body has income, it can be obtained with NewsBodyGet() function;
■PUMP_UPDATE_MAIL — the new message has income that was saved on the disk in 'mailbox、' folder of the working directory of Manager API; the file name and the size of the new message can be obtained with the MailLast() function;
■PUMP_UPDATE_TRADES — the orders list has been 叩dated, the 叩dated list of orders can be obtained with functions named TradesGet(), TradesGetBySymbolO, TradesGetByLogin()z TradesGetByMarket(), and
TradeRecordGet()・ The updated trade summary for symbols and for security groups can be obtained with functions named SummaryGetAIIO, SummaryGetO, SummaryGetByCountO, and SummaryGetByType()・ The updated company's exposure for currencies can be obtained with functions named ExposureGet() and Exposu reVa lueGet();
■PUMP_UPDATE_REQUESTS — the list of trade requests has been 叩dated, the 叩dated list can be obtained with functions named RequestsGet() and RequestInfoGet();
■PUMP_UPDATE_PLUGINS — the list of server plugins has been 叩dated, the 叩dated list can be obtained with functions named PluginsGet() and PluginParamGet();
■PUMP_UPDATE_ACTIVATION — the list of orders having triggered Stop Loss or Take Profit level, pending orders to be activated and the most unprofitable orders for accounts in the stop out mode, the corresponding activation flags being set in the 'activation' field of the 叩dated orders logs: ACTIVATION_SL/ ACTIVATION_TP/ ACTIVATION.PENDING, ACTIVATION.STOPOUT, or, if the price has rolled back from the activation level: ACTIVATION_SL_ROLLBACK, ACTIVAfiON.TP.ROLLBACK, ACTIVATION_PENDING_ROLLBACK; if the price has rolled back the activation flag can be cleared with the TradeClearRollback() function;
■PUMP_UPDATE_MARGINCALL — the list of accounts in margin call mode has been 叩dated, the 叩dated list of margin requirements of accounts can be obtained with functions named MarginsGet() and MarginLevelGet();
■PUMP_STOP_PUMPING — the pumping mode has been stopped・

其中参数有很多 这里不光是能监控报价信息还有很多功能 这里就不一一说明了, 我们如果想监控行情报价主要用到的就是PUMP_UPDATE_BIDASK

具体实现

void __stdcall  PumpingNotify2(int type)
{
	switch (type)
	{
	case PUMP_UPDATE_SYMBOLS:
	{
		int            total = 0, i;
		ConSymbol *cs = NULL;
		cs = manager->SymbolsGetAll(&total);
		for (i = 0; i < total; i++)
		{
			manager->SymbolAdd(cs[i].symbol);

		}
		if (cs != NULL) { manager->MemFree(cs); cs = NULL; total = 0; }
		cout << "PUMP_UPDATE_SYMBOLS" << endl;
		break;      // update symbols
	}
	case PUMP_UPDATE_TRADES:
	{
		int m_trades_total;
		manager->TradesRequest(&m_trades_total);
		cout << "PUMP_UPDATE_TRADES" << endl;
		break;        // update trades
	}
	case PUMP_UPDATE_BIDASK:
	{
		//获取
	}
	default: break;
	}
}
	
    manager->PumpingSwitch(PumpingNotify, NULL, NULL, NULL);

结束语

关于更多Pumping相关的方法,可以参考我另一篇博文《MT4 managerAPI 接口(头文件)》里面有全部的Pumping相关的方法,如有任何疑问可私信或者留言。