iBearsPower

이 기능은 베어즈 파워(Bears Power) 지표 핸들을 반환합니다. 버퍼가 하나뿐입니다.

int  iBearsPower(
   string              symbol,            // 심볼명
   ENUM_TIMEFRAMES     period,            // 기간
   int                 ma_period,         // 평균 주기
   );

Parameters

symbol

[in] 지표를 계산하는 데 사용해야 하는 보안의 기호 이름. NULL 값은 현재 심볼을 의미합니다.

period

[in] 기간의 값은 ENUM_TIMEFRAMES 값 중 하나일 수 있으며, 0은 현재 기간을 의미합니다.

ma_period

[in]  지표 계산을 위한 평균 기간의 값.

반환 값

오류가 INVALID_HANDLE를 반환하는 경우 지정된 기술 지표의 핸들을 반환합니다. 컴퓨터 메모리는 지표 핸들이 전달되는 IndicatorRelease() 함수를 사용하여 더 이상 사용되지 않는 지표에서 해방될 수 있습니다.

예제:

//+------------------------------------------------------------------+
//|                                             Demo_iBearsPower.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 "iBearsPower 기술 지표에 대한 지표 버퍼."
#property description "지표 계산에 사용되는 심볼 및 타임프레임은"
#property description "심볼 및 주기 매개변수로 설정됩니다."
#property description "핸들을 만드는 방법은 'type' 매개변수(함수 유형)를 통해 설정됩니다."
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- the iBearsPower plot
#property indicator_label1  "iBearsPower"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrSilver
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//+------------------------------------------------------------------+
//| 핸들 생성 방법 열거                    |
//+------------------------------------------------------------------+
enum Creation
  {
   Call_iBearsPower,       // iBearsPower 사용
   Call_IndicatorCreate    // IndicatorCreate 사용
  };
//--- 입력 매개변수
input Creation             type=Call_iBearsPower;  // 함수 유형 
input int                  ma_period=13;           // 이동 평균 주기
input string               symbol=" ";             // 심볼
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 타임프레임
//--- 지표 버퍼
double         iBearsPowerBuffer[];
//--- iBearsPower 지표 핸들을 저장하기 위한 변수
int    handle;
//--- 저장 변수
string name=symbol;
//--- 차트의 지표명
string short_name;
//--- 우리는 Bears Power 지표의 값 수를 유지할 것입니다
int    bars_calculated=0;
//+------------------------------------------------------------------+
//| 커스텀 지표 초기화 함수                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 지표 버퍼에 배열 할당
   SetIndexBuffer(0,iBearsPowerBuffer,INDICATOR_DATA);
//--- 지표가 그려지는 심볼 결정
   name=symbol;
//--- 오른쪽과 왼쪽의 공백을 삭제
   StringTrimRight(name);
   StringTrimLeft(name);
//--- 'name' 문자열의 길이가 0이 되는 경우
   if(StringLen(name)==0)
     {
      //--- 지표가 부착된 차트의 심볼을 취합니다
      name=_Symbol;
     }
//--- 지표 핸들 생성
   if(type==Call_iBearsPower)
      handle=iBearsPower(name,period,ma_period);
   else
     {
      //--- 구조물을 지표의 매개변수로 채움
      MqlParam pars[1];
      //--- period of ma      
      pars[0].type=TYPE_INT;
      pars[0].integer_value=ma_period;
      handle=IndicatorCreate(name,period,IND_BEARS,1,pars);
     }
//--- 핸들이 생성되지 않은 경우
   if(handle==INVALID_HANDLE)
     {
      //--- 실패에 대해 구분하고 오류 코드를 출력
      PrintFormat("%s/%s 심볼에 대한 iBearsPower 지표 핸들 생성 실패, 오류 코드 %d",
                  name,
                  EnumToString(period),
                  GetLastError());
      //--- 지표가 일찍 중단됨
      return(INIT_FAILED);
     }
//--- Bears Power 지표가 계산된 심볼/타임프레임 표시
   short_name=StringFormat("iBearsPower(%s/%s, period=%d)",name,EnumToString(period),ma_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[])
  {
//--- iBearsPower 지표에서 복사된 값 수
   int values_to_copy;
//--- 지표에서 계산된 값 수를 결정
   int calculated=BarsCalculated(handle);
   if(calculated<=0)
     {
      PrintFormat("BarsCalculated() 가 %d 을(를) 반환, 오류 코드 %d",calculated,GetLastError());
      return(0);
     }
//--- 지표의 첫 번째 계산 시작이거나 iBearsPower 지표의 값 수가 변경된 경우
//---또는 두 개 이상의 막대에 대한 지표 계산이 필요한지 여부(가격 이력에서 무언가가 변경되었음을 의미함)
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
     {
      //--- iBearsPowerBuffer 배열이 심볼/주기 동안 iBearsPower 지표 값 수보다 크면 모든 항목을 복사하지 않습니다 
      //--- 모든 항목을 복사하지 않고 지표ㅍ 버퍼 크기보다 작게 복사합니다.
      if(calculated>rates_total) values_to_copy=rates_total;
      else                       values_to_copy=calculated;
     }
   else
     {
      //--- 이는 지표 계산의 첫 번째가 아니며 계산을 위한 OnCalculate()의 마지막 호출이 
      //--- 두 개 이하의 막대가 추가되었음을 의미합니다
      values_to_copy=(rates_total-prev_calculated)+1;
     }
//--- iBearsPowerBuffer 배열을 Bears Power 지표 값으로 채움
//--- FillArrayFromBuffer가 false를 반환하면 정보가 아직 준비되지 않았음을 의미하므로 작업을 종료합니다
   if(!FillArrayFromBuffer(iBearsPowerBuffer,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);
  }
//+------------------------------------------------------------------+
//| iBearsPower 지표에서 지표 버퍼 채우기         |
//+------------------------------------------------------------------+
bool FillArrayFromBuffer(double &values[],  // 베어즈 파워 값에 대한 지표 버퍼
                         int ind_handle,    // iBearsPower 지표 핸들
                         int amount         // 복제된 값 수
                         )
  {
//--- 오류 코드 재설정
   ResetLastError();
//--- iBearsPowerBuffer 배열의 일부를 인덱스가 0인 지표 버퍼의 값으로 채웁니다
   if(CopyBuffer(ind_handle,0,0,amount,values)<0)
     {
      //--- 복사가 실패하면 오류 코드를 알려줍니다
      PrintFormat("iBearsPower 지표로부터 데이터 복제 실패, 오류 코드 %d",GetLastError());
      //--- 결과가 0인 종료 - 지표가 계산되지 않은 것으로 간주됨을 의미합니다
      return(false);
     }
//--- 모든 게 좋습니다
   return(true);
  }
//+------------------------------------------------------------------+
//| 지표 초기화 해제 함수                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(handle!=INVALID_HANDLE)
      IndicatorRelease(handle);
//--- 지표 삭제 후 차트 지우기
   Comment("");
  }