Jan
4

Formula 201 – #18 Memory Effect

This example show how formula language store value in a specific time of day, or when a specific condition occur. These two coding examples can be used in place of BarSince or Reftime indicator which takes up more resources, implementation is done by utilizing if function and a single slot holder local variable.

What the code does is monitor specific condition with if function, when condition is valid replace value stored in holder variable else retain previous value.

The Codes

First example shows how to store high value of the bar when indicator reach a certain value. The following code will replace $ma_high with current bar high whenever fast moving averages cross above slow moving averages.


fma := average(data1, 12);
sma := average(data1, 26);
$ma_high := if(xabove(fma,sma) > 0, h, $ma_high);

fma variable store indicator result for fast simple moving average.
sma variable store indicator result for slow simple moving average.
$ma_high is a single slot holder variable.
xabove(fma,sma) indicator function that return 1 when fast moving average cross above slow moving average.

Source code of above formula indicator is available for download in download area below.

Second example shows how to store high and low value of a time range. This example store morning time range high and low, what the code does is test for start and end time of time range, during this time range high and low value are replaced when new high/low is established, updating of high/low value stops when bar time is no longer within time range.


$morning_high := if(data1.date(0)<>data1.date(1),high,$morning_high);
$morning_low := if(data1.date(0)<>data1.date(1),low,$morning_low);
$IsMorning := (data1.time(0) >= maketime(9,30,0)) and
(data1.time(0) < maketime(10,0,0)); $morning_high := if(($IsMorning > 0) and (h > $morning_high),high,$morning_high);
$morning_low := if(($IsMorning > 0) and (l < $morning_low),low,$morning_low);

First part of the code reset stored high/low values on a new day; $IsMorning store evaluation of time range this value return 1 when current time is within 9:30AM and 10AM else it will return 0. Finally the two single slot variable that change high/low value only when it is within morning time range and its greater than/lesser than previous high/low.

Download

Moving average cross above high formula example (ex_for_b2011801.for)
Morning range high/low formula example (ex_for_b2011802.for)

Blog Developed
By ContentRobot