Download MetaTrader 5

Global variables

To add comments, please log in or register
James Occhiogrosso
18
James Occhiogrosso  

Hi:

In an indicator I've been developing, I need to save 2 variables on changing chart period for use in the new chart. The code below is truncated from the indicator and uses only the OnInit and OnDeInit events. I stripped and simplified it from the main file so I could troubleshoot it. Basically, It sets a couple values in OnInit, Changes them in OnDeInit and the tries to recover them in changing the chart period. Obviously, I making a wrong assumption somewhere, or global values just aren't saved on a chart period change. Code is shown below. Appreciate any help.

//+------------------------------------------------------------------+
//|                                       test_globals_on_USDCHF.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

// Horiz lines and sound alerts
extern color TPAlertColor = clrLimeGreen;
extern color SLAlertColor = clrRed;
extern string TPAlertSound = "Alert" ;
extern string SLAlertSound = "Uhoh" ;

double PositionOpenPrice = 0.97101 ;
double TPInitPrice = 0.97201;
double SLInitPrice = 0.97001;
string GLBL_TP = "" ;
string GLBL_SL = "" ;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
   {

   // Establish unique (to this position) global var names
   GLBL_TP = Symbol() + "_TP_"+ DoubleToString(PositionOpenPrice, Digits) ;
   GLBL_SL = Symbol() + "_SL_"+ DoubleToString(PositionOpenPrice, Digits) ;
   Alert("Global Alert names are " + GLBL_TP + " and " + GLBL_SL) ;
   TPInitPrice = GlobalVariableGet(GLBL_TP) ;
   SLInitPrice = GlobalVariableGet(GLBL_SL) ;
   Alert("Reading globals - TP=" + TPInitPrice + " SL=" + SLInitPrice) ;

   return(INIT_SUCCEEDED);
   }

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
   {
   // global vars are set to new pricing
   TPInitPrice = 0.98201;
   SLInitPrice = 0.98001;
   GlobalVariableSet("GLBL_TP",TPInitPrice);
   GlobalVariableSet("GLBL_SL",SLInitPrice);     

// Reading vars is OK here

Alert("Global vars = " + GlobalVariablesTotal() + " DeInit reason code = " + UninitializeReason()
            + " TPAlert=" + GlobalVariableGet("GLBL_TP")
            + " SLAlert=" + GlobalVariableGet("GLBL_SL")+ " Period was = " + Period());
   
   }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   return(rates_total);
  }
 

 

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Stuart Browne
6584
Stuart Browne  

Your problem is in the DeInit. You're attempting to set the global variable using strings "GLBL_TP" and "GLBL_SL". You should be calling it with variable names GLBL_TP and GLBL_SL. So just remove the quotes (" ")

(You also have the same issue in your alert)

James Occhiogrosso
18
James Occhiogrosso  
Stuart Browne:

Your problem is in the DeInit. You're attempting to set the global variable using strings "GLBL_TP" and "GLBL_SL". You should be calling it with variable names GLBL_TP and GLBL_SL. So just remove the quotes (" ")

(You also have the same issue in your alert)

Stuart:  --- Thanks much!!  I'm relatively new to MQL5 programming, but an experienced programmer in many other languages. I should have seen this but there is nothing like another set of eyes.  I haven't tried it yet, but I can immediately see you are right! 

Jim

Stuart Browne
6584
Stuart Browne  
James Occhiogrosso:

Stuart:  --- Thanks much!!  I'm relatively new to MQL5 programming, but an experienced programmer in many other languages. I should have seen this but there is nothing like another set of eyes.  I haven't tried it yet, but I can immediately see you are right! 

Jim


No worries Jim. Happens to all of us at some time mate. The simplest mistakes are the hardest to find! :)

James Occhiogrosso
18
James Occhiogrosso  

Hey Stuart

How right you are!! Got it working this am. Now that I have isolated the issue, I can apply it to OnDeInit() in the indicator I developed, which has turned out to be quite handy.

One of the things that was snagging me is that GlobalVariableSet() creates the global if it does not exist. So, the names in quotes got saved as empty vars, and the alert correctly showed 2 vars existing, sending me on a run-around trying to figure out why it wasn't reading the vars correctly.  It actually was, but both contained zero.

Isn't programming fun?? --- As they say, "Been There, Done That!"  FYI, back in the "olden 1995ish days" of early object oriented programming, I wrote several books and a library about Clipper programming (a C'-like language). (Google my name).

I assume (by your use of "mate") you are located on other side of the world from me. (AU, NZ). I'm in SW Florida, USA, land of perpetual summer and tons of visitors.

Warm regards, mate.............. Jim

To add comments, please log in or register