//+------------------------------------------------------------------+
//| DRAW_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_SECTION을 시연하기 위한 지표"
#property description "모든 막대에 직선 섹션 그리기"
#property description "섹션의 색상, 너비 및 스타일이 임의로 변경됩니다"
#property description "모든 N 틱 후"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//--- Section 플롯
#property indicator_label1 "섹션"
#property indicator_type1 DRAW_SECTION
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 매개변수 입력
input int bars=5; // 막대의 섹션 길이
input int N=5; // 섹션 스타일을 변경할 틱 수
//--- 플롯에 대한 지표 버퍼
double SectionBuffer[];
//--- 섹션의 끝을 계산하는 보조 변수
int divider;
//--- 색상을 저장할 배열
color colors[]={clrRed,clrBlue,clrGreen};
//--- 선 스타일을 저장할 배열
ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| 사용자 지정 지표 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 지표 버퍼 및 배열 바인딩
SetIndexBuffer(0,SectionBuffer,INDICATOR_DATA);
//--- 0(빈) 값은 그리기에 포함되지 않습니다
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//--- 지표 매개변수 확인
if(bars<=0)
{
PrintFormat("유효하지 않은 매개변수 막대의 값=%d",bars);
return(INIT_PARAMETERS_INCORRECT);
}
else divider=2*bars;
//---+
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();
//--- 틱 카운터를 0으로 재설정
ticks=0;
}
//--- 지표 값 계산이 시작되는 막대의 수
int start=0;
//--- 지표가 이전에 계산된 적이 있는 경우 이전 막대에서 시작을 설정하십시오
if(prev_calculated>0) start=prev_calculated-1;
//--- 다음은 지표 값의 모든 계산값입니다
for(int i=start;i<rates_total;i++)
{
//--- 막대 번호의 나머지 2*막대를 가져오기
int rest=i%divider;
//--- 막대 번호를 2*막대로 나눈 경우
if(rest==0)
{
//--- 섹션 끝을 이 막대의 고가로 설정
SectionBuffer[i]=high[i];
}
//---나눗셈의 나머지가 막대와 같은 경우,
else
{
//--- 섹션 끝을 이 막대의 고가로 설정
if(rest==bars) SectionBuffer[i]=low[i];
//--- 아무 일도 없는 경우, 막대 세트 0을 무시
else SectionBuffer[i]=0;
}
}
//--- 함수의 다음 호출에 대해 prev_calculated 값을 반환
return(rates_total);
}
//+------------------------------------------------------------------+
//| 지표의 섹션 모양을 변경 |
//+------------------------------------------------------------------+
void ChangeLineAppearance()
{
//--- 라인 속성에 대한 정보를 구성하기 위한 문자열
string comm="";
//--- 선 색상 변경 블록
int number=MathRand(); // 임의 숫자 가져오기
//--- 제수는 colors[] 배열의 크기와 같습니다
int size=ArraySize(colors);
//--- 정수 나눗셈의 나머지의 새로운 색상을 선택할 인덱스를 가져오기
int color_index=number%size;
//--- 색상을 PLOT_LINE_COLOR 속성으로 설정
PlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);
//--- 선 색상 쓰기
comm=comm+"\r\n"+(string)colors[color_index];
//--- 선 너비 변경 블록
number=MathRand();
//--- 정수 나눗셈의 나머지 너비 가져오기
int width=number%5; // 너비는 0 ~ 4로 설정됩니다
//--- 너비 설정
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
//--- 선 너비 쓰기
comm=comm+"\r\nWidth="+IntegerToString(width);
//--- 선의 스타일을 변경하기 위한 블록
number=MathRand();
//--- 제수는 스타일 배열의 크기와 같습니다
size=ArraySize(styles);
//--- 정수 나눗셈의 나머지 항목으로 새 스타일을 선택할 인덱스를 가져오기
int style_index=number%size;
//--- 선 스타일 설정
PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);
//--- 선 스타일 쓰기
comm="\r\n"+EnumToString(styles[style_index])+""+comm;
//--- 설명을 사용하여 차트에 정보 표시
Comment(comm);
}
|