//+------------------------------------------------------------------+
//| DRAW_COLOR_ARROW.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_ARROW를 시연하기 위한 지표"
#property description "유니코드 문자로 설정된 다양한 색상의 화살표를 차트에 그립니다"
#property description "화살표의 색상, 크기, 이동 및 심볼 코드가 임의로 변경됩니다"
#property description " 모든 N 틱을 임의로"
#property description "코드 매개변수는 기준 값을 설정: code=159 (a circle)"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 1
//--- ColorArrow 플롯
#property indicator_label1 "ColorArrow"
#property indicator_type1 DRAW_COLOR_ARROW
//--- 히스토그램 컬러링을 위한 8가지 색상 정의 (특수 배열에 저장됨)
#property indicator_color1 clrRed,clrBlue,clrSeaGreen,clrGold,clrDarkOrange,clrMagenta,clrYellowGreen,clrChocolate
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 매개변수 입력
input int N=5; // 변경할 틱의 수
input ushort code=159; // DRAW_ARROW에 그릴 심볼 코드
int color_sections;
//--- 플롯에 대한 지표 버퍼
double ColorArrowBuffer[];
//--- 색상 인덱스를 저장할 버퍼
double ColorArrowColors[];
//--- 색상을 저장하는 배열에는 14개의 요소가 포함됩니다
color colors[]=
{
clrRed,clrBlue,clrGreen,clrChocolate,clrMagenta,clrDodgerBlue,clrGoldenrod,
clrIndigo,clrLightBlue,clrAliceBlue,clrMoccasin,clrWhiteSmoke,clrCyan,clrMediumPurple
};
//+------------------------------------------------------------------+
//| 사용자 지정 지표 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 지표 버퍼 맵핑
SetIndexBuffer(0,ColorArrowBuffer,INDICATOR_DATA);
SetIndexBuffer(1,ColorArrowColors,INDICATOR_COLOR_INDEX);
//--- PLOT_ARROW에서 도면에 대한 심볼 코드 정의
PlotIndexSetInteger(0,PLOT_ARROW,code);
//--- 화살표의 수직 이동 설정(픽셀)
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,5);
//--- 빈 값 0으로 설정
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---- 사인파를 채색할 색상의 수
color_sections=8; // #property indicator_color1 에 대한 코멘트 보기
//---
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=1;
if(prev_calculated>0) start=prev_calculated-1;
//--- 계산 루프
for(int i=1;i<rates_total;i++)
{
//--- 현재 종가가 이전 가격보다 높은 경우 화살표를 그립니다
if(close[i]>close[i-1])
ColorArrowBuffer[i]=close[i];
//--- 그렇지 않으면 null 값을 지정
else
ColorArrowBuffer[i]=0;
//--- 화살표 색상
int index=i%color_sections;
ColorArrowColors[i]=index;
}
//--- 다음 호출을 위한 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("ArrowColorIndex[%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);
//--- 화살표 코드 변경 블록(PLOT_ARROW)
number=MathRand();
//--- 화살표의 새 코드(0 ~ 19)를 계산할 수 있는 정수 분할의 나머지를 가져오기
int code_add=number%20;
//--- code+code_add의 결과로 새 기호 코드를 설정
PlotIndexSetInteger(0,PLOT_ARROW,code+code_add);
//--- 심볼 코드 PLOT_ARROW 쓰기
comm="\r\n"+"PLOT_ARROW="+IntegerToString(code+code_add)+comm;
//--- 화살표의 수직 이동(픽셀)을 변경하기 위한 블록
number=MathRand();
//--- 이동을 정수 나눗셈의 나머지 값으로 가져오기
int shift=20-number%41;
//--- 다음에서 새 이동을 설정
PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,shift);
//--- 이동 PLOT_ARROW_SHIFT 쓰기
comm="\r\n"+"PLOT_ARROW_SHIFT="+IntegerToString(shift)+comm;
//--- 설명을 사용하여 차트에 정보 표시
Comment(comm);
}
|