こんばんは、たかはしです。
手元にあるお気に入りのインジケーターをフィルターとして使いたい場合や、不要な情報を消して使いたい場合の使用方法をシェアします。
MQLコードを少しいじる必要がありますが、下記にベースとなるのコードをおいておきますので、コピペして流用してください。
コードの下にも本文は続きます。
//+------------------------------------------------------------------+ //| MAAngleFilter.mq4 | //| Copyright 2017, FX取引に関するあれこれ. | //| https://fxtrading.greeds.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, FX取引に関するあれこれ." #property link "https://fxtrading.greeds.net" #property version "1.00" #property strict //#property indicator_chart_window #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 LimeGreen double Buffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexStyle(0,DRAW_LINE,EMPTY,2); SetIndexBuffer(0,Buffer); //--- 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 i, limit; int counted_bars = IndicatorCounted(); if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; limit = Bars - counted_bars; for(i=0;i<limit;i++) { // double hoge1 = iCustom(NULL,0,"MAAngle",0,i); double hoge1 = iCustom(NULL,0,"MAAngle",25,0.1,0,i); Buffer[i]=hoge1; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
iCustomと言う関数は可変パラメータなので、”MAAngle”で指定しているインジケーター名に与えるパラメータを指定して使うこともできます。
51行目はコメントアウトしていますが、パラメータを省略するとデフォルト値が採用されます。
52行目はパラメータを指定して使っています。
hoge1と言う変数にiCustomの結果が返ってきますので、その値を持ってご自由に制御してください。
例えば、iCustomから得た値で0以上は買い矢印、0以下は売り矢印を出したい場合のコードはこうなります。
//+------------------------------------------------------------------+ //| MAAngleFilter.mq4 | //| Copyright 2017, FX取引に関するあれこれ. | //| https://fxtrading.greeds.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, FX取引に関するあれこれ." #property link "https://fxtrading.greeds.net" #property version "1.00" #property strict //#property indicator_chart_window #property indicator_separate_window #property indicator_buffers 3 #property indicator_color1 LimeGreen #property indicator_color2 Blue #property indicator_color3 Red double Buffer[]; double SignalUp[]; double SignalDn[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexStyle(0,DRAW_LINE,EMPTY,2); SetIndexBuffer(0,Buffer); SetIndexStyle(1,DRAW_ARROW,EMPTY,2); SetIndexArrow(1,236); SetIndexBuffer(1,SignalUp); SetIndexStyle(2,DRAW_ARROW,EMPTY,2); SetIndexArrow(2,238); SetIndexBuffer(2,SignalDn); //--- 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 i, limit; int counted_bars = IndicatorCounted(); if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; limit = Bars - counted_bars; for(i=0;i<limit;i++) { // double hoge1 = iCustom(NULL,0,"MAAngle",0,i); double hoge1 = iCustom(NULL,0,"MAAngle",25,0.1,0,i); Buffer[i]=hoge1; if(hoge1>0) { SignalUp[i]=hoge1; } else if(hoge1<0) { SignalDn[i]=hoge1; } } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
一応動きますが、あくまでもサンプルなので、実際に使用する場合はもう少しロジックを考慮する必要がありますが、こういう感じで手元にあるインジケーターの情報を利用する事ができます。
手元にあるコードは完成された物として基本的には手を加えずに、出力される情報だけを使うようにすることで、本来の動きが保証されるのです。
自分がいじったせいで挙動がおかしく鳴る可能性のあるインジケーターは信用できませんので(・´з`・)
ある程度MQLコードをいじれる人向けの記事ですが、これが理解出来ると色々と試したいことが出来ると思いますので、興味のある人はこれを機会に覚えてみてください!
Icustomでインジケーターをcallするときはチャートにインジケーターをださなくても大丈夫ですか?あとインジケーターの矢印とバックテストの矢印位置があわないのはなぜでしょうか?!
iCustomでコールするインジは、指定ディレクトリに存在すればokなので、画面に表示させる必要はありません。
バックテストのズレなんですが、これについては僕も以前から気になっていました。
mt4 自体のバグかティック単位の誤差で仕方ないのかなと解釈しています。
どちらを正にするかで考えると、リアル描画の方を正とした方が良いでしょう。
以上です!