Using variables

Variables in any programming language are the necessity for moving information around the code. Here is some HaasScript-specific information about them and how they work.

Global variables

The global variable syntax examples:

hello = "Hello World!"
n1, n2 = 123, 321
unassigned = nil
  • The global variable is declaring variable name which is using entire coding.
  • The global variable does not need any keyword to declare the name.
  • The global variable identifies using only variable names and their values.

Local variables

The local variable syntax examples:

local hello = "Hello World!"
local n1, n2 = 123, 321
local unassigned
  • The local variable is declaring variable name inside of the method, if-then block, or function.
  • The variable is used the “local” keyword to declare the local variable name.

Table variable

Table variable syntax examples:

------------------------------------------
-- Define the table variable:

my_table = {} -- global empty table
-- or...
local my_table = {} -- local empty table

------------------------------------------
-- Define index for our new value
index = "SomeIndex"
-- or...
index = 123

------------------------------------------
-- Assign a value into the table
my_table[ index ] = "some data"

-- NOTE that "index" can be a text string or a number in assignments like this.

------------------------------------------
-- Assign a value to a "field" in the table
my_table.field_name = "some other data"

------------------------------------------
-- Remove the values from the table
my_table[ index ] = nil
my_table.field_name = nil

------------------------------------------
-- Remove the reference to the table itself
my_table = nil

Variable Data Types

HaasScript is a dynamically typed language, so the variables don’t have types, only the values have types. Values can be stored in variables, passed as parameters and returned as results.

The list of data types for values are given below.

ReferenceTypeDescription
nullNilDataTypeUsed to differentiate the value from having some data or no (nil) data.
booleanBoolDataTypeIncludes true and false as values. Generally used for condition checking.
numberNumberDataTypeRepresents a real (double precision floating point) numbers.
stringTextDataTypeRepresents an array of characters.
functionFunctionDataTypeRepresents a method that is written in HaasScript.
commandCommandDataTypeRepresents a built-in command of HaasScript.
user-dataUserDataDataTypeRepresents a custom data array (i.e. HNC and return value of price commands)
arrayArrayDataTypeRepresents a data array/object

GetType Command

In HaasScript, there is a built-in command called ‘GetType’ that enables us to know the type of the variable. Some examples are given in the following code.

Log(GetType("What is my type"))   --> TextDataType

function sayHello() Log("Hello!") end

Log(GetType(1.618*PI))            --> NumberDataType
Log(GetType(true))                --> BoolDataType
Log(GetType(sayHello))            --> FunctionDataType
Log(GetType(ClosePrices))         --> CommandDataType
Log(GetType(ClosePrices()))       --> UserDataDataType

When you save or backtest the above snippet, it produces the following results:

TextDataType
NumberDataType
BoolDataType
FunctionDataType
CommandDataType
UserDataDataType

By default, all the variables will point to nil until they are assigned a value or initialized. In HaasScript, any values (except false and nil), including zero and empty strings are all considered to be true in case of condition checks. Hence, you have to be careful when using boolean operations. We will know more about using these types in the next chapters.

Back to: HaasScript Fundamentals > Variables