//+------------------------------------------------------------------+
//| Demo_iGator.mq5 |
//| Copyright 2000-2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "이 지표는 다음 데이터를 얻는 방법을 보여줍니다:"
#property description "iGator 기술 지표용 지표 버퍼 크기."
#property description "지표 계산에 사용되는 심볼 및 타임프레임은"
#property description "심볼 및 주기 매개변수로 설정됩니다."
#property description "핸들을 만드는 방법은 'type' 매개변수(함수 유형)를 통해 설정됩니다."
#property description "다른 모든 파라미터는 표준 Gator 오실레이터와 같습니다."
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 2
//--- 드로잉 GatorUp
#property indicator_label1 "GatorUp"
#property indicator_type1 DRAW_COLOR_HISTOGRAM
#property indicator_color1 clrGreen, clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 드로잉 GatorDown
#property indicator_label2 "GatorDown"
#property indicator_type2 DRAW_COLOR_HISTOGRAM
#property indicator_color2 clrGreen, clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//+------------------------------------------------------------------+
//| 핸들 생성 방법 열거 |
//+------------------------------------------------------------------+
enum Creation
{
Call_iGator, // iGator 사용
Call_IndicatorCreate // IndicatorCreate 사용
};
//--- 입력 매개변수
input Creation type=Call_iGator; // 함수 유형
input string symbol=" "; // 심볼
input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 타임프레임
input int jaw_period=13; // 턱선 주기
input int jaw_shift=8; // 턱선 이동
input int teeth_period=8; // 이빨선 주기
input int teeth_shift=5; // 이빨선 이동
input int lips_period=5; // 입술선 주기
input int lips_shift=3; // 입술선 이동
input ENUM_MA_METHOD MA_method=MODE_SMMA; // 앨리게이터 선 평균 산출 방법
input ENUM_APPLIED_PRICE applied_price=PRICE_MEDIAN;// 앨리게이터 계산에 사용되는 가격 유형
//--- 지표 버퍼
double GatorUpBuffer[];
double GatorUpColors[];
double GatorDownBuffer[];
double GatorDownColors[];
//--- iGator 지표 핸들을 저장하기 위한 변수
int handle;
//--- 저장 변수
string name=symbol;
//--- 차트의 지표명
string short_name;
//--- 위쪽 및 아래쪽 히스토그램에 대한 이동 값
int shift;
//--- Gator 오실레이터 지표 값 수를 유지할 것입니다
int bars_calculated=0;
//+------------------------------------------------------------------+
//| 커스텀 지표 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 지표 버퍼에 배열 할당
SetIndexBuffer(0,GatorUpBuffer,INDICATOR_DATA);
SetIndexBuffer(1,GatorUpColors,INDICATOR_COLOR_INDEX);
SetIndexBuffer(2,GatorDownBuffer,INDICATOR_DATA);
SetIndexBuffer(3,GatorDownColors,INDICATOR_COLOR_INDEX);
/*
파라미터에 지정된 모든 시프트는 게이터 오실레이터가 그려진 기준으로 악어 지표를 참조합니다!
그래서 게이터 표시기 자체를 움직이지 않고 엘리게이터 선을 움직입니다,
어떤 값이 Gator 오실레이터 계산에 사용되는지에 대해!
*/
//--- 위 히스토그램과 아래 히스토그램의 이동을 계산해 봅시다. 이는 턱 선과 이빨 선 사이의 차이와 같습니다.
shift=MathMin(jaw_shift,teeth_shift);
PlotIndexSetInteger(0,PLOT_SHIFT,shift);
//--- 위 히스토그램과 아래 히스토그램의 이동을 계산해 봅시다. 이는 턱 선과 이빨 선 사이의 차이와 같습니다.
PlotIndexSetInteger(1,PLOT_SHIFT,shift);
//--- 지표가 그려지는 심볼 결정
name=symbol;
//--- 오른쪽과 왼쪽의 공백을 삭제
StringTrimRight(name);
StringTrimLeft(name);
//--- 'name' 문자열의 길이가 0이 되는 경우
if(StringLen(name)==0)
{
//--- 지표가 부착된 차트의 심볼을 취합니다
name=_Symbol;
}
//--- 지표 핸들 생성
if(type==Call_iGator)
handle=iGator(name,period,jaw_period,jaw_shift,teeth_period,teeth_shift,
lips_period,lips_shift,MA_method,applied_price);
else
{
//--- 구조물을 지표의 매개변수로 채움
MqlParam pars[8];
//--- 엘리게이터 선의 주기 및 이동
pars[0].type=TYPE_INT;
pars[0].integer_value=jaw_period;
pars[1].type=TYPE_INT;
pars[1].integer_value=jaw_shift;
pars[2].type=TYPE_INT;
pars[2].integer_value=teeth_period;
pars[3].type=TYPE_INT;
pars[3].integer_value=teeth_shift;
pars[4].type=TYPE_INT;
pars[4].integer_value=lips_period;
pars[5].type=TYPE_INT;
pars[5].integer_value=lips_shift;
//--- 평활유형
pars[6].type=TYPE_INT;
pars[6].integer_value=MA_method;
//--- 가격 유형
pars[7].type=TYPE_INT;
pars[7].integer_value=applied_price;
//--- create handle
handle=IndicatorCreate(name,period,IND_GATOR,8,pars);
}
//--- 핸들이 생성되지 않은 경우
if(handle==INVALID_HANDLE)
{
//--- 실패에 대해 구분하고 오류 코드를 출력
PrintFormat("%s/%s 심볼에 대한 iGator 지표 핸들 생성 실패, 오류 코드 %d",
name,
EnumToString(period),
GetLastError());
//--- 지표가 일찍 중단됨
return(INIT_FAILED);
}
//--- 게이터 오실레이터 지표가 계산되는 심볼/타임프레임 표시
short_name=StringFormat("iGator(%s/%s, %d, %d ,%d, %d, %d, %d)",name,EnumToString(period),
jaw_period,jaw_shift,teeth_period,teeth_shift,lips_period,lips_shift);
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- 지표의 정상 초기화
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[])
{
//--- iGator 지표에서 복사된 값 수
int values_to_copy;
//--- 지표에서 계산된 값 수를 결정
int calculated=BarsCalculated(handle);
if(calculated<=0)
{
PrintFormat("BarsCalculated() 가 %d 을(를) 반환, 오류 코드 %d",calculated,GetLastError());
return(0);
}
//--- 지표의 첫 번째 계산 시작이거나 iGator 지표 값 수가 변경된 경우
//---또는 두 개 이상의 막대에 대한 지표 계산이 필요한지 여부(가격 이력에서 무언가가 변경되었음을 의미함)
if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- GatorUpBuffer 배열이 iGator 지표의 심볼/주기 값보다 크면, 모든 것을 복사하지 않습니다.
//--- 모든 항목을 복사하지 않고 지표ㅍ 버퍼 크기보다 작게 복사합니다.
if(calculated>rates_total) values_to_copy=rates_total;
else values_to_copy=calculated;
}
else
{
//--- 이는 지표 계산의 첫 번째가 아니며 계산을 위한 OnCalculate()의 마지막 호출이
//--- 두 개 이하의 막대가 추가되었음을 의미합니다
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- Gator Oscillator 지표 값 배열을 채웁니다
//--- FillArraysFromBuffer가 false를 반환하면 정보가 아직 준비되지 않았음을 의미하므로 작업을 종료합니다
if(!FillArraysFromBuffers(GatorUpBuffer,GatorUpColors,GatorDownBuffer,GatorDownColors,
shift,handle,values_to_copy)) return(0);
//--- 메시지 형성
string comm=StringFormat("%s ==> 지표 %s 값 업데이트: %d",
TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS),
short_name,
values_to_copy);
//--- 차트에 서비스 메시지 표시
Comment(comm);
//--- Gator Oscillator 지표에서의 값 수 기억
bars_calculated=calculated;
//--- 다음 호출에 대해 prev_calculated 값을 반환합니다
return(rates_total);
}
//+------------------------------------------------------------------+
//| iGator 지표에서 지표 버퍼 채우기 |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &ups_buffer[], // 상단 히스토그램용 지표 버퍼
double &up_color_buffer[], // 상단 히스토그램의 가격 지수에 대한 지표 버퍼
double &downs_buffer[], // 하단 히스토그램용 지표 버퍼
double &downs_color_buffer[], // 하위 히스토그램의 가격 지수에 대한 지표 버퍼
int u_shift, // 상단 및 하단 히스토그램 시프트
int ind_handle, // iGator 지표 핸들
int amount // 복제된 값 수
)
{
//--- 오류 코드 재설정
ResetLastError();
//--- GatorUpBuffer 배열의 일부를 0 인덱스를 가진 지표 버퍼의 값으로 채웁니다
if(CopyBuffer(ind_handle,0,-u_shift,amount,ups_buffer)<0)
{
//--- 복사가 실패하면 오류 코드를 알려줍니다
PrintFormat("iGator 지표로부터 데이터 복제 실패, 오류 코드 %d",GetLastError());
//--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
return(false);
}
//--- GatorUpColors 배열 일부를 인덱스 1이 있는 지표 버퍼의 값으로 채웁니다
if(CopyBuffer(ind_handle,1,-u_shift,amount,up_color_buffer)<0)
{
//--- 복사가 실패하면 오류 코드를 알려줍니다
PrintFormat("iGator 지표로부터 데이터 복제 실패, 오류 코드 %d",GetLastError());
//--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
return(false);
}
//--- GatorDownBuffer 배열의 일부를 인덱스 2가 있는 지표 버퍼의 값으로 채웁니다
if(CopyBuffer(ind_handle,2,-u_shift,amount,downs_buffer)<0)
{
//--- 복사가 실패하면 오류 코드를 알려줍니다
PrintFormat("iGator 지표로부터 데이터 복제 실패, 오류 코드 %d",GetLastError());
//--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
return(false);
}
//--- GatorDownColors 배열의 일부를 인덱스 3이 있는 지표 버퍼 값으로 채웁니다
if(CopyBuffer(ind_handle,3,-u_shift,amount,downs_color_buffer)<0)
{
//--- 복사가 실패하면 오류 코드를 알려줍니다
PrintFormat("iGator 지표로부터 데이터 복제 실패, 오류 코드 %d",GetLastError());
//--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
return(false);
}
//--- 모든 게 좋습니다
return(true);
}
//+------------------------------------------------------------------+
//| 지표 초기화 해제 함수 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(handle!=INVALID_HANDLE)
IndicatorRelease(handle);
//--- 지표 삭제 후 차트 지우기
Comment("");
}
|