*th*root of the product of the data points or:

this was mentioned by the tastytrade geek as his preferred method of calculating averages.(x1*x2*x3...*xn)^(1/n)

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

**statement.**

*fold*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:

thus the working definition for the algorithm becomes:(a*b)^n = a^n * b^n

performing the calculation this way keeps the intermediate results of the product from overflowing the numerical limits of thinkScript variables.x1^(1/n) * x2^(1/n) ... *xn^(1/n)

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

*statement is the thinkscript equivalent of a*

**fold***loop in other general purpose languages. in my use of the*

**for****statement above, the loop repeats**

*fold***times and the intermediate value of the loop counter is stored in the variable,**

*length***. the variable,**

*n***, is intermediate storage for the**

*s***expression which implicitly assigns the result of the expression to**

*do***on each iteration of the loop. it is necessary to use the**

*s***function because thinkscript does not allow variables to be used in subscripting (the standard method of historical reference denoted by square brackets**

*getvalue***.)**

*[]*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

**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.**

*fold*best.

-allen

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?

ReplyDeleteMatt

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.

DeleteThis comment has been removed by the author.

ReplyDeleteThis comment has been removed by the author.

ReplyDeleteThis comment has been removed by the author.

ReplyDelete