What is a variable?

Variables are simply a name which represents a value or series of values. If you have used a spreadsheet then you have used variables. For example, in a spreadsheet column B row 4 might be the total sales for the month. In Excel you could name this cell to something like "monthlySales" then in other cells you could refer to that variable (cell) as either B4 or "monthlySales".

In Blox Basic you can create variables which have names and can hold values just like a spreadsheet cell can.

Why would you do this?  Suppose you want to compute the stop price for a buy and it will be 3 ATR less than the long moving average. You could use the expression:

longMovingAverage - (3 * averageTrueRange)

everywhere you needed to use the stop like:

broker.EnterLongOnOpen( longMovingAverage - (3 * averageTrueRange) )

Or you could simply define a variable and set its value using that expression:

VARIABLES: stopPrice TYPE: Price

' Compute the stop.
stopPrice = longMovingAverage - (3 * averageTrueRange)

' Enter the order to buy
broker.EnterLongOnOpen( stopPrice )

The method provides several benefits. First, it is easier to understand. Second, when you read the code later, you can easily see where the stop price is being calculated.

Variables are essential for dealing with user input, calculations, simplifying code, and output in a program.  For instance, to print out the multiplication tables up to 10 x 10 manually:

PRINT "1 x 1 = 1"
PRINT "1 x 2 = 2"
PRINT "1 x 3 = 3"
PRINT "1 x 4 = 4"

It would take a long time!  Using variables and a simple program you can do this more quickly:

columnOne = 1
columnTwo = 1

DO
PRINT columnOne, " x ", columnTwo, " = ", columnOne * columnTwo

columnTwo = columnTwo + 1

IF columnTwo = 11 THEN
columnOne = columnOne + 1
columnTwo = 1
ENDIF

LOOP UNTIL columnOne = 11

As you can see using variables can make life easier for repetitive tasks.

How do I make a variable?

Trading Blox Builder provides several ways of "declaring" variables.

1) Use the VARIABLES statement

2) Create an Instrument Permanent Variable

3) Create a Block Permanent Variable

How do I know which kind of variable to create?

Each of these types of variables have different lifetimes and qualities:

1) Variables declared with the VARIABLES statement only maintain their value in and during the script in which they are declared.  They are undefined at the start of the script. These variables are temporary and not permanent. So you cannot assume the variable will hold its value from one instrument to the next, or one day to the next.

2) Variables you create as an Instrument Permanent Variable retain their value from one script to the next but their value is specific to the current instrument. In other words, each instrument has its own copy of the variable. For instance, an Instrument Permanent Variable named channelWidth could be 20 for Gold and 10 for Corn.  Instrument Permanent Variables get their name because they are instrument-specific and permanent (i.e. they maintain their value across script invocations).

3) Block Permanent Variables are not instrument-specific, there is only one variable for the entire block. For example, if you increment a Block Permanent Variable named totalUnits each time a new position is added and you put on three units in three different instruments/markets the value of totalUnits will be 3 in every script that accesses it.

Variable Data Types:

When you use one of these methods to declare a variable, you must choose a "type". The variable's type determines what kind of information that variable can contain.  A variable can be one of the following.

Variable Type Names:

Descriptions:

Floating

numbers like 1.24 or 3.14159 which are not whole numbers

Instrument

An instrument which can be accessed like the global 'instrument' trading object.

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.

Series

A list, or an array of Floating numbers or characters (only Block Permanent and Instrument Permanent variables support number and character series).

String Series

A list or array of strings.

String

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

After a variable is declared, it cannot change its type.  If you make it a string it must stay a string.  Values of one type can be converted to another type using the conversion functions.

 Edit Time: 5/10/2017 10:02:10 AM Topic ID#: 111

Created with Help & Manual 7 and styled with Premium Pack Version 2.80 © by EC Software