このページでは、制御文となる「if」と「for」について解説します。
制御文とは
制御文とは、プログラムの実行を制御するもので、大きく分けて、選択文(if)、繰り返し分(for)、ジャンプ文(return)の3種類があります。
このうち、「if」と「for」について解説します。
バックテストを取るうえで欠かせないものになりますし、「if」と「for」以外の他の制御文については、この2つ以上に使う機会が無いと感じていますので、この2つに重点を置いてしっかり理解していきましょう。
if文とは
if文は、条件分岐文とも言いますが、「もし○○がtrueだったら××、falseだったら△△」というようにコードの実行を条件によって分岐することが出来ます。
さらに具体例を挙げると「RSI値が「70」以上だったら、アロー(矢印)サインを出す、RSI値が「70」未満だったら、何もしない」といった内容です。
条件式を書くと以下の通りです。
ifから始まる部分が制御文となります。ifのカッコ内()に条件式を書くことで、条件を満たしている場合の次の動作を波かっこ{}内に書くことが出来ます。

アローサインを出すコードは省略しています。
そして条件を満たさなかった場合、elseの波かっこ{}内の処理を実行しようとします。
「何もしない」といった場合は、else{}自体を書かずに省略することも可能です。
あえて、「double rsi_value = 70;」と書きましたが、本来はiRSI関数などを使って現在足のRSI値がいくつなのかを確認して、その値が70以上だった時というような式を書きます。
関数については別で説明しますので、ここでは直接「70」という数値を指定しています。
ちなみに、「もしRSI値が「70」以上だったら、下向きのアロー(矢印)サインを出し、もしRSI値が「30」以下だったら、上向きのアロー(矢印)サインを出す」といった書き方もできます。
else の後ろにif()を追加することで、このような条件を付けることもできます。
for文とは
for文は、ループ処理とも呼ばれ、条件が成立している間処理を繰り返すといった場合に使用します。
チャート上に表示されているローソク足の本数を確認し、その本数分の各足の位置にあるRSI値を取得するといった場合、1本1本コードを書いていったら終わりません。そんな時にfor文が活躍します。
for文の式は以下の通りです。
for(初期化式; 条件判定; 更新式) { コードの実行文 }
例えば、ローソク足100本あった場合、各ローソク足のRSI値をiRSI関数を使って求める場合、以下のようになります。
それぞれについて説明します。
int limit = 100;
ローソク足100本を表現しています。
double rsi_value[100];
各ローソク足に対応するために配列を宣言しました。
for(int i = 0; i < limit; i++) { rsi_value[i] = iRSI(NULL,NULL,14,PRICE_CLOSE,i); }
そしてfor文ですが、初期化式の部分で「int i = 0;」というように「i」を初期化している分を書いています。「i」が「0」から始まるということがわかります。
次に条件判定ですが、「i < limit;」です。こちら初回は「0 < 100」と置き換えられますね。
この場合条件式は成立しています。
条件式が成立している場合、コードの実行文に入ります。
rsi_value[i] = iRSI(NULL,NULL,14,PRICE_CLOSE,i);
関数の説明は省略しますが、初回はrsi_value[0]と置き換えられますので、「0」番目の配列にRSI値を代入しています。
コードの実行分が終わると 更新式に移り「i++」となっています。この「++」は「1を足す」という意味になりますので、初回の「i」は「0」なので、「i」が「1」に変わります。
繰り返しに入り、条件判定「i < limit;」は、今度は「1 < 100」と置き換えられますね。



初期化式は初めの1回だけ
この条件式も成立しているので、再びコードの実行分に入り、rsi_value[i]も「1」に置き換えられることがわかると思います。
この繰り返しによって「i」の値は1ずつ増えていき、最終的に「99 < 100」まで条件式が成り立つ形となります。
その後、「i」の値は1ずつ増えて「100」になったとき、条件式が「100 < 100」となり、この条件式は成立しなくなります。
このタイミングでfor文は終了します。
おまけ:他の制御文
if文やfor文のほかにも制御文は存在します。
ただ、使わなくてもif文やfor文で表現できることも多いので、ここでの説明は省略しました。
しかし、実際に使っていないかというとそうでもないので、紹介だけしておきます。
switch
選択処理といい、「○○がAの時は××、○○がBの時は△△、○○がCの時は■■」というように「○○」の部分の違いによって制御を変えるという内容です。
これif文でも表現できますよね?
while
条件式が「true」の間ループ処理を実行します。
for文に近いものがありますが、while文に更新式が無いため、自分でコードを書くなり、どこかのタイミングで「false」になるように切り替えなければなりません。
コードの書き方を間違えると無限ループに陥って面倒になることもあります。
なので、使い方に慣れるまではfor文を使っていけば良いと思います。
コメント
コメント一覧 (66件)
uzyf6k
http://pinupaz.top/# pin up az
order Viagra discreetly: safe online pharmacy – trusted Viagra suppliers
purchase Modafinil without prescription: modafinil 2025 – purchase Modafinil without prescription
order Cialis online no prescription: cheap Cialis online – online Cialis pharmacy
safe online pharmacy: discreet shipping – best price for Viagra
best price Cialis tablets: discreet shipping ED pills – order Cialis online no prescription
verified Modafinil vendors: buy modafinil online – doctor-reviewed advice
buy generic Cialis online: online Cialis pharmacy – affordable ED medication
buy cheap clomid prices: Clom Health – where to buy generic clomid pill
buy prednisone online without a script: PredniHealth – prednisone sale
where to get clomid: Clom Health – clomid cheap
can i buy prednisone online without prescription: order prednisone on line – PredniHealth
avmddk
purchase amoxil – https://combamoxi.com/ amoxicillin tablets
Good shout.
buy fluconazole 200mg generic – https://gpdifluca.com/# buy fluconazole 200mg without prescription
buy escitalopram 10mg pill – https://escitapro.com/# escitalopram pills
Nice
Nice
cenforce for sale – https://cenforcers.com/# cenforce tablet
full spectrum cbd gummies area 52
thca carts area 52
thcv gummies area 52
best cbd sleep edibles area 52
disposable weed pen area 52
thc gummies
thc pen area 52
hybrid thc vape area 52
buy thca area 52
magic mushrooms for sale area 52
hybrid gummies area 52
live resin area 52
best indica thc weed pens area 52
thca gummies area 52
thc gummies for pain area 52
thc microdose gummies area 52
thc tinctures area 52
infused pre rolls area 52
distillate carts area 52
sleep gummies area 52
pre rolls area 52
live resin carts area 52
best sativa thc carts area 52
live resin gummies area 52
best amanita edibles area 52
thca diamonds area 52
thc gummies for anxiety area 52
2 gram carts area 52
thca disposable area 52
1 gram carts area 52
cheap cialis – https://ciltadgn.com/ what doe cialis look like
how much does cialis cost with insurance – https://strongtadafl.com/# cialis without prescription
ranitidine 300mg oral – site zantac pills
can you buy viagra ebay – viagra usa buy viagra cheap online
Thanks on putting this up. It’s understandably done. order gabapentin 100mg sale
This is a theme which is near to my fundamentals… Diverse thanks! Faithfully where can I find the contact details in the course of questions? https://gnolvade.com/
I’ll certainly carry back to be familiar with more. https://ursxdol.com/sildenafil-50-mg-in/
Proof blog you procure here.. It’s obdurate to find strong status script like yours these days. I truly respect individuals like you! Take vigilance!! https://prohnrg.com/product/get-allopurinol-pills/
More content pieces like this would insinuate the интернет better. https://aranitidine.com/fr/lasix_en_ligne_achat/
The depth in this piece is exceptional. https://ondactone.com/spironolactone/
This website positively has all of the tidings and facts I needed there this thesis and didn’t know who to ask.
buy generic spironolactone
This is the gentle of literature I positively appreciate. http://bbs.dubu.cn/home.php?mod=space&uid=395573
dapagliflozin 10mg cheap – https://janozin.com/# buy forxiga 10 mg pills
buy orlistat sale – click order orlistat 60mg pill
More articles like this would make the blogosphere richer. http://bbs.51pinzhi.cn/home.php?mod=space&uid=7112902