cubed moving average

MLlalala

Member
I am trying to write a cube weighted moving average, the logic is quite simple, instead of traditional WMA whic is 4*price+3*price[1]+2*price[2]+price[3], i wanna cube the weighting, so it goes like 64*price+27*price[1]+8*price[2]+price[3]

Here is my code, but i am at a loss how to do it correctly

input price = hl2;
input length=10;

def weight=power(length,3);
plot CWMA = Sum(weight * price, Length) / Sum(weight, Length);

Much appreaciated if anyone could help out!
 
Solution
The code seems only plotting a limited portion. and if i just make the power function as
power(wt_index, 1); it should be equal to a normal weighted moving average. I have compared a normal weighted moving average with same length but they differ. The green line is TOS built in WMA. The parameters for both averages are price=hl2 and length =10

this will calculate a weighted average on every bar, except the first 'length' bars.
i added 2 loops, to sum the product and sum the weights. then divide them at the end.
untested.

Code:
def bn = BarNumber(); 
def na = Double.NaN; 

input price = hl2;
input length =10;
input wt_power = 3;

def wtpr_sum;
def wt_sum;
if bn <= length then {
wtpr_sum = 0;
wt_sum = 0;
} else {
wtpr_sum = fold i...
I am trying to write a cube weighted moving average, the logic is quite simple, instead of traditional WMA whic is 4*price+3*price[1]+2*price[2]+price[3], i wanna cube the weighting, so it goes like 64*price+27*price[1]+8*price[2]+price[3]

Here is my code, but i am at a loss how to do it correctly

input price = hl2;
input length=10;

def weight=power(length,3);
plot CWMA = Sum(weight * price, Length) / Sum(weight, Length);

Much appreaciated if anyone could help out!

you need to find the last 'length' bars on the chart.
find the last bar with a price, and count back from it, length bars. the difference of barnumber and barnumber of last bar and length, can be used to calc a series of numbers 1,2,3,...length. then use those values to calculate the weight.

i am away , so didn't verify.
i think your cwma formula will work.

Code:
def bn = BarNumber(); 
def na = Double.NaN; 

def lastbn = HighestAll( If(IsNaN(close), 0, bn )); 
def lastbar = (bn == lastbn);
def barstolast = (lastbn - bn);

input price = hl2;
input length=10;

def wt_index = length - barstolast;

def weight = if wt_index < 0 then 0 else 
power(wt_index, 3);

plot CWMA = Sum(weight * price, Length) / Sum(weight, Length);

#------------------
# test data

input test1 = no;
addchartbubble(test1, low,
wt_index + "\n" +
weight + "\n" +
price + "\n" +
CWMA
, color.yellow, no);


weighted avg
https://www.investopedia.com/terms/w/weightedaverage.asp
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

you need to find the last 'length' bars on the chart.
find the last bar with a price, and count back from it, length bars. the difference of barnumber and barnumber of last bar and length, can be used to calc a series of numbers 1,2,3,...length. then use those values to calculate the weight.

i am away , so didn't verify.
i think your cwma formula will work.

Code:
def bn = BarNumber();
def na = Double.NaN;

def lastbn = HighestAll( If(IsNaN(close), 0, bn ));
def lastbar = (bn == lastbn);
def barstolast = (lastbn - bn);

input price = hl2;
input length=10;

def wt_index = length - barstolast;

def weight = if wt_index < 0 then 0 else
power(wt_index, 3);

plot CWMA = Sum(weight * price, Length) / Sum(weight, Length);

#------------------
# test data

input test1 = no;
addchartbubble(test1, low,
wt_index + "\n" +
weight + "\n" +
price + "\n" +
CWMA
, color.yellow, no);


weighted avg
https://www.investopedia.com/terms/w/weightedaverage.asp
The code seems only plotting a limited portion. and if i just make the power function as
power(wt_index, 1); it should be equal to a normal weighted moving average. I have compared a normal weighted moving average with same length but they differ. The green line is TOS built in WMA. The parameters for both averages are price=hl2 and length =10

qyYfBMH.png
 
The code seems only plotting a limited portion. and if i just make the power function as
power(wt_index, 1); it should be equal to a normal weighted moving average. I have compared a normal weighted moving average with same length but they differ. The green line is TOS built in WMA. The parameters for both averages are price=hl2 and length =10

my mistake. i was thinking that just the value on the last bar was important, so i made it to only calculates on the last 'length' bars. thinking...
 
The code seems only plotting a limited portion. and if i just make the power function as
power(wt_index, 1); it should be equal to a normal weighted moving average. I have compared a normal weighted moving average with same length but they differ. The green line is TOS built in WMA. The parameters for both averages are price=hl2 and length =10

this will calculate a weighted average on every bar, except the first 'length' bars.
i added 2 loops, to sum the product and sum the weights. then divide them at the end.
untested.

Code:
def bn = BarNumber(); 
def na = Double.NaN; 

input price = hl2;
input length =10;
input wt_power = 3;

def wtpr_sum;
def wt_sum;
if bn <= length then {
wtpr_sum = 0;
wt_sum = 0;
} else {
wtpr_sum = fold i = 0 to length
with p
do p + ( getvalue(price, i) * power((length - i), wt_power) );

wt_sum = fold k = 0 to length
with r
do r + ( power((length - k), wt_power) );
}

plot CWMA = if wt_sum > 0 then round(wtpr_sum / wt_sum, 2) else na;
#
 
Solution
this will calculate a weighted average on every bar, except the first 'length' bars.
i added 2 loops, to sum the product and sum the weights. then divide them at the end.
untested.

Code:
def bn = BarNumber();
def na = Double.NaN;

input price = hl2;
input length =10;
input wt_power = 3;

def wtpr_sum;
def wt_sum;
if bn <= length then {
wtpr_sum = 0;
wt_sum = 0;
} else {
wtpr_sum = fold i = 0 to length
with p
do p + ( getvalue(price, i) * power((length - i), wt_power) );

wt_sum = fold k = 0 to length
with r
do r + ( power((length - k), wt_power) );
}

plot CWMA = if wt_sum > 0 then round(wtpr_sum / wt_sum, 2) else na;
#
This is perfect!!!
 
I am trying to write a cube weighted moving average, the logic is quite simple, instead of traditional WMA whic is 4*price+3*price[1]+2*price[2]+price[3], i wanna cube the weighting, so it goes like 64*price+27*price[1]+8*price[2]+price[3]
Interesting concept. Can you explain how this MA is of value to you and where did the idea come from? thanks for your insights.
 
Interesting concept. Can you explain how this MA is of value to you and where did the idea come from? thanks for your insights.
Yes i would be very happy to discuss this idea. For traditional WMA, the weight is assigned linearly like 4321, then divided by the sum, in a 4 bar WMA, current data is "diluted" by previous data and this linear relationship is not really true. So i just use a quick way to change the relationship by giving more weight to most recent data and less for previous data. You can play with square or square root too. Hope this helps
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
526 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top