【解説】 【MQL5 community】 Commodity Channel Index (商品チャネル指数):
CCIは季節的、循環的な市場用に設計されたもので、統計的な平均値からの乖離を数値化します。
【計算式】
CCI =(TP − MA)/(0.015×MD)、
(↑直近の価格水準が設定期間中の変動幅の中でどのあたりの位置にあるかを把握)
TP(基準値) = (高値+安値+終値)/3、
MA(移動平均) =(TPのn日間の単純移動平均)、
MD(平均偏差) ={(MA−P1)+(MA−P2)+(MA−P3)+・・・+(MA−Pn)}/n
Pn = n期のPrice
【シグナル】
CCIは基本的には-100%〜+100%の間で動きます。 売買ポイントしては、-100%、+100%を交差するタイミングで行います。 またゼロラインの交差でトレンドの転換と捉えます。
買いシグナル
−100%以下で推移していたCCIが−100%を上抜いた時を買いとします。
売りシグナル
+100%以上で推移していたCCIが+100%を下抜いた時を売りとします。
//+------------------------------------------------------------------+
//| CCI.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property description "Commodity Channel Index"
#include <MovingAverages.mqh>
//---
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 LightSeaGreen
#property indicator_level1 -100.0
#property indicator_level2 100.0
#property indicator_applied_price PRICE_TYPICAL
//--- input parametrs
input int InpCCIPeriod=14; // Period
//--- global variable
int ExtCCIPeriod;
//---- indicator buffer
double ExtSPBuffer[];
double ExtDBuffer[];
double ExtMBuffer[];
double ExtCCIBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- check for input value of period
if(InpCCIPeriod<=0)
{
ExtCCIPeriod=14;
printf("Incorrect value for input variable InpCCIPeriod=%d. Indicator will use value=%d for calculations.",InpCCIPeriod,ExtCCIPeriod);
}
else ExtCCIPeriod=InpCCIPeriod;
//--- define buffers
SetIndexBuffer(0,ExtCCIBuffer);
SetIndexBuffer(1,ExtDBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(2,ExtMBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(3,ExtSPBuffer,INDICATOR_CALCULATIONS);
//--- indicator name
IndicatorSetString(INDICATOR_SHORTNAME,"CCI("+string(ExtCCIPeriod)+")");
//--- indexes draw begin settings
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtCCIPeriod-1);
//--- number of digits of indicator value
IndicatorSetInteger(INDICATOR_DIGITS,2);
//---- OnInit done
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
//--- variables
int i,j;
double dTmp,dMul=0.015/ExtCCIPeriod;
//--- start calculation
int StartCalcPosition=(ExtCCIPeriod-1)+begin;
//--- check for bars count
if(rates_total<StartCalcPosition)
return(0);
//--- correct draw begin
if(begin>0) PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartCalcPosition+(ExtCCIPeriod-1));
//--- calculate position
int pos=prev_calculated-1;
if(pos<StartCalcPosition)
pos=StartCalcPosition;
//--- main cycle
for(i=pos;i<rates_total;i++)
{
//--- SMA on price buffer
ExtSPBuffer[i]=SimpleMA(i,ExtCCIPeriod,price);
//--- calculate D
dTmp=0.0;
for(j=0;j<ExtCCIPeriod;j++) dTmp+=MathAbs(price[i-j]-ExtSPBuffer[i]);
ExtDBuffer[i]=dTmp*dMul;
//--- calculate M
ExtMBuffer[i]=price[i]-ExtSPBuffer[i];
//--- calculate CCI
if(ExtDBuffer[i]!=0.0) ExtCCIBuffer[i]=ExtMBuffer[i]/ExtDBuffer[i];
else ExtCCIBuffer[i]=0.0;
//---
}
//---- OnCalculate done. Return new prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+
【表示結果】

Back to Meta Trader







