MultiCharts | PowerLanguage Tutorials | CH8: Function

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:

  1. Click the compile button to compile your code.
  2. If there are no errors, your function is ready to be used in your trading strategies or indicators.

Function Input Types

Numeric

Numeric
NumericSimpleExplanationDeclare 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.
SyntaxInput: InputName(NumericSimple);
ExampleInput: Length(NumericSimple);
NumericSeriesExplanationDeclare 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.
SyntaxInput: InputName(NumericSeries)
ExampleInput: Price(NumericSeries);
NumericExplanationDeclare an input as Numeric type that the passed value is either NumericSimple or NumericSeries type.
SyntaxInput: InputName(Numeric)
ExampleInput: Length(Numeric);
NumericRefExplanationDeclare 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.
SyntaxInput: InputName(NumericRef)
ExampleInput: BarCount(NumericRef);
NumericArrayExplanationDeclare an input as Numeric Array type.
SyntaxInput: InputName[M1,M2,M3,etc.](NumericArray)
where
InputName – input name。
– an input variable that is the maximum index value for each dimension of the array passed.
ExampleInput: Table[X,Y,Z](NumericArray);
X, Y, Z values are the maximum index values for each corresponding dimension of the passed array.
NumericArrayRefExplanationDeclare 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.
SyntaxInput: InputName[M1,M2,M3,etc.](NumericArrayRef)
where
InputName – input name.
– an input variable that is the maximum index value for each dimension of the array passed.
ExampleInput: Table[X,Y,Z](NumericArrayRef);
X, Y, Z values are the maximum index values for each corresponding dimension of the passed array.

String

String
StringSimpleExplanationDeclare 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.
SyntaxInput: InputName(StringSimple)
ExampleInput: Name(StringSimple);
StringSeriesExplanationDeclare 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.
SyntaxInput: InputName(StringSeries)
ExampleInput: Messages(StringSeries);
StringExplanationDeclare an input as String type that the passed value is either StringSimple or StringSeries type.
SyntaxInput: InputName(String)
ExampleInput: Name(String);
StringRefExplanationDeclare 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.
SyntaxInput: InputName(StringRef)
ExampleInput: Message(StringRef);
StringArrayExplanationDeclare an input as String Array type.
SyntaxInput: InputName[M1,M2,M3,etc.](StringArray)
where
InputName – input name.
– an input variable that is the maximum index value for each dimension of the array passed.
ExampleInput: MessageTable[X,Y,Z](StringArray);
X, Y, Z values are the maximum index values for each corresponding dimension of the passed array.
StringArrayRefExplanationDeclare 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.
SyntaxInput: InputName[M1,M2,M3,etc.](StringArrayRef)
where
InputName – input name.
– an input variable that is the maximum index value for each dimension of the array passed.
ExampleInput: CommentsTable[X,Y,Z](StringArrayRef);
X, Y, Z values are the maximum index values for each corresponding dimension of the passed array.

TrueFalse

TrueFalse
TrueFalseSimpleExplanationDeclare 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.
SyntaxInput: InputName(TrueFalseSimple)
ExampleInput: Overnight(TrueFalseSimple);
TrueFalseSeriesExplanationDeclare 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.
SyntaxInput: InputName(TrueFalseSeries)
ExampleInput: UpTrend(TrueFalseSeries);
TrueFalseExplanationDeclare an input as TrueFalse type that the passed value is either TrueFalseSimple or TrueFalseSeries type.
SyntaxInput: InputName(TrueFalse)
ExampleInput: Overnight(TrueFalse);
TrueFalseRefExplanationDeclare 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.
SyntaxInput: InputName(TrueFalseRef)
ExampleInput: Flag(TrueFalseRef);
TrueFalseArrayExplanationDeclare an input as TrueFalse Array type.
SyntaxInput: InputName[M1,M2,M3,etc.](TrueFalseArray)
where
InputName – input name.
– an input variable that is the maximum index value for each dimension of the array passed.
ExampleInput: FlagTable[X,Y,Z](TrueFalseArray);
X, Y, Z values are the maximum index values for each corresponding dimension of the passed array.
TrueFalseArrayRefExplanationDeclare 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.
SyntaxInput: InputName[M1,M2,M3,etc.](TrueFalseArrayRef)
where
InputName – input name.
– an input variable that is the maximum index value for each dimension of the array passed.
ExampleInput: 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(NumericRef) and ValueB(NumericRef) of the Function: swapByRefFunc are Passing By Reference.
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

Leave a Reply