Passing and returning Parameters between Seer objects

This topic contains 0 replies, has 1 voice, and was last updated by Seer Seer 7 years, 1 month ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #616
    Seer
    Seer
    Keymaster

    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:

     

    EMA(Close,9);

    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:

     

    BuyStop(ClosePrice-0.20);

    In this case we’re passing a list, containing one scalar, with the value of ClosePrice-0.20.

     

    Lists

    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:

     

    BB(20);

    The scalar 20 ends up as a list in @_ so when:

     

    my ($period,$dev)=@_;

    is executed, the variable $period ends up with the value 20, in other words @_ is expanded:

     

    my ($period,$dev)=(20);

    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:

     

    BB(20,3);

    We are passing a two item list to the indicator, which ends up as the following in the indicator:

     

    my ($period,$dev)=(20,3);

    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:

     

    return 45;

    Returns the number 45 to the calling routine.

     

    return $var;

    Returns the variable $var to the calling routine.

     

    return Close-Open;

    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:

     

    return ($macdline,$macds,$macdhist);

    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:

     

    my $macd=MACD(12,26,9);

    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);
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.