どんな記事?
MT4 の MQL4 でつくるインジケーターのよく使う組み合わせをまとめてみました(主に自分用)。どれもコピペで機能をつけ加えることができる簡単なものです。
今後も、新たに見つけたり思いついたものがあれば、ここに加えていく予定です!
更新
2019年9月1日
「別の時間軸の価格や計算値を取得」「各市場のオープン・クローズ、米経済指標の時刻をタテ線で描画」を追加しました。
特定の条件下でアラート、メール、アプリのプッシュ通知
MQL4
input bool D_Alert = false; //アラート機能
input bool M_Alert = false; //メール機能
input bool P_Alert = false; //プッシュ通知機能
input bool Sig_Alert = false; //シグナルのアラート
double GCBuffer[];
double DCBuffer[];
datetime b4time;
// 初期化処理
int OnInit() {
SetIndexBuffer( /*適切な数字*/, GCBuffer );
SetIndexBuffer( /*適切な数字*/, DCBuffer );
return( INIT_SUCCEEDED );
}
// ゴールデンクロス・デットクロスを計算する
void calcGcDc( int i ){
// 計算処理
}
// 計算処理
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[]) {
bool check1 ,check2 ,newGC ,newDC ,someSig;
string title ,titleSig ,text ,textSig;
int limit = rates_total - prev_calculated;
if( prev_calculated > 0 ) limit++;
bool isNewCandle = Time[0]!=b4time && limit==2;
if( isNewCandle ) {
calcGcDc(1);
check1 = D_Alert || M_Alert || P_Alert;
check2 = Sig_Alert;
if( check1 && check2 ) {
newGC = GCBuffer[1] != EMPTY_VALUE;
newDC = DCBuffer[1] != EMPTY_VALUE;
someSig = ( Sig_Alert && ( newGC || newDC ) );
if( someSig ) {
textSig = Sig_Alert ? ( newGC ? "GC Sig Line!! " : ( newDC ? "DC Sig Line!! " : "" ) ) : "";
text = "( "+StringSubstr( Symbol() ,0 ,6 )+" - "+IntegerToString( Period() )+" ) "+textSig;
if( D_Alert ) Alert( text );
if( P_Alert ) SendNotification( text );
if( M_Alert ) {
titleSig = Sig_Alert ? newGC ? "GC Sig!" : newDC ? "DC Sig!" : "" : "";
title = "( "+StringSubstr( Symbol() ,0 ,6 )+" - "+IntegerToString( Period() )+" ) "+titleSig;
SendMail( title ,text );
}
}
}
}
b4time = Time[0];
return( rates_total );
}
2本のラインの間を塗りつぶす
TradingViewだと、標準でついている機能。MT4だと対応していない。2本の線の間に縦線(ヒストグラム)を描画することはできるけど、見づらいしカッコわるい。あと、オシレーターではそれすらできない。MT4では三角形を大量に描画することで似たようなことが実現できる。
MQL4
input int Show_fill = 200; //塗りつぶしの表示期間(多いと重い、0で非表示)
datetime b4time;
// ラインの間を塗りつぶすための関数
void PaintFill( int k ,bool repaint ) {
if( !repaint ) obj_num++;
string objname1 = obj_prefix + IntegerToString( obj_num ) + "-1";
string objname2 = obj_prefix + IntegerToString( obj_num ) + "-2";
datetime t0 = Time[k] ,t1 = Time[k+1];
double a0 = mSlowD[k] ,a1 = mSlowD[k+1];
double b0 = lSlowD[k] ,b1 = lSlowD[k+1];
color c0 = a0 >= b0 ? c_up : c_down;
color c1 = a1 >= b1 ? c_up : c_down;
if( !repaint ) {
ObjectCreate( objname1 ,OBJ_TRIANGLE ,win ,t1,a1 ,t1,b1 ,t0,a0 );
ObjectCreate( objname2 ,OBJ_TRIANGLE ,win ,t1,b1 ,t0,a0 ,t0,b0 );
ObjectSet( objname1 ,OBJPROP_SELECTABLE ,false);
ObjectSet( objname2 ,OBJPROP_SELECTABLE ,false);
ObjectSet( objname1 ,OBJPROP_HIDDEN ,true);
ObjectSet( objname2 ,OBJPROP_HIDDEN ,true);
} else {
ObjectMove( objname1 ,0 ,t1,a1 );
ObjectMove( objname1 ,1 ,t1,b1 );
ObjectMove( objname1 ,2 ,t0,a0 );
ObjectMove( objname2 ,0 ,t1,b1 );
ObjectMove( objname2 ,1 ,t0,a0 );
ObjectMove( objname2 ,2 ,t0,b0 );
}
ObjectSet( objname1 ,OBJPROP_COLOR ,c1);
ObjectSet( objname2 ,OBJPROP_COLOR ,c0);
}
int OnInit() {
// 塗りつぶしのオブジェクトをすべて削除
ObjectsDeleteAll( chart_id ,obj_prefix );
// 各種処理
}
// インジケーター削除時の処理
void OnDeinit( const int reason ) {
// 塗りつぶしのオブジェクトをすべて削除
ObjectsDeleteAll( chart_id ,obj_prefix );
}
// 計算処理
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[] ) {
int limit = rates_total - prev_calculated;
if( prev_calculated > 0 ) limit++;
bool isNewCandle = Time[0]!=b4time && limit==2;
if( !isNewCandle )
if( Show_fill > 0 ) PaintFill( 0 ,true );
if( isNewCandle ) {
if( Show_fill > 0 ) {
ObjectsDeleteAll( chart_id ,obj_prefix );
for( int i=rates_total-2; i>=0; i-- ) {
if( i < Show_fill ) PaintFill( i ,false );
}
}
}
b4time = Time[0];
return( rates_total );
}
別の時間軸の価格や計算値を取得
以下の関数では、日足の20日最高値・最安値、指数平滑のATRを取得。
MQL4
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 C' 37,134,202'
#property indicator_color2 C' 37,134,202'
#property indicator_style1 DRAW_LINE
#property indicator_style2 DRAW_LINE
#property indicator_width1 2
#property indicator_width2 2
input int lenHL = 20; //日足のHLバンド(HighLowバンド)
input int lenATR = 20; //日足のATR(PIVOTに使用)
double highestDay[];
double lowestDay[];
double openDay[];
double b4AtrDay = NULL;
int b4iDay;
double atrDay;
// 初期化処理
int OnInit() {
IndicatorShortName( "Test" );
IndicatorDigits( Digits + 1 );
SetIndexBuffer( 0 ,highestDay );
SetIndexBuffer( 1 ,lowestDay );
SetIndexLabel( 0 ,"Highest" );
SetIndexLabel( 1 ,"Lowest" );
return( INIT_SUCCEEDED );
}
// 計算処理
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[]) {
int limit = rates_total - prev_calculated;
if( prev_calculated > 0 ) limit++;
for( int i=limit-1; i>=0; i-- ) {
int iDay = iBarShift( NULL ,PERIOD_D1 ,Time[i] );
bool isSameDate = b4iDay == iDay;
int iDayHighest = iHighest( NULL ,PERIOD_D1 ,MODE_HIGH ,lenHL ,iDay+1 );
int iDayLowest = iLowest( NULL ,PERIOD_D1 ,MODE_LOW ,lenHL ,iDay+1 );
if( b4AtrDay == NULL ) atrDay = iATR( NULL ,PERIOD_D1 ,lenATR ,iDay+1 );
else if( isSameDate ) atrDay = b4AtrDay;
else{
double highDay = iHigh( NULL ,PERIOD_D1 ,iDay+1 );
double lowDay = iLow( NULL ,PERIOD_D1 ,iDay+1 );
double b4CloseDay = iClose( NULL ,PERIOD_D1 ,iDay+2 );
double tr = MathMax( MathMax( MathAbs( highDay - b4CloseDay ) ,MathAbs( lowDay - b4CloseDay ) ) ,MathAbs( highDay - lowDay ) );
atrDay = ( b4AtrDay * ( lenATR - 1 ) + tr * 2 ) / ( lenATR + 1 );
}
highestDay[i] = iHigh( NULL ,PERIOD_D1 ,iDayHighest );
lowestDay[i] = iLow( NULL ,PERIOD_D1 ,iDayLowest );
b4iDay = iDay;
b4AtrDay = atrDay;
}
return( rates_total );
}
各市場のオープン・クローズ、米経済指標の時刻をタテ線で描画
夏時間は手動のON/OFF、設定した各時刻でタテ線を描画する。
MQL4
#property strict
#property indicator_chart_window
input bool show_vline = true; //当日のタテ線を描画する
input color c_vl = C' 53,202, 13'; //タテ線の色
input bool summertime_ny = true; //NYの夏時間を適用
input bool summertime_eu = true; //EUの夏時間を適用
input string jp1_op = "09:00"; //東京 前場寄付き
input string jp_fix = "09:55"; //東京 仲値
input string hk_fix = "10:15"; //人民元 基準レート
input string hk1_op = "10:30"; //香港 寄付き
input string jp1_cl = "11:30"; //東京 前引け
input string jp2_op = "12:30"; //東京 後場入り
input string hk1_cl = "13:00"; //香港 前引け
input string hk2_op = "14:00"; //香港 後場入り
input string jp2_cl = "15:00"; //東京 大引け
input string uk_op_s = "16:00"; //ロンドン 寄付き(夏)
input string uk_op_w = "17:00"; //ロンドン 寄付き(冬)
input string hk2_cl = "17:00"; //香港 大引け
input string us_econ_s = "21:30"; //NY 指標発表(夏)
input string us_econ_w = "22:30"; //NY 指標発表(冬)
input string ny_op_s = "22:30"; //NY 寄付き(夏)
input string ny_op_w = "23:30"; //NY 寄付き(冬)
input string uk_fix_s = "00:00"; //ロンドン FIX(夏)
input string uk_fix_w = "01:00"; //ロンドン FIX(冬)
input string uk_cl_s = "00:30"; //ロンドン 大引け(夏)
input string uk_cl_w = "01:30"; //ロンドン 大引け(冬)
input string ny_cl_s = "05:00"; //NY 大引け(夏)
input string ny_cl_w = "06:00"; //NY 大引け(冬)
string us_econ = summertime_ny ? us_econ_s : us_econ_w;
string ny_op = summertime_ny ? ny_op_s : ny_op_w;
string ny_cl = summertime_ny ? ny_cl_s : ny_cl_w;
string uk_fix = summertime_eu ? uk_fix_s : uk_fix_w;
string uk_op = summertime_eu ? uk_op_s : uk_op_w;
string uk_cl = summertime_eu ? uk_cl_s : uk_cl_w;
long chart_id = ChartID();
int win = ChartWindowFind();
string obj_prefix = "Test";
int obj_num = 0;
datetime b4time;
double b4AtrDay = NULL;
int b4iDay;
int diff = ( 9 - ( TimeCurrent() - TimeGMT() ) / 60 / 60 ) * 60 * 60;
int oneDay = 24 * 60 * 60;
// ラインの間を塗りつぶすための関数
void PaintAndDraw( int k ) {
ObjectsDeleteAll( chart_id ,obj_prefix + "_line" );
obj_num++;
string linename8 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-8";
string linename9 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-9";
string linename10 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-10";
string linename11 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-11";
string linename12 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-12";
string linename13 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-13";
string linename14 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-14";
string linename15 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-15";
string linename16 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-16";
string linename17 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-17";
string linename18 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-18";
string linename19 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-19";
string linename20 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-20";
string linename21 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-21";
string linename22 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-22";
string linename23 = obj_prefix + "_line" + IntegerToString( obj_num ) + "-23";
datetime t0 = Time[k];
datetime t1 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " 23:59" );
double a0 = High[k] * 2;
double a6 = Low[k] / 2;
if( show_vline ){
datetime t8 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + jp1_op ) - diff;
datetime t9 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + jp_fix ) - diff;
datetime t10 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + hk_fix ) - diff;
datetime t11 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + hk1_op ) - diff;
datetime t12 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + jp1_cl ) - diff;
datetime t13 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + jp2_op ) - diff;
datetime t14 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + hk1_cl ) - diff;
datetime t15 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + hk2_op ) - diff;
datetime t16 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + jp2_cl ) - diff;
datetime t17 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + uk_op ) - diff;
datetime t18 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + hk2_cl ) - diff;
datetime t19 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + us_econ ) - diff;
datetime t20 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + ny_op ) - diff;
datetime t21 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + uk_fix ) - diff + oneDay;
datetime t22 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + uk_cl ) - diff + oneDay;
datetime t23 = StrToTime( TimeToStr( Time[k] ,TIME_DATE ) + " " + ny_cl ) - diff + oneDay;
ObjectCreate( linename8 ,OBJ_TRENDBYANGLE ,win ,t8 ,a0 ,t8 ,a6 );
ObjectCreate( linename9 ,OBJ_TRENDBYANGLE ,win ,t9 ,a0 ,t9 ,a6 );
ObjectCreate( linename10 ,OBJ_TRENDBYANGLE ,win ,t10 ,a0 ,t10 ,a6 );
ObjectCreate( linename11 ,OBJ_TRENDBYANGLE ,win ,t11 ,a0 ,t11 ,a6 );
ObjectCreate( linename12 ,OBJ_TRENDBYANGLE ,win ,t12 ,a0 ,t12 ,a6 );
ObjectCreate( linename13 ,OBJ_TRENDBYANGLE ,win ,t13 ,a0 ,t13 ,a6 );
ObjectCreate( linename14 ,OBJ_TRENDBYANGLE ,win ,t14 ,a0 ,t14 ,a6 );
ObjectCreate( linename15 ,OBJ_TRENDBYANGLE ,win ,t15 ,a0 ,t15 ,a6 );
ObjectCreate( linename16 ,OBJ_TRENDBYANGLE ,win ,t16 ,a0 ,t16 ,a6 );
ObjectCreate( linename17 ,OBJ_TRENDBYANGLE ,win ,t17 ,a0 ,t17 ,a6 );
ObjectCreate( linename18 ,OBJ_TRENDBYANGLE ,win ,t18 ,a0 ,t18 ,a6 );
ObjectCreate( linename19 ,OBJ_TRENDBYANGLE ,win ,t19 ,a0 ,t19 ,a6 );
ObjectCreate( linename20 ,OBJ_TRENDBYANGLE ,win ,t20 ,a0 ,t20 ,a6 );
ObjectCreate( linename21 ,OBJ_TRENDBYANGLE ,win ,t21 ,a0 ,t21 ,a6 );
ObjectCreate( linename22 ,OBJ_TRENDBYANGLE ,win ,t22 ,a0 ,t22 ,a6 );
ObjectCreate( linename23 ,OBJ_TRENDBYANGLE ,win ,t23 ,a0 ,t23 ,a6 );
ObjectSet( linename8 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename9 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename10 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename11 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename12 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename13 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename14 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename15 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename16 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename17 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename18 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename19 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename20 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename21 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename22 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename23 ,OBJPROP_SELECTABLE ,false );
ObjectSet( linename8 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename9 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename10 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename11 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename12 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename13 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename14 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename15 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename16 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename17 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename18 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename19 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename20 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename21 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename22 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename23 ,OBJPROP_HIDDEN ,true );
ObjectSet( linename8 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename9 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename10 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename11 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename12 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename13 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename14 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename15 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename16 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename17 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename18 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename19 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename20 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename21 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename22 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename23 ,OBJPROP_COLOR ,c_vl );
ObjectSet( linename8 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename9 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename10 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename11 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename12 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename13 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename14 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename15 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename16 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename17 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename18 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename19 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename20 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename21 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename22 ,OBJPROP_RAY_RIGHT ,false );
ObjectSet( linename23 ,OBJPROP_RAY_RIGHT ,false );
}
}
// 初期化処理
int OnInit() {
// 塗りつぶしのオブジェクトをすべて削除
ObjectsDeleteAll( chart_id ,obj_prefix );
IndicatorShortName( "Test" );
return( INIT_SUCCEEDED );
}
// 計算処理
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[]) {
int limit = rates_total - prev_calculated;
if( prev_calculated > 0 ) limit++;
for( int i=limit-1; i>=0; i-- ) {
int iDay = iBarShift( NULL ,PERIOD_D1 ,Time[i] );
bool isSameDate = b4iDay == iDay;
if( !isSameDate ) PaintAndDraw( i );
b4iDay = iDay;
}
return( rates_total );
}
開発を承っています
- Pineスクリプト(インジケーターやストラテジー)
- Google Apps Script
- Python
- MQL4
などの開発を承っています。とくに投資関連が得意です。過去の事例は「実績ページ(不定期更新)」でご確認ください。ご相談は「お問い合わせ」からお願いします。
同じタグの記事
- 記事をシェア