DYO: Global Variables


The power of the Alert Objects is extended through the use of Global Variables.  The Global Variables (GV) are an array of 256 variables where study and alert values can be written and read.  Our short hand notation for a specific Global Variable will be the variable number in square brackets.

Writing Study Values:

The study property forms have a Global column and a spinner box for each of the lines drawn by the study.  In this example for Moving Averages, the 1st Average value is being saved in Global Variable 1, [1].  The 2nd Average value has been assigned to [2].

The Spread value stores in [0], but [0] will be treated as the throw away place.  Although [0] is a valid variable, the selection of 0 in the Global Variable spinner should be thought of as 'not saved.'

Plotting Studies from Another Chart:

A Moving Average can plot a Global Variable as the average's Data Point.   This is one way to transfer a study from another chart.   Write Global Variables for the study on the other chart as shown in the previous section.   Then plot the GVs using an Exponential Average study with parameters set to 1 as shown in this parameter form.

It is important to use the Exponential formula.  Use parameters of 1 to plot the study from the other chart.   In the following example the top chart is a 10 minute chart with 2 moving averages that store values in [1] and [2].  The 2nd chart is a 2 minute chart that plots the averages from the 10 minute chart.   The Average's parameter form for the 2 minute chart is shown above.  The Data Points plotted are Global Value | Variable 1 and Global Value | Variable 2. 

The two charts open and calculate independently of each other.  The 10 minute chart was opened at 15:20 and the two averages had values of 1090 (Blue) and 1088 (Red).  These are the two values last written to [1] and [2].   Then the 2 minute chart opened, calculated, found constant values in [1] and [2] and plotted these as the flat Cyan and Green lines on the left side of the Yellow vertical line.   Live data updated the chart on the right side of the Yellow line.

As new ticks arrived, both charts updated tick by tick and recalculated their averages tick by tick.   The Cyan and Green lines on the 2 minute chart are plotting the 10 minute's Blue and Red averages.

Click this link for to see another example of a Study Transfer.

Information From Other Charts:

There is popular interest in writing study and alert values from one chart to Global Variables, so they can be used as part of the signal logic on another chart.   Possibly the charts are different symbols, or the same symbol but different time frames.   Global Variables make it easy to pass study and alert data among charts.

However, there is the following characteristic that needs to be understood and tolerated.  While the information can be shared during real-time operation, there is a problem in trying to show previous results or Alert graphics when a chart is reopened or a workspace is redisplayed.   The issue is that each chart does its calculation independently of other charts.   Chart A might need study data from Chart B that Chart B stores in Global Variables.    When Chart A opens and calculates across all of its bars, it will use whatever values are present in the Global Variables.  These GV values intended to be set by Chart B will be constant while Chart A calculates across all its bars.   Chart B might not even be open yet.

So, the clever interaction between Chart A and Chart B will work fine in real-time where each chart gets a chance to calculate tick by tick.  But it will not show correctly what happened in the past when either chart does a full recalculate across all bars.  

The second issue to be aware of is that the Windows operating system has a Z-order list for the child windows.  When a chart receives focus, it moves to the top of the Z-order list.   Execution for calculation will be according to the Z-order list, from top to bottom.  Thus, you may intend for Chart B to calculate before Chart A so the values being passed are updated in the Global Variables before they are needed.  However, you cannot control the way the charts will change positions on the Z-order list.   At times, Chart A will calculate ahead of Chart B and at other times Chart B will calculate first.   Hopefully this will not be much of a problem for the signals and systems you design, but it is a fact about what is really happening.

Reusing Global Variables:

A Global Variable (GV) is just a storage place in an array.  The GV reference is the index for the array.     Global means this array can be written and read by any study, alert or DYO.  A value or flag can be written to the array, and will remain stored there until the same global variable is written to again.  The life of the variable content can be short or long depending on what the designer needs.

Studies calculate when a trade tick is received.  The order of processing is from the top of the chart's object list to the bottom of the list.   For a DYO, the execution is from Line A through Line J.   Perhaps Line A writes a GV so Line B can use it.   Then the Line A value might never be needed again, so the GV written to by Line A can be reused by Line B or another line.

The only time a truly unique global variable is needed is when a value is to be shared with another chart.  Possibly a 2-minute chart writes a GV that a10-minute chart reads.  You do not know when the 2-min chart or the 10-min chart will execute, or which will execute first.  Therefore, the GV written by the 2-min chart must never be used by another chart, lest it be overwritten before the 10-min chart reads its value.

Global Variables Owned Privately by a Chart:

Global Variables [0] through [239] are public, and can be read or written by any study or draw tool.   There are times when a template needs private storage that will be unique to the chart the template occupies.  Therefore, Global Variables [240] through [255] are owned privately by each chart and can only be written and read by the studies and tools (including ESPL) that are owned by that chart.  GVs [240] through [255] cannot be used to transfer study values from one chart to another.

Global Value:

The values stored in the Global Variables can be used in the Alert Objects.   In this example, the panel A category is Function.  The Ave[#] selection will average the Number of adjacent Global Variables (GV)  beginning at the location of the Global Variable specified by the Read GV setting.   Assume the chart has a Bollinger Band study with the Upper Band value stored in [1] and the Middle Line value stored in [2].  Panel A returns the average of these two lines, and plots the average using the Blue curve marker.   The following chart shows the effect.

The notation of [#] will be used when a series of Global Variables are being used as a set.  In our example the function selected was Ave[#], and it will average a group of adjacent Global Variables.  The size of the set is specified by the Number field, which was 2 in the example.   The Read GV spinner identifies the location of the first Global Variable in the set.

                   

'Value' will refer to the GV at the location indicated by the Global Variable spinner value.
'Next' will refer to the next GV in sequence.   'Value' and 'Next' are adjacent GVs.
'3rd' will refer to the third GV in sequence.  'Value', 'Next' and '3rd' are adjacent GVs.

'Sum' calculates the summation for the values in a set of GVs.
'Ave' calculates the average across a set of GVs.
'Min' finds the minimum value in a set of GVs.
'Max' finds the maximum value in a set of GVs.

'#' will refer to the value in the Number field.
'[#]' will refer to a set of GVs, where # is the size of the set and is the value in the Number field.
If  [xx]  is used in the Number field, then the value from the GV reference is used as the number.

'Scale Low' is the value for the lowest scale shown on the chart.
'Scale Mid' is the midpoint between the chart's highest scale value and its lowest scale value.
'Range' is the chart's scale range which is the Scale High - Scale Low.

Modulo returns the remainder as an integer from a division.   Example:  5 Modulo 4 = 1
Round returns an integer.  Fractions above 0.5 round up, and fractions below 0.5 round down.
Trunc returns an integer.  The fraction part is dropped.
Frac return the fraction part of the expression.
Abs returns the absolute value of the expression.  Negative values are returned as positive values.
Exp returns the value of  e  raised to the power of X, where  e  is the base of the natural logarithms.
Power raises the Base (first parameter) to any power (2nd parameter).
Ln returns the natural logarithm of the expression.   Ln( e ) = 1
Log10 returns the log base 10 of the expression.

See the Historical Volatility example where the Panel B uses  'Value * # + Scale Low'   to plot the study.

Nearest[#] will return the number that is closest to Value from a set of GVs that follow Value's index.
Above[#] will return the closest number that Value is above from a set of GVs that follow Value's index.
Below[#] will return the closest number that Value is below from a set of GVs that follow Value's index.

Number Field:

A fixed numeric value can be entered.   The number field can reference a Global Variable value by enclosing the GV number in square brackets.  Example:  [5]  will get the number value from GV[5].  Permitted GV references are [0] through [255].

Bar values such as these can be referenced.

[$O],[$H],[$L],[$C] Bar Open, High, Low, Close
[$V],[$T] Bar Volume, Tick Count
[$N],[$R] Bar Net, Range

Click this link to see a Full List of the items that can be referenced in the Number Field.

Writing Alert Values:

The Alert calculations can be written to Global Variable just like Study Values.  The Alert result will be stored in the Global Variable identified by the Write Variable spinner box which is on the right side of the Marker frame.   In this example the Alert result is stored in [4].   

The Condition selection greatly affects the Alert result that is stored in the GV.   The selections 'A above B' through 'A AND Not B' generate a Boolean True or False result.  The Boolean result will store a True using a value of 255, and store a False result using a value of zero.  This result for the alert is stored in the GV identified by the Write Variable.

The next five Conditions, 'Test A, Do B' through 'Do A, Do B' will store the value from panel A in the Write Variable GV.   Panel A may be returning a value such as the Ave[#] in our example of a Boolean result.   Panel A value is stored in GV, and then panel B is evaluated.   Panel B may be a Global Action that is always done, or conditionally done depending on the result of panel A.   Panel B may use the value stored by Panel A because the GV was written before before panel B is evaluated.   Panel B has a separate Write GV spinner box that can be used to store the panel B value in a GV.

Global Action:

Value[#] = 0  will fill a set of GVs with zero.
Value[#] = True  will fill a set of GVs with the True flag.
Swap(Value,GV(#))  will exchange the contents of Value and the GV whose index in the Number.
'Shift Right[#]' will copy a set rightward. The last value in the original set is lost.
     Example set values:   6, 7, 8, 9   becomes   6, 6, 7, 8  when shifted right.
'Shift Left[#]' will copy a set leftward.   The first value in the original set is lost.  Similar to right shift, but moves values in the set in the other direction.   Example:   6, 7, 8, 9   becomes  7, 8, 9, 9
'Rotate Right[#]' is similar to Shift Right, with the exception that the last value is moved to the first position.
'Rotate Left[#]' is similar to Shift Left, with the exception that the first value is moved to the last position.

Global Flag:

                    

'Count True[#]' will count the number of GVs in a set that hold a Boolean True, ie. value is non-zero.
'Count False[#]' will count the number of Boolean False values in a set of GVs.   These two selections are very useful in establishing a Consensus among a set of signals stored in Global Variables.   See the Study Consensus example where these global flags are used to create the final Buy and Sell signals.

'AND[#]' will perform the Boolean AND operation between each member of the set of GVs.
   Example:  With  # = 3, and Read GV = 5, results in:   Flag = [5] AND [6] AND [7]
'AND[#] OR Next' will perform the AND on the set, and then the GV following the set is ORed.
   Example:  With  # = 3, and Read GV = 5, results in:   Flag = ( [5] AND [6] AND [7] ) OR [8]
'AND[#] OR AND[#]' will AND two equal size sets, one following the other, and then OR them.
   Example:  With  # = 2, and Read GV = 4, results in:   Flag = ( [4] AND [5] ) OR ( [6] AND [7] )

The OR operator sets mimic what has been described for the AND operator sets.
The Modulo operator returns the remainder as an integer from a division.   Example:  5 Modulo 4 = 1

Alert Sections Panel:

The Alert Object can control the color and text that shows in 7 sections at the bottom of a chart.  This is a great place to show the status of various study signals, and/or show a consensus signal.   See the Study Consensus example where these global flags are used to create the final Buy and Sell signals.

Start by selecting one of the Section selections in the Position dropdown box.   The background of the Section will use the Show True color when the Alert result is True and the Show True box is checked.  The background of the Section will use the Show False color when the Alert result is False and the Show False box is checked.   It is possible to use two different alerts to color the Section by having their Show True boxes checked and their Show False boxes unchecked.  An example of this is the RSI signal in the Study Consensus example.

The Section text comes from the Message text on the Alert property form.   Both a message to show when True and a message to show when False can be entered in the Message text, separated by a vertical line character.  See the example above, where 'MA Buy' is the True text and 'MA Sell' is the False text.  The text font color will use the Text color parameter in the Message frame.   The Panel color is not used.

The Section text can also show the value of a Global Variable.  Include in the message text the global variable number enclosed in square brackets.  Example:   High=[10]   would show the value from GV [10] after the equal sign.  Use [A] to show the panel A value.  Use [B] to show the panel B value.

Section text that is too long can be split into multiple lines.  Use a comma character in the text message to mark the split point.   For example, the message  'First Line,Second Line' will be displayed on two lines in the Section panel.

Variables Quote Page:

A special custom quote page named  'Variables'  will display the 240 public Global Variables in a matrix.   The GV values will update in real-time.   Zero values are shown as a blank.   Boolean True values are shown using the word 'True'.


Last modified 8/8/08 12:14 PM