Multi-Line Formulas

See Also

 

 

 

      

Note that while you can write multi-line formulas, there's nothing to prohibit you from writing formulas in traditional formula syntax. Further, if you have a formula suite written in traditional syntax, your formulas are still functional.

 

The Aspen formula language in version 4.0 supports multi-line expressions. What’s a multi-line expression? Take the traditional syntax of an Aspen if() function describing the occurrence of an up gap:

 

GapUp($1,$2)=if(c_gu_1($1,$2) AND c_gu_2($1,$2),1,0)

 

Here, the formula GapUp declares two arguments, $1 and $2. The expression asks whether the c_gu_1 and c_gu_2 formulas, applied to the arguments, returns a value of 1, or TRUE. If true, GapUp returns TRUE. If false, GapUp returns FALSE. Notice that all this processing takes place in a single-line.

 

In 4.0, this expression can be stated in multiple lines:

 

GapUp(instrument, targetBar)=begin

retval = FALSE

if c_gu_1(instrument, targetBar) then begin

if c_gu_2(instrument, targetBar) then retval = TRUE

end

retval

end

 

While multi-line expressions make formulas easier to read and write, they also allow multiple statements, or instructions, to occur between begin and end keywords.

Expression Blocks

The begin and end keywords in the example above define an expression block. The beginning of a block tells the Aspen formula compiler that multiple lines of processing may follow. The end of the block causes the function to terminate and return the value of the last statement. In addition to being required for multi-line expressions, blocks are also required when nesting statements.

 

There are two conventions for defining a block:

 

Convention

Similar to

Begin...end

Visual Basic.

{...}

C, C++, Java, etc.

 

Use the convention that best suites you.

 

Avoid mixing conventions—that is, don’t start a block with a begin statement and end it with }. This won’t work. You can, however, write one formula using begin...end, and another using {...}.

 

Once you have asserted a block, you can write as many lines as necessary to determine a return value. No matter how many lines you write, a multi-line expression returns the value of the expression just before the end of the block.

Nesting Statements