# Chande Variable Moving Average Indicator for ThinkorSwim

#### skynetgen

##### Well-known member
TOS has built in variable MA , however what I wanted is flatter ma in case of chop. One from tradingview by lazybear fit the bill.
https://tos.mx/yANqUy
Code:
Code:
``````#added coloration
# formula from lazy bear #https://www.tradingview.com/script/HT99VkZb-Variable-Moving-Average-Bands-LazyBear/

input price = close;
input length = 13;

script VMA
{
input price=close;
input length=13;
def k = 1.0/length;
def src=if isnan(price[1]) then 0 else price;
def pdm = max((src - src[1]), 0);
def mdm = max((src[1] - src), 0);
def pdmS = if isnan(pdms[1]) then k*pdm else ((1 - k)*(pdmS[1]) + k*pdm);
def mdmS = if isnan(mdms[1]) then k*mdm else ((1 - k)*(mdmS[1]) + k*mdm);
def s = pdmS + mdmS;
def pdi = pdmS/s; def mdi = mdmS/s;
def pdiS = if isnan(pdis[1]) then k*pdi else ((1 - k)*(pdiS[1]) + k*pdi);
def mdiS = if isnan(mdis[1]) then k*mdi else ((1 - k)*(mdiS[1]) + k*mdi);
def d = absvalue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = if isnan(is[1]) then  k*d/s1 else ((1 - k)*(iS[1]) + k*d/s1);
def hhv = highest(iS, length);
def llv = lowest(iS, length) ;
def d1 = hhv - llv;
def vI = (iS - llv)/d1;
def val= if isnan(val[1]) then k*vI*src else (1 - k*vI)*(val[1]) + k*vI*src;
plot vma=val;
};

plot pVMA = vma(price,length);
pVMA.assignValueColor(if pvma>pvma[1] then color.blue
else if   pvma<pvma[1] then color.orange
else color.gray);
pvma.setlineWeight(3);``````

Fixed some bugs

Last edited:
@skynetgen if you so choose, put Chande in front of the VMA Title. Otherwise others may think it's just another one of "those" and not open the thread. This is a great indicator. Thank you for translating...

This Moving Average is Dynamic to help remove Chop. Thanks @skynetgen

@skynetgen this looks great. need to try. so in the particular configuration, blue = long, yellow/orange = short, and gray is neutral.

could be a great chop reducer with a 9 length/1 minute.

Last edited by a moderator:
I'm learning to appreciate this MA. Came across this version with step height coefficients variable. Code is not shared and wondered if anyone here would know how to make this modification?

Ruby:
``````#https://www.tradingview.com/script/6Ix0E5Yr-Variable-Moving-Average-LazyBear/
def src = close;
def l = 6;
def std = no;
def bc = no;
def k = 1.0 / l;
script nz {
input data = 0;
def ret_val = if IsNaN(data) then 0 else data;
plot return = ret_val;
}
def pdm = Max((src - src[1]), 0);
def mdm = Max((src[1] - src), 0);
def pdmS = ((1 - k) * nz(pdmS[1]) + k * pdm);
def mdmS = ((1 - k) * nz(mdmS[1]) + k * mdm);
def s = pdmS + mdmS;
def pdi = pdmS / s;
def mdi = mdmS / s;
def pdiS = ((1 - k) * nz(pdiS[1]) + k * pdi);
def mdiS = ((1 - k) * nz(mdiS[1]) + k * mdi);
def d = AbsValue(pdiS - mdiS);
def s1 = pdiS + mdiS;
def iS = ((1 - k) * nz(iS[1]) + k * d / s1);
def hhv = Highest(iS, l);
def llv = Lowest(iS, l);
def d1 = hhv - llv;
def vI = (iS - llv) / d1;
def vma = (1 - k * vI) * nz(vma[1]) + k * vI * src;
plot line = vma;
line.AssignValueColor(if line> line[1] then Color.GREEN else if line <line[1] then Color.MAGENTA else color.gray);
line.SetLineWeight(4);``````

87k+ Posts
673 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?