#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;
//--- 1番目の呼び出し形式を使用して、クライアント端末グローバル変数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*100.0))
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呼び小田氏の2番目の形式を用いてクライアント端末のグローバル変数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);
}
|