Function to know how many "weights" in a WMA?


I have no idea if this exists, actualy no idea what to look for in help or anywhere, so I thought, I better ask before I start doing something difficult.

I would like to know how many "weights" there are in a WMA.

For example, if we have the WMA(C,10) I know that the first number is in there once, the second twice, and the last one 10 times.

That means there are a total of 55 "weights" in a 10 bar WMA.

Is there an easy way to know these numbers ? Or an easy way to calculate it ?

At this moment I don't know how....

Thanks in advance,




Your example just demonstrated that the counting of the weights is just the Sum of the series. So for a 20 element series, just sum the series again an you have your number.

Thanks Snoopy, yes, exactly, you are correct.

But is there an easy way to do it ?

Because I want to use it for 150 different WMA's (it is used to calculate buy prices and I want to run optimizations with it)

So you are correct, I can manualy all calculate them, and put all those numbers in my script but it will create a long series of code...

So maybe there is an easier way or there is some "math" way. (I am not that good at math so I have not found it yet....)

@Henri I am not sure what you are looking for but you could calculate the number of weights like this perhaps

totalWts = 0;
pds = 10; // wma periods

for( i = 1; i <= pds; i++ )
    totalWts += i;

Or if you want to visualize what you are summing up (test this carefully before using as I didn't spend much time on it)

function wmaWeights( Periods )
    totalW = 0;

    for( i = 1; i <= Periods; i++ )
        totalW += i;

    return totalW;

pds = 10; // wma periods

///  Exploration  ///
Filter = 1;
for( j = 1; j <= pds; j++ )
    AddColumn( wmaWeights( j ) , "#wts WMA(" + j + ")", 1.0 );

Producing this type of exploration output

1 Like

You do not need loop at all.
You just need calculate as Gauss did in one line.

periods = 10;
gauss_sum = periods/2*(1+periods);
printf("%g", gauss_sum);

Great ! Thank you very much. I am honestly not sure what your code does but that will come when I get some more understanding of AFL but I can see in your output that it is doing exactly what I want it do do.

So thank you very much, it was exactly what I was looking for.

Using this I can calculate what a price needs to be for a WMA to cross my buy limit. With these numbers I can calculate the weight of the last bar, and then, using a 1 period shorter WMA, what the price needs to be. I can then create buy stops for the next bar, hoping ofcourse that the close will also be above the buy limit but that is another story :slight_smile:

I allready did it manualy, for a 50 bar WMA, but I wanted to optimize and use that buy limit.

MinROCGain = (Ref (tamroc, -1) + 0.025);
NEEDEDWMA = IIf (Ref (tamroc, -1) < 0, IIf (MinROCGain < 0, (Ref (ROC_WMA, (paramTamRoc * -1))), ((MinROCGain / 100)+1) * Ref (ROC_WMA, (paramTamRoc * -1))) ,Null);
BuyStop = IIf (Ref (tamroc,-1) < 0, (((NEEDEDWMA) - (Ref (ROC_WMAmin1, -1) * 0.96078432)) / 0.03921568)  ,Null);

That 0.03921568 and 0.96078432 is the percentage weight of the last bar and the previous 49 bars.

With your code, I guess I will be able to do it for multiple WMA's that can be optimized.

Thanks again, greetings

@fxshrat, I remember learning that at University (ages ago). Just could not rub the little grey cells together fast enough to spark it up.

Thanks for kicking them back into gear. :smiley:


Thanks fxshrat !

I had the feeling there would be some math way to solve my problem. Unfortunatly I never learned math so I always have to use the difficult solutions so thanks :slight_smile:

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.