Input and Variable#
Input and Variable can be thought of as a container (Ex: bottle) that stores the desired value (Ex: 5 liters). Before using a container, you need to declare it first. The declaration tells the compiler the Type of the value to be stored. The type determines what operations can be performed on the value (Ex: arithmetic operations or string concatenation).
Therefore, Input and Variable are divided into declaration and usage phases:
- Declaration: Declare the name of the Input or Variable, assign a default value or initial value, and define the Type. The type can be Numerical, String, or TrueFalse.
- Usage: Use the name of the Input or Variable in the code. When the program executes to the name, it uses the stored value.
Input vs Variable#
Both Input and Variable are containers. The main differences are:
| Input | Variable | |
|---|---|---|
| Declaration Reserved Word | Input (synonym: Inputs) | Variable (synonyms: Var, Vars, Variables) |
| Value Setting Method | Default Value (DefaultValue) | Initial Value (InitialValue) |
| Can the value be changed in script | No | Yes, through assignment |
| Can it be modified from MultiCharts | Yes, no recompilation needed | No |
| Type determined by | Default value determines Type | Initial value determines Type |
Type#
Type represents the meaning and operation of a value. There are three types:
| Type | Representation | Example |
|---|---|---|
| Numerical | Use numbers directly | 1 + 1 results in 2 |
| String | Enclose content with double quotes ("") | "12" + "34" results in "1234" |
| TrueFalse | Use reserved words true and false | true represents logical true, false represents logical false |
Simple vs Series#
Each type is further divided into Simple and Series:
| Simple | Series | |
|---|---|---|
| Characteristic | Constant, does not change between bars | Changes between bars |
| Historical Values | No | Yes, use N bars ago or [N] to get the value of the previous N bars |
| Numerical Type | NumericSimple | NumericSeries |
| String Type | StringSimple | StringSeries |
| TrueFalse Type | TrueFalseSimple | TrueFalseSeries |
In signal scripts and indicator scripts:
- The default value of Input determines whether it is Simple or Series.
- The initial value of Variable determines whether it is Simple or Series.
The 6 type names above (Ex:
NumericSimple,NumericSeries) are also reserved words, used in function scripts to declare the type of Input.
Series Example#
Close (closing price) is an example of NumericSeries, because the closing price changes between bars.
Get the closing price of the previous 2 bars: Close 2 bars ago or Close[2].
Input#
Below is the declaration syntax in signal scripts and indicator scripts. For function script declaration, see Function.
Declaration Syntax#
| |
| Syntax Element | Description |
|---|---|
Input | Declares an Input |
InputName | The name of the Input. Not case-sensitive. Cannot start with a number or period (.) |
DefaultValue | The default value of the Input. The default value determines the Type |
A semicolon (
;) must be added at the end of the declaration. Omitting it will cause a compilation error.
Input values can only be set through the default value. You cannot change Input values through code in the script.
You can set the default value of Input in signal scripts and indicator scripts from MultiCharts. If no new default value is set from MultiCharts, the DefaultValue defined in the script will be used. New default values set from MultiCharts can be applied directly without recompilation.
Function script Input declarations do not have default values. The Input values are provided when the function is called.
Example#
| |
Declares Input Length with a default value of 20 (Numerical type).
| |
Declares Input Price with a default value of Close (Numerical type); Input Name with a default value of "Last Close" (String type).
| |
Declares Input Draw_Line with a default value of True (TrueFalse type).
Variable#
Below is the declaration syntax in signal scripts and indicator scripts. For function script declaration, see Function.
Declaration Syntax#
| |
| Syntax Element | Optional | Description |
|---|---|---|
Variable | Required | Declares a Variable |
IntraBarPersist | Optional | Recalculates the variable value on every tick (update by tick). By default, recalculates only at the end of each bar (update by bar) |
VariableName | Required | The name of the Variable. Can consist of letters, underscores, numbers, and periods (.). Not case-sensitive. Cannot start with a number or period (.) |
InitialValue | Required | The initial value of the Variable. The initial value determines the Type |
DataN | Optional | Specifies the Data the Variable is bound to. By default, binds to the default Data |
A semicolon (
;) must be added at the end of the declaration. Omitting it will cause a compilation error.
You cannot set the initial value of a Variable from MultiCharts. After setting through the initial value, Variable values can be changed through code in the script (assign other values).
Example#
| |
Declares Variable Max with an initial value of 100 (Numerical type), using IntraBarPersist for tick-by-tick updates.
| |
Declares Variable Min_Price with an initial value of Close (Numerical type), bound to Data2.
| |
Declares Variable Overnight with an initial value of False (TrueFalse type); Variable Name with an initial value of "Intra-Day" (String type).
Reference#
https://www.multicharts.com/trading-software/index.php?title=Input
https://www.multicharts.com/trading-software/index.php?title=Variable