#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
//---- ColorLine をプロットする
#property indicator_label1
"ColorLine"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1
clrRed,clrGreen,clrBlue
#property indicator_style1
STYLE_SOLID
#property indicator_width1 3
//--- 指標バッファ
double ColorLineBuffer[];
double ColorBuffer[];
int MA_handle;
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数 |
//+------------------------------------------------------------------+
void OnInit()
{
//--- 指標バッファマッピング
SetIndexBuffer(0,ColorLineBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ColorBuffer,INDICATOR_COLOR_INDEX);
//--- MA ハンドルの取得
MA_handle=iMA(Symbol(),0,10,0,MODE_EMA,PRICE_CLOSE);
//---
}
//+------------------------------------------------------------------+
//| 色インデックスを取得 |
//+------------------------------------------------------------------+
int getIndexOfColor(int i)
{
int j=i%300;
if(j<100) return(0);// 1 番目のインデックス
if(j<200) return(1);// 2 番目のインデックス
return(2); // 3 番目のインデックス
}
//+------------------------------------------------------------------+
//| カスタム指標の反復関数 |
//+------------------------------------------------------------------+
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[])
{
//---
static int ticks=0,modified=0;
int limit;
//--- 初めての計算、またはバーの数が変わった
if(prev_calculated==0)
{
//--- MA の値を ColorLineBuffer 指標バッファに複製する
int copied=CopyBuffer(MA_handle,0,0,rates_total,ColorLineBuffer);
if(copied<=0) return(0);// 複製に失敗。廃棄する。
//--- バー全部で線の色を設定
for(int i=0;i<rates_total;i++)
ColorBuffer[i]=getIndexOfColor(i);
}
else
{
//--- MA の値を ColorLineBuffer 指標バッファに複製する
int copied=CopyBuffer(MA_handle,0,0,rates_total,ColorLineBuffer);
if(copied<=0) return(0);
ticks++;// ティックを数える
if(ticks>=5)//基本配色を変更する時
{
ticks=0; // カウンタをリセット
modified++; // 色変換のカウンタ
if(modified>=3)modified=0;// カウンタをリセット
ResetLastError();
switch(modified)
{
case 0:// 1 番目の基本配色
PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,clrRed);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,clrBlue);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,clrGreen);
Print("Color scheme "+modified);
break;
case 1:// 2 番目の基本配色
PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,clrYellow);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,clrPink);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,clrLightSlateGray);
Print("Color scheme "+modified);
break;
default:// 3 番目の基本配色
PlotIndexSetInteger(0,PLOT_LINE_COLOR,0,clrLightGoldenrod);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,1,clrOrchid);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,2,clrLimeGreen);
Print("Color scheme "+modified);
}
}
else
{
//--- 開始位置を設定
limit=prev_calculated-1;
//--- バー全部で線の色を設定
for(int i=limit;i<rates_total;i++)
ColorBuffer[i]=getIndexOfColor(i);
}
}
//--- 次の呼び出しのために prev_calculated の値を返す
return(rates_total);
}
//+------------------------------------------------------------------+
|