Function
A function, like inputs and variables, can be thought of as a container.
However, instead of holding values, this container holds a block of code designed to perform a particular task.
It can take inputs, process them, and return a result.
Functions are usually written for frequently used functionalities, so there’s no need to rewrite the same calculations or features every time.
Functions, like inputs and variables, also have two phases:
- The first phase is definition (implementation): writing and compiling the implementation code of function.
- The second phase is usage (calling): using the function name and passing values to function input.
Defining (Implementing) a Function
Creating a New Function Script
Go to PowerLanguage Editor -> File -> New File -> Function
Writing Functionality and Compiling
Function inputs are different from those of signal indicators or indicator scripts.
The inputs cannot have be passed into from MultiCharts.
Function inputs are passed into when using (calling) function.
inputs: InputName1(InputType), InputName2(InputType)...;
//Write Functionality
FunctionName=ReturnValue;
- Inputs: Declares inputs for the function.
- InputName: The name of the input.
- InputType: The type of the input, as detailed in the Function Input Types table below.
- Functionality: The operational logic of the function.
- FunctionName: Indicates that this function will return a value,
ReturnValue
.
After writing your function, you need to compile it:
- Click the compile button to compile your code.
- If there are no errors, your function is ready to be used in your trading strategies or indicators.
Function Input Types
Numeric
Numeric | ||
---|---|---|
NumericSimple | Explanation | Declare an input as Numeric Simple type. An input declared as Simple has the same constant value for each bar, therefore it does not have historical values. |
Syntax | Input: InputName(NumericSimple); | |
Example | Input: Length(NumericSimple); | |
NumericSeries | Explanation | Declare an input as Numeric Series type. An input declared as Series can have different values for each bar, therefore it has historical values that can be referenced. |
Syntax | Input: InputName(NumericSeries) | |
Example | Input: Price(NumericSeries); | |
Numeric | Explanation | Declare an input as Numeric type that the passed value is either NumericSimple or NumericSeries type. |
Syntax | Input: InputName(Numeric) | |
Example | Input: Length(Numeric); | |
NumericRef | Explanation | Declare an input as Passing by Reference Numeric type. Passing by Reference: If the value of input is changed within the function, the value of argument is also changed. |
Syntax | Input: InputName(NumericRef) | |
Example | Input: BarCount(NumericRef); | |
NumericArray | Explanation | Declare an input as Numeric Array type. |
Syntax | Input: InputName[M1,M2,M3,etc.](NumericArray) where InputName – input name。M – an input variable that is the maximum index value for each dimension of the array passed. | |
Example | Input: Table[X,Y,Z](NumericArray); X, Y, Z values are the maximum index values for each corresponding dimension of the passed array. | |
NumericArrayRef | Explanation | Declare an input as Passing by Reference Numeric Array type. Passing by Reference: If the value of input is changed within the function, the value of argument is also changed. |
Syntax | Input: InputName[M1,M2,M3,etc.](NumericArrayRef) where InputName – input name.M – an input variable that is the maximum index value for each dimension of the array passed. | |
Example | Input: Table[X,Y,Z](NumericArrayRef); X, Y, Z values are the maximum index values for each corresponding dimension of the passed array. |
String
String | ||
---|---|---|
StringSimple | Explanation | Declare an input as String Simple type. An input declared as Simple has the same constant value for each bar, therefore it does not have historical values. |
Syntax | Input: InputName(StringSimple) | |
Example | Input: Name(StringSimple); | |
StringSeries | Explanation | Declare an input as String Series type. An input declared as Series can have different values for each bar, therefore it has historical values that can be referenced. |
Syntax | Input: InputName(StringSeries) | |
Example | Input: Messages(StringSeries); | |
String | Explanation | Declare an input as String type that the passed value is either StringSimple or StringSeries type. |
Syntax | Input: InputName(String) | |
Example | Input: Name(String); | |
StringRef | Explanation | Declare an input as Passing by Reference String type. Passing by Reference: If the value of input is changed within the function, the value of argument is also changed. |
Syntax | Input: InputName(StringRef) | |
Example | Input: Message(StringRef); | |
StringArray | Explanation | Declare an input as String Array type. |
Syntax | Input: InputName[M1,M2,M3,etc.](StringArray) whereInputName – input name.M – an input variable that is the maximum index value for each dimension of the array passed. | |
Example | Input: MessageTable[X,Y,Z](StringArray); X, Y, Z values are the maximum index values for each corresponding dimension of the passed array. | |
StringArrayRef | Explanation | Declare an input as Passing by Reference String Array type. Passing by Reference: If the value of input is changed within the function, the value of argument is also changed. |
Syntax | Input: InputName[M1,M2,M3,etc.](StringArrayRef) whereInputName – input name.M – an input variable that is the maximum index value for each dimension of the array passed. | |
Example | Input: CommentsTable[X,Y,Z](StringArrayRef); X, Y, Z values are the maximum index values for each corresponding dimension of the passed array. |
TrueFalse
TrueFalse | ||
---|---|---|
TrueFalseSimple | Explanation | Declare an input as TrueFalse Simple type. An input declared as Simple has the same constant value for each bar, therefore it does not have historical values. |
Syntax | Input: InputName(TrueFalseSimple) | |
Example | Input: Overnight(TrueFalseSimple); | |
TrueFalseSeries | Explanation | Declare an input as TrueFalse Series type. An input declared as Series can have different values for each bar, therefore it has historical values that can be referenced. |
Syntax | Input: InputName(TrueFalseSeries) | |
Example | Input: UpTrend(TrueFalseSeries); | |
TrueFalse | Explanation | Declare an input as TrueFalse type that the passed value is either TrueFalseSimple or TrueFalseSeries type. |
Syntax | Input: InputName(TrueFalse) | |
Example | Input: Overnight(TrueFalse); | |
TrueFalseRef | Explanation | Declare an input as Passing by Reference TrueFalse type. Passing by Reference: If the value of input is changed within the function, the value of argument is also changed. |
Syntax | Input: InputName(TrueFalseRef) | |
Example | Input: Flag(TrueFalseRef); | |
TrueFalseArray | Explanation | Declare an input as TrueFalse Array type. |
Syntax | Input: InputName[M1,M2,M3,etc.](TrueFalseArray) where InputName – input name.M – an input variable that is the maximum index value for each dimension of the array passed. | |
Example | Input: FlagTable[X,Y,Z](TrueFalseArray); X, Y, Z values are the maximum index values for each corresponding dimension of the passed array. | |
TrueFalseArrayRef | Explanation | Declare an input as Passing by Reference TrueFalse Array type. Passing by Reference: If the value of input is changed within the function, the value of argument is also changed. |
Syntax | Input: InputName[M1,M2,M3,etc.](TrueFalseArrayRef) whereInputName – input name.M – an input variable that is the maximum index value for each dimension of the array passed. | |
Example | Input: TrendTable[X,Y,Z](TrueFalseArrayRef); X, Y, Z values are the maximum index values for each corresponding dimension of the passed array. |
Using (Calling) Functions
FunctionName(input1, input2, ...);
- FunctionName: The name of the function to use.
- input: The value passed to the input when calling the function, also called an argument.
Example
Built-in function: AerageFC
.
File -> Open File -> Function (check the checkbox) -> AverageFC
inputs:
PriceValue( numericseries ),
Len( numericsimple ) ;
AverageFC = SummationFC( PriceValue, Len ) / Len ;
AverageFC
is a function implementation that also calls another function, SummationFC
, to complete its functionality.
The AverageFC
function declares two inputs, one named PriceValue
of type numericseries
, and one named Len
of type numericsimple
.
Then, it calls the SummationFC
function to perform calculations, with PriceValue
and Len
passed as arguments to the inputs of SummationFC
.
Finally, AverageFC
returns the result of the SummationFC
calculation divided by Len
.
Passing By Value and Passing By Reference
Argument
The concrete value passed to input of function when you call it, enabling the functions to execute their defined tasks with specific values.
There are two method of passing arguments to input:
- Passing by Value
- Passing by Reference.
Passing by Value
The input types without a “Ref” suffix use Passing By Value method.
The value of input can NOT be changed within the function implementation.
Attempting to change the value of input within the function will result in a compile error: Array, variable or refinput expected
.
Example
The inputs ValueA(NumericSimple)
and ValueB(NumericSimple)
of the Function: swapByValFunc are Passing By Value.
Attempts to change the values of ValueA
and ValueB
on lines 6 and 7 result in a compile error: Array, variable or refinput expected
.
Function: swapByValFunc
inputs: ValueA(NumericSimple), ValueB(NumericSimple);
Print("In sawpFunc, before swap ValueA: ", ValueA, ", ValueB: ", ValueB);
temp = ValueA;
ValueA = ValueB; //compile error: Array, variable or refinput expected
ValueB = temp; //compile error: Array, variable or refinput expected
Print("In sawpFunc, after swap ValueA: ", ValueA, ", ValueB: ", ValueB
Passing by Reference
The input types with a “Ref” suffix use Passing By Reference method.
The value of input can be changed within the function implementation.
If the value of input is changed within the function, the value of argument is also changed.
The most common use case of passing by reference is for additional outputs of function.
Since the function only return value, there can only be one output.
When a function needs to produce multiple outputs, passing by reference can be utilized.
Example
The inputs ValueA(
and
)NumericRef
ValueB(
of the Function: swapByRefFunc are Passing By Reference.
)NumericRef
The Function: swapByRefFunc swaps the values of inputs ValueA
and
.ValueB
Hence, The variable ValueA
and ValueB
of Signal: testSwapFunc have their values swapped after calling Function: swapByRefFunc.
Print
output of Signal: testSwapFunc shows that after calling the swapByRefFunc
, the values of ValueA
and ValueB
have been exchanged.
Function: swapByRefFunc
inputs: ValueA(NumericRef), ValueB(NumericRef);
var: temp(0);
Print("In sawpFunc, before swap ValueA: ", ValueA, ", ValueB: ", ValueB);
//swap ValueA and ValueB
temp = ValueA;
ValueA = ValueB;
ValueB = temp;
Print("In sawpFunc, after swap ValueA: ", ValueA, ", ValueB: ", ValueB);
Signal: testSwapFunc
var: ValueA(12), ValueB(34);
//ValueA: 12.00, ValueB: 34.00
Print("Before call swap, Func, ValueA: ", ValueA, ", ValueB: ", ValueB);
swapByRefFunc(ValueA, ValueB);
//ValueA: 34.00, ValueB: 12.00
Print("After call swap, Func, ValueA: ", ValueA, ", ValueB: ", ValueB);
Reference
https://www.multicharts.com/trading-software/index.php?title=Category:Declaration
https://www.multicharts.com/trading-software/index.php?title=Passing_values_to_and_from_a_function