teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

シンタックスハイライト対応言語の変更に対応、回答の脱字修正。

2024/04/30 11:54

投稿

mah
mah

スコア598

answer CHANGED
@@ -1,190 +1,190 @@
1
- ObjectCreate関数とマウスイベントを駆使して**ダイアログのように見えるもの**を描画しているだけです。
2
- それだと作るのが大変なので、標準ライブラリにダイアログ用のクラスがあります。
3
- MT4をインストールしたディレクトリのMQL4\Include\Controlsにソースがあります。
4
-
5
- MT4ではなく[MT5のリファレンス](https://www.mql5.com/ja/docs/standardlibrary/controls)を見るのが早いと思います。
6
-
7
- インジケータの動作中に移動平均の期間を変更するサンプル
8
-
9
- ```MQL4
10
- #property strict
11
- #property indicator_chart_window
12
-
13
- #property indicator_buffers 3
14
- #property indicator_plots 3
15
-
16
- #property indicator_color1 clrDodgerBlue
17
- #property indicator_color2 clrGreen
18
- #property indicator_color3 clrCrimson
19
-
20
- #property indicator_width1 1
21
- #property indicator_width2 2
22
- #property indicator_width3 3
23
-
24
- #include <Controls\Dialog.mqh>
25
- #include <Controls\Button.mqh>
26
- #include <Controls\Edit.mqh>
27
- #include <MovingAverages.mqh>
28
-
29
- input int InpPeriod1 = 62;
30
- input int InpPeriod2 = 200;
31
- input int InpPeriod3 = 800;
32
-
33
- double ExtMa1[];
34
- double ExtMa2[];
35
- double ExtMa3[];
36
-
37
- int Period1;
38
- int Period2;
39
- int Period3;
40
-
41
- class CTestDialog : public CAppDialog
42
- {
43
- CEdit m_edit1;
44
- CEdit m_edit2;
45
- CEdit m_edit3;
46
- CButton m_button;
47
-
48
- public:
49
- virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1,const int x2,const int y2);
50
- virtual bool OnEvent(const int id, const long &lparam, const double &dparam,const string &sparam);
51
- void OnClickButton(void);
52
- };
53
-
54
- EVENT_MAP_BEGIN(CTestDialog)
55
- ON_EVENT(ON_CLICK, m_button, OnClickButton)
56
- EVENT_MAP_END(CAppDialog)
57
-
58
- bool CTestDialog::Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2)
59
- {
60
- CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2);
61
-
62
- int x = 5;
63
- int y = 5;
64
- int w = 60;
65
- int h = 20;
66
- int margin = 5;
67
-
68
- m_edit1.Create(m_chart_id, m_name + "Edit1", m_subwin, x, y, x + w, y + h);
69
- m_edit1.Text(IntegerToString(Period1));
70
- Add(m_edit1);
71
-
72
- y += h + margin;
73
-
74
- m_edit2.Create(m_chart_id, m_name + "Edit2", m_subwin, x, y, x + w, y + h);
75
- m_edit2.Text(IntegerToString(Period2));
76
- Add(m_edit2);
77
-
78
- y += h + margin;
79
-
80
- m_edit3.Create(m_chart_id, m_name + "Edit3", m_subwin, x, y, x + w, y + h);
81
- m_edit3.Text(IntegerToString(Period3));
82
- Add(m_edit3);
83
-
84
- y += h + margin;
85
-
86
- m_button.Create(m_chart_id, m_name + "Button", m_subwin, x, y, x + w, y + h);
87
- m_button.Text("Update");
88
- Add(m_button);
89
-
90
- return true;
91
- }
92
-
93
- void CTestDialog::OnClickButton(void)
94
- {
95
- int period1 = StrToInteger(m_edit1.Text());
96
- int period2 = StrToInteger(m_edit2.Text());
97
- int period3 = StrToInteger(m_edit3.Text());
98
-
99
- if (period1 <= 0 || period2 <= 0 || period3 <= 3)
100
- {
101
- return;
102
- }
103
-
104
- Period1 = period1;
105
- Period2 = period2;
106
- Period3 = period3;
107
-
108
- PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period1 - 1);
109
- PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, Period2 - 1);
110
- PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, Period3 - 1);
111
-
112
- ChartSetSymbolPeriod(0, NULL, PERIOD_CURRENT);
113
- }
114
-
115
- CTestDialog ExtDialog;
116
-
117
- int OnInit()
118
- {
119
- Period1 = InpPeriod1;
120
- Period2 = InpPeriod2;
121
- Period3 = InpPeriod3;
122
-
123
- SetIndexBuffer(0, ExtMa1);
124
- SetIndexBuffer(1, ExtMa2);
125
- SetIndexBuffer(2, ExtMa3);
126
-
127
- PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period1 - 1);
128
- PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, Period2 - 1);
129
- PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, Period3 - 1);
130
-
131
- if(!ExtDialog.Create(0, "MA3", 0, 20, 20, 100, 160))
132
- {
133
- return INIT_FAILED;
134
- }
135
-
136
- ExtDialog.Run();
137
-
138
- return INIT_SUCCEEDED;
139
- }
140
-
141
- void OnDeinit(const int reason)
142
- {
143
- ExtDialog.Destroy(reason);
144
- }
145
-
146
- int OnCalculate(const int rates_total,
147
- const int prev_calculated,
148
- const datetime &time[],
149
- const double &open[],
150
- const double &high[],
151
- const double &low[],
152
- const double &close[],
153
- const long &tick_volume[],
154
- const long &volume[],
155
- const int &spread[])
156
- {
157
- ArraySetAsSeries(ExtMa1, false);
158
- ArraySetAsSeries(ExtMa2, false);
159
- ArraySetAsSeries(ExtMa3, false);
160
- ArraySetAsSeries(close, false);
161
-
162
- int minPeriod = MathMin(Period1, MathMin(Period2, Period3));
163
- int first = MathMax(minPeriod - 1, prev_calculated - 1);
164
-
165
- for (int i = first; i < rates_total; i++)
166
- {
167
- if (i == (Period1 - 1))
168
- ExtMa1[i] = SimpleMA(i, Period1, close);
169
- else if (i >= (Period1 - 1))
170
- ExtMa1[i] = ExponentialMA(i, Period1, ExtMa1[i - 1], close);
171
-
172
- if (i >= (Period2 - 1))
173
- ExtMa2[i] = SimpleMA(i, Period2, close);
174
-
175
- if (i >= (Period3 - 1))
176
- ExtMa3[i] = SimpleMA(i, Period3, close);
177
- }
178
-
179
- return rates_total;
180
- }
181
-
182
- void OnChartEvent(const int id,
183
- const long &lparam,
184
- const double &dparam,
185
- const string &sparam)
186
- {
187
- ExtDialog.ChartEvent(id,lparam,dparam,sparam);
188
- }
189
-
1
+ MT4のダイアログボックスは、ObjectCreate関数とマウスイベントを駆使して**ダイアログのように見えるもの**を描画しているだけです。
2
+ それだと作るのが大変なので、標準ライブラリにダイアログ用のクラスがあります。
3
+ MT4をインストールしたディレクトリのMQL4\Include\Controlsにソースがあります。
4
+
5
+ MT4ではなく[MT5のリファレンス](https://www.mql5.com/ja/docs/standardlibrary/controls)を見るのが早いと思います。
6
+
7
+ インジケータの動作中に移動平均の期間を変更するサンプル
8
+
9
+ ```cpp
10
+ #property strict
11
+ #property indicator_chart_window
12
+
13
+ #property indicator_buffers 3
14
+ #property indicator_plots 3
15
+
16
+ #property indicator_color1 clrDodgerBlue
17
+ #property indicator_color2 clrGreen
18
+ #property indicator_color3 clrCrimson
19
+
20
+ #property indicator_width1 1
21
+ #property indicator_width2 2
22
+ #property indicator_width3 3
23
+
24
+ #include <Controls\Dialog.mqh>
25
+ #include <Controls\Button.mqh>
26
+ #include <Controls\Edit.mqh>
27
+ #include <MovingAverages.mqh>
28
+
29
+ input int InpPeriod1 = 62;
30
+ input int InpPeriod2 = 200;
31
+ input int InpPeriod3 = 800;
32
+
33
+ double ExtMa1[];
34
+ double ExtMa2[];
35
+ double ExtMa3[];
36
+
37
+ int Period1;
38
+ int Period2;
39
+ int Period3;
40
+
41
+ class CTestDialog : public CAppDialog
42
+ {
43
+ CEdit m_edit1;
44
+ CEdit m_edit2;
45
+ CEdit m_edit3;
46
+ CButton m_button;
47
+
48
+ public:
49
+ virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1,const int x2,const int y2);
50
+ virtual bool OnEvent(const int id, const long &lparam, const double &dparam,const string &sparam);
51
+ void OnClickButton(void);
52
+ };
53
+
54
+ EVENT_MAP_BEGIN(CTestDialog)
55
+ ON_EVENT(ON_CLICK, m_button, OnClickButton)
56
+ EVENT_MAP_END(CAppDialog)
57
+
58
+ bool CTestDialog::Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2)
59
+ {
60
+ CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2);
61
+
62
+ int x = 5;
63
+ int y = 5;
64
+ int w = 60;
65
+ int h = 20;
66
+ int margin = 5;
67
+
68
+ m_edit1.Create(m_chart_id, m_name + "Edit1", m_subwin, x, y, x + w, y + h);
69
+ m_edit1.Text(IntegerToString(Period1));
70
+ Add(m_edit1);
71
+
72
+ y += h + margin;
73
+
74
+ m_edit2.Create(m_chart_id, m_name + "Edit2", m_subwin, x, y, x + w, y + h);
75
+ m_edit2.Text(IntegerToString(Period2));
76
+ Add(m_edit2);
77
+
78
+ y += h + margin;
79
+
80
+ m_edit3.Create(m_chart_id, m_name + "Edit3", m_subwin, x, y, x + w, y + h);
81
+ m_edit3.Text(IntegerToString(Period3));
82
+ Add(m_edit3);
83
+
84
+ y += h + margin;
85
+
86
+ m_button.Create(m_chart_id, m_name + "Button", m_subwin, x, y, x + w, y + h);
87
+ m_button.Text("Update");
88
+ Add(m_button);
89
+
90
+ return true;
91
+ }
92
+
93
+ void CTestDialog::OnClickButton(void)
94
+ {
95
+ int period1 = StrToInteger(m_edit1.Text());
96
+ int period2 = StrToInteger(m_edit2.Text());
97
+ int period3 = StrToInteger(m_edit3.Text());
98
+
99
+ if (period1 <= 0 || period2 <= 0 || period3 <= 3)
100
+ {
101
+ return;
102
+ }
103
+
104
+ Period1 = period1;
105
+ Period2 = period2;
106
+ Period3 = period3;
107
+
108
+ PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period1 - 1);
109
+ PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, Period2 - 1);
110
+ PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, Period3 - 1);
111
+
112
+ ChartSetSymbolPeriod(0, NULL, PERIOD_CURRENT);
113
+ }
114
+
115
+ CTestDialog ExtDialog;
116
+
117
+ int OnInit()
118
+ {
119
+ Period1 = InpPeriod1;
120
+ Period2 = InpPeriod2;
121
+ Period3 = InpPeriod3;
122
+
123
+ SetIndexBuffer(0, ExtMa1);
124
+ SetIndexBuffer(1, ExtMa2);
125
+ SetIndexBuffer(2, ExtMa3);
126
+
127
+ PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period1 - 1);
128
+ PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, Period2 - 1);
129
+ PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, Period3 - 1);
130
+
131
+ if(!ExtDialog.Create(0, "MA3", 0, 20, 20, 100, 160))
132
+ {
133
+ return INIT_FAILED;
134
+ }
135
+
136
+ ExtDialog.Run();
137
+
138
+ return INIT_SUCCEEDED;
139
+ }
140
+
141
+ void OnDeinit(const int reason)
142
+ {
143
+ ExtDialog.Destroy(reason);
144
+ }
145
+
146
+ int OnCalculate(const int rates_total,
147
+ const int prev_calculated,
148
+ const datetime &time[],
149
+ const double &open[],
150
+ const double &high[],
151
+ const double &low[],
152
+ const double &close[],
153
+ const long &tick_volume[],
154
+ const long &volume[],
155
+ const int &spread[])
156
+ {
157
+ ArraySetAsSeries(ExtMa1, false);
158
+ ArraySetAsSeries(ExtMa2, false);
159
+ ArraySetAsSeries(ExtMa3, false);
160
+ ArraySetAsSeries(close, false);
161
+
162
+ int minPeriod = MathMin(Period1, MathMin(Period2, Period3));
163
+ int first = MathMax(minPeriod - 1, prev_calculated - 1);
164
+
165
+ for (int i = first; i < rates_total; i++)
166
+ {
167
+ if (i == (Period1 - 1))
168
+ ExtMa1[i] = SimpleMA(i, Period1, close);
169
+ else if (i >= (Period1 - 1))
170
+ ExtMa1[i] = ExponentialMA(i, Period1, ExtMa1[i - 1], close);
171
+
172
+ if (i >= (Period2 - 1))
173
+ ExtMa2[i] = SimpleMA(i, Period2, close);
174
+
175
+ if (i >= (Period3 - 1))
176
+ ExtMa3[i] = SimpleMA(i, Period3, close);
177
+ }
178
+
179
+ return rates_total;
180
+ }
181
+
182
+ void OnChartEvent(const int id,
183
+ const long &lparam,
184
+ const double &dparam,
185
+ const string &sparam)
186
+ {
187
+ ExtDialog.ChartEvent(id,lparam,dparam,sparam);
188
+ }
189
+
190
190
  ```

1

期間の取得処理修正

2021/09/05 00:57

投稿

mah
mah

スコア598

answer CHANGED
@@ -159,7 +159,8 @@
159
159
  ArraySetAsSeries(ExtMa3, false);
160
160
  ArraySetAsSeries(close, false);
161
161
 
162
+ int minPeriod = MathMin(Period1, MathMin(Period2, Period3));
162
- int first = MathMax(Period1 - 1, prev_calculated - 1);
163
+ int first = MathMax(minPeriod - 1, prev_calculated - 1);
163
164
 
164
165
  for (int i = first; i < rates_total; i++)
165
166
  {