Hello all, got an interesting little project here with a pesky bug I just can't seem to squash. I was building out a new type of filter/moving average I came up with and got the bright idea to try and take the average of all averages between two levels to smooth the data even further. As you can't feed a script a non-constant variable, I mimicked Script F within my fold function to properly iterate between average levels. This is where I am running into problems with how the output is displaying on the chart. The values are far too small. The design intent is to have the length1 and length2 variables be inclusive for the averaging of the averages. Also included in the script is the output of Script F, and an example of the math I began with and automated via Script F. You can see Script F and the "simple math" are the same for their given length. So those parts if the code are good. It's just that giant function giving me grief.
Rich (BB code):
#Averaging Expiramental Averages With Nested Fold
#StoneMan(StoneF)
#6.3.2023
#Hunting down bug SuperAverage
Declare Upper;
Script F {
Input Length = 10;
def data = close;
def FilterData =
fold a = 0
to Length
with K = 0
do K + data[a]*(Length - a);
def FilterCount =
fold i = 0
to Length + 1
with P = 0
do P + i;
def FilterAverage = FilterData / FilterCount;
plot Filter = FilterAverage;
}
input SupAvgLength1 = 10;
input SupAvgLength2 = 5;
def data = close;
#Number to divide moving averages by
def LengthDiff = AbsValue(SupAvgLength1 - SupAvgLength2) + 1;
def SuperAverage =
if(SupAvgLength1 == SupAvgLength2, F(SupAvgLength1),
(Fold q = if(SupAvgLength1 < SupAvgLength2, SupAvgLength1, SupAvgLength2)
to if(SupAvgLength1 > SupAvgLength2, SupAvgLength1, SupAvgLength2)
with R = 0
do R + (
#Implementing nested fold function because ThinkScript cannot do F(q) but we can recreate function F
(Fold u = 0
to q
with X = 0
do X + GetValue(data, u)*(q-u)) /
#Now divide this by the filter count function as we do in F
(Fold w = 0
to q + 1
with G = 0
do G + q)
)
) / LengthDiff
)
;
plot test = superaverage;
input FunctionF = 10;
Plot ScriptF = F(FunctionF);
#Example showing the calculation process function F is automating. This is equivalent to length = 10
def Average10 =(
(data*10) +
(data[1]*9) +
(data[2]*8) +
(data[3]*7) +
(data[4]*6) +
(data[5]*5) +
(data[6]*4) +
(data[7]*3) +
(data[8]*2) +
(data[9])
) /55;
plot ManualCalcTest = Average10;