iBarShift

시간별 막대 검색. 이 함수는 지정된 시간에 해당하는 막대 인덱스를 반환합니다.

int  iBarShift(
   const string        symbol,          // 심볼
   ENUM_TIMEFRAMES     timeframe,       // 주기
   datetime            time,            // 시간
   bool                exact=false      // 모드
   );

Parameters

symbol

[in]  금융상품의 심볼명. NULL은 현재 심볼을 의미합니다.

timeframe

[in]  주기. 이 값은 ENUM_TIMEFRAMES 열거값 중 하나일 수 있습니다. PERIOD_CURRENT은 현재 차트를 의미합니다.

time

[in]  검색할 시간 값.

exact=false

[in]  지정된 시간의 막대를 찾을 수 없는 경우 반환 값. exact=false인 경우, iBarShift는 가장 가까운 막대의 색인을 반환하며, 이 색인의 오픈 시간은 지정된 시간(time_open<time)보다 작습니다. 이러한 막대를 찾을 수 없으면(지정된 시간 이전의 내역) 함수는 -1을 반환합니다. exact=true인 경우, iBarShift는 가장 가까운 막대를 검색하지 않고 즉시 -1을 반환합니다.

반환 값

지정된 시간에 해당하는 막대의 색인. 지정된 시간에 해당하는 막대가 발견되지 않으면(내역에 공백이 있을 경우), 함수는 -1 또는 가장 가까운 막대의 색인을 반환합니다('정확한' 파라미터에 따라 다름).

예:

//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 날짜는 일요일입니다.
   datetime time=D'2002.04.25 12:00';
   string symbol="GBPUSD";
   ENUM_TIMEFRAMES tf=PERIOD_H1;
   bool exact=false;
//--- 지정된 시간에 막대가 없으면 iBarShift가 가장 가까운 막대의 색인을 반환합니다.
   int bar_index=iBarShift(symbol,tf,time,exact);
//--- iBarShift() 호출 후 오류 코드를 확인하십시오.
   int error=GetLastError();
   if(error!=0)
     {
      PrintFormat("iBarShift(): GetLastError=%d - The requested date %s "+
                  "for %s %s는 가용한 내역에서 찾을 수 없습니다",
                  error,TimeToString(time),symbol,EnumToString(tf));
      return;
     }
//--- iBarShift() 기능이 성공적으로 실행되었습니다. exact=false에 대한 결과를 반환합니다.
   PrintFormat("1. %s %s %s(%s): bar index is %d (exact=%s)",
               symbol,EnumToString(tf),TimeToString(time),
               DayOfWeek(time),bar_index,string(exact));
   datetime bar_time=iTime(symbol,tf,bar_index);
   PrintFormat("Time of bar #%d is %s (%s)",
               bar_index,TimeToString(bar_time),DayOfWeek(bar_time));
//--- 지정된 시간으로 막대 색인을 요청합니다; 막대가 없으면 -1이 반환됩니다.
   exact=true;
   bar_index=iBarShift(symbol,tf,time,exact);
//--- iBarShift() 기능이 성공적으로 실행되었으며, exact=true에 대한 결과를 반환합니다.
   PrintFormat("2. %s %s %s (%s):bar index is %d (exact=%s)",
               symbol,EnumToString(tf),TimeToString(time)
               ,DayOfWeek(time),bar_index,string(exact));
  }
//+------------------------------------------------------------------+
//| 주의 요일 이름을 반환합니다                          |
//+------------------------------------------------------------------+
string DayOfWeek(const datetime time)
  {
   MqlDateTime dt;
   string day="";
   TimeToStruct(time,dt);
   switch(dt.day_of_week)
     {
      case 0: day=EnumToString(SUNDAY);
      break;
      case 1: day=EnumToString(MONDAY);
      break;
      case 2: day=EnumToString(TUESDAY);
      break;
      case 3: day=EnumToString(WEDNESDAY);
      break;
      case 4: day=EnumToString(THURSDAY);
      break;
      case 5: day=EnumToString(FRIDAY);
      break;
      default:day=EnumToString(SATURDAY);
      break;
     }
//---
   return day;
  }
//+------------------------------------------------------------------+
/* Execution result
   1. GBPUSD PERIOD_H1 2018.06.10 12:00(SUNDAY): bar index is 64 (exact=false)
   Time of bar #64 is 2018.06.08 23:00 (FRIDAY)
   2. GBPUSD PERIOD_H1 2018.06.10 12:00 (SUNDAY):bar index is -1 (exact=true)
*/