GlobalVariableSetOnCondition

Global değişkenin geçerli değeri, üçüncü parametre olarak belirtilen check_value değerine eşitse, değişken için yeni değer ayarlar. Eğer hiç global değişken yoksa, fonksiyon bir hata oluşturur ERR_GLOBALVARIABLE_NOT_FOUND (4501) ve 'false' dönüşü yapar.

bool  GlobalVariableSetOnCondition(
   string  name,            // Global değişkenin ismi
   double  value,           // Koşul "true" ise, değişkenin yeni değeri
   double  check_value      // Kontrol değeri koşulu
   );

Parametreler

name

[in]  Global değişkenin ismi.

value

[in]  Yeni değer.

check_value

[in]   Global değişkenin mevcut değerini kontrol etmek için değer.

Dönüş değeri

Başarı durumunda 'true' değerine, aksi durumda 'false' değerine dönüş yapacaktır. Hata hakkında daha detaylı bilgi almak için GetLastError() fonksiyonunu çağırın. Eğer mevcut değer, check_value değerinden küçükse, fonksiyon 'false' dönüşü yapar.

Not

Fonksiyon bir global değişkene atomik erişim sağlar, bu yüzden müşteri terminali içinde aynı anda birden çok Uzman Danışman arasında mutex düzenlemek için kullanılabilir.

 

Örnek:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property description   "GlobalVariableSetOnCondition fonksiyonunun işlevselliğini test etmek için,"
#property description   "Uzman Danışmanı aynı anda birkaç grafik üzerinde çalıştırmak gerekir."
 
#define   EXP_NAME      StringSubstr(__FILE__0StringLen(__FILE__)-4)   // program adı
#define   MUTEX         EXP_NAME+"_MUTEX"                                  // mutex global değişken adı
#define   DELAY         5000                                               // Uzman Danışman emülasyonunun milisaniye süresi
 
input long  InpExpMagic =  0; /* Expert magic number  */ // Uzman Danışman kimliği. 0 ise grafik kimliği kullanılır
 
union LongDouble              // double'dan long değerleri yazmak ve almak için birlik
  {
   long   lvalue;             // long türünde değer
   double dvalue;             // double türünde değer
  };
  
//--- Uzman Danışman global değişkenleri
long  ExtExpMagic;            // Uzman Danışman kimliği
ulong ExtStart;               // Uzman Danışman “hesaplamalarının” başladığı an
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- sıfır değeri belirtilmişse grafik kimliğini Uzman Danışman kimliğine ata
   ExtExpMagic=(InpExpMagic==0 ? ChartID() : InpExpMagic);
 
//--- mutex oluştur, hata varsa başlatma hatası geri döndür
   if(!MutexCreate())
      return(INIT_FAILED);
      
//--- başarılı başlatma
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- mutex Uzman Danışman tarafından yakalanmışsa, mutex'i serbest bırak
   if(MutexGetExpertID()==ExtExpMagic)
      MutexRelease();
      
//--- temizle
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- mutex yoksa oluştur, hata varsa bir sonraki tike ilerle
   if(!GlobalVariableCheck(MUTEX) && !MutexCreate())
      return;
      
//--- terminal global değişkeninde ayarlanan uzman kimliğini al
   long magic=MutexGetExpertID();
 
//--- kimlik Uzman Danışmana aitse, çalışmasını simüle et
   if(magic==ExtExpMagic)
     {
      //--- Uzman Danışman “çalışması” tamamlandığında, mutex'i serbest bırak
      if(EAProgressHandler(ExtStart))
         MutexRelease();
      return;
     }
//--- başka bir Uzman Danışman tarafından yakalanmış mutex
   else
     {
      //--- mutex zaten serbest bırakılmışsa
      if(magic==0)
        {
         //--- mutex'i yakala ve çalışma erişim zamanını hatırla
         if(MutexOccupy())
           {
            PrintFormat("%s: Mutex is occupied by %s",Symbol(), ExpertIDDescription());
            ExtStart=GetTickCount64();
           }
         return;
        }
      //--- mutex hala başka bir Uzman Danışman tarafından tutuluyor - çalışma yasak
      else
         return;
     }
   /*
   EURUSD ve AUDUSD grafiklerinde iki özdeş Uzman Danışman çalıştırmanın sonucu:
   EURUSDMutex is occupied by Expert 133829812107724569
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 end
   EURUSDMutex is occupied by Expert 133829812107724569
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 end
   AUDUSDMutex is occupied by Expert 128968168951083984
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 end
   AUDUSDMutex is occupied by Expert 128968168951083984
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 end
   EURUSDMutex is occupied by Expert 133829812107724569
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 end
   EURUSDMutex is occupied by Expert 133829812107724569
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 end
   AUDUSDMutex is occupied by Expert 128968168951083984
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 next tick
   AUDUSDExpert 128968168951083984 end
   EURUSDMutex is occupied by Expert 133829812107724569
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 next tick
   EURUSDExpert 133829812107724569 end
   
   mutex'i yakalayan ilk Uzman Danışmanın, tikin ilk geldiği Uzman Danışman olduğunu görüyoruz
   çalışma döngüsünün tamamlanmasından sonra, eğer tik burada tekrar önce gelirse,
   aynı Uzman Danışman mutex'i tekrar yakalar
   */
  }
//+------------------------------------------------------------------+
//| Uzman Danışman çalışma işleyicisi emülatörü                      |
//+------------------------------------------------------------------+
bool EAProgressHandler(ulong start)
  {
   //--- işleyicinin çalışmaya başlamasından bu yana belirtilen süre geçtiyse
   if(GetTickCount64()-start>=DELAY)
     {
      //--- işleyicinin tamamlandığını günlüğe raporla,
      //--- işleyici için yeni bir başlangıç zamanı ayarla ve ‘true’ geri döndür
      PrintFormat("%s: %s end"Symbol(), ExpertIDDescription());
      start=GetTickCount64();
      return(true);
     }
   //--- Uzman Danışman hesaplama emülatörünün çalışma süresi henüz tamamlanmadı -
   //--- bir sonraki tiki günlüğe raporla
   else
     {
      PrintFormat("%s: %s next tick"Symbol(), ExpertIDDescription());
     }
//--- çalışma süresi henüz bitmedi - ‘false’ geri döndür
   return(false);
  }
//+------------------------------------------------------------------+
//| Mutex oluştur, kilitli duruma ayarla                             |
//+------------------------------------------------------------------+
bool MutexCreate(void)
  {
   if(!GlobalVariableCheck(MUTEX))
     {
      LongDouble magic={};
      magic.lvalue=ExtExpMagic;
      ResetLastError();
      if(!GlobalVariableSet(MUTEXmagic.dvalue))
        {
         PrintFormat("%s: GlobalVariableSet() failed. Error %d",__FUNCTION__GetLastError());
         return(false);
        }
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Mutex'i yakala                                                   |
//+------------------------------------------------------------------+
bool MutexOccupy(void)
  {
   if(!GlobalVariableCheck(MUTEX))
     {
      PrintFormat("%s: Error! Mutex is missing. First create it with MutexCreate()",__FUNCTION__);
      return(false);
     }
   LongDouble magic={};
   magic.lvalue=ExtExpMagic;
   ResetLastError();
   if(!GlobalVariableSetOnCondition(MUTEXmagic.dvalue0))
     {
      PrintFormat("%s: GlobalVariableSetOnCondition() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Mutex'i serbest bırak                                            |
//+------------------------------------------------------------------+
bool MutexRelease(void)
  {
   if(!GlobalVariableCheck(MUTEX))
     {
      PrintFormat("%s: Error! Mutex is missing. First create it with MutexCreate()",__FUNCTION__);
      return(false);
     }
   LongDouble magic={};
   magic.lvalue=ExtExpMagic;
   ResetLastError();
   if(!GlobalVariableSetOnCondition(MUTEX0magic.dvalue))
     {
      PrintFormat("%s: GlobalVariableSetOnCondition() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   return(true);
  }
//+------------------------------------------------------------------+
//| Mutex değerini geri döndür                                       |
//+------------------------------------------------------------------+
long MutexGetExpertID(void)
  {
   LongDouble magic={};
   ResetLastError();
   if(!GlobalVariableGet(MUTEXmagic.dvalue))
     {
      PrintFormat("%s: GlobalVariableGet() failed. Error %d",__FUNCTION__GetLastError());
      return(WRONG_VALUE);
     }
   return(magic.lvalue);
  }
//+------------------------------------------------------------------+
//| Uzman Danışman kimliğini dizge olarak geri döndür               |
//+------------------------------------------------------------------+
string ExpertIDDescription(void)
  {
   return("Expert "+(string)ExtExpMagic);
  }