質問編集履歴

2

描画はできるようになりましたが、描画しても、期間設定が機能してなくくて、チャートが崩れてしまいます。コードを張りなおしました。MQL4ファイルは、そのままです。

2025/03/24 23:09

投稿

kohan
kohan

スコア10

test CHANGED
File without changes
test CHANGED
@@ -12,69 +12,66 @@
12
12
  ```
13
13
 
14
14
  ### 該当のソースコード
15
-
16
- ```
17
- /+------------------------------------------------------------------+
18
- //| Snake_alfa.mq5 |
19
- //| Copyright 2022, MetaQuotes Ltd. |
20
- //| https://www.mql5.com |
21
15
  //+------------------------------------------------------------------+
16
+ //| snake_alfa.mq5 |
17
+ //+------------------------------------------------------------------+
18
+ //| snake_alfa.mq5 |
19
+ //| Copyright 2025, MetaQuotes Ltd. |
20
+ //| https://www.mql5.com |
21
+ //+------------------------------------------------------------------+
22
- #property copyright "Copyright 2022, MetaQuotes Ltd."
22
+ #property copyright "Copyright 2025, MetaQuotes Ltd."
23
23
  #property link "https://www.mql5.com"
24
- #property version "1.00"
24
+ #property version "1.01"
25
25
  #property strict
26
26
  #property indicator_chart_window
27
27
 
28
- #property indicator_buffers 1
28
+ #property indicator_buffers 2
29
+ #property indicator_plots 1
30
+ #property indicator_type1 DRAW_LINE
29
31
  #property indicator_color1 clrWhite
30
32
 
31
- //パラメーター
33
+ // パラメーター
32
34
  input int Snake_HalfCycle = 7; // Snake_HalfCycle
33
35
  input int InpBars = 1000; // Custom_Bars
34
36
 
35
- //バッファ
37
+ // バッファ
36
38
  double Snake_Buffer[];
39
+ double TempBuffer[];
37
40
 
38
- //変数
41
+ // 変数
39
42
  double Snake_Sum, Snake_Weight, Snake_Sum_Minus, Snake_Sum_Plus;
40
43
  int Snake_FullCycle;
41
44
  int cnt = 0, cnt_lim = 8;
42
-
43
45
 
44
46
  //+------------------------------------------------------------------+
45
47
  //| Custom indicator initialization function |
46
48
  //+------------------------------------------------------------------+
47
49
  int draw_begin;
48
50
 
51
+ int OnInit()
52
+ {
53
+ // バッファの設定
54
+ SetIndexBuffer(0, Snake_Buffer, INDICATOR_DATA);
55
+ SetIndexBuffer(1, TempBuffer, INDICATOR_CALCULATIONS);
49
56
 
50
- int OnInit()
57
+ // バッファ初期化
58
+ ArrayInitialize(Snake_Buffer, EMPTY_VALUE);
59
+ ArrayInitialize(TempBuffer, EMPTY_VALUE);
51
60
 
52
- {
53
-
54
- // バッファの設定
55
- SetIndexBuffer(0, Snake_Buffer, INDICATOR_DATA);
56
- //--- indicator buffers mapping
57
- // バッファ初期化
58
- ArrayInitialize(Snake_Buffer, EMPTY_VALUE); // 初期化
59
-
60
- // その他の設定
61
+ // その他の設定
61
62
  Snake_FullCycle = Snake_HalfCycle * 2 + 1;
62
- draw_begin = Snake_FullCycle + 1;
63
+ draw_begin = Snake_FullCycle;
63
64
 
64
65
  PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, draw_begin);
65
- SetIndexBuffer(0, Snake_Buffer, INDICATOR_DATA);
66
66
  PlotIndexSetInteger(0, PLOT_LINE_STYLE, STYLE_SOLID);
67
67
  PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 2);
68
68
 
69
- //---
70
69
  return(INIT_SUCCEEDED);
71
- }
70
+ }
72
71
 
73
72
  //+------------------------------------------------------------------+
74
73
  //| Custom indicator iteration function |
75
74
  //+------------------------------------------------------------------+
76
-
77
-
78
75
  int OnCalculate(const int rates_total,
79
76
  const int prev_calculated,
80
77
  const datetime &time[],
@@ -85,15 +82,11 @@
85
82
  const long &tick_volume[],
86
83
  const long &volume[],
87
84
  const int &spread[])
88
- {
85
+ {
89
86
  int FirstPos, ExtCountedBars = 0;
90
87
 
91
- if(rates_total <= 150) return(0);
88
+ if(rates_total <= Snake_HalfCycle + draw_begin) return(0);
92
89
 
93
- if(Snake_HalfCycle < 3)
94
- {
95
- //Snake_HalfCycle = 3;
96
- }
97
90
  ExtCountedBars = prev_calculated;
98
91
 
99
92
  if (ExtCountedBars < 0) return(-1);
@@ -101,117 +94,69 @@
101
94
  FirstPos = rates_total - ExtCountedBars - 1;
102
95
 
103
96
  if(FirstPos > rates_total - Snake_HalfCycle - 1)
104
- {
97
+ {
105
98
  FirstPos = rates_total - Snake_HalfCycle - 1;
106
- }
99
+ }
107
100
 
108
101
  // Snake計算関数を呼び出し
109
- Snake(FirstPos, close, high, low);
102
+ Snake(FirstPos, rates_total, close, high, low);
110
103
 
111
- // 表示削除処理の修正
104
+ // デバッグ用出力
105
+ Print("FirstPos: ", FirstPos, " rates_total: ", rates_total);
106
+
107
+ /*// バッファ初期化処理改善
112
- int i;
108
+ int buffer_clear_limit = MathMin(rates_total, InpBars);
113
- for(i = 0; i < rates_total - InpBars; i++)
109
+ for(int i = 0; i < rates_total - buffer_clear_limit; i++)
114
- {
110
+ {
115
- Snake_Buffer[i] = EMPTY_VALUE;
111
+ Snake_Buffer[i] = EMPTY_VALUE;*/
116
- }
112
+
117
113
 
118
114
  return(rates_total);
119
- }
115
+ }
120
116
 
121
117
  //+------------------------------------------------------------------+
122
118
  //| Snake 計算ロジック |
123
119
  //+------------------------------------------------------------------+
124
- void Snake(int Pos, const double &close_array[], const double &high_array[], const double &low_array[])
120
+ void Snake(int Pos, int rates_total, const double &close_array[], const double &high_array[], const double &low_array[])
125
- {
121
+ {
126
- if(Pos <= Snake_HalfCycle + 1)
122
+ if(Pos <= Snake_HalfCycle)
127
- {
123
+ {
128
- Pos = Snake_HalfCycle + 2;
124
+ Pos = Snake_HalfCycle + 1;
129
- }
125
+ }
130
126
 
131
- Snake_Buffer[Pos] = SnakeFirstCalc(Pos, close_array, high_array, low_array);
132
- Pos--;
133
-
134
- while(Pos >= Snake_HalfCycle)
127
+ for(int i = Pos; i >= 0; i--)
135
- {
128
+ {
136
- Snake_Buffer[Pos] = SnakeNextCalc(Pos, close_array, high_array, low_array);
129
+ Snake_Buffer[i] = SnakeFirstCalc(i, close_array, high_array, low_array);
137
- Pos--;
138
- }
130
+ }
139
- while(Pos > 0)
140
- {
141
- Snake_Buffer[Pos] = SnakeFirstCalc(Pos, close_array, high_array, low_array);
142
- Pos--;
143
- }
144
- if(Pos == 0)
145
- {
146
- // 最後の値は移動平均に設定
147
- Snake_Buffer[0] = iMA(Symbol(), Period(), Snake_HalfCycle, 0, MODE_LWMA, PRICE_TYPICAL);
148
- }
149
131
  }
150
132
 
151
133
  //+------------------------------------------------------------------+
152
134
  //| Snake価格取得 |
153
135
  //+------------------------------------------------------------------+
154
136
  double SnakePrice(int Shift, const double &close_array[], const double &high_array[], const double &low_array[])
155
- {
137
+ {
156
138
  return ((2 * close_array[Shift] + high_array[Shift] + low_array[Shift]) / 4);
157
- }
139
+ }
140
+
158
141
  //+------------------------------------------------------------------+
159
142
  //| Snakeの最初の計算 |
160
143
  //+------------------------------------------------------------------+
161
144
  double SnakeFirstCalc(int Shift, const double &close_array[], const double &high_array[], const double &low_array[])
162
- {
145
+ {
163
- int i, j, w;
146
+ double Sum = 0;
147
+ double Weight = 0;
164
- if(Shift < Snake_HalfCycle)
148
+ for(int i = -Snake_HalfCycle; i <= Snake_HalfCycle; i++)
165
- {
149
+ {
166
- i = 0;
167
- w = Shift + Snake_HalfCycle;
150
+ int index = Shift + i;
168
- リスト while(w >= Shift)
151
+ if(index >= 0)
169
- {
152
+ {
170
- i++;
171
- Snake_Sum += i * SnakePrice(w, close_array, high_array, low_array);
153
+ Sum += (Snake_HalfCycle - MathAbs(i) + 1) * SnakePrice(index, close_array, high_array, low_array);
172
- Snake_Weight += i;
154
+ Weight += (Snake_HalfCycle - MathAbs(i) + 1);
173
- w--;
174
- }
155
+ }
175
- while(w >= 0)
176
- {
177
- i--;
178
- Snake_Sum += i * SnakePrice(w, close_array, high_array, low_array);
179
- Snake_Weight += i;
180
- w--;
181
- }
156
+ }
157
+ return Sum / Weight;
182
- }
158
+ }
183
- else
184
- {
185
- for(j = Shift - Snake_HalfCycle, i = Shift + Snake_HalfCycle, w = 1; w <= Snake_HalfCycle; j++, i--, w++)
186
- {
187
- Snake_Sum += w * (SnakePrice(i, close_array, high_array, low_array) + SnakePrice(j, close_array, high_array, low_array));
188
- Snake_Weight += 2 * w;
189
- Snake_Sum_Minus += SnakePrice(i, close_array, high_array, low_array);
190
- Snake_Sum_Plus += SnakePrice(j, close_array, high_array, low_array);
191
- }
192
- Snake_Sum += (Snake_HalfCycle + 1) * SnakePrice(Shift, close_array, high_array, low_array);
193
- Snake_Weight += Snake_HalfCycle + 1;
194
- Snake_Sum_Minus += SnakePrice(Shift, close_array, high_array, low_array);
195
- }
196
- return Snake_Sum / Snake_Weight;
197
- }
198
159
 
199
- //+------------------------------------------------------------------+
200
- //| Snakeの次の計算 |
201
- //+------------------------------------------------------------------+
202
- double SnakeNextCalc(int Shift, const double &close_array[], const double &high_array[], const double &low_array[])
203
- {
204
- Snake_Sum_Plus += SnakePrice(Shift - Snake_HalfCycle, close_array, high_array, low_array);
205
- Snake_Sum = Snake_Sum - Snake_Sum_Minus + Snake_Sum_Plus;
206
- Snake_Sum_Minus -= SnakePrice(Shift + Snake_HalfCycle + 1, close_array, high_array, low_array) - SnakePrice(Shift, close_array, high_array, low_array);
207
- Snake_Sum_Plus -= SnakePrice(Shift, close_array, high_array, low_array);
208
- return Snake_Sum / Snake_Weight; //修正、Weightで割る。
209
- }
210
- ### 試したこと・調べたこと
211
- - [x] teratailやGoogle等で検索した
212
- - [x] ソースコードを自分なりに変更した
213
- - [ ] 知人に聞いた
214
- - [x] その他
215
160
 
216
161
  ##### 上記の詳細・結果
217
162
  全く解決ができないです、、

1

MQL4ファイルをドロップボックスに置きました。文字数制限を超えるためです。

2025/03/24 08:03

投稿

kohan
kohan

スコア10

test CHANGED
File without changes
test CHANGED
@@ -141,7 +141,6 @@
141
141
  Snake_Buffer[Pos] = SnakeFirstCalc(Pos, close_array, high_array, low_array);
142
142
  Pos--;
143
143
  }
144
-
145
144
  if(Pos == 0)
146
145
  {
147
146
  // 最後の値は移動平均に設定
@@ -156,20 +155,17 @@
156
155
  {
157
156
  return ((2 * close_array[Shift] + high_array[Shift] + low_array[Shift]) / 4);
158
157
  }
159
-
160
158
  //+------------------------------------------------------------------+
161
159
  //| Snakeの最初の計算 |
162
160
  //+------------------------------------------------------------------+
163
161
  double SnakeFirstCalc(int Shift, const double &close_array[], const double &high_array[], const double &low_array[])
164
162
  {
165
163
  int i, j, w;
166
-
167
164
  if(Shift < Snake_HalfCycle)
168
165
  {
169
166
  i = 0;
170
167
  w = Shift + Snake_HalfCycle;
171
-
172
- while(w >= Shift)
168
+ リスト while(w >= Shift)
173
169
  {
174
170
  i++;
175
171
  Snake_Sum += i * SnakePrice(w, close_array, high_array, low_array);
@@ -211,8 +207,6 @@
211
207
  Snake_Sum_Plus -= SnakePrice(Shift, close_array, high_array, low_array);
212
208
  return Snake_Sum / Snake_Weight; //修正、Weightで割る。
213
209
  }
214
- ```
215
-
216
210
  ### 試したこと・調べたこと
217
211
  - [x] teratailやGoogle等で検索した
218
212
  - [x] ソースコードを自分なりに変更した
@@ -224,3 +218,6 @@
224
218
 
225
219
  ### 補足
226
220
  特になし
221
+
222
+ 追記 MQL4ファイルをドロップボックスに置きました。文字数制限を超えてしまうためです。
223
+ [リンク内容](https://www.dropbox.com/scl/fi/lcmyhi756g6ggr8ofzea4/Snake_alfa.mq4?rlkey=21ff83glp1v8641wviuqs7bl3&st=179amjd2&dl=0)