SQL Sequence Window Aggregate Average True Range (ATR) Function

The seq_window_agg_atr() function is calculated using the True Range and the iterative algorithm defined below (see http://en.wikipedia.org/wiki/Moving_average)

The true range is the largest of the:

• Most recent period's high minus the most recent period's low

• Absolute value of the most recent period's high minus the previous close

• Absolute value of the most recent period's low minus the previous close

The ATR at the moment of time t is calculated using the following formula:

The first ATR value is calculated using the arithmetic mean formula:

Following is an example script demonstrating this function:

 
    SELECT symbol, seq_search(day, 20130101, 20130331) as Q1_13,
        volume@Q1_13 as "Volume_Q1_13",
        seq_window_agg_atr(volume@Q1_13, 7) as "atr_Q1_13"
    FROM Quote WHERE symbol='SYM0';
 
    symbol
    Q1_13{}
    Volume_Q1_13
    atr_Q1_13
    ------------------------------------------------------------------------------
    SYM0 
       {20130101, 20130104, 20130106, 20130110, 20130123, 20130125, 20130129, 
    20130213, 20130214, 20130216, 20130311, 20130326}        
    {335, 808, 45, 402, 732, 48, 805, 450, 385, 420, 367, 69}       
    {335.00, 571.50, 396.00, 397.50, 464.40, 395.00,
       453.57, 453.06, 443.34, 440.00, 429.57, 378.06}
     

Note that the first 6 values for seq_window_agg_atr() (highlighted) should be ignored for this interval of 7. (Please see the Window versus Grid Aggregate page for a more detailed explanation.)

Sample script

A sample script to demonstrate this select statement using xSQL can be run from the samples/xsql/scripts/financial directory with the following command:

     
    g 11