## Thursday, June 27, 2013

### thinkscript included: sdi_gma - geometric moving average

the geometric average is an alternate way that mathematicians define average. it is defined as the nth root of the product of the data points or:
(x1*x2*x3...*xn)^(1/n)
this was mentioned by the tastytrade geek as his preferred method of calculating averages.

my interest in the geometric mean is mainly that it is a challenge to expand my knowledge of thinkscript because the calculation necessitates the use of the fold statement.

to compute this average for any number of data points it is necessary to transform the definition of the geometric average by use of algebra which i do by applying the associative rule for exponents:
(a*b)^n = a^n * b^n
thus the working definition for the algorithm becomes:
x1^(1/n) * x2^(1/n) ... *xn^(1/n)
performing the calculation this way keeps the intermediate results of the product from overflowing the numerical limits of thinkScript variables.

here is the thinkscript:
########################################################
# sdi_gma: geometric moving average
#hint: Displays the geometric moving average. rev: 1.0
http://www.smallDogInvestor.com
# author: allen everhart
# date: 6/26/2013
# copylefts reserved. This is free software. That means you are free
# to use or modify it for your own usage but not for resale.
# Help me get the word out about my blog by keeping this header
# in place.
input price = close;
input length = 9;
plot gma = (fold n = 0 to length with s=1 do s*power(getValue(price, n),1/length));

###########################################################

the fold statement is the thinkscript equivalent of a for loop in other general purpose languages. in my use of the fold statement above, the loop repeats length times and the intermediate value of the loop counter is stored in the variable, n. the variable, s, is intermediate storage for the do expression which implicitly assigns the result of the expression to s on each iteration of the loop. it is necessary to use the getvalue function because thinkscript does not allow variables to be used in subscripting (the standard method of historical reference denoted by square brackets [].)

to demonstrate why this script is only a programming exercise just take a look at this chart which compares the 50 period simpleMovingAverage(green solid line) to a 50 period plot of sdi_gma(red dashed line):

the geometric average is always a smidgen less than the arithmetic average. there might be a larger difference for prices that dip below 1 because the geometric average overweights numbers between 0 and 1. in the extreme case where a price is zero the geometric moving average would plot as a horizontal line at value zero.

there are several uses of the fold statement i have in mind, one of which is a rewrite of my seapro study to allow the selection of the number of seasons to average.

best.
-allen

1. Hi Allen, have you thought of conditional sell orders? While some condition is true; place orders based on an additional signal until the condition is no longer true?

Matt

1. YES! TOS advanced order entry allows you place complex conditions on orders and cancellations. I typically use aoe condition to trigger option trades to go off at the 'MARK' when the underlying reaches some target price. Another use I have experience with is to trigger a trailing-stop trade based on price reaching a target. Many interesting possibilities there.

2. This comment has been removed by the author.

3. This comment has been removed by the author.

4. This comment has been removed by the author.