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.終端設(shè)備屬性(Integer模式其它可以類推)
//4.1版本號
int build=TerminalInfoInteger(TERMINAL_BUILD);
//4.2終端里面是否登錄mql5賬號
bool isLoginCommunity=TerminalInfoInteger(TERMINAL_COMMUNITY_ACCOUNT);
//4.3是否連接到交易服務(wù)器
bool is_connect_trade_server=TerminalInfoInteger(TERMINAL_CONNECTED);
//4.4當前設(shè)置是否允許dll
bool is_dll_allowed=TerminalInfoInteger(TERMINAL_DLLS_ALLOWED);
//4.5終端是否允許交易
bool is_terminal_trade_allowed=TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);
//4.6是否允許發(fā)送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.終端設(shè)備屬性(Integer模式其它可以類推) 社區(qū)賬號還有多少余額,購買信號用
double community_balance=TerminalInfoDouble(TERMINAL_COMMUNITY_BALANCE);
//6.終端設(shè)備屬性(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小數(shù)點位數(shù)
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指數(shù)");
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.數(shù)據(jù)是否和服務(wù)器同步
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有新訂單成立就會觸發(fā)改接口
void OnTick()
{
//2.不需要訂閱 直接獲取盤口數(shù)據(jù)
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快,不需要訂單成立才觸發(fā)
void OnBookEvent(const string &symbol)
{
//1.2.獲取盤口數(shù)據(jù)
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());
}
}
//+------------------------------------------------------------------+
如果您發(fā)現(xiàn)本文對你有所幫助劲绪,如果您認為其他人也可能受益男窟,請把它分享出去。