//+------------------------------------------------------------------+
//| CustomSymbolSetSessionQuote.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 SESSION_0_FROM D'1970.01.01 00:15:00' // 세션 0 시작 시간
#define SESSION_0_TO D'1970.01.01 11:59:00' // 세션 0 종료 시간
#define SESSION_1_FROM D'1970.01.01 12:15:00' // 세션 1 시작 시간
#define SESSION_1_TO D'1970.01.01 23:59:00' // 세션 1 종료 시간
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 사용자 정의 심볼을 생성할 때 오류 코드를 가져옵니다.
int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN);
//--- 오류 코드가 0(심볼 생성 성공)도 아니고 5304(심볼이 이미 생성됨)도 아닌 경우 - 그대로 둠
if(create!=0 && create!=5304)
return;
//--- 기본 심볼과 세션 인덱스가 포함된 헤더를 인쇄하고
//--- 월요일부터 금요일까지 요일별 루프에서 각 쿼트 세션의 시작 및 종료 시간을 저널에 출력합니다.
for(int session=0; session<2; session++)
{
PrintFormat("Quote session %d of '%s' symbol from which the custom '%s' was created", session, CUSTOM_SYMBOL_ORIGIN, CUSTOM_SYMBOL_NAME);
for(int day_of_week=MONDAY; day_of_week<SATURDAY; day_of_week++)
SymbolInfoSessionQuotePrint(CUSTOM_SYMBOL_ORIGIN, (ENUM_DAY_OF_WEEK)day_of_week, session);
}
//--- 두 세션의 루프에서
bool res=true;
for(int session=0; session<2; session++)
{
datetime from = SESSION_0_FROM;
datetime to = SESSION_0_TO;
if(session>0)
{
from = SESSION_1_FROM;
to = SESSION_1_TO;
}
//--- 각 요일의 맞춤 심볼에 대한 쿼트 세션 시간 설정
ResetLastError();
for(int day_of_week=MONDAY; day_of_week<SATURDAY; day_of_week++)
res &=CustomSymbolSetSessionQuote(CUSTOM_SYMBOL_NAME, (ENUM_DAY_OF_WEEK)day_of_week, session, from, to);
}
//--- 세션 설정 시 오류가 발생한 경우 저널에 해당 메시지를 표시합니다.
if(!res)
Print("CustomSymbolSetSessionQuote() failed. Error ", GetLastError());
//--- 사용자 정의 심볼과 세션 인덱스로 헤더를 인쇄하고
//--- 월요일부터 금요일까지 요일별 루프에서 각 쿼트 세션의 시작 및 종료 시간을 저널에 출력합니다.
for(int session=0; session<2; session++)
{
PrintFormat("Quote session %d of custom symbol '%s' based on '%s'", session, CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_ORIGIN);
for(int day_of_week=MONDAY; day_of_week<SATURDAY; day_of_week++)
SymbolInfoSessionQuotePrint(CUSTOM_SYMBOL_NAME, (ENUM_DAY_OF_WEEK)day_of_week, session);
}
//--- 차트 주석에 스크립트 종료 키에 대한 힌트를 표시합니다.
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)
{
if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME);
break;
}
}
//--- 종료하기 전에 차트를 삭제
Comment("");
/*
결과:
Quote session 0 of 'EURUSD' symbol from which the custom 'EURUSD.C' was created
- Monday 00:15 - 23:55
- Tuesday 00:15 - 23:55
- Wednesday 00:15 - 23:55
- Thursday 00:15 - 23:55
- Friday 00:15 - 23:55
Quote session 1 of 'EURUSD' symbol from which the custom 'EURUSD.C' was created
- Monday Session not set
- Tuesday Session not set
- Wednesday Session not set
- Thursday Session not set
- Friday Session not set
Quote session 0 of custom symbol 'EURUSD.C' based on 'EURUSD'
- Monday 00:15 - 11:59
- Tuesday 00:15 - 11:59
- Wednesday 00:15 - 11:59
- Thursday 00:15 - 11:59
- Friday 00:15 - 11:59
Quote session 1 of custom symbol 'EURUSD.C' based on 'EURUSD'
- Monday 12:15 - 23:59
- Tuesday 12:15 - 23:59
- Wednesday 12:15 - 23:59
- Thursday 12:15 - 23:59
- Friday 12:15 - 23:59
*/
}
//+------------------------------------------------------------------+
//| Create a custom symbol, return an error code |
//+------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_name, const string symbol_path, const string symbol_origin=NULL)
{
//--- 사용자 정의 심볼의 기반이 될 심볼명을 정의합니다.
string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);
//--- 사용자 정의 심볼 생성에 실패했고 오류 5304가 아닌 경우 저널에 이를 보고합니다.
ResetLastError();
int error=0;
if(!CustomSymbolCreate(symbol_name, symbol_path, origin))
{
error=GetLastError();
if(error!=5304)
PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d", symbol_name, symbol_path, origin, error);
}
//--- 성공
return(error);
}
//+------------------------------------------------------------------+
//| 사용자 정의 심볼 삭제 |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
{
//--- 종합시세 창에서 심볼 숨기기
ResetLastError();
if(!SymbolSelect(symbol_name, false))
{
PrintFormat("SymbolSelect(%s, false) failed. Error %d", GetLastError());
return(false);
}
//--- 사용자 정의 심볼 삭제에 실패한 경우 이를 저널에 보고하고 'false'를 반환합니다.
ResetLastError();
if(!CustomSymbolDelete(symbol_name))
{
PrintFormat("CustomSymbolDelete(%s) failed. Error %d", symbol_name, GetLastError());
return(false);
}
//--- 성공
return(true);
}
//+------------------------------------------------------------------+
//| 지정된 쿼트 세션의 시작 및 종료 시간 보내기 |
//| 지정된 심볼 및 요일에 대한 저널 |
//+------------------------------------------------------------------+
void SymbolInfoSessionQuotePrint(const string symbol, const ENUM_DAY_OF_WEEK day_of_week, const uint session_index)
{
//--- 쿼트 세션의 시작과 끝을 기록하는 변수를 선언합니다.
datetime date_from; // session start time
datetime date_to; // session end time
//--- 열거형 상수로부터 요일 이름을 생성합니다.
string week_day=EnumToString(day_of_week);
if(week_day.Lower())
week_day.SetChar(0, ushort(week_day.GetChar(0)-32));
//--- 심볼 및 요일별로 쿼트 세션에서 데이터를 가져옵니다.
if(!SymbolInfoSessionQuote(symbol, day_of_week, session_index, date_from, date_to))
{
int err=GetLastError();
string message=(err==4307 ? StringFormat("- %-10s Session not set", week_day) :
StringFormat("SymbolInfoSessionQuote(%s, %s, session %d) failed. Error %d", symbol, week_day, session_index, GetLastError()));
Print(message);
return;
}
//--- 지정된 쿼트 세션에 대한 데이터를 저널로 보냅니다.
PrintFormat("- %-10s %s - %s", week_day, TimeToString(date_from, TIME_MINUTES), TimeToString(date_to, TIME_MINUTES));
}
|