Sunday, June 20, 2010

Running Simple Moving Average (SMA)

When building a platform to test trading ideas...one of the big issues to deal with is all the indicators that require a spin through the price series in order to calculate. For example, in order to calculate the 200 day simple moving average (SMA) of closing prices for Google today you would have to loop back 200 - 1 days ago and sum the closing prices and divide by 200.

When you are backtesting an idea you often need to start from day 1 of a stock's trading history and loop forward to the most current day. In essence, pretending each day is the current day at that point in time. Thus, you are looping back 200 - 1 data points for each day in the series. This isn't such a big deal with a stock such as Google whose trading history is rather limited (2004). But, take a stock like IBM with a more extensive trading history and your code is going to bog down with each call to the SMA indicator. Throw 20,000 securities into your backtest and the looping adds up.

Therefore, running calculations are the preferred method in order to spin just once through the data points. So, in order to calculate the running simple moving average for closing prices you apply the following formula:
\(SMA_{today} = SMA_{yesterday} + ((Price_{today} - Price_{today - n}) /n)\)
Where
  • \(n\) = number of values included in your rolling computational window.
Straight-forward and avoids the loop. Here's the sample Python code for the Running SMA:
def cumulative_sma(bar, series, prevma):
    """
    Returns the cumulative or unweighted simple moving average.
    Avoids sum of series per call.

    Keyword arguments:
    bar     --  current index or location of the value in the series
    series  --  list or tuple of data to average
    prevma  --  previous average (n - 1) of the series.
    """
   
    if bar <= 0:
        return series[0]

    return prevma + ((series[bar] - prevma) / (bar + 1.0))
def running_sma(bar, series, period, prevma):
    """
    Returns the running simple moving average - avoids sum of series per call.

    Keyword arguments:
    bar     --  current index or location of the value in the series
    series  --  list or tuple of data to average
    period  --  number of values to include in average
    prevma  --  previous simple moving average (n - 1) of the series
    """

    if period < 1:
        raise ValueError("period must be 1 or greater")

    if bar <= 0:
        return series[0]

    elif bar < period:
        return cumulative_sma(bar, series, prevma)

    return prevma + ((series[bar] - series[bar - period]) / float(period))
And the example call and results:
prices = [10, 15, 25, 18, 13, 16]
prevsma = prices[0]   #1st day nothing to average so return itself.
for bar, close in enumerate(prices):
    currentsma = running_sma(bar, prices, 3, prevsma)
    print "Today's 3-day SMA = %.4f" % currentsma
    prevsma = currentsma

------- Results ----------------
Today's 3-day SMA = 10.0000
Today's 3-day SMA = 12.5000
Today's 3-day SMA = 16.6667
Today's 3-day SMA = 19.3333
Today's 3-day SMA = 18.6667
Today's 3-day SMA = 15.6667

Sunday, May 30, 2010

Russell 3000 Weekly Stats (05/24/10-05/28/10)

Key Figures Russell 3000 IWV
Total Active Components2961 1
Total New Listings this period0 0
Total Inactive this period1 0
# All-time Closing Highs 28 (0.19%) 0 (0.00%)
# All-time Closing Lows 87 (0.59%) 0 (0.00%)

# Higher Closes 6755 (45.63%) 2 (40.00%)
# Lower Closes 7775 (52.52%) 3 (60.00%)

Total Gains/Losses $1059.55 (0.28%) $0.45 (0.14%)
Average True Range (4.59%) (2.51%)

* Results are cumulative for the period.

Price verification found 0 errors

Friday, May 28, 2010

Russell 3000 Daily Stats (05/28/10)

Key Figures Russell 3000 IWV
Total Active Components2961 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 10 (0.34%) 0 (0.00%)
# All-time Closing Lows 6 (0.20%) 0 (0.00%)

# Higher Closes 508 (17.16%) 0 (0.00%)
# Lower Closes 2402 (81.12%) 1 (100.00%)

Total Gains/Losses $-1023.98 (-1.33%) $-0.72 (-1.10%)
Average True Range (3.77%) (1.66%)

* Results are cumulative for the period.

Price verification found 0 errors

Thursday, May 27, 2010

Russell 3000 Daily Stats (05/27/10)

Key Figures Russell 3000 IWV
Total Active Components2961 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 9 (0.30%) 0 (0.00%)
# All-time Closing Lows 4 (0.14%) 0 (0.00%)

# Higher Closes 2876 (97.13%) 1 (100.00%)
# Lower Closes 65 (2.20%) 0 (0.00%)

Total Gains/Losses $2892.48 (3.90%) $2.25 (3.55%)
Average True Range (5.27%) (3.57%)

* Results are cumulative for the period.

Price verification found 0 errors

Wednesday, May 26, 2010

Russell 3000 Daily Stats (05/26/10)

Key Figures Russell 3000 IWV
Total Active Components2961 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 2 (0.07%) 0 (0.00%)
# All-time Closing Lows 22 (0.74%) 0 (0.00%)

# Higher Closes 1549 (52.31%) 0 (0.00%)
# Lower Closes 1341 (45.29%) 1 (100.00%)

Total Gains/Losses $196.08 (0.26%) $-0.29 (-0.46%)
Average True Range (4.70%) (2.40%)

* Results are cumulative for the period.

Price verification found 0 errors

Tuesday, May 25, 2010

Russell 3000 Daily Stats (05/25/10)

Key Figures Russell 3000 IWV
Total Active Components2961 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 5 (0.17%) 0 (0.00%)
# All-time Closing Lows 32 (1.08%) 0 (0.00%)

# Higher Closes 1206 (40.73%) 1 (100.00%)
# Lower Closes 1682 (56.81%) 0 (0.00%)

Total Gains/Losses $-111.54 (-0.15%) $0.04 (0.06%)
Average True Range (5.27%) (3.32%)

* Results are cumulative for the period.

Price verification found 0 errors

Monday, May 24, 2010

Russell 3000 Daily Stats (05/24/10)

Key Figures Russell 3000 IWV
Total Active Components2961 1
Total New Listings this period0 0
Total Inactive this period1 0
# All-time Closing Highs 3 (0.10%) 0 (0.00%)
# All-time Closing Lows 24 (0.81%) 0 (0.00%)

# Higher Closes 615 (20.77%) 0 (0.00%)
# Lower Closes 2285 (77.17%) 1 (100.00%)

Total Gains/Losses $-844.58 (-1.13%) $-0.83 (-1.29%)
Average True Range (3.97%) (1.60%)

* Results are cumulative for the period.

Price verification found 0 errors

Sunday, May 23, 2010

Russell 3000 Weekly Stats (05/17/10-05/21/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 26 (0.18%) 0 (0.00%)
# All-time Closing Lows 81 (0.55%) 0 (0.00%)

# Higher Closes 4927 (33.25%) 1 (20.00%)
# Lower Closes 9619 (64.91%) 4 (80.00%)

Total Gains/Losses $-4073.13 (-1.05%) $-3.06 (-0.92%)
Average True Range (5.11%) (3.10%)

* Results are cumulative for the period.

Price verification found 0 errors

Friday, May 21, 2010

Russell 3000 Daily Stats (05/21/10)

Key Figures Russell 3000 IWV
Total Active Components2962 1
Total New Listings this period0 0
Total Inactive this period2 0
# All-time Closing Highs 1 (0.03%) 0 (0.00%)
# All-time Closing Lows 18 (0.61%) 0 (0.00%)

# Higher Closes 2218 (74.88%) 1 (100.00%)
# Lower Closes 682 (23.02%) 0 (0.00%)

Total Gains/Losses $1160.92 (1.57%) $0.96 (1.51%)
Average True Range (5.87%) (3.78%)

* Results are cumulative for the period.

Price verification found 0 errors

Thursday, May 20, 2010

Russell 3000 Daily Stats (05/20/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 1 (0.03%) 0 (0.00%)
# All-time Closing Lows 30 (1.01%) 0 (0.00%)

# Higher Closes 42 (1.42%) 0 (0.00%)
# Lower Closes 2907 (98.08%) 1 (100.00%)

Total Gains/Losses $-3449.92 (-4.45%) $-2.58 (-3.91%)
Average True Range (5.87%) (3.94%)

* Results are cumulative for the period.

Price verification found 0 errors

Wednesday, May 19, 2010

Russell 3000 Daily Stats (05/19/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 3 (0.10%) 0 (0.00%)
# All-time Closing Lows 16 (0.54%) 0 (0.00%)

# Higher Closes 620 (20.92%) 0 (0.00%)
# Lower Closes 2273 (76.69%) 1 (100.00%)

Total Gains/Losses $-747.23 (-0.96%) $-0.49 (-0.74%)
Average True Range (4.30%) (2.28%)

* Results are cumulative for the period.

Price verification found 0 errors

Tuesday, May 18, 2010

Russell 3000 Daily Stats (05/18/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 5 (0.17%) 0 (0.00%)
# All-time Closing Lows 8 (0.27%) 0 (0.00%)

# Higher Closes 378 (12.75%) 0 (0.00%)
# Lower Closes 2531 (85.39%) 1 (100.00%)

Total Gains/Losses $-1251.13 (-1.57%) $-0.94 (-1.39%)
Average True Range (4.76%) (2.89%)

* Results are cumulative for the period.

Price verification found 0 errors

Monday, May 17, 2010

Russell 3000 Daily Stats (05/17/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 16 (0.54%) 0 (0.00%)
# All-time Closing Lows 9 (0.30%) 0 (0.00%)

# Higher Closes 1669 (56.31%) 0 (0.00%)
# Lower Closes 1227 (41.40%) 1 (100.00%)

Total Gains/Losses $210.44 (0.27%) $-0.01 (-0.01%)
Average True Range (4.75%) (2.61%)

* Results are cumulative for the period.

Price verification found 0 errors

Sunday, May 16, 2010

Russell 3000 Weekly Stats (05/10/10-05/14/10)

Key Figures Russell 3000 IWV
Total Active Components2965 1
Total New Listings this period0 0
Total Inactive this period3 0
# All-time Closing Highs 110 (0.74%) 0 (0.00%)
# All-time Closing Lows 13 (0.09%) 0 (0.00%)

# Higher Closes 8331 (56.20%) 2 (40.00%)
# Lower Closes 6245 (42.13%) 3 (60.00%)

Total Gains/Losses $3634.27 (0.91%) $1.88 (0.55%)
Average True Range (4.86%) (2.63%)

* Results are cumulative for the period.

Price verification found 0 errors

Friday, May 14, 2010

Russell 3000 Daily Stats (05/14/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 7 (0.24%) 0 (0.00%)
# All-time Closing Lows 4 (0.13%) 0 (0.00%)

# Higher Closes 196 (6.61%) 0 (0.00%)
# Lower Closes 2727 (92.00%) 1 (100.00%)

Total Gains/Losses $-1704.33 (-2.10%) $-1.23 (-1.79%)
Average True Range (4.23%) (2.72%)

* Results are cumulative for the period.

Price verification found 0 errors

Thursday, May 13, 2010

Russell 3000 Daily Stats (05/13/10)

Key Figures Russell 3000 IWV
Total Active Components2964 1
Total New Listings this period0 0
Total Inactive this period1 0
# All-time Closing Highs 23 (0.78%) 0 (0.00%)
# All-time Closing Lows 3 (0.10%) 0 (0.00%)

# Higher Closes 782 (26.38%) 0 (0.00%)
# Lower Closes 2105 (71.02%) 1 (100.00%)

Total Gains/Losses $-724.20 (-0.89%) $-0.87 (-1.25%)
Average True Range (3.58%) (1.58%)

* Results are cumulative for the period.

Price verification found 0 errors

Wednesday, May 12, 2010

Russell 3000 Daily Stats (05/12/10)

Key Figures Russell 3000 IWV
Total Active Components2965 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 51 (1.72%) 0 (0.00%)
# All-time Closing Lows 3 (0.10%) 0 (0.00%)

# Higher Closes 2720 (91.74%) 1 (100.00%)
# Lower Closes 204 (6.88%) 0 (0.00%)

Total Gains/Losses $1853.05 (2.32%) $1.18 (1.72%)
Average True Range (4.25%) (1.78%)

* Results are cumulative for the period.

Price verification found 0 errors

Tuesday, May 11, 2010

Russell 3000 Daily Stats (05/11/10)

Key Figures Russell 3000 IWV
Total Active Components2965 1
Total New Listings this period0 0
Total Inactive this period0 0
# All-time Closing Highs 16 (0.54%) 0 (0.00%)
# All-time Closing Lows 1 (0.03%) 0 (0.00%)

# Higher Closes 1769 (59.66%) 0 (0.00%)
# Lower Closes 1126 (37.98%) 1 (100.00%)

Total Gains/Losses $318.25 (0.40%) $-0.15 (-0.22%)
Average True Range (4.86%) (2.13%)

* Results are cumulative for the period.

Price verification found 0 errors

Monday, May 10, 2010

Russell 3000 Daily Stats (05/10/10)

Key Figures Russell 3000 IWV
Total Active Components2965 1
Total New Listings this period0 0
Total Inactive this period3 0
# All-time Closing Highs 13 (0.44%) 0 (0.00%)
# All-time Closing Lows 2 (0.07%) 0 (0.00%)

# Higher Closes 2864 (96.59%) 1 (100.00%)
# Lower Closes 83 (2.80%) 0 (0.00%)

Total Gains/Losses $3892.87 (5.14%) $2.95 (4.50%)
Average True Range (7.40%) (4.92%)

* Results are cumulative for the period.

Price verification found 0 errors

Sunday, May 09, 2010

Russell 3000 Weekly Stats (05/03/10-05/07/10)

Key Figures Russell 3000 IWV
Total Active Components2969 1
Total New Listings this period0 0
Total Inactive this period2 0
# All-time Closing Highs 92 (0.62%) 0 (0.00%)
# All-time Closing Lows 80 (0.54%) 0 (0.00%)

# Higher Closes 3804 (25.63%) 1 (20.00%)
# Lower Closes 10824 (72.92%) 4 (80.00%)

Total Gains/Losses $-6416.43 (-1.59%) $-4.71 (-1.36%)
Average True Range (6.28%) (22.29%)

* Results are cumulative for the period.

Price verification found 0 errors