VARIABLES Declaration

Top  Previous  Next

Variables declared using the VARIABLES statement are only useable in the script in which they are declared.  You cannot, for instance, define a variable in the entry script and then use it in the exit script.  Use a permanent variable or parameter if you want a variable that is useable by more than one script.

 

Variables declared with the VARIABLES statement are undefined every time a script is run, so they should be set to a known value at the top of the script before being used.  Therefore, it is sometimes faster to use a Block Permanent Variable or Instrument Permanent Variable, especially if you are using large amounts of data.

 

Syntax

 

VARIABLES: varname1  [TYPE: type ], varname2  [TYPE: type ] ...

 

varname                Name of the variable.

type                        Type of the variable.

 

The following examples illustrate the use of the VARIABLES statement:

 

VARIABLES: someValue TYPE: Integer

' someValue was defined and can be only integer

 

someValue = 10        ' someValue is integer and contains 10

someValue = 3.15      ' someValue is integer and contains 3

 

VARIABLES: a TYPE: Floating

' Single VARIABLES statement

 

VARIABLES: a, b, c TYPE: Integer

' Three variables in single VARIABLE statement

 

VARIABLES: str1 TYPE: String, int1 TYPE: Integer

' Multiple variables of different types

 

A VARIABLES statement can define variables of any of the following types:


 

 

String

 

characters or combinations of characters like "Hello", "A", and "November Soybeans"

 

 

 

Floating

 

numbers like 1.24 or 3.14159 which are not whole numbers

 

 

 

Integer

 

whole numbers like 1, 200, 582, -5

 

 

 

Money

 

variables which hold money.  Internally Money variables are stored in the same way as a floating point variables. Money variables are printed differently and show in the debugger with different formatting.

 

 

 

Price

 

variables which hold price information.  Internally Price variables are stored in the same way as a floating point variables. Price variables are printed according the current instrument's formatting and show in the debugger using that format. Price variables are also unadjusted for any negative value adjustment that may be present because of a negative price series in the instrument's data.

 

 

 

Series

 

A list or array of Floating numbers

 

The variable someValue should be a Price in the following situations:

someValue = instrument.close

someValue = instrument.high - averageTrueRange

someValue = instrument.low * 1.2

someValue = longMovingAverage (where longMovingAverage is a moving average indicator)

 

The variable someValue should be a Floating type in the following situations:

someValue = instrument.close - instrument.close[1]

someValue = instrument.high - instrument.low

someValue = averageTrueRange