Skip to content

Memory Management

Script Editor

You can preserve data between update cycles in two ways: with IfNull() or with Load() and Save().

IfNull

The IfNull() command initializes a variable when it has no value set.

-- Initialize value as global
highestValue = IfNull(highestValue, 0)
-- Update value
highestValue = Max(highestValue, CurrentPrice().high)

Info

IfNull() only works with global variables. Defining a variable as local and assigning to it with IfNull() will produce an error about unknown references.

Load & Save

Together, Load() and Save() achieve the same result as IfNull(), but split across two commands. They can be used in normal scripts, but they're also the only way to persist data inside custom command scripts — since custom commands don't allow global variables, IfNull() isn't available there.

-- Initialize value as local
local highestValue = Load('highestValue', 0)
-- Update value
highestValue = Max(highestValue, CurrentPrice().high)
-- Save value
Save('highestValue', highestValue)

Info

Load and Save work best with local variables.

Info

Always save loaded values.

Visual Editor

Between update cycles

Since IfNull() isn't available for Visual Scripts, the Save() and Load() is the way to preserve data between update cycles.

Inside updates

The Visual Editor has two special blocks that transfer values from A to B without connecting a line. This is useful if you need to use a value in multiple places or want to remove long connection-lines from your script. SessionSet() stores the value and SessionGet() retrieves it and continues execution. This set can also be used to control execution flow of your script.

Info

The values saved by SessionSet() are reset after every update cycle.