Skip to Main Content

Using Script Input Parameters

In many cases, it is beneficial to have input parameters for indicators, such as moving average (MA) length, multipliers, and other settings. These parameters can be easily specified using the dialogs on charts, eliminating the need to modify the code whenever adjustments are required.

Input parameters can be defined using the input() function.

// This indicator will paint any MA type of any length at any price source (like High or Close e.t.c.)
// depending on what you select in its Properties window
describe_indicator('Universal MA');

// you declare a "select one option from a list" kind of an input parameter.
// you pass "constants.ma_types" as a list of options (it contains names of all MAs we support),
// and you define "sma" as a default value for this input.
// it will be visible under a "MA Type" name in the interface.
const maType = input('MA type', 'sma', constants.ma_types);

// you declare a "select one option from a list" kind of an input parameter.
// you pass "constants.price_source_options" as a list of options (it contains things like "open", "close", etc),
// and you define "close" as a default value for this input.
// It will be visible under a "MA Type" name in the interface.
const priceSource = input('Price', 'close', constants.price_source_options);

// you declare a numeric input parameter which will be named "Length" in the visual dialogs
// this inout will have a default value of 20. whatever you will assign to this parameter
// in the interface, will go into a variable named "maLength" in this case
const maLength = input('Length', 20);

paint(
    indicators[maType](prices[priceSource], maLength),
    { color: 'red' }
);
Using Script Input Parameters