S

#### skynetgen

##### Well-known member

**Warehouse**

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.

here its adaption for TOS

Fixed some bugs

### Variable Moving Average [LazyBear] — Indicator by LazyBear

Variable Moving Average, often abbreviated as VMA, is an Exponential Moving Average developed by Tushar S. Chande. VMA automatically adjusts its smoothing constant on the basis of Market Volatility. Use this like other Moving Averages. I have added the following options that can be enabled via...

www.tradingview.com

**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);
```

Last edited: