期間内の高値安値を求めるインディケーターの作り方
HL多くの人がトレードの目安として、高値安値を気にすると思います。
高値安値は、多くの人に意識されるので、トレードに有効だと思います。
今回は、過去の高値安値にラインを引くインディケーターの作り方を紹介します。
今回のコードはコチラ
つっくたものはこちら→HL.zip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
//+------------------------------------------------------------------+ //| HL2.mq4 | //| Copyright 2016, fxzeniken | //| http://fx.zeniken.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 5 #property indicator_plots 5 //--- plot F_Hi #property indicator_label1 "F_Hi" #property indicator_type1 DRAW_LINE #property indicator_color1 Red #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot F_Lo #property indicator_label2 "F_Lo" #property indicator_type2 DRAW_LINE #property indicator_color2 Red #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot S_Hi #property indicator_label3 "S_Hi" #property indicator_type3 DRAW_LINE #property indicator_color3 Blue #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- plot S_Lo #property indicator_label4 "S_Lo" #property indicator_type4 DRAW_LINE #property indicator_color4 Blue #property indicator_style4 STYLE_SOLID #property indicator_width4 1 //--- input parameters input int First_HL=10; input int Slow_HL=20; //--- indicator buffers double F_HiBuffer[]; double F_LoBuffer[]; double S_HiBuffer[]; double S_LoBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,F_HiBuffer); SetIndexBuffer(1,F_LoBuffer); SetIndexBuffer(2,S_HiBuffer); SetIndexBuffer(3,S_LoBuffer); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| 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[]) { //--- int limit; limit=Bars-IndicatorCounted(); for(int i=limit-1; i>=0; i--) { F_HiBuffer[i]=High[iHighest(NULL,0,MODE_HIGH, First_HL,i)]; F_LoBuffer[i]=Low[Lowest(NULL,0,MODE_LOW,First_HL,i)]; S_HiBuffer[i]=High[iHighest(NULL,0,MODE_HIGH,Slow_HL,i)]; S_LoBuffer[i]=Low[iLowest(NULL,0,MODE_LOW,Slow_HL,i)]; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ |
iHighestとiLowest
MQLで指定期間内の高値と安値を取得するには、次の2工程を踏まないといけません。
1.一番高値のあるバーの位置を探す
2.高値を取得する
高値のあるバーは、MQL関数のiHighest で位置を返すことができます。
逆にいうとiHighestは、高値を調べるのではなく、高値のあるバーの位置を調べます
iLowestはそのまま反対に、安値の位置を調べます
iHighest(),iLowestの引数
iHighest、iLowestの文は
iHighest(Symbol,timeframe,type,count,start)
という風に定義されています。
Symbolは、通貨ペアです。現在のチャートの通貨ペアに適応する場合NULLでOK
timeframeはどの時間足を使うかです。現在の時間軸を使う場合は0にします。
typeはどのデーターを使うかを指定します。 MODE_CLOSEで終値、MODE_HIGHで高値を使用します。
countは検索する範囲のバーの数を指定します バーは、現在から過去に向かって検索します
startは検索を開始するバーの位置を指定します。 今回はループの中なのでiとしています。
iHighest()、iLowestで期間内の高値安値がある位置がわかれば、
高値安値の配列 High[] 、Low[]でそれぞれの価格を取得します。
今回はこのように書いています
=High[iHighest(NULL,0,MODE_HIGH, First_HL,i)];
First_HLは変数で後で数値を変更できるようにしています。