Click here to Login

GetVariable Multiple Usage

by Josh McCormick, 1191 days ago
Share |

By default, users are unable to take advantage of global variables inside of charts and other features. This function reads the specified global variable and returns its value as a string (not a number). If the global variable does not exist, this function will return a blank string.

By itself, GetVariable is unlikely to be very useful. When combined with other features such as Scripts and Keyboard Shortcuts, this function can provide unlimited possibilities which may be of interest to advanced users.

If you need to work with numbers instead of strings, you might download ToNumeric which performs that conversion. If you've downloaded ToNumeric, be aware that if a variable is a string or if a variable does not exist, ToNumeric will represent both of these conditions with the number zero.

a=GetVariable("NumberOfUnits"); // Returns a string
a=ToNumeric(GetVariable("NumberOfUnits")); // Returns a number (requires ToNumeric)

Figured out a good use for GetVariable? Do you use it to casually toggle your SMA plots between 20, 50, and 200 day periods? Or perhaps you compare the current symbol against a pre-defined list of symbols that you advance one at a time? Add a review below (with some code too, if you're willing) and let others know how you've put this to use!


The instructions below will show you how to create an infobox in the lower-left hander corner of an existing chart that contains the full name, industry, sector, and market associated with your currently active symbol. Using this function, the infobox can shown or hidden at any time by holding down [SHIFT] and [CONTROL] and tapping the letter [I].

FIRST: Add the following lines to your chart's formula:

ShowSymbolInfo=StringEqual(GetVariable("ShowSymbolInfo"),"1");      // 1=Show, 0=Hide
newline=" / "; // For a vertical infobox, make this string equal two characters: a BACKSLASH and a LOWERCASE N
PrintChart(text, "", BottomLeft, colorBlack, colorwhiteSmoke, colorwhitesmoke, 255*ShowSymbolInfo, 1);

The first line retreives the value of a global variable called ShowSymbolInfo. If the variable exists and its value is equal to "1" (a string, not a number), then the numeric variable ShowSymbolInfoVal will equal the number 1. Otherwise, ShowSymbolInfoVal will equal the number 0.

The second line determine how we separate our fields. If you'd like each piece of information to be on its own line, follow the instructions in the comments. (This is preferred, but the QuantShare website does not allow us to directly share that character sequence, so it must be described.)

The third line establishes the text we're going to display inside of the infobox.

The fourth line displays the box and its contents. The seventh parameter contains the "alpha" or the transparency of the box. With an alpha of 255, the box is solid. With an alpha of 0, the box is invisible. So we hide and unhide the box by multiplying 255 by the ShowSymbolsInfoVal variable (which is either 1 or 0).

SECOND: Create a script to toggle ShowSymbolInfo between "0" and "1".

Go into the script editor [Tools] -> [Script Editor].
Select [File] --> [New] and select the name "Toggle-ShowSymbolInfo".
Cut and paste the script below:

string keyword="ShowSymbolInfo";
Chart[] charts = Charts.GetAllCharts();
string res = (string)Global.GetVariable(keyword);
if (res == "1") Global.SetVariable(keyword, "0");
else Global.SetVariable(keyword, "1");
for(int i=0;i < charts.Length;i++) charts[i].Update();
//DEBUGGING: Uncomment the next line to display the keyword/value pair in an Output window.
//App.Main.Sleep(1500); Global.Trace("Global variable "+keyword+" = "+Global.GetVariable(keyword));

This script retrieves a single global variable named "ShowSymbolInfo". The name of the variable to be modified must be hard-coded into the script. If the variable equals the string "1", then it sets it to the string "0". Otherwise, it sets the variable back to "1" again.

Once you've pasted the script, select [File] --> [Save]. And then hit the [Execute] button along the top of the Script Editor. Wait a couple of seconds. Hit it again. And again. The Symbol Info Box that you created earler should appear and then disappear each time to execute the script. If not, you'll need to do some debugging.

THIRD: We want to be able to call our script with a keypress.

At the bottom of the QuantShare window, go to [Divers] -> [List of Shortcuts]. This is where tell QuantShare to launch a script when certain keys are pressed. Click [Add Shortcut] in the lower-right corner of the screen.

Fill in the Add Shortcut window as follows:
NAME: Toggle-ShowSymbolInfo
DESCRIPTION: Toggles the ShowSymbolInfo variable between "0" and "1"
Put a checkmark next to Control and Shift.
KEY: I (just the letter I, uppercase, all by itself)
PATH: Scripts\Toggle-ShowSymbolInfo.azf

Double-check your work and then press the [Save] button.

FOURTH: Try it out!

Every time you hold down the [SHIFT] and [CONTROL] keys and tap the letter [I], it will execute the Toggle-ShowSymbolInfo.azf script which will change the value of the global variable ShowSymbolInfo and refresh your charts. Your chart's formula will read the changes to the ShowSymbolInfoVar variable which it will use to hide and unhide the infobox you just created.

Enjoy! And let us know how you've put this to good use.

Share This ->
Share |

You have to log in to bookmark this object
What is this?
Additional Information

Type: Trading Indicator

Object ID: 1825




You must log in first

Join now
and get instant access for free to the trading software, the Sharing server and the Social network website.
Click here

Related objects


Number of reviews
Click to add a review
Average rate
Click to rate this item
Number of times this object was downloaded
Number of rates the current object received
Report an object
if you can't run it for example or if it contains errors
Click to report this object

Technical Analysis

Fundamental Analysis

Random Blog Posts

Profile Graphs

More about QuantShare Programming Language

Backtest Trading Strategies Programmatically and on Schedule

QuantShare Trading Software: New Features in the 3.7.0 Version

Compare Multiple Securities in a Single Chart

Dynamic Position Sizing in your Trading System

How to Create an Automated Trading System in QuantShare

QuantShare Trading Software: New Features in the 3.6.0 Version

Show All

Number of reviews
Click to add a review
Average rate
Click to rate this item
Number of times this object was downloaded
Number of rates the current object received
Report an object
if you can't run it for example or if it contains errors
Click to report this object

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2023
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.