Normalized Velocity [Loxx] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
BMivhi9.png


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:

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

BMivhi9.png


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
Hi Samer800, can you please help to place sigup and sigdn spikes at the swinglow and swinghigh points. it seems the spikes are not in the right place. Thanks,
 
Hi Samer800, can you please help to place sigup and sigdn spikes at the swinglow and swinghigh points. it seems the spikes are not in the right place. Thanks,
Signal spikes based on "Slope", "Middle Crosses", or "Levels Crosses". I just added pivot high/ low option but it may delay/repaint based on the Pivot Point lookback period.

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 = {"Pivot High/Low", "Slope", default "Middle Crosses", "Levels Crosses"}; # "Signal type"
input PivotPointLookback = 9;
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 last = isNaN(close);
def src = srcOption;
def SM01 = sigtype==sigtype."Pivot High/Low";
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));
Script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !isNaN(dat) and lbr > 0 and lbl > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat,-a) else dat < GetValue(dat,-a) else _nan;
   if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL+1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL+1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#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 ph =  findpivots(out, 1, PivotPointLookback, PivotPointLookback);
def pl =  findpivots(out, -1, PivotPointLookback,PivotPointLookback);
def LongL = !isNaN(pl);
def ShortH = !isNaN(ph);
def PvtH = if LongL then out else PvtH[1];
def PvtL = if ShortH then out else PvtL[1];

def state;
if SM01 {
    state = if (PvtL-PvtL[1]) then -1 else if (PvtH-PvtH[1]) then 1 else state[1];
    } else
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);

#--- Bar Color
AssignPriceColor(if !colorbars then Color.CURRENT else
                 if state>0 then GlobalColor("green") else
                 if state<0 then GlobalColor("red") else Color.GRAY);

#--- Signal
def goLong  = if SM01 then LongL else
              if SM02 then (out Crosses above sig) else
              if SM03 then (out Crosses Above mid) else (out Crosses Above lvlup);
def goShort = if SM01 then ShortH else
              if SM02 then (out Crosses Below sig) else
              if SM03 then (out Crosses Below mid) else (out Crosses Below lvldn);

plot SigUp = if !showSigs or last then na else if goLong then out else -0.95;
SigUp.SetDefaultColor(GlobalColor("dgreen"));

plot SigDn = if !showSigs or last then na else if goShort then out else 0.95;
SigDn.SetDefaultColor(GlobalColor("dred"));

AddCloud(pos, SigDn, GlobalColor("dred"));
AddCloud(SigUp, Neg, GlobalColor("dgreen"));

#--- END CODE
 
Hi @samer800
It seems like the overbought and oversold level in itself will give one the view of the relatively cheap or pricier the data series have become.... not by comparing when price is over the level ... but rather when that happen, compare to the last time it happens with the same overbought or oversold level ... and since the level itself is not bound by any boundaries, thus giving some sense of what is the magnitude the move is going to be ...
or put it in another way, the overbought or oversold level kind of like a bigger cycle of the velocity data ...
So, I was wondering how this overbought and oversold level look like when it's applied the to some of your work like "CCI and Bollinger Bands", 'Trendilo', "Momentum Ratio Oscillator [Loxx]"
and the mother of all indicator ... "Supertrend ANY INDICATOR", where user can put this level on ANY INDICATOR 🤩
should be some awesome work ...
Again, if I didn't mention it already, u work breaks a lot of ground for TV/TOS trader. Turly amazing ⚡👏✨
 
Last edited by a moderator:
Is there any way to scan for signals with this indicator? i cant seem to get it to work, it gives me a timeout error. thanks
 
Is there any way to scan for signals with this indicator? i cant seem to get it to work, it gives me a timeout error. thanks
try the below. Remove # for Long or Short at the end of the 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
# scan Sam4Cok@Samer800     - 12/2023
Declare Lower;

input srcOption = Close;    # "Source"
input per = 32;             # "Period"
input minMaxPeriod = 50;    # "Min/Max Period"

def na = Double.NaN;
def src = srcOption;

#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 = fold k0 = 0 to per with p0 do
               p0 + src[k0] * Power(per - k0, powSlow);
    def sumb = fold k1 = 0 to per with p1 do
               p1 + src[k1] * Power(per - k1, powFast);
    def sumwa = fold k2 = 0 to per with p2 do
               p2 + Power(per - k2, powSlow);
    def sumwb = fold k3 = 0 to per with p3 do
               p3 + Power(per - k3, powFast);
    def 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 fmin = Lowest(out, outMaxminper);
def fmax = Highest(out, outMaxminper);
def rng = fmax - fmin;
def mid = EhlersSuperSmootherFilter(fmin + 0.5 * rng, 14);

#--- Signal

plot goLong  = (out Crosses Above mid) within 5 Bars;
#plot goShort = (out Crosses Below mid) within 5 Bars;


#--- END CODE
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
452 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