VMA value color Scan and Watchlist


Category:
0
0

I am using this Variable Moving Average Study on my charts.

#added coloration
# formula from lazy bear #https://www.tradingview.com/script/HT99VkZb-Variable-Moving-Average-Bands-LazyBear/

input price = close;
input length = 5;
#input length = 3;
script VMA
{
input price=close;
#input length=13;
input length=5;
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.CYAN else if pvma<pvma[1] then color.DARK_ORANGE else color.gray);
pvma.setlineWeight(3);

I wish to make a Scan with this study and I wish to use this scan for a watchlist.

I want the Scan to collect the stocks that have the Value Color of CYAN and a separate scan to have the Value Color of Magenta as per the VMA script.

I have searched your web site and I can not find any information on using a VMA.

Thanks

Marked as spam
Posted by (Questions: 5, Answers: 5)
Asked on April 27, 2020 10:28 am
85 views
0
Private answer

First thing I must mention is the code you provided is very poorly written. I am not criticizing. Merely pointing out to everyone that the code contains elements that everyone should avoid. Code like this is what leads to newbies writing code like this. It perpetuates poor code writing among those who are learning how to write code.

I won't waste any time correcting the code. But I will mention that it makes use of excessive abbreviations. Variable names such as k, src, pdm, mdm.... garbage. They mean absolutely nothing. Variable names should explain exactly what is being computed. Otherwise it is impossible to read the code and know what it's doing. The other huge mistake is not including spaces between operators.

This line itself says it all, pure garbage:

def iS = if isnan(is[1]) then k*d/s1 else ((1 – k)*(iS[1]) + k*d/s1);

Seriously...

I really hate to see code like this. If you had to write something like this in C++ it would never work. Because C++ actually forces you to write code properly

So you want a scan statement that returns a list of stocks for which the main plot is colored Cyan and another for when the main plot is color Dark Orange. The logic we need to construct this is contained on the last three lines. Well, one of the last three lines.

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

The first step in converting to scan is to change the plot to def and remove the last two lines. This is what remains:

def pVMA = vma(price, length);

Next we add two lines that you can use to run your scans. Only one if these can be used at any given time. So comment out the one you don't want to run before trying to save and run the scan:

plot scan = pVMA > pVMA[1];

OR....

plot scan = pVMA < pVMA[1];

That's it. Notice I added spaces between the operators and variables. The original contained no spaces. I also corrected the variable names to match what is used for the variable assignment. The end result is the same except added the spaces and correct capitalization makes the code much easier to read.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on April 27, 2020 12:04 pm
0
Thank you for your help. It works exactly as I wanted. In reference to your notes on the bad coding of the original study, I agree with everything you said. I have extremely limited thinkscript coding knowledge. What little I know, I have obtained by studying coding by individuals, like yourself, who have written code in a manner that sometimes novices can understand. For that, I am very grateful. The code of this study was Greek to me. Thanks again.
( at April 28, 2020 8:45 am)
0
Very good. I am glad I took the time to explain the ways in which that code was poorly written. You represent the group of individuals I want to help to avoid learning things the wrong way. Best of luck to you!
( at April 28, 2020 12:52 pm)