MQL4初心者
※MQLのサイトで有名な自作しちゃおーで復習する日記
鬼畜班の課題をやっているけど、自分のMQLの基礎が出来ていないから
初心に戻って復習していきます。
処理の順番から。
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
int OnInit() {
return(INIT_SUCCEEDED);
}
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 template_value; // int型の変数を宣言
template_value = 0; // 変数を初期化
Print( template_value ); // 変数の値をログ出力
template_value++; // 変数に1を加算(インクリメント)
Print( template_value ); // 変数の値をログ出力
template_value++; // 変数に1を加算(インクリメント)
Print( template_value ); // 変数の値をログ出力
template_value++; // 変数に1を加算(インクリメント)
Print( template_value ); // 変数の値をログ出力
template_value++; // 変数に1を加算(インクリメント)
Print( template_value ); // 変数の値をログ出力
template_value++; // 変数に1を加算(インクリメント)
Print( template_value ); // 変数の値をログ出力
//--- return value of prev_calculated for next call
return(rates_total);
}

ここでちょっと横道にずれて、僕が最近まで知らなかった単純な事だけど
int template_value; // int型の変数を宣言
template_value = 0; // 変数を初期化
この部分のコードを書く位置で全然内容が変わるんですよね。
知っている人は、やさしい目で見てください(笑)
上記までは、0から5まで足し算してあげる簡単なコードなんですが
では、ローソク足が動くたびにずっと足し算されるコードを書くには、どうしたらいいと思いますか??
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
int template_value = 0; // int型の変数を宣言
int OnInit() {
return(INIT_SUCCEEDED);
}
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[]) {
//---
Print( template_value ); // 変数の値をログ出力
template_value++; // 変数に1を加算(インクリメント)
return(rates_total);
}
じゃん!!
グローバル変数にtemplate_valueという変数を宣言してあげたら0に初期化されないからずっと足し算されるんです!
上から下にコード流れるんでOnCalculateに変数を宣言していたら毎回0に初期化されるから今までは、足し算が5で止まっていたんです!!

では次に、奇数は、printさせないってコード書いてみましょう。
0から9で2,4,6,8だったらコンテニューさせるコード
continueを使った場合↓
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
//int template_value = 0; // int型の変数を宣言
int OnInit() {
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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[]) {
//---
for(int a = 0; a < 10; a++) {
if(a==2||a==4||a==6||a==8)continue;
Print( a ); // 変数の値をログ出力
}
// Print( template_value ); // 変数の値をログ出力
// template_value++; // 変数に1を加算(インクリメント)
return(rates_total);
}

breakを使った場合↓
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
//int template_value = 0; // int型の変数を宣言
int OnInit() {
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
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[]) {
//---
for(int a = 0; a < 10; a++) {
if(a==6)break;
Print( a ); // 変数の値をログ出力
}
// Print( template_value ); // 変数の値をログ出力
// template_value++; // 変数に1を加算(インクリメント)
return(rates_total);
}

aが6だったらブレイク。要するにそれ以降計算させない処理(ループを抜ける)になりました。
breakとcontinueは、結構使うんで覚えてたが良いです!!
久々こういう初心に戻って復習するの良いですね!!
ちょっとこの作業も続けていきます!!自作しちゃおーのサイト
また明日(@^^)/~~~
2023/02/13