The code posted is a script for an indicator called Normalized Velocity.
This indicator is a type of momentum indicator that is designed to be smoother than other types of momentum indicators.
Normalized Velocity is calculated by taking the difference between two moving averages of the price, dividing the result by the average true range (ATR) of the price, and then normalizing the values so that they are between zero and one.
The indicator is then plotted on a chart, along with two horizontal lines that represent overbought and oversold levels. The script also includes options for customizing the period length, the minimum and maximum periods for the overbought and oversold levels, and the type of signal coloring used.
Creator Message: https://www.tradingview.com/v/1K0vWSrj/
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
# https://www.tradingview.com/v/1K0vWSrj/
#indicator("Normalized Velocity [Loxx]", shorttitle='NV [Loxx]'
# converted and mod by Sam4Cok@Samer800 - 03/2023
Declare Lower;
input srcOption = Close; # "Source"
input per = 32; # "Period"
input minMaxPeriod = 50; # "Min/Max Period"
input LevelUp = 80; # "Upper Level"
input LevelDown = 20; # "Bottom Level"
input sigtype = {"Slope", default "Middle Crosses", "Levels Crosses"}; # "Signal type"
input colorbars = yes; # "Color bars?"
input showSigs = yes; # "Show signals?"
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def src = srcOption;
def SM02 = sigtype==sigtype."Slope";
def SM03 = sigtype==sigtype."Middle Crosses";
def SM04 = sigtype==sigtype."Levels Crosses";
#---Color
DefineGlobalColor("green" , CreateColor(45,210,4));
DefineGlobalColor("red" , CreateColor(210,4,45));
DefineGlobalColor("dgreen" , CreateColor(27,126,2));
DefineGlobalColor("dred" , CreateColor(147,2,31));
#moms(float src, float per, float powSlow, float powFast)=>
script moms {
input src = close;
input per = 32;
input powSlow = 1;
input powFast = 2;
def suma;# = 0.0
def sumwa;#=0
def sumb;# = 0.0
def sumwb;#=0
def out;# = 0.
suma = fold k0 = 0 to per - 1 with p0 do
p0 + src[k0] * Power(per - k0, powSlow);
sumb = fold k1 = 0 to per - 1 with p1 do
p1 + src[k1] * Power(per - k1, powFast);
sumwa = fold k2 = 0 to per - 1 with p2 do
p2 + Power(per - k2, powSlow);
sumwb = fold k3 = 0 to per - 1 with p3 do
p3 + Power(per - k3, powFast);
out = sumb / sumwb - suma / sumwa;
plot return = out;
}
def outMaxminper = if (minMaxPeriod > 0) then minMaxPeriod else per;
def div = ATR(Length = 15);
def out = moms(src, per, 1, 2) / div;
def sig = out[1];
def fmin = Lowest(out, outMaxminper);
def fmax = Highest(out, outMaxminper);
def rng = fmax - fmin;
def lvlup = fmin + LevelUp * rng / 100.0;
def lvldn = fmin + LevelDown * rng / 100.0;
def mid = fmin + 0.5 * rng;
def state;# = 0.
if SM02 {
state = if (out < sig) then -1 else if (out > sig) then 1 else state[1];
} else
if SM03 {
state = if (out < mid) then -1 else if (out > mid) then 1 else state[1];
} else
if SM04 {
state = if (out < lvldn) then -1 else if (out > lvlup) then 1 else state[1];
} else {
state = state[1];
}
plot NormVel = out;#, "Normalized Velocity"
NormVel.SetLineWeight(2);
NormVel.AssignValueColor(if state>0 then GlobalColor("green") else
if state<0 then GlobalColor("red") else Color.GRAY);
plot Uplevel = lvlup; # "Up level"
plot Dnlevel = lvldn; # "Down level"
plot Midlevel = mid; # "Mid"
plot Zero = if isNaN(close) then na else 0;
Uplevel.SetDefaultColor(Color.VIOLET);
Dnlevel.SetDefaultColor(Color.VIOLET);
Midlevel.SetDefaultColor(Color.VIOLET);
Zero.SetDefaultColor(Color.DARK_GRAY);
Midlevel.SetStyle(Curve.SHORT_DASH);
Zero.SetStyle(Curve.LONG_DASH);
AssignPriceColor(if !colorbars then Color.CURRENT else
if state>0 then GlobalColor("green") else
if state<0 then GlobalColor("red") else Color.GRAY);
def goLong = if SM02 then (out Crosses above sig) else
if SM03 then (out Crosses Above mid) else (out Crosses Above lvlup);
def goShort = if SM02 then (out Crosses Below sig) else
if SM03 then (out Crosses Below mid) else (out Crosses Below lvldn);
plot SigUp = if !showSigs then na else if goLong then -0.6 else -1;
SigUp.SetDefaultColor(GlobalColor("dgreen"));
plot SigDn = if !showSigs then na else if goShort then 0.6 else 1;
SigDn.SetDefaultColor(GlobalColor("dred"));
AddCloud(pos, SigDn, GlobalColor("dred"));
AddCloud(SigUp, Neg, GlobalColor("dgreen"));
#--- END CODE
Last edited by a moderator: