|
Instrument Permanent Variables |
Top Previous Next |
|
These variables can be different for each instrument, but retain their value (unlike variables declared with the VARIABLES statement). For instance, totalProfit (shown below) can be 100 for Soybeans and 50 for Gold.
To create a Instrument Permanent Variable, select "Instrument Permanent Variables" on the left and click the "New" button.
The Name for Code is the name of variable as you would use it in a Script. Common convention is that you start variable names with a lower case, and use upper case for the first letter of each subsequent word or part of the variable name. No spaces or special characters are allowed in variable names.
The Name for Humans is a more friendly description of the variable. In the case of Instrument Permanent this name is not displayed anywhere, but is useful to remember what the variables purpose is.
Defined Externally in Another Block -- check this option if this variable has been declared as System Scope in another block in the system. This option lets the Syntax Checker know about this variable.
The Variable Type is the kind of value the variable will be, and cannot be changed in a script. For a description of the different types, see the VARIABLES section.
The Default Value is the value that will be assigned to the variable when it is first used. So in this case at the start of running the program, the variable lastMonth will be set to 0.
Upon creation, you can now use this variable in any of the Scripts of the Block using the variable name "lastMonth."
The Scope determines where you can use this variable. Instrument Permanent Variables cannot be Test scope.
Note that for Instrument Permanent Variables, if you select the Series variable type, you have the option to plot. If you want to plot the value, it is recommended to use the Auto Index feature.
As with Indicators, select Plots on Trade Graph, Select the color, set the Graph Area, Select Offset by One Day if desired. The Graph Style has many options. Use the one most appropriate to your situation.
For both Block Permanent and Instrument Permanent, if you do not select Auto Index, you must specify a size for the array. This example, plotStopPrice, is an Auto Index series.
Access
You can access Instrument Permanent Variables through scripting two ways.
myInstrumentVariable = 5 IF myInstrumentVariable = 5 THEN PRINT "It is 5"
If a Series object you can access and set the index values: mySeriesVariable[1] = 5 IF mySeriesVariable[1] = 5 THEN PRINT "Yesterday was 5"
instrument.myInstrumentVariable = 5 sp500Index.myInstrumentVariable = 5 sp500Index.mySeriesVariable[1] = 5
IF instrument.myInstrumentVariable = 5 THEN PRINT "S&P has 5" IF sp500Index.mySeriesVariable[1] = 5 THEN PRINT "S&P has 5"
Accessing a variable using the instrument '.' syntax is equivalent to using the variable directly. The '.' syntax is the only way to access the value of instrument variables which are not part of the current instrument.
|