Passing and returning Parameters between Seer objects
Trading Platform Software for backtesting & auto trading, for stock, futures and forex trading systems › Forums › Tutorials › Passing and returning Parameters between Seer objects
June 16, 2013 at 11:14 am #616
Throughout the Seer environment you will need to pass (and return) parameters from a variety of objects such as Indicators, Events and functions.
The commonest use is that of an indicator, for example:
In the above example we are passing the Close column and the number 9 to the indicator EMA. The Close column and the number 9 are called parameters, and together they are called a list. Objects can also return a list.
Other common examples, include calling Money Management events such as BuyStop.
The BuyStop event handler will typically allow you to pass a scalar value which is then used in the stop:
In this case we’re passing a list, containing one scalar, with the value of ClosePrice-0.20.
As we have already seen, a scalar variable is a single thing, typically a number, string or column. A list is simply an ordered collection of scalars, enclosed in parentheses, separated by commas:
(Close,9); (8,4,1,6,8); (Close,Open,'item');
All the above are examples of valid lists. A list can be long as needed, and they can contain any scalar value. A list can also be empty:
When you pass parameters, such as with the first example you are passing them as a list.
Processing a list in the called object
Many indicators such as RSI and BB allow you to pass parameters to the indicator as a list. The indicator then uses the parameters in the calculation. The first three lines of the Bollinger Band (BB) indicator are as follows:
#Bollinger Bands. my ($period,$dev)=@_; $dev=2 unless $dev;
You will notice that the $period and $dev variables are contained with in list, and that = is used to set them to @_.
The @_ is a special symbol that contains the list of passed parameters, from the calling object. So when we call BB as:
The scalar 20 ends up as a list in @_ so when:
is executed, the variable $period ends up with the value 20, in other words @_ is expanded:
You will notice that the list on the left hand side of the equals sigh contains more items that the list on the right hand side (2 items verses 1). In the cases where lists are not equal, as this case, the missing items are set as undef. Undef is special value, meaning undefined/unknown which when used in a expression evaluates to false. Consider the 3rd line of the Bollinger indicator:
$dev=2 unless $dev;
We are seting the value of $dev to 2 if $dev is false – as undef evaluates to false, $dev gets set to 2.
If we call the bollinger band indicator as:
We are passing a two item list to the indicator, which ends up as the following in the indicator:
As both lists on either side of the = are the same size, $period gets set to 20 and $dev to 3.
$dev=2 unless $dev;
As $dev contains the value 3, and evaluated to true, the value in $dev remains unchanged at 2.
Note: The @ character is actually used to signify array variables.
Returning a scalar to the caller
To return a single scalar from an object such as an indicator or called event such as BuyStop, use the keyword return:
Returns the number 45 to the calling routine.
Returns the variable $var to the calling routine.
Returns the column of the difference between the Close and Open columns to the calling routine.
Returning a list to the caller
As well as passing a list to objects such as Indicators and events you can also return a list.
Consider the Indicator MACD, it creates three new columns, the MACD line, the MACD signal and the MACD histogram. It makes sense for it to return these three columns as a list, and if you look at the last line of code for MACD you will see:
You can see the variables $macdline,$macds and $macdhist are contained within a list, and when used with the return keyword are returned to the calling routine. The ADX and BB are other examples of indicators that return a list of more than one column.
If you look at the documentation for the MACD indicator, you see the following line: In scalar context returns the histogram, in list context returns the columns MACD line, MACD signal and the MACD histogram. Scalar context means that the indicator was called when only one scalar was expected to be returned, for example:
While list context means that a list was expected to be returned. For example:
my ($line,$signal)=MACD(12,26,9); my ($line,$signal,$hist)=MACD(12,26,9);
You must be logged in to reply to this topic.