//+------------------------------------------------------------------+
//| DRAW_COLOR_SECTION.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "DRAW_COLOR_SECTION을 시연하기 위한 지표"
#property description "지정된 막대 수와 같은 길이의 색상 섹션을 그립니다"
#property description "섹션의 색상, 너비 및 스타일이 임의로 변경됩니다"
#property description "모든 N 틱 후"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
//--- ColorSection 플롯
#property indicator_label1 "ColorSection"
#property indicator_type1 DRAW_COLOR_SECTION
//--- 채색 섹션을 위한 8가지 색상 정의(특수 배열에 저장됨)
#property indicator_color1 clrRed,clrGold,clrMediumBlue,clrLime,clrMagenta,clrBrown,clrTan,clrMediumVioletRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 매개변수 입력
input int N=5; // 변경할 틱 수
input int bars_in_section=5; // 막대에서 섹션의 길이
//--- 섹션의 끝을 계산하는 보조 변수
int divider;
int color_sections;
//--- 플로팅할 버퍼
double ColorSectionBuffer[];
//--- 각 막대에 선 색상을 저장하는 버퍼
double ColorSectionColors[];
//--- 색상을 저장하는 배열에는 14개의 요소가 포함됩니다
color colors[]=
{
clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,
clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple
};
//--- 선 스타일을 저장할 배열
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| 사용자 지정 지표 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 지표 버퍼 맵핑
SetIndexBuffer(0,ColorSectionBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ColorSectionColors,INDICATOR_COLOR_INDEX);
//--- 0(빈) 값은 그리기에 포함되지 않습니다
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---- 섹션을 채색할 색상의 수
int color_sections=8; // #property indicator_color1에 대한 코멘트 보기
//--- 지표 매개변수 확인
if(bars_in_section<=0)
{
PrintFormat("유효하지 않은 섹션 길이=%d",bars_in_section);
return(INIT_PARAMETERS_INCORRECT);
}
else divider=color_sections*bars_in_section;
//---
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[])
{
static int ticks=0;
//--- 틱을 계산하여 선의 스타일, 색상 및 너비 변경
ticks++;
//--- 틱의 임계 수가 누적된 경우
if(ticks>=N)
{
//--- 선 속성 변경
ChangeLineAppearance();
//--- 섹션을 플롯하는 데 사용되는 색상 변경
ChangeColors(colors,color_sections);
//--- 틱 카운터를 0으로 재설정
ticks=0;
}
//--- 지표 값 계산이 시작되는 막대의 수
int start=0;
//--- 지표가 이전에 계산된 적이 있는 경우 이전 막대에서 시작을 설정하십시오
if(prev_calculated>0) start=prev_calculated-1;
//--- 다음은 지표 값의 모든 계산값입니다
for(int i=start;i<rates_total;i++)
{
//--- 막대의 번호가 section_length로 나누어진 경우, 이는 섹션의 끝임을 의미합니다
if(i%bars_in_section==0)
{
//--- 섹션 끝을 이 막대의 고가로 설정
ColorSectionBuffer[i]=high[i];
//--- 막대 번호를 scetion_length*number_of_colors로 나눈 나머지 부분
int rest=i%divider;
//색상 = 0 ~ number_of_colors-1까지 번호 가져오기
int color_indext=rest/bars_in_section;
ColorSectionColors[i]=color_indext;
}
//---나눗셈의 나머지가 막대와 같은 경우,
else
{
//--- 아무 일도 없는 경우, 막대 세트 0을 무시
else ColorSectionBuffer[i]=0;
}
}
//--- 함수의 다음 호출에 대해 prev_calculated 값을 반환
return(rates_total);
}
//+------------------------------------------------------------------+
//| 선 세그먼트의 색상을 변경 |
//+------------------------------------------------------------------+
void ChangeColors(color &cols[],int plot_colors)
{
//--- 색상의 수
int size=ArraySize(cols);
//---
string comm=ChartGetString(0,CHART_COMMENT)+"\r\n\r\n";
//--- 각 색상 인덱스에 대해 임의로 새 색상 정의
for(int plot_color_ind=0;plot_color_ind<plot_colors;plot_color_ind++)
{
//--- 임의 값 가져오기
int number=MathRand();
//--- col[] 배열의 인덱스를 정수 나눗셈의 나머지 항목으로 가져오기
int i=number%size;
//--- 각 인덱스의 색상을 PLOT_LINE_COLOR 속성으로 설정
PlotIndexSetInteger(0, // 그래픽 스타일의 수
PLOT_LINE_COLOR, // 속성 식별자
plot_color_ind, // 색의 인덱스, 색상을 쓰는 곳
cols[i]); // 새 색상
//--- 색상 쓰기
comm=comm+StringFormat("SectionColorIndex[%d]=%s \r\n",plot_color_ind,ColorToString(cols[i],true));
ChartSetString(0,CHART_COMMENT,comm);
}
//---
}
//+------------------------------------------------------------------+
//| 지표에 표시된 선의 모양을 변경 |
//+------------------------------------------------------------------+
void ChangeLineAppearance()
{
//--- 라인 속성에 대한 정보를 구성하기 위한 문자열
string comm="";
//--- 선 너비 변경 블록
int number=MathRand();
//--- 정수 나눗셈의 나머지 너비 가져오기
int width=number%5; // 너비는 0 ~ 4로 설정됩니다
//--- 색상을 PLOT_LINE_WIDTH 속성으로 설정
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 선 너비 쓰기
comm=comm+" Width="+IntegerToString(width);
//--- 선의 스타일을 변경하기 위한 블록
number=MathRand();
//--- 제수는 스타일 배열의 크기와 같습니다
int size=ArraySize(styles);
//--- 정수 나눗셈의 나머지 항목으로 새 스타일을 선택할 인덱스를 가져오기
int style_index=number%size;
//--- 색상을 PLOT_LINE_COLOR 속성으로 설정
PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);
//--- 선 스타일 쓰기
comm=EnumToString(styles[style_index])+", "+comm;
//--- 설명을 사용하여 차트에 정보 표시
Comment(comm);
}
|