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

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

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

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

Q&A

解決済

1回答

4364閲覧

mql4でiCustom関数を使ってインジケーターの値を取得できない

hiroshi77

総合スコア12

MQL4

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

0グッド

0クリップ

投稿2020/07/23 01:00

編集2020/07/24 00:27

現在、iCustom関数を使用して既存のRCIインジケータのバッファーから値の抽出を試みています。
1番目のバッファーについては、上手く出来ているのですが、2番目のバッファーについて、上手くいきません。どなたかご助言いただければ、助かります。

インデックスの概要 

mql4

1  2 // インデックス名 fxnav_RCI 3 4 IndicatorBuffers(6); 5 6 SetIndexBuffer(0, rci); 7 SetIndexBuffer(1, signal); ← このバッファーの値を取得したい 8 SetIndexBuffer(2, up); 9 SetIndexBuffer(3, down); 10 SetIndexBuffer(4, rci_up); 11 SetIndexBuffer(5, rci_down); 12 13 以下省略 14 15

自分のスクリプト全文

mql4

1//+------------------------------------------------------------------+ 2//| custom_test.mq4 | 3//| Copyright 2020, MetaQuotes Software Corp. | 4//| https://www.mql5.com | 5//+------------------------------------------------------------------+ 6#property copyright "Copyright 2020, MetaQuotes Software Corp." 7#property link "https://www.mql5.com" 8#property version "1.00" 9#property strict 10//+------------------------------------------------------------------+ 11//| Script program start function | 12//+------------------------------------------------------------------+ 13 14input int RCI_period = 21; 15 16input int singal_period = 5; 17 18//input int mode = 0; 19 20void OnStart() 21 { 22 double curRCI_cus =iCustom(NULL,0,"Bands",RCI_period,0,0); 23 24 double curSIG_cus =iCustom(NULL,0,"Bands",singal_period,1,0); 25 26 Alert("RCI_custom: ",curRCI_cus); 27 Alert("preRCI_custom: ",preRCI_cus); 28 Alert("curSIG_cus: ",curSIG_cus); 29 Alert("preSIG_cus: ",preSIG_cus); 30 31 }

インジケータのコード全文

https://drive.google.com/file/d/1UnOwQ5KpsKRpHlXySI9BbcsE8FOmEf-4/view?usp=sharing

回答欄にて頂いたコードの結果と比較

イメージ説明

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

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

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

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

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

guest

回答1

0

ベストアンサー

呼び出しに関しては特に問題があるようには見えません。
前後のコードに何か問題があるのではないでしょうか?

参考までにfxnav_RCIを呼び出すだけのインジケータのサンプルです。
1本目のラインがrci_period1のRCIで2本目のラインがrci_period2のSignalのラインです。

MQL4

1#property strict 2#property indicator_separate_window 3#property indicator_buffers 2 4#property indicator_plots 2 5//--- plot RCI 6#property indicator_label1 "RCI" 7#property indicator_type1 DRAW_LINE 8#property indicator_color1 clrMediumBlue 9#property indicator_style1 STYLE_SOLID 10#property indicator_width1 1 11//--- plot Signal 12#property indicator_label2 "Signal" 13#property indicator_type2 DRAW_LINE 14#property indicator_color2 clrRed 15#property indicator_style2 STYLE_SOLID 16#property indicator_width2 1 17//--- 18input int rci_period1 = 5; 19input int rci_period2 = 22; 20//--- indicator buffers 21double RCIBuffer[]; 22double SignalBuffer[]; 23//--- 24int max_period = 0; 25//+------------------------------------------------------------------+ 26//| Custom indicator initialization function | 27//+------------------------------------------------------------------+ 28int OnInit() 29{ 30 max_period = MathMax(rci_period1, rci_period2); 31 32 SetIndexBuffer(0, RCIBuffer); 33 SetIndexBuffer(1, SignalBuffer); 34 35 SetIndexDrawBegin(0, max_period); 36 SetIndexDrawBegin(1, max_period); 37 38 IndicatorShortName("fxnav_RCI_test (" + IntegerToString(rci_period1) + "," + IntegerToString(rci_period2) + ")"); 39 40 SetIndexLabel(0, "RCI (" + IntegerToString(rci_period1) + ")"); 41 SetIndexLabel(1, "Signal (" + IntegerToString(rci_period2) + ")"); 42 43 return INIT_SUCCEEDED; 44} 45//+------------------------------------------------------------------+ 46//| Custom indicator iteration function | 47//+------------------------------------------------------------------+ 48int OnCalculate(const int rates_total, 49 const int prev_calculated, 50 const datetime &time[], 51 const double &open[], 52 const double &high[], 53 const double &low[], 54 const double &close[], 55 const long &tick_volume[], 56 const long &volume[], 57 const int &spread[]) 58{ 59 if (rates_total < max_period) { 60 return 0; 61 } 62 63 if (prev_calculated <= 0) { 64 ArrayFill(RCIBuffer, 0, rates_total, EMPTY_VALUE); 65 ArrayFill(SignalBuffer, 0, rates_total, EMPTY_VALUE); 66 } 67 68 int limit = MathMin(rates_total - max_period - 1, rates_total - prev_calculated); 69 70 for (int index = limit; index >= 0; index--) { 71 RCIBuffer[index] = iCustom(NULL, 0, "fxnav_RCI", rci_period1, 0, index); 72 SignalBuffer[index] = iCustom(NULL, 0, "fxnav_RCI", rci_period2, 1, index); 73 } 74 75 return rates_total; 76}

同じ値になっているのが確認できると思います。
イメージ説明

投稿2020/07/23 15:17

mah

総合スコア591

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

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

hiroshi77

2020/07/24 00:28

ご丁寧にご回答ありがとうございます。不思議なのですが、自分のもっているfxnav_RCIのインジケータを使った際と、上記コードを貼り付けて新たに作ったインジケータの値が異なります。期間設定は、RCIを5とシグナルを21で設定しているのですが、理由がわかりません。結果と、自分の持っているインジケータのコードリンクを追記しますので、改めてご助言いただけないでしょうか。 indicator window1 が自分の持っているインジケーター(fxnav_RCI)で、Value2がシグナルラインの値になります。 下のindicator window2 が今回コードを頂いたインジケータになります。
mah

2020/07/24 06:07 編集

質問の画像のindicator window2のSignalにindicator window1のValue 2が表示されてほしいのでしょうか? その場合、スクリプトの記述は下記のようにする必要があります。 double curSIG_cus =iCustom(NULL,0,"fxnav_RCI",RCI_period,singal_period,1,0); 下記の記述だと、fxnav_RCIの最初のパラメータのみ指定することになります。 つまり、fxnav_RCIのrci_periodパラメータにsingal_period(5)を指定しています。 double curSIG_cus =iCustom(NULL,0,"fxnav_RCI",singal_period,1,0);
hiroshi77

2020/07/24 12:51

お~凄いです!上記のとおりにしたらインジケータ通りの結果になりました。 どうもありがとうございます! signalラインのバッファーは2番目のバッファなので、iCustomのmodeを1にすれば良いと考えていたのですが、なぜそれだけでは1番目のバッファーのパラメータを指定することになるのでしょうか?
mah

2020/07/24 13:13

元の書き方だと iCustom(NULL,0,"fxnav_RCI",5,1,0); と書いたのと同じになります。 指定したパラメータは対象のインジケータの宣言順に渡されるので、 これだと、fxnav_RCIのパラメータを rci_period = 5 signal_period = 5 に設定したのと同じになってしまいます。 詳細はリファレンスを参照してください。 https://yukifx.web.fc2.com/sub/reference/22_technical_ind/cone/tec_custom.html https://docs.mql4.com/indicators/icustom
hiroshi77

2020/07/25 00:45

ありがとうございます。ようやく腑に落ちました。 バッファーが複数あり、そのパラメータが分かれている場合は、modeの引数でそのバッファーが何番目に宣言されるているかを指定するだけではなく、その前のパラメーターの引数をそのバッファー分(1番目のバッファーのパラメータ、2番目のバッファーのパラメータ)を宣言順に記載しなければならなかったのですね。 何度もご質問してしまいましてすみませんでした。とても助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問