CustomRatesReplace

지정된 시간 간격 내에 사용자 지정 심볼의 가격 내역을 MqlRates 유형 배열의 데이터로 완전히 대체합니다.

int  CustomRatesReplace(
   const string     symbol,             // 심볼 이름
   datetime         from,               // 시작일
   datetime         to,                 // 종료일
   const MqlRates&  rates[],            // 사용자 지정 심볼에 적용할 데이터의 배열
   uint             count=WHOLE_ARRAY   // 사용할 rates[] 배열 요소의 수
   );

매개변수

심볼

[in]  사용자 지정 심볼 이름.

from

[in]  지정된 범위 내에서 가격 내역에 있는 첫 번째 막대의 업데이트 시간.

to

[in]  지정된 범위 내에서 가격 내역에 있는 마지막 막대의 업데이트 시간.

rates[]

[in]   M1에 대한 MqlRates 유형 내역 데이터의 배열.

count=WHOLE_ARRAY

[in]  교체를 위해 사용할 rates[] 배열 요소의 수. WHOLE_ARRAY 모든 rates[] 배열 요소를 교체용으로 사용해야 함을 의미합니다.

값 반환

오류가 발생한 경우 업데이트된 수 또는 -1.

주의

rates[] 배열의 막대가 지정된 범위를 벗어나면 무시됩니다. 그러한 막대가 이미 가격 내역에 존재하고 주어진 범위에 들어간 경우, 대체됩니다. 지정된 범위를 벗어난 현재 가격 내역의 다른 모든 막대는 변경되지 않습니다. rates[] 배열 데이터는 OHLC 가격에 대해 정확해야 하며 막대 오프닝 시간은 M1 시간 프레임에 해당되어야 합니다.

 

예:

//+------------------------------------------------------------------+
//|                                           CustomRatesReplace.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   CUSTOM_SYMBOL_NAME     Symbol()+".C"     // 사용자 지정 심볼명
#define   CUSTOM_SYMBOL_PATH     "Forex"           // 심볼을 생성할 그룹 명
#define   CUSTOM_SYMBOL_ORIGIN   Symbol()          // 사용자 정의 심볼의 기반이 되는 심볼 명
 
#define   DATARATES_COUNT        4                 // 저널에 보내진 바의 개수
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 사용자 정의 심볼을 생성할 때 오류 코드를 가져옵니다.
   int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAMECUSTOM_SYMBOL_PATHCUSTOM_SYMBOL_ORIGIN);
   
//--- 오류 코드가 0(심볼 생성 성공)도 아니고 5304(심볼이 이미 생성됨)도 아닌 경우 - 그대로 둠
   if(create!=0 && create!=5304)
      return;
 
//--- 표준 심볼 바의 개수를 가져옵니다.
   int bars=Bars(CUSTOM_SYMBOL_ORIGINPERIOD_M1);
      
//--- 표준 심볼 분 시간대의 모든 바의 데이터를 MqlRates 배열로 가져옵니다.
   MqlRates rates[]={};
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_ORIGINPERIOD_M10barsrates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d"CUSTOM_SYMBOL_ORIGINbarsGetLastError());
      return;
     }
 
//--- 복사된 데이터를 사용자 정의 심볼의 분 히스토리로 설정합니다.
   ResetLastError();
   if(CustomRatesUpdate(CUSTOM_SYMBOL_NAMErates)<0)
     {
      PrintFormat("CustomRatesUpdate(%s) failed. Error %d"CUSTOM_SYMBOL_NAMEGetLastError());
      return;
     }
     
//--- 과거 데이터를 업데이트한 후 사용자 정의 심볼의 바의 개수를 가져옵니다.
   bars=Bars(CUSTOM_SYMBOL_NAMEPERIOD_M1);
   
//--- 사용자 정의 심볼 분 시간대의 모든 바의 데이터를 MqlRates 배열로 가져옵니다.
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_NAMEPERIOD_M10barsrates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d"CUSTOM_SYMBOL_NAMEbarsGetLastError());
      return;
     }
 
//--- 저널에 있는 사용자 정의 심볼 분 히스토리의 마지막 DATARATES_COUNT 바를 출력합니다.
   int digits=(int)SymbolInfoInteger(CUSTOM_SYMBOL_NAMESYMBOL_DIGITS);
   PrintFormat("Last %d bars of the custom symbol's minute history:"DATARATES_COUNT);
   ArrayPrint(ratesdigitsNULLbars-DATARATES_COUNTDATARATES_COUNT);
   
//--- 사용자 정의 기호 분 히스토리에서 어미에서 두 번째 데이터 바를 변경합니다.
   datetime time_fromrates[bars-3].time;
   datetime time_to  = rates[bars-2].time;
   
//--- 끝에서 두 번째 바의 모든 가격을 'rates' 배열에 있는 이 바의 시가와 동일하게 만듭니다.
   rates[bars-3].high=rates[bars-3].open;
   rates[bars-3].low=rates[bars-3].open;
   rates[bars-3].close=rates[bars-3].open;
   
   rates[bars-2].high=rates[bars-2].open;
   rates[bars-2].low=rates[bars-2].open;
   rates[bars-2].close=rates[bars-2].open;
   
//--- 기존 바를 수정된 'rates' 배열의 데이터로 바꿉니다.
   ResetLastError();
   int replaced=CustomRatesUpdate(CUSTOM_SYMBOL_NAMErates);
   if(replaced<0)
     {
      PrintFormat("CustomRatesUpdate(%s) failed. Error %d"CUSTOM_SYMBOL_NAMEGetLastError());
      return;
     }
     
//--- 과거 데이터의 두 바를 변경한 후 사용자 정의 심볼 바의 개수를 다시 가져옵니다.
   bars=Bars(CUSTOM_SYMBOL_NAMEPERIOD_M1);
   
//--- 사용자 정의 심볼 분 �Q 주기의 모든 바의 데이터를 다시 가져옵니다.
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_NAMEPERIOD_M10barsrates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d"CUSTOM_SYMBOL_NAMEbarsGetLastError());
      return;
     }
 
//--- 저널에 업데이트된 사용자 정의 바 분 히스토리의 마지막 DATARATES_COUNT 바를 인쇄합니다.
   PrintFormat("\nLast %d bars after applying CustomRatesUpdate() with %d replaced bars:"DATARATES_COUNTreplaced);
   ArrayPrint(ratesdigitsNULLbars-DATARATES_COUNTDATARATES_COUNT);
     
//--- 차트 주석에 스크립트 종료 키에 대한 힌트를 표시합니다.
   Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit"CUSTOM_SYMBOL_NAME));
//--- Esc 또는 Del 키를 눌러 무한 루프를 종료할 때까지 기다립니다.
   while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0)
     {
      Sleep(16);
//--- Del 키를 누르면 생성된 사용자 정의 심볼 및 해당 데이터가 삭제됩니다.
      if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0)
        {
//--- 바 데이터 삭제
         int deleted=CustomRatesDelete(CUSTOM_SYMBOL_NAME0LONG_MAX);
         if(deleted>0)
            PrintFormat("%d history bars of the custom symbol '%s' were successfully deleted"deletedCUSTOM_SYMBOL_NAME);
         
         //--- 틱 데이터 삭제
         deleted=CustomTicksDelete(CUSTOM_SYMBOL_NAME0LONG_MAX);
         if(deleted>0)
            PrintFormat("%d history ticks of the custom symbol '%s' were successfully deleted"deletedCUSTOM_SYMBOL_NAME);
         
         //--- 심볼 삭제
         if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
            PrintFormat("Custom symbol '%s' deleted successfully"CUSTOM_SYMBOL_NAME);
         break;
        }
     }
//--- 종료하기 전에 차트를 삭제
   Comment("");
   /*
   결과:
   사용자 정의 심볼 분 히스토리의 마지막 4개 바:
                    [time]  [open]  [high]   [low] [close] [tick_volume] [spread] [real_volume]
   [0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390            16        1             0
   [1] 2024.07.29 13:38:00 1.08389 1.08400 1.08389 1.08398            35        1             0
   [2] 2024.07.29 13:39:00 1.08398 1.08410 1.08394 1.08410            29        1             0
   [3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414            14        1             0
   
  250820개의 바에 대체된 CustomRatesUpdate()를 적용한 후 마지막 4개 바:
                    [time]  [open]  [high]   [low] [close] [tick_volume] [spread] [real_volume]
   [0] 2024.07.29 13:37:00 1.08394 1.08396 1.08388 1.08390            16        1             0
   [1] 2024.07.29 13:38:00 1.08389 1.08389 1.08389 1.08389            35        1             0
   [2] 2024.07.29 13:39:00 1.08398 1.08398 1.08398 1.08398            29        1             0
   [3] 2024.07.29 13:40:00 1.08409 1.08414 1.08408 1.08414            14        1             0
   */
  }
//+------------------------------------------------------------------+
//| Create a custom symbol, return an error code                     |
//+------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_nameconst string symbol_pathconst string symbol_origin=NULL)
  {
//--- 사용자 정의 심볼의 기반이 될 심볼명을 정의합니다.
   string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);
   
//--- 사용자 정의 심볼 생성에 실패했고 오류 5304가 아닌 경우 저널에 이를 보고합니다.
   ResetLastError();
   int error=0;
   if(!CustomSymbolCreate(symbol_namesymbol_pathorigin))
     {
      error=GetLastError();
      if(error!=5304)
         PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d"symbol_namesymbol_pathoriginerror);
     }
//--- 성공
   return(error);
  }
//+------------------------------------------------------------------+
//| Remove a custom symbol                                           |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
  {
//--- 종합시세 창에서 심볼 숨기기
   ResetLastError();
   if(!SymbolSelect(symbol_namefalse))
     {
      PrintFormat("SymbolSelect(%s, false) failed. Error %d"GetLastError());
      return(false);
     }
      
//--- 사용자 정의 심볼 삭제에 실패한 경우 이를 저널에 보고하고 'false'를 반환합니다.
   ResetLastError();
   if(!CustomSymbolDelete(symbol_name))
     {
      PrintFormat("CustomSymbolDelete(%s) failed. Error %d"symbol_nameGetLastError());
      return(false);
     }
//--- 성공
   return(true);
  }

 

추가 참조

CustomRatesDelete, CustomRatesUpdate, CopyRates