Volume Risk Avoidance Indicator For ThinkOrSwim

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

Is there a chance someone can convert this tradingview code to TOS, please?

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// Name: VRAI - Volume Risk Avoidance Indicator [Sherwin Shao]
// Created: 2022/01/07 - from VCO928

//@version=5
indicator(title='Volume Risk Avoidance Indicator', shorttitle='VRAI', format=format.inherit)

// Inputs
vco_short = input.int(3, title='Short Period', minval=1)
vco_long = input.int(10, title='Long Period', minval=2)
vco_showFill = input(true, title='Apply Filling')
vco_normalizeLookback = input(25, title='Normalization look-back period')
vco_reverseColor = input(false, title='Reverse Color')

kNeutralColor = color.rgb(245,220,0,0)
kBearColor = color.red
kBullColor = color.rgb(0,160,0,0) // color.green

// Functionality

isFlat(sig) =>
not(sig>0.5 or sig<-0.5 or math.abs(sig)>math.abs(sig[1])) //? color.gray : // sig == sig[1]

vco_color(sig) =>
isFlat(sig) ? color.gray : sig > 0 ? kBullColor : kBearColor

osc_color(sig) =>
sig == 0 ? kNeutralColor : sig > 0 ? kBullColor : kBearColor

momo_color(sig) =>
sig > math.max(sig[1],0) ? color.new(kBullColor,0) : sig < math.min(sig[1],0) ? color.new(kBearColor,60) : color.new(kNeutralColor,60)

smooth(sig, len) =>
ma = float(sig) // None
ma := ta.wma(sig, len)
ma

calc_vco(sig, short, long) =>
src = sig
vt = ta.cum(close == high and close == low or high == low ? 0 : (close - low / 4 - high / 4 - open / 2) / (high - low) * nz(volume, 1)) // from Accum/Dist
sh = smooth(vt, short)
lg = smooth(vt, long)
sh - lg

maxAbs(series, len) =>
max = float(0)
for i = 0 to len - 1 by 1
max := math.max(max, math.abs(series))
max
max



raw_vco = calc_vco(close, vco_short, vco_long)
maxAbs_vco = maxAbs(raw_vco, vco_normalizeLookback)
range_vco = 1.0 // range to fit values into
value_vco = raw_vco / maxAbs_vco * range_vco

color_vco = vco_color(value_vco)
color_stall = vco_color(-value_vco[1])
color_osc = vco_showFill ? osc_color(value_vco) : na

fill_color = math.abs(value_vco)<0.5 and math.abs(value_vco)<math.abs(value_vco[1]) ? 85 : 75
plot_vco = plot(value_vco, title='VCO', color=momo_color(value_vco), linewidth=1, style=plot.style_histogram) // green-yellow-red
plot_fill = plot(0, color=color.new(color_osc, fill_color), editable=false)
fill(plot_vco, plot_fill, title='Oscillator Fill', color=color.new(color_vco, fill_color))
check the below

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at
#// Name:    VRAI  - Volume Risk Avoidance Indicator [Sherwin Shao]
#// Created: 2022/01/07 - from VCO928
#indicator(title='Volume Risk Avoidance Indicator', shorttitle='VRAI', format=format.inherit)
# Converted by Sam4Cok@Samer800     - 09/2023
declare lower;
#// Inputs
input ShortPeriod = 3;
input LongPeriod = 10;
input normalizeLookback = 25;    # 'Normalization look-back period'

def na = Double.NaN;

#// Functionality
#calc_vco(sig, short, long) =>
script calc_vco {
    input sig = close;
    input short = 3;
    input long  = 10;
    def v = if !volume then 1 else volume;
    def src = sig;
    def vtv = if close == high and close == low or high == low then 0 else
                (close - low / 4 - high / 4 - open / 2) / (high - low) * v;
    def vt = TotalSum(vtv);
    def sh = WMA(vt, short);
    def lg = WMA(vt, long);
    def vco = sh - lg;
    plot out = vco;
}
#maxAbs(series, len) =>
script maxAbs {
    input series = close;
    input len = 25;
    def max = fold i = 0 to len with p do
              Max(p, AbsValue(series[i]));
    def maxAbs = max;
    plot out = maxAbs;
}

def raw_vco = calc_vco(close, ShortPeriod, LongPeriod);
def maxAbs_vco = maxAbs(raw_vco, normalizeLookback);
def value_vco = raw_vco / maxAbs_vco;
def sig = value_vco;
def vcol = if sig > 0 then
           if sig > 0.5 then 2 else if sig>sig[1] then 1 else 0 else
           if sig < 0 then
           if sig < -0.5 then -2 else if sig<sig[1] then -1 else 0 else 0;

plot vco = value_vco;
vco.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
vco.AssignValueColor(if vcol==2 then Color.GREEN else
                     if vcol==1 then Color.DARK_GREEN else
                     if vcol==-1 then Color.DARK_RED else
                     if vcol==-2 then Color.RED else Color.GRAY);

#-- END of CODE
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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