Nesting

See Also  

Related Topics  

 

 

Nesting enables you to compose several expression blocks inside a function. In the example that follows, there are two, "nested" blocks.

 

Nasdaq100PercentMove(arg)=begin

 

retval = '.' clr_red

p = percentage(arg.net,arg.last)

nas100 = percentage(ndx_x.net,ndx_x.last)

 

if nas100 > 0 then nas100 = nas100 + DISPLAY_PERCENTAGE

if nas100 < 0 then nas100 = nas100 - DISPLAY_PERCENTAGE

 

if arg.net > 0 OR arg.last > arg.open then begin

retval = '^' clr_green

if arg.tvol > 1000000 then begin

if p > abs(nas100) then retval = p

end

end

if arg.net < 0 OR arg.last < arg.open then begin

retval = '-' clr_red

if arg.tvol > 1000000 then begin

if abs(p) > abs(nas100) then retval = p

end

end

retval

end

 

Perhaps the most remarkable attribute of this function is that the return value may contain either a string with a color attribute (i.e., retval = '.' clr_red), or a value (e.g., if abs(p) > abs(nas100) then retval = p). In fact, the function defines several conditions, any of which may serve as the formula's return value.

 

Using multi-line syntax enables you to get more mileage out of a formula. Think back to the traditional syntax of an if() function. The if() function gives two possible return values, a value if true or a value if false. (Yes, you could pack an if() function with nested expressions, but remember, in traditional syntax you are restricted to one line.) With multi-line functions, the only limits on possible return values are practicability and scope.

 

The example above illustrates a formula capable of returning any of 5 values, numeric or string: a default value, and two other values. The two expression blocks commence evaluation based on whether the instrument's net change is positive or negative. Within each expression block, the first test determines whether an instrument is up or down on the day. The second tests whether the instrument’s volume is greater than one million and whether the absolute value of instrument’s percentage change on the day is greater than the absolute value of the NASDAQ 100 Index's percent change on the day.

 

Note this would be even easier with If...Then...Else syntax--which is coming in a future release.

 

Note also the role of the DISPLAY_PERCENTAGE formula. This formula serves as an "adjustable" constant—no, this is not a true constant because you can change it by editing its value, but that’s the beauty of it. If you want the actual percent change of the NASDAQ 100 Index, you would set DISPLAY_PERCENTAGE to 0. As the trading day progresses, however, you can increment the value of DISPLAY_PERCENTAGE to emphasize NASDAQ 100 members that are a) pulling or pushing the NASDAQ 100 index in its current direction, and b) trading against the index, i.e., instruments perceived to be refuge.

 

©2008 Aspen Research Group, Ltd. All rights reserved. Terms of Use.