2017/4/23
MT5入门到精通之七
一.获取账户,软件和货币信息
//+------------------------------------------------------------------+
//| getInformationScript.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//1.获取账号信息(Integer模式其它可以类推)
//1.1账号id
int accountId=AccountInfoInteger(ACCOUNT_LOGIN);
//1.2.测试账号 真实账号
int accountType= AccountInfoInteger(ACCOUNT_TRADE_MODE);
if(accountType == ACCOUNT_TRADE_MODE_REAL)
{
printf("真实账号");
}
else if(accountType==ACCOUNT_TRADE_MODE_DEMO)
{
printf("测试账号");
}
//1.3 是否允许交易
bool isTradeAllowed=AccountInfoInteger(ACCOUNT_TRADE_ALLOWED);
//1.4 运行EA交易
bool isEAAllowed=AccountInfoInteger(ACCOUNT_TRADE_EXPERT);
//1.5 爆仓模式
int stopout_mode= AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE);
if(stopout_mode == ACCOUNT_STOPOUT_MODE_PERCENT)
{
printf("百分比模式");
}
else if(stopout_mode==ACCOUNT_STOPOUT_MODE_MONEY)
{
printf("money模式");
}
//1.6账号是否允许交易
bool is_accout_trade_allowed=AccountInfoInteger(ACCOUNT_TRADE_ALLOWED);
//2.获取账号信息(Double模式其它可以类推)
//2.1余额
double balance=AccountInfoDouble(ACCOUNT_BALANCE);
//2.2 盈利
double profit=AccountInfoDouble(ACCOUNT_PROFIT);
//3.获取账号信息(String模式其它可以类推)
string accountName=AccountInfoString(ACCOUNT_NAME);
string server=AccountInfoString(ACCOUNT_SERVER);
string cuurency=AccountInfoString(ACCOUNT_CURRENCY);
string company=AccountInfoString(ACCOUNT_COMPANY);
//4.终端设备属性(Integer模式其它可以类推)
//4.1版本号
int build=TerminalInfoInteger(TERMINAL_BUILD);
//4.2终端里面是否登录mql5账号
bool isLoginCommunity=TerminalInfoInteger(TERMINAL_COMMUNITY_ACCOUNT);
//4.3是否连接到交易服务器
bool is_connect_trade_server=TerminalInfoInteger(TERMINAL_CONNECTED);
//4.4当前设置是否允许dll
bool is_dll_allowed=TerminalInfoInteger(TERMINAL_DLLS_ALLOWED);
//4.5终端是否允许交易
bool is_terminal_trade_allowed=TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);
//4.6是否允许发送email
bool is_terminal_email_allowed=TerminalInfoInteger(TERMINAL_EMAIL_ENABLED);
//4.7是否允许通知智能手机
bool is_notification_phone_allowed=TerminalInfoInteger(TERMINAL_NOTIFICATIONS_ENABLED);
//4.8 是否是64位
bool is_64=TerminalInfoInteger(TERMINAL_X64);
//5.终端设备属性(Integer模式其它可以类推) 社区账号还有多少余额,购买信号用
double community_balance=TerminalInfoDouble(TERMINAL_COMMUNITY_BALANCE);
//6.终端设备属性(Integer模式其它可以类推)
string language=TerminalInfoString(TERMINAL_LANGUAGE);
string t_company=TerminalInfoString(TERMINAL_COMPANY);
string t_name=TerminalInfoString(TERMINAL_NAME);
string t_path=TerminalInfoString(TERMINAL_PATH);
string t_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
string t_commondata_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH);
//7.mql编辑器
//7.1当前文件的名字
string mql_fileName=MQLInfoString(MQL_PROGRAM_NAME);
string mql_filePath=MQLInfoString(MQL_PROGRAM_PATH);
}
//+------------------------------------------------------------------+
二.获取商品信息
//+------------------------------------------------------------------+
//| getSymbolInfomationScript.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//1.获取货币信息(Integer模式其它可以类推)
string symbol=Symbol();
//1.1最新成交量
int vol=SymbolInfoInteger(symbol,SYMBOL_VOLUME);
//1.2 点差
int spread=SymbolInfoInteger(symbol,SYMBOL_SPREAD);
//1.3点差是否固定(true就是浮动的)
bool is_spead_float=SymbolInfoInteger(symbol,SYMBOL_SPREAD_FLOAT);
//1.4 时间
datetime dt=SymbolInfoInteger(symbol,SYMBOL_TIME);
//1.5小数点位数
int digit=SymbolInfoInteger(symbol,SYMBOL_DIGITS);
//1.6 止损止盈离市价至少多少个点
int stops_level_points=SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL);
//1.7获得商品类型
int symbolType=SymbolInfoInteger(symbol,SYMBOL_TRADE_CALC_MODE);
if(symbolType==SYMBOL_CALC_MODE_FOREX) Alert("外汇");
if(symbolType==SYMBOL_CALC_MODE_FUTURES) Alert("期货");
if(symbolType==SYMBOL_CALC_MODE_CFD) Alert("cfd");
if(symbolType==SYMBOL_CALC_MODE_CFDINDEX) Alert("cfd指数");
if(symbolType==SYMBOL_CALC_MODE_CFDLEVERAGE) Alert("cfd杠杆合约");
if(symbolType==SYMBOL_CALC_MODE_EXCH_STOCKS) Alert("股票");
if(symbolType==SYMBOL_CALC_MODE_EXCH_FUTURES) Alert("exch期货");
//2.获取货币信息(Double模式其它可以类推)
//2.1下一手多单隔一夜能得的利息
double swap_long=SymbolInfoDouble(symbol,SYMBOL_SWAP_LONG);
//2.2下一手空单隔一夜能得的利息
double swap_short=SymbolInfoDouble(symbol,SYMBOL_SWAP_SHORT);
//3.获取货币信息(String模式其它可以类推)
string s_bank=SymbolInfoString(symbol,SYMBOL_BANK);
//4.获取session 行情时间,交易时间(返回值的日期可以忽略不计)
datetime fromDT,toDT;
SymbolInfoSessionQuote(symbol,MONDAY,0,fromDT,toDT);
printf(symbol+"行情更新时间为:"+fromDT+"-"+toDT);
datetime from1DT,to1DT;
SymbolInfoSessionTrade(symbol,MONDAY,0,from1DT,to1DT);
printf(symbol+"可交易时间为:"+from1DT+"-"+to1DT);
//5.遍历可用交易品种(ture 只显示市价里面(Market Watch)显示的)
string curSymbol;
double s_bid;
for(int i=0;i<SymbolsTotal(true);i++)
{
curSymbol=SymbolName(i,true);
s_bid=SymbolInfoDouble(curSymbol,SYMBOL_BID);
}
//6.删除或显示(Market Watch)
SymbolSelect("EURGBP",true);
//7.数据是否和服务器同步
bool is_synchro=SymbolIsSynchronized(Symbol());
}
//+------------------------------------------------------------------+
三.获取市场深度,类似A 股的5档10档行情,或盘口
//+------------------------------------------------------------------+
//| 获取盘口信息.mq5 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
//1.1订阅盘口行情
MarketBookAdd(Symbol());
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//1.3取消订阅
MarketBookRelease(Symbol());
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//2.1有新订单成立就会触发改接口
void OnTick()
{
//2.不需要订阅 直接获取盘口数据
MqlBookInfo priceArray[];
bool getBook=MarketBookGet(NULL,priceArray);
for(int i=0;i<ArraySize(priceArray);i++)
{
if(priceArray[i].type==BOOK_TYPE_SELL)
{
printf(i+" , "+priceArray[i].price+"up做空价格Ask");
}
if(priceArray[i].type==BOOK_TYPE_BUY)
{
printf(i+" , "+priceArray[i].price+"down做多价格Bid");
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//1.这个接口比OnTick快,不需要订单成立才触发
void OnBookEvent(const string &symbol)
{
//1.2.获取盘口数据
MqlBookInfo priceArray[];
bool getBook=MarketBookGet(NULL,priceArray);
if(getBook)
{
int size=ArraySize(priceArray);
Print("MarketBookInfo for ",Symbol());
for(int i=0;i<size;i++)
{
Print(i+":",priceArray[i].price
+" Volume = "+priceArray[i].volume,
" type = ",priceArray[i].type);
}
}
else
{
Print("Could not get contents of the symbol DOM ",Symbol());
}
}
//+------------------------------------------------------------------+
如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。