程式交易教學

誠邀您參加全球知名外匯經紀商OANDA的自營交易(Prop Trader)

報名OANDA自營交易(Prop Trader),並通過我們的考核,您就可以使用OANDA提供的資金進行交易,獲得高達80%的交易利潤分成。



限時優惠代碼
在購買鈦金挑戰賽時輸入“TITANIUM30”,即獲得30%的折扣(優惠截止日期為2024年4月30日)。

優化了挑戰塞交易規則
無最低交易天數限制等優化了挑戰賽的交易規則。

500,000美元交易資金
您可以使用最高500,000美元的資金進行交易。

豐富的交易商品
您可以交易包括外匯、黃金、原油、股票指數等多種商品。



利用WebRequest函數來取得數據並簡易顯示的方法說明

利用OnTimer函數

在本章節中,將說明如何透過用來獲取網絡數據的WebRequest函數,來取得網路上的文字列數據、擷取其中資訊,並加以簡易顯示。

首先將說明建立新檔案時的注意事項;由於WebRequest函數為EA(Expert Advisor)專用、無法作為客製化的指標,因此需開啟MetaEditor、點擊「創建新文檔」按鈕,勾選「EA交易(範本)」並點選「下一步」。

EA交易(範本)

此處將檔案名稱命名為「WebRequest_demo」;而為了使其在週六週日時也能運作,於「EA交易事件處理常式」勾選「OnTimer」並點擊「下一步」。「EA交易r的測試時間處理常式」無須做任何變更,如此點選「完成」即可結束建立新檔案的步驟。

接下來將先設定計時器。此處在最初以每1秒鐘為標準來處理,其後則設定為每1小時進行動作,故應在OnTimer之下寫入以下編碼。

void OnTimer()

{

//—

static int timeHour = -1;

if (Hour() == timeHour) return;

timeHour = Hour();
此處的if文體意指「若時間並未改變,便於此處結束計算」;納入目前的時間,若其並未出現變動則不會進行計算。考量到0點的狀況,可將初始值設定為「-1」。

如此便完成了當下立即開始處理、其後改為每1小時進行動作的編碼。

設定欲取得數據的URL

可從MQL4幫助檔中取得WebRequest函數的範例編碼;點擊MQL4幫助檔的選單中的「Common Functions」→「WebRequest」,並複製該頁面內的下列編碼,再貼至OnTimer序列下面。

string cookie=NULL,headers;

char post[],result[];

int res;

//— to enable access to the server, you should add URL “https://www.google.com/finance”

//— in the list of allowed URLs (Main Menu->Tools->Options, tab “Expert Advisors”):

string google_url=”https://www.google.com/finance”;

//— Reset the last error code

ResetLastError();

//— Loading a html page from Google Finance

int timeout=5000; //— Timeout below 1000 (1 sec.) is not enough for slow Internet connection

res=WebRequest(“GET”,google_url,cookie,NULL,timeout,post,0,result,headers);

//— Checking errors

if(res==-1)

{

Print(“Error in WebRequest. Error code =”,GetLastError());

//— Perhaps the URL is not listed, display a message about the necessity to add the address

MessageBox(“Add the address ‘”+google_url+”‘ in the list of allowed URLs on tab ‘Expert Advisors'”,”Error”,MB_ICONINFORMATION);

}

else

{

//— Load successfully

PrintFormat(“The file has been successfully loaded, File size =%d bytes.”,ArraySize(result));

//— Save the data to a file

int filehandle=FileOpen(“GoogleFinance.htm”,FILE_WRITE|FILE_BIN);

//— Checking errors

if(filehandle!=INVALID_HANDLE)

{

//— Save the contents of the result[] array to a file

FileWriteArray(filehandle,result,0,ArraySize(result));

//— Close the file

FileClose(filehandle);

}

else Print(“Error in FileOpen. Error code=”,GetLastError());

}
該範例編碼可進入Google網站、取得數據並作為檔案加以保存。

由於本次不會進行「保存檔案」,因此可刪除「//— Save the data to a file」以下內容。另外,因本次的目標網頁並非Google,而是刊載了經濟指標的公佈時間與事件內容的「Yahoo!經濟」,故應刪除與「google」有關的部分,並將URL修改為「https://m.finance.yahoo.co.jp/fx/marketcalendar」。

下列紅字為修改的部分。

string cookie = NULL, headers;

char post[], result[];

int res;

//— to enable access to the server, you should add URL

//— in the list of allowed URLs (Main Menu->Tools->Options, tab “Expert Advisors”):

string url = “https://m.finance.yahoo.co.jp/fx/marketcalendar“;

//— Reset the last error code

ResetLastError();

//— Loading a html page

int timeout = 5000; //— Timeout below 1000 (1 sec.) is not enough for slow Internet connection

res = WebRequest(“GET”, url, cookie, NULL, timeout, post, 0, result, headers);

//— Checking errors

if(res == -1) {

Print(“Error in WebRequest. Error code =”, GetLastError());

//— Perhaps the URL is not listed, display a message about the necessity to add the address

MessageBox(“Add the address ‘” + url + “‘ in the list of allowed URLs on tab ‘Expert Advisors'”, “Error”, MB_ICONINFORMATION);

} else {

//— Load successfully

PrintFormat(“The file has been successfully loaded, File size =%d bytes.”, ArraySize(result));
如此即可從指定的URL網頁接收數據,並將數據納入定義的result序列中。由於本次將會把該序列變更為string型,並利用Commen函數將變更的數據以圖表呈現,因此應在最後加上下列公式。

string text = CharArrayToString(result);

Comment(text);
如此便能夠顯示出變更的數據。

登錄目標網站

目前已利用Comment函數顯示出變更的數據;但若是使用WebRequest函數,則需於MT4進行適用設定。

點擊MT4選單中的「工具」→「選項」,雙點擊「EA交易」標籤,並勾選「允許WebRequest用於所列的URL、登錄目標網站的URL。因本次將進入「Yahoo!經濟」的頁面,故將新增「https://m.finance.yahoo.co.jp」並點擊「OK」。

EA交易

登錄URL之後,只要將編譯的「WebRequest_demo」設定於圖表,便會顯示出「https://m.finance.yahoo.co.jp/fx/marketcalendar」的網頁原始碼(在Yahoo!經濟的經濟指標頁面上點擊右鍵→選擇「顯示網頁原始碼」後出現的原始碼)。

原始碼

由於並未於所顯示的原始碼中指定文字編碼,因此會亂碼的狀態;如欲解決此問題,便須使用可變更序列至String型的「CharArrayToString」函數,並如以下所示來指定參數。

string text = CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8);

此編碼意指將序列自0號開始全數變更,並指定「UTF8」的文字編碼;若於此處進行編譯,即可解除亂碼。

UTF8的文字編碼

擷取經濟指標的數據

接下來,將從顯示的原始碼中擷取經濟指標數據。瀏覽Yahoo!經濟的經濟指標網頁原始碼,將會發現經濟指標的時間資訊前方皆為「”time”>」的7字所組成;可在將其找出後,組成用於搜尋的序列。

此處將利用for文體在文字列中進行搜尋;由於將從文字列的0號開始搜尋全部文字,因此使用可返回文字列的「StringLen」函數。

for (int i = 0; i < StringLen(text); i++) {
其次,將以「StringSubstr」函數從指定位置擷取文字列。為了使其從第i個文字中取出7字、並在符合「”time”>」時進行處理,應加上以下公式。如欲使雙引號中的內容以文字列呈現,則須在其前方添加「¥」符號。

if (StringSubstr(text, i, 7) == “\”time\”>”) {
如此即可找出「”time”>」的位置,因此將前方「”」處的7字後方抓出5字,並如以下所示加以指定。前方「”」7字後方的5字,代表公布經濟指標的時間。

string strTime = StringSubstr(text, i + 7, 5);
若在最後添加「str += strTime + “wn”;」的公式,即可將擷取的經濟指標公佈時間換行顯示。

for (int i = 0; i < StringLen(text); i++) {

if (StringSubstr(text, i, 7) == “\”time\”>”) {

string strTime = StringSubstr(text, i + 7, 5);

str += strTime + “\n”;

}

}

Comment(str);
經濟指標公佈時間換行顯示

擷取經濟指標的★符號

在時間的部分之後,將擷取標明重要性的★符號。

在經濟指標的頁面中,必定會有1~3個★符號,因此需先搜尋★符號的位置。此處可利用出現在★符號後方的「</span>」文字列,並如以下所示,以「StringFind」函數來搜尋★符號。

int posStar0 = StringFind(text, “★”, i);
StringFind函數的設定項目
string_value 指定被搜尋的文字列
match_substring 指定欲搜尋的文字列
start_pos 指定開始搜尋的文字列位置

接下來是最末端的部分;搜尋的文字列為「</span>」,並將開始搜尋的位置放在找到★之後。

int posStar1 = StringFind(text, “<]span>”, posStar0);
接著將利用該位置資訊,以 string定義strStar。開始的位置為「posStar0」、文字長度為「文字結束的位置(posStar1)」-「文字開始的位置(posStar0)」。

string strStar = StringSubstr(text, posStar0, posStar1 – posStar0);
在顯示文字列的公式「str += strTime + “¥n”;」中,添加代表★符號的「strStar」並進行編譯,即可在時間的後方顯示★符號。

str += strTime + ” ” + strStar + “¥n”;
時間的後方顯示★符號

擷取經濟指標的事件內容

同樣地,此處將擷取經濟指標的事件內容。為了搜尋事件開始的部分,將利用「]</span>」的文字列;結束的部分則是「</dt>」標籤。可如以下所示添加公式。

int posEvent0 = StringFind(text, “]</span>”, i) + 8;
int posEvent1 = StringFind(text, “</dt>”, posEvent0);
string strEvent = StringSubstr(text, posEvent0, posEvent1 – posEvent0);
由於最初找到的「]</span>」為8字,因此將該8字後方作為事件的文字列開頭;結尾則是「</dt>」。長度則是「事件文字列的結束位置(posEvent1)」-「事件文字列的開始位置(posEvent0)」。

在顯示方才文字列的公式「str += strTime + ” ” + strStar + “¥n”;」中,添加顯示事件的「strEvent」即可完成。

str += strTime + ” ” + strStar + ” ” + strEvent + “¥n”;
如此進行編譯後,就能顯示時間、★符號、事件內容等三個項目,並能夠從Web完整擷取數據。

Web完整擷取數據

原始碼

本次製作的原始碼如以下所示。

//+——————————————————————+

//| WebRequest_demo.mq4 |

//| Copyright 2021, MetaQuotes Software Corp. |

//| https://www.mql5.com |

//+——————————————————————+

#property copyright “Copyright 2021, MetaQuotes Software Corp.”

#property link “https://www.mql5.com”

#property version “1.00”

#property strict

//+——————————————————————+

//| Expert initialization function |

//+——————————————————————+

int OnInit()

{

//— create timer

EventSetTimer(1);

//—

return(INIT_SUCCEEDED);

}

//+——————————————————————+

//| Expert deinitialization function |

//+——————————————————————+

void OnDeinit(const int reason)

{

//— destroy timer

EventKillTimer();

}

//+——————————————————————+

//| Expert tick function |

//+——————————————————————+

void OnTick()

{

//—

}

//+——————————————————————+

//| Timer function |

//+——————————————————————+

void OnTimer()

{

//—

static int timeHour = -1;

if (Hour() == timeHour) return;

timeHour = Hour();

string cookie = NULL, headers;

char post[], result[];

int res;

//— to enable access to the server, you should add URL

//— in the list of allowed URLs (Main Menu->Tools->Options, tab “Expert Advisors”):

string url = “https://m.finance.yahoo.co.jp/fx/marketcalendar”;

//— Reset the last error code

ResetLastError();

//— Loading a html page

int timeout = 5000; //— Timeout below 1000 (1 sec.) is not enough for slow Internet connection

res = WebRequest(“GET”, url, cookie, NULL, timeout, post, 0, result, headers);

//— Checking errors

if(res == -1) {

Print(“Error in WebRequest. Error code =”, GetLastError());

//— Perhaps the URL is not listed, display a message about the necessity to add the address

MessageBox(“Add the address ‘” + url + “‘ in the list of allowed URLs on tab ‘Expert Advisors'”, “Error”, MB_ICONINFORMATION);

} else {

//— Load successfully

PrintFormat(“The file has been successfully loaded, File size =%d bytes.”, ArraySize(result));

string text = CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8);

string str = NULL;

for (int i = 0; i < StringLen(text); i++) {

if (StringSubstr(text, i, 7) == “\”time\”>”) {

string strTime = StringSubstr(text, i + 7, 5);

int posStar0 = StringFind(text, “★”, i);

int posStar1 = StringFind(text, “</span>”, posStar0);

string strStar = StringSubstr(text, posStar0, posStar1 – posStar0);

int posEvent0 = StringFind(text, “]</span>”, i) + 8;

int posEvent1 = StringFind(text, “</dt>”, posEvent0);

string strEvent = StringSubstr(text, posEvent0, posEvent1 – posEvent0);

str += strTime + ” ” + strStar + ” ” + strEvent + “\n”;

}

}

Comment(str);

}

}

//+——————————————————————+

將EA自動程式交易應用於外匯與差價合約交易中

EA

我們以圖文形式詳細介紹有關EA自動程式交易的基本知識,以及在MT4/MT5平台上的安裝、參數設定方法、編碼等等內容。另外,對持有OANDA帳戶的客戶,還可以免費使用我們的獨有EA與指標工具。

誠邀您參加全球知名外匯經紀商OANDA的自營交易(Prop Trader)

報名OANDA自營交易(Prop Trader),並通過我們的考核,您就可以使用OANDA提供的資金進行交易,獲得高達80%的交易利潤分成。



限時優惠代碼
在購買鈦金挑戰賽時輸入“TITANIUM30”,即獲得30%的折扣(優惠截止日期為2024年4月30日)。

優化了挑戰塞交易規則
無最低交易天數限制等優化了挑戰賽的交易規則。

500,000美元交易資金
您可以使用最高500,000美元的資金進行交易。

豐富的交易商品
您可以交易包括外匯、黃金、原油、股票指數等多種商品。