質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
MQL4

MQL4とは、MT4(MetaTrader4)で用いられるプログラム言語です。MT4は無料で使えるチャートソフトあり、MQL4を使うことで分析ツールのオリジナルスクリプトの作成ができます。

Q&A

解決済

1回答

1373閲覧

MQL4で外部情報の取得

nisei2

総合スコア19

MQL4

MQL4とは、MT4(MetaTrader4)で用いられるプログラム言語です。MT4は無料で使えるチャートソフトあり、MQL4を使うことで分析ツールのオリジナルスクリプトの作成ができます。

0グッド

1クリップ

投稿2020/12/20 23:51

編集2020/12/27 10:53

https://www.youtube.com/watch?v=NG-nq8LdBHY&list=WL&index=12
こちらの動画を参考にしてサイトから情報を取得したかったがやり方がわからない。

動画で取得しているサイトではなくこちらのサイトの曜日 国 イベント名を取得したい。
https://fx.minkabu.jp/references/holidays

該当のソースコード

MQL4

1int OnInit() 2{ 3 EventSetTimer(1); 4 return(INIT_SUCCEEDED); 5} 6 7void OnDeinit(const int reason) 8{ 9 EventKillTimer(); 10} 11 12void OnTimer() 13{ 14 static int timeHour = -1; 15 if(Hour() == timeHour) return; 16 timeHour = Hour(); 17 18 string cookie=NULL,headers; 19 char post[],result[]; 20 int res; 21//--- to enable access to the server, you should add URL 22//--- in the list of allowed URLs (Main Menu->Tools->Options, tab "Expert Advisors"): 23 string url="https://fx.minkabu.jp/references/holidays"; 24//--- Reset the last error code 25 ResetLastError(); 26//--- Loading a html page 27 int timeout=5000; //--- Timeout below 1000 (1 sec.) is not enough for slow Internet connection 28 res=WebRequest("GET",url,cookie,NULL,timeout,post,0,result,headers); 29//--- Checking errors 30 if(res==-1) 31 { 32 Print("Error in WebRequest. Error code =",GetLastError()); 33 //--- Perhaps the URL is not listed, display a message about the necessity to add the address 34 MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION); 35 } 36 else 37 { 38 //--- Load successfully 39 PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result)); 40 41 string text = CharArrayToString(result,0,WHOLE_ARRAY,CP_UTF8); 42 string str = NULL; 43 44 for(int i = 0; i < StringLen(fs-s fc-sub w25); i++){ 45 if(StringSubstr(text, i + 7, 5) == "\"time\">"){ 46 //曜日の取得に変更したい 47 string strTime = StringSubstr(text, i + 7, 5); 48 49 //国名の取得に変更したい 50 int posStar0 = StringFind(text, "★", i); 51 int posStar1 = StringFind(text, "</span>", posStar0); 52 string strStar = StringSubstr(text, posStar0, posStar1 - posStar0); 53 54 //イベントの取得に変更したい 55 int posEvent0 = StringFind(text, "]</span>", i) + 8; 56 int posEvent1 = StringFind(text, "</dt>", posEvent0); 57 string strEvent = StringSubstr(text, posEvent0, posEvent1 - posEvent0); 58 59 str += strTime + " " + strStar + " " + strEvent + "\n"; 60 } 61 } 62 Comment(str); 63 } 64}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mah

2020/12/23 19:29

動画を見ないと質問内容がわからないのでは回答を得るのは難しいと思います。 (WebRequestを使用してスクレイピングしたいということなのはまあわかるんですが) また、スクリプト等で実行できる完全なソースでないと、回答者側が残りを想像するしかなくなります。 そもそも提示されているソースコードもリンク先の動画内のソースの一部のようですし、自分でどこまで書けているのでしょう?
nisei2

2020/12/27 10:56

これは失礼しました。 ソースコード修正しました。 動画のコードは全部書いて、URLを自分の取得したいサイトのURLに変更までした所です。
mah

2020/12/27 19:54

一応回答はしておきましたが、自分で書いた部分がURLを書き換えただけなのでは単なる作業依頼でしかないですよ。 この手のサイトはある日突然構成が変わるので、自分で全て書けるようになったほうがいいです。
nisei2

2020/12/27 22:46

ありがとうございます。 自分で書けるようにコードの意味を理解できるようにしていきます。 コードの処理なのですが 1.自分の欲しい情報が載っているサイトを指定 2.サイトの中にある欲しい文字列の前後?の文字列を指定 3.その結果欲しい文字列だけ出力するみたいな感じの処理になっている という感じなのかなと思っているのですがあってますでしょうか?
mah

2020/12/27 23:42

あってます。
nisei2

2020/12/28 01:50

コンパイルはエラーなしなのですが出力がうまくされません。 コードになにか追加で記入する必要があるのでしょうか?
mah

2020/12/28 04:16

出力しているエラーは確認してます? WebRequestはEAとスクリプトでしか使用できないのでEAとして実行してください。 ストラテジーテスターも不可です。
nisei2

2020/12/29 07:31

EAとしてしないといけなかったのですね、ありがとうございました。
guest

回答1

0

ベストアンサー

MQL4

1#property strict 2 3int OnInit() 4{ 5 bool err = EventSetTimer(1); 6 if (!err) 7 { 8 PrintFormat("EventSetTimer failed [%d]", GetLastError()); 9 return INIT_FAILED; 10 } 11 12 return INIT_SUCCEEDED; 13} 14 15void OnDeinit(const int reason) 16{ 17 EventKillTimer(); 18} 19 20int GetTagText(string source, int startIndex, string tagText, string endTag, string& text) 21{ 22 text = ""; 23 24 int index = StringFind(source, tagText, startIndex); 25 if (index < 0) 26 { 27 return -1; 28 } 29 30 int last = StringFind(source, endTag, index); 31 if (last < 0) 32 { 33 return -1; 34 } 35 36 last += StringLen(endTag); 37 38 text = StringSubstr(source, index, last - index); 39 40 return last; 41} 42 43int GetInnerText(string source, int index, string startTag, string endTag, string& text) 44{ 45 text = ""; 46 47 index = StringFind(source, startTag, index); 48 if (index < 0) 49 { 50 return -1; 51 } 52 53 index += StringLen(startTag); 54 55 int last = StringFind(source, endTag, index); 56 57 text = StringSubstr(source, index, last - index); 58 59 index += StringLen(endTag); 60 61 return index; 62} 63 64string GetHolidays(string source) 65{ 66 string dateTag = "<span class=\"fnums w60\">"; 67 string weekTag = "<span class=\"fc-sub fs-s w30 pr5\">"; 68 string countryTag = "<p class=\"fs-s fc-sub w25\">"; 69 string holidayTag = "<p>"; 70 string spanEndTag = "</span>"; 71 string pEndTag = "</p>"; 72 73 int index = 0; 74 75 string result = ""; 76 77 string date; 78 string week; 79 80 index = GetInnerText(source, index, dateTag, spanEndTag, date); 81 if (index < 0) 82 { 83 return result; 84 } 85 86 index = GetInnerText(source, index, weekTag, spanEndTag, week); 87 if (index < 0) 88 { 89 return result; 90 } 91 92 while (true) 93 { 94 string country; 95 string holiday; 96 97 index = GetInnerText(source, index, countryTag, pEndTag, country); 98 if (index < 0) 99 { 100 return result; 101 } 102 103 index = GetInnerText(source, index, holidayTag, pEndTag, holiday); 104 if (index < 0) 105 { 106 return result; 107 } 108 109 result += StringFormat("%s %s %s %s\n", date, week, country, holiday); 110 } 111 112 return result; 113} 114 115void OnTimer() 116{ 117 static int timeHour = -1; 118 if(Hour() == timeHour) return; 119 timeHour = Hour(); 120 121 string cookie = NULL; 122 string headers; 123 char post[]; 124 char result[]; 125 126 string req_url = "https://fx.minkabu.jp/references/holidays"; 127 128 int res = WebRequest( 129 "GET", // HTTPメソッド 130 req_url, // URL 131 cookie, // cookie 132 NULL, // リファラ 133 5000, // タイムアウト 134 post, // HTTPメッセージ本体 135 0, // HTTPメッセージサイズ 136 result, // 応答データ配列 137 headers // 応答ヘッダ 138 ); 139 140 if (res == -1) // エラーチェック 141 { 142 Print("WebRequesエラー。 エラーコード = ", GetLastError()); 143 144 return; 145 } 146 147 string text = CharArrayToString(result, 0, WHOLE_ARRAY,CP_UTF8); 148 149 string dlTag = "<dl class=\"holidayslist__day flexbox flexbox_wrap flexbox_l-end"; 150 string dlEndTag = "</dl>"; 151 152 int index = 0; 153 154 string comment = ""; 155 156 while (true) 157 { 158 string dl; 159 160 index = GetTagText(text, index, dlTag, dlEndTag, dl); 161 if (index < 0) 162 { 163 break; 164 } 165 166 comment += GetHolidays(dl); 167 } 168 169 Comment(comment); 170}

投稿2020/12/27 19:49

編集2020/12/27 19:59
mah

総合スコア591

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問