//+------------------------------------------------------------------+
//| Demo_iFractals.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 "이 지표는 다음 데이터를 얻는 방법을 보여줍니다:"
#property description "iFractals 기술 지표용 지표 버퍼의 크기."
#property description "지표 계산에 사용되는 심볼 및 타임프레임은"
#property description "심볼 및 주기 매개변수로 설정됩니다."
#property description "핸들을 만드는 방법은 'type' 매개변수(함수 유형)를 통해 설정됩니다."
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- the FractalUp plot
#property indicator_label1 "FractalUp"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrBlue
//--- the FractalDown plot
#property indicator_label2 "FractalDown"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
//+------------------------------------------------------------------+
//| 핸들 생성 방법 열거 |
//+------------------------------------------------------------------+
enum Creation
{
Call_iFractals, // iFractals 사용
Call_IndicatorCreate // IndicatorCreate 사용
};
//--- 입력 매개변수
input Creation type=Call_iFractals; // 함수 유형
input string symbol=" "; // 심볼
input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 타임프레임
//--- 지표 버퍼
double FractalUpBuffer[];
double FractalDownBuffer[];
//--- iFractals 지표핸들을 저장하는 변수
int handle;
//--- 저장 변수
string name=symbol;
//--- 차트의 지표명
string short_name;
//--- 프랙탈 지표 값의 수를 유지할 것입니다
int bars_calculated=0;
//+------------------------------------------------------------------+
//| 커스텀 지표 초기화 함수 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 지표 버퍼에 배열 할당
SetIndexBuffer(0,FractalUpBuffer,INDICATOR_DATA);
SetIndexBuffer(1,FractalDownBuffer,INDICATOR_DATA);
//--- set codes using a symbol from the Wingdings charset for the PLOT_ARROW property
PlotIndexSetInteger(0,PLOT_ARROW,217); // 위 화살표
PlotIndexSetInteger(1,PLOT_ARROW,218); // 아래 화살표
//--- 지표가 그려지는 심볼 결정
name=symbol;
//--- 오른쪽과 왼쪽의 공백을 삭제
StringTrimRight(name);
StringTrimLeft(name);
//--- 'name' 문자열의 길이가 0이 되는 경우
if(StringLen(name)==0)
{
//--- 지표가 부착된 차트의 심볼을 취합니다
name=_Symbol;
}
//--- 지표 핸들 생성
if(type==Call_iFractals)
handle=iFractals(name,period);
else
handle=IndicatorCreate(name,period,IND_FRACTALS);
//--- 핸들이 생성되지 않은 경우
if(handle==INVALID_HANDLE)
{
//--- 실패에 대해 구분하고 오류 코드를 출력
PrintFormat("%s/%s 심볼에 대한 iFractals 지표 핸들 생성 실패 , 오류 코드 %d",
name,
EnumToString(period),
GetLastError());
//--- 지표가 일찍 중단됨
return(INIT_FAILED);
}
//--- 프랙탈 지표가 계산되는 심볼/타임프레임을 표시
short_name=StringFormat("iFractals(%s/%s)",name,EnumToString(period));
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[])
{
//--- iFractals 지표에서 복사된 값 수
int values_to_copy;
//--- 지표에서 계산된 값 수를 결정
int calculated=BarsCalculated(handle);
if(calculated<=0)
{
PrintFormat("BarsCalculated() 가 %d 을(를) 반환, 오류 코드 %d",calculated,GetLastError());
return(0);
}
//--- 지표의 첫 번째 연산 시작이거나 iFractals 지표의 값 수가 변경된 경우
//---또는 두 개 이상의 막대에 대한 지표 계산이 필요한지 여부(가격 이력에서 무언가가 변경되었음을 의미함)
if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- FractalUpBuffer 배열이 심볼/주기에 대한 iFractals 지표 값 수보다 크면, 모든 것을 복사하지 않습니다
//--- 모든 항목을 복사하지 않고 지표ㅍ 버퍼 크기보다 작게 복사합니다.
if(calculated>rates_total) values_to_copy=rates_total;
else values_to_copy=calculated;
}
else
{
//--- 이는 지표 계산의 첫 번째가 아니며 계산을 위한 OnCalculate()의 마지막 호출이
//--- 두 개 이하의 막대가 추가되었음을 의미합니다
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- FractalUpBuffer 및 FractalDownBuffer 배열에 Fractals 지표의 값을 채웁니다
//--- FillArrayFromBuffer가 false를 반환하면 정보가 아직 준비되지 않았음을 의미하므로 작업을 종료합니다
if(!FillArraysFromBuffers(FractalUpBuffer,FractalDownBuffer,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);
//--- 프랙탈 지표의 값 수를 기억
bars_calculated=calculated;
//--- 다음 호출에 대해 prev_calculated 값을 반환합니다
return(rates_total);
}
//+------------------------------------------------------------------+
//| iFractals 지표에서 지표 버퍼 채우기 |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &up_arrows[], // 위쪽 화살표 지표 버퍼
double &down_arrows[], // 아래쪽 화살표 지표 버퍼
int ind_handle, // iFractals 지표 핸들
int amount // 복제된 값 수
)
{
//--- 오류 코드 재설정
ResetLastError();
//--- FractalUpBuffer 배열의 일부를 0 인덱스를 가진 지표 버퍼의 값으로 채웁니다
if(CopyBuffer(ind_handle,0,0,amount,up_arrows)<0)
{
//--- 복사가 실패하면 오류 코드를 알려줍니다
PrintFormat("FractalUpBuffer 배열에 대한 iFractals 지표 데이터 복제 실패, 오류 코드 %d",
GetLastError());
//--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
return(false);
}
//--- FractalDownBuffer 배열의 일부를 인덱스 1이 있는 지표 버퍼의 값으로 채웁니다
if(CopyBuffer(ind_handle,1,0,amount,down_arrows)<0)
{
//--- 복사가 실패하면 오류 코드를 알려줍니다
PrintFormat("FractalDownBuffer 배열에 대한 iFractals 로부터 데이터 복제 실패, 오류 코드 %d",
GetLastError());
//--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
return(false);
}
//--- 모든 게 좋습니다
return(true);
}
//+------------------------------------------------------------------+
//| 지표 초기화 해제 함수 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(handle!=INVALID_HANDLE)
IndicatorRelease(handle);
//--- 지표 삭제 후 차트 지우기
Comment("");
}
|