About Functions

An EasyLanguage function is a set of EasyLanguage statements that returns one or more values to the function caller. Like indicators, PaintBars, and ShowMe studies, functions have their own document type in EasyLanguage. That is, the code for a function resides in a different code document than the code for the indicator, PaintBar, ShowMe study, strategy, or other analysis technique that calls the function.

A function call is an EasyLanguage expression that calls an EasyLanguage function. Such calls, by default, appear in purple) in the code editor. Many of the built-in studies in the TradeStation platform call EasyLanguage functions.

A function's statements can be used to calculate a mathematical formula or perform any other EasyLanguage action. All EasyLanguage functions must return a value. In the code for the function, then, an assignment statement must appear that assigns a value to the function. The data type of the returned value (integer, Boolean, string, double-precision floating-point, etc.) is set in the properties of the function. For example, a statement like the following might appear in the code for a function named MyFunc that always returns the value 1 to the caller:

MyFunc = 1;

Common Usage

A function's return value can be assigned to a variable in the code that calls the function.

For example, in the following, the function calculates the average closing price over 10 bars and assigns the returned value to a pre-declared variable:

Value1 = Average(Close, 10);

Series Function

A series function automatically stores its own previous values and executes on every bar (even if used within a conditional statement).  For more information, refer to series function. Most functions are simple functions, not series functions.

Multiple Output Function

Some built-in functions need to return more than a single value and do this by using one or more output parameters within the parameter list.  Built-in multiple output functions typically preface the parameter name with an 'o' to indicate that it is an output parameter used to return a value.  These are also known as 'input-output' parameters because they are declared within a function as a 'ref' type of  input (i.e. NumericRef, TrueFalseRef, etc.) which allows it output a value, by reference, to a variable in the EasyLanguage code calling the function.

For example, the following built-in function looks for the 2nd highest Close over the last 30 bars. The first four parameters are inputs used to specify 1) what price to find, 2) over how many bars,  3) which occurrence (1st, 2nd, etc.), and  4) look for the highest or lowest.  The fifth and sixth parameters are outputs.   It uses one output parameter to return the 2nd highest Close to Value2 and a second output parameter to return the number of bars ago it occurred to Value3.  The function itself returns the status of the search (1 if successful or -1 if an error occurred) to Value1.

Var: oExtremeVal(0), oExtremeBar(0);

Value1 = NthExtreme(Close, 30, 2, 1, oExtremeVal, oExtremeBar);

Value2 = oExtremeVal;

Value3 = oExtremeBar;

Wrapper Function

When looking at the EasyLanguage code for a built-in function you may find the term 'wrapper function'. This refers to a single-return function that references a multiple output function to obtain the return value.  Calling a single-return 'wrapper' function helps make your EasyLanguage code simpler by avoiding the need to set up output parameters or understand multiple output functions.  For example, the ADX function returns the average directional movement index value that is one of several output parameters of the DirMovement function.