ショートカットファイル経由がポイント
チャートにニュース等を表示していると詳細を知りたくなることがあります。
その際、いちいちブラウザを開いて該当のニュースサイトを開くのは面倒です。
チャートから直接開ければ便利です。
というわけで、チャートに貼り付けたオブジェクトをクリックすると、ブラウザを開いてパラメータで指定したURLに飛んでくれるインジケーターを作成してみました。
#property copyright "Copyright (c) 2020, autoFX"
#property link "https://autofx100.com/"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| ライブラリ |
//+------------------------------------------------------------------+
#include <stderror.mqh>
#include <stdlib.mqh>
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| インポート |
//+------------------------------------------------------------------+
#import "shell32.dll"
int ShellExecuteW(int hwnd, string lpOperation, string lpFile,
string lpParameters, string lpDirectory, int nShowCmd);
#import
//+------------------------------------------------------------------+
//| パラメータ |
//+------------------------------------------------------------------+
extern string URL = "https://www.yahoo.co.jp/";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
string objNm = "OBJECT_NAME";
bool created = ObjectCreate(0, objNm, OBJ_LABEL, 0, 0, 0);
if(created){
ObjectSetInteger(0, objNm, OBJPROP_XDISTANCE, 200);
ObjectSetInteger(0, objNm, OBJPROP_YDISTANCE, 100);
ObjectSetInteger(0, objNm, OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, objNm, OBJPROP_FONTSIZE, 20);
ObjectSetInteger(0, objNm, OBJPROP_COLOR, clrYellow);
ObjectSetInteger(0, objNm, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, objNm, OBJPROP_SELECTED, false);
ObjectSetString(0, objNm, OBJPROP_FONT, "Yu Gothic UI Bold");
ObjectSetString(0, objNm, OBJPROP_TEXT, "指定URLにジャンプ");
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
return(rates_total);
}
//+------------------------------------------------------------------+
//| Chart Event function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if(id == CHARTEVENT_OBJECT_CLICK){
if(sparam == "OBJECT_NAME"){
bool result = OpenURL(URL);
}
}
}
//+------------------------------------------------------------------+
//|【関数】URLを指定してブラウザを起動 |
//| |
//|【戻値】true: 成功 |
//| false: 失敗 |
//| |
//|【備考】なし |
//+------------------------------------------------------------------+
bool OpenURL(string aURL){
// ブラウザのショートカット作成
// ショートカットファイル名「SHORTCUT_NAME」はお好きな名前で
string file = "SHORTCUT_NAME" + ".URL";
int handle = FileOpen(file, FILE_CSV|FILE_WRITE, '~');
if(handle < 1){
return(false);
}
FileWrite(handle, "[InternetShortcut]");
FileWrite(handle, "URL=" + aURL);
FileClose(handle);
// ブラウザ起動
string path = TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL4\\Files\\" + file;
int SW_SHOWNORMAL = 1;
int result = ShellExecuteW(0, "open", path, "", NULL, SW_SHOWNORMAL);
if(result > 32){
return(true);
}else{
return(false);
}
}
OpenURL関数でショートカットファイルをMQL4¥Filesフォルダに作成するところがポイントです。
そのショートカットファイルをWindows APIのShellExecuteW関数で実行します。
これだけでURL指定のブラウザ起動が可能になります。
ちなみにURLを指定せずブラウザを起動するだけだったら、ショートカットファイルは不要です。
