#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define GV_NAME "TestGlobalVariableGet"
#define GV_VALUE 1.23
//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 클라이언트 터미널의 전역 변수인 GV_NAME에 값을 설정합니다.
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE))
return;
//--- GV_NAME이라는 이름의 클라이언트 터미널 전역 변수의 실제 값을 가져옵니다.
//--- GlobalVariableGet을 호출하는 첫 번째 형식을 사용할 때 0은 오류 신호이기 때문입니다
//--- 결과를 읽는 동안 마지막 오류 코드를 분석해야 합니다.
double dvalue=GlobalVariableGet(GV_NAME);
if(dvalue==0 && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- 획득한 결과를 보여줍니다
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f", dvalue);
//--- 클라이언트 터미널의 전역 변수인 GV_NAME에 0 값을 설정합니다.
if(!GlobalVariableSetValue(GV_NAME, 0))
return;
//--- 호출의 첫 번째 형식을 사용하여 GV_NAME이라는 이름의 클라이언트 터미널 전역 변수의 부울 값을 얻습니다.
bool bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- 획득한 결과를 보여줍니다
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- 클라이언트 터미널의 전역 변수인 GV_NAME에 0이 아닌 값을 설정합니다.
if(!GlobalVariableSetValue(GV_NAME, GV_VALUE))
return;
//--- GV_NAME이라는 클라이언트 터미널 전역 변수의 부울 값을 다시 읽습니다.
bvalue=GlobalVariableGet(GV_NAME);
if(!bvalue && GetLastError()!=0)
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- 획득한 결과를 보여줍니다
PrintFormat("The first form of the GlobalVariableGet() function call returned the value %.2f with type bool as %s", bvalue, (string)bvalue);
//--- GlobalVariableGet 호출의 두 번째 형식을 사용하여 이름이 GV_NAME인 클라이언트 터미널 전역 변수의 실제 값을 가져옵니다.
if(!GlobalVariableGet(GV_NAME, dvalue))
{
Print("GlobalVariableGet() failed. Error ", GetLastError());
return;
}
//--- 결과 실수값을 long형 정수로 변환하여 결과를 보여줍니다.
long lvalue=(long)dvalue;
PrintFormat("The second form of the GlobalVariableGet() function call returned the value %.2f with type long as %I64d", dvalue, lvalue);
//--- GV_NAME이라는 클라이언트 터미널 전역 변수를 삭제합니다.
if(!GlobalVariableDel(GV_NAME))
{
Print("GlobalVariableDel() failed. Error ", GetLastError());
}
/*
결과:
The first form of the GlobalVariableGet() function call returned the value 1.23
The first form of the GlobalVariableGet() function call returned the value 0.00 with type bool as false
The first form of the GlobalVariableGet() function call returned the value 1.00 with type bool as true
The second form of the GlobalVariableGet() function call returned the value 123.00 with type long as 123
*/
}
//+------------------------------------------------------------------+
//| 터미널 전역 변수에 값을 설정합니다. |
//| 변수가 없으면 생성합니다. |
//+------------------------------------------------------------------+
bool GlobalVariableSetValue(const string gv_name, const double value)
{
if(GlobalVariableSet(gv_name, value)==0)
{
Print("GlobalVariableSet() failed. Error ", GetLastError());
return(false);
}
return(true);
}
|