2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MQL4でCMOを計算する

Last updated at Posted at 2019-12-21

以前iMAやiBands関数と同じような使い方でRCIを取得できる、iRCI関数を作って記事にしたところ、同じようにCMOを取得したいとのコメントをいただいたので記事にしました。
MQL4でRCIを計算する

iCMO

double iCMO(const string symbol, int timeframe, int period, int index)
{
    double up = 0.0;
    double down = 0.0;
    double diff = 0.0;

    for(int i = index + period - 1; i >= index; i--) { 
        diff = iClose(symbol, timeframe, i) - iClose(symbol, timeframe, i + 1); 
        if(diff > 0){ 
            up += diff; 
        } else if(diff < 0) {
            down -= diff;
        }
    }

    if (up + down == 0) {
        return(0);
    }
    
    return(100 * (up - down) / (up + down));
}

CMO.png
上が参考にしたCMOインジケーター、下がiCMO関数で計算したCMOです。
どちらも同じ結果になります。

追記

終値の変動がperiod回連続で起こらなかった場合にエラー(Zero divide)となる不具合があったので修正しました。

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?