Volume Divergence For ThinkOrSwim

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

Is anyone interested in converting this TradingView code to TOS? Volume divergence is very useful.
https://www.tradingview.com/script/nxR6JqcL-Volume-Divergence-Indicator/
check below.

Upper plot:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © cyatophilum
#indicator("Volume Divergence Indicator",overlay= true)
# Converted by Sam4Cok@Samer800    - 05/2023
declare upper;

#// Inputs {
input priceLookBack             = 20;  # 'Price and volume Lookback'
input bull_Sensitivity          = 3;   # 'Bull Sensitivity'
input bear_Sensitivity          = 4;   # 'Bear Sensitivity'
input VolumeMovAvgLength        = 20;  # 'Volume MA length'
input SensitivityOfSpikes       = 3;   # 'Sensitivity of Spikes'
input SensitivityOfContractions = 1;   # 'Sensitivity of Contractions'
input trendSensitivity          = 5;   # 'Trend sensitivity'

def na = Double.NaN;
def last = IsNaN(close);
def mult = SensitivityOfSpikes;
def mult2 = SensitivityOfContractions;
def maLength = VolumeMovAvgLength;
def bullSensitivity = bull_Sensitivity + 1;
def bearSensitivity = bear_Sensitivity + 1;

#barssince(Condition) =>
script barssince {
    input Condition = 0;
    def barssince = if Condition then 0 else barssince[1] + 1;
    plot return = barssince;
}
#// Algorithm {
#// Bullish Divergence
def priceLowerLows    = Lowest(low, priceLookBack) == Lowest(low, priceLookBack * bullSensitivity);
def lowVol1 = Lowest(volume, priceLookBack);
def lowVol2 = Lowest(volume, priceLookBack * bullSensitivity);
def volumeHigherLows  = lowVol1 > lowVol2;
def bulDiv = priceLowerLows and volumeHigherLows;
def bullishDivergence = priceLowerLows and volumeHigherLows and !bulDiv[1];

#// Bearish Divergence
def priceHigherHighs  = Highest(high, priceLookBack) == Highest(high, priceLookBack * bearSensitivity);
def highVol1 = Highest(volume, priceLookBack);
def highVol2 = Highest(volume, priceLookBack * bearSensitivity);
def volumeLowerHighs  = highVol1 < highVol2;
def bearDiv = priceHigherHighs and volumeLowerHighs;
def bearishDivergence = priceHigherHighs and volumeLowerHighs and !bearDiv[1];

#// Volume Spike:
def volMA = Average(volume, maLength);
def volDev = mult * StDev(volume, maLength);
def volDev2 = mult2 * StDev(volume, maLength);
def upperDevVol1 =  volMA + volDev;
def volumeSpike = Crosses(volume, upperDevVol1, CrossingDirection.ABOVE);

#// Volume Contraction:
def lowerDevVol1 =  volMA - volDev2;
def volumeContraction = Crosses(volume, lowerDevVol1, CrossingDirection.BELOW) and !last;

#// Volume Trend:
def increasingTrend = barssince(volume < volMA) > trendSensitivity;
def decreasingTrend = barssince(volume > volMA) > trendSensitivity;

#// Rendering {
AddChartBubble(bullishDivergence, low, "Bull", Color.GREEN, no);
AddChartBubble(bearishDivergence,high, "Bear", Color.RED, yes);


plot VolSpike = if volumeSpike then high+ATR(3)/2 else na;#, "Volume Spike"
VolSpike.SetLineWeight(2);
VolSpike.SetPaintingStrategy(PaintingStrategy.SQUARES);
VolSpike.SetDefaultColor(Color.CYAN);
plot VolContraction = if volumeContraction then high+ATR(3)/2 else na;# "Volume Contraction"
VolContraction.SetLineWeight(2);
VolContraction.SetPaintingStrategy(PaintingStrategy.SQUARES);
VolContraction.SetDefaultColor(Color.MAGENTA);

plot StrongTrend = if increasingTrend and !increasingTrend[1] then low else na;#"Strong Trend",
StrongTrend.SetLineWeight(2);
StrongTrend.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
StrongTrend.SetDefaultColor(CreateColor(77, 160, 255));
plot WeakTrend = if decreasingTrend and !decreasingTrend[1] then high else na;#, "Weak Trend"
WeakTrend.SetLineWeight(2);
WeakTrend.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
WeakTrend.SetDefaultColor(Color.LIGHT_RED);


#--- END of CODE

Lower plot:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © cyatophilum
#indicator("Volume Divergence Indicator",overlay= true)
# Converted by Sam4Cok@Samer800    - 05/2023
declare lower;

#// Inputs {
input priceLookBack             = 20;  # 'Price and volume Lookback'
input bull_Sensitivity          = 3;   # 'Bull Sensitivity'
input bear_Sensitivity          = 4;   # 'Bear Sensitivity'
input VolumeMovAvgLength        = 20;  # 'Volume MA length'
input SensitivityOfSpikes       = 3;   # 'Sensitivity of Spikes'
input SensitivityOfContractions = 1;   # 'Sensitivity of Contractions'
input trendSensitivity          = 5;   # 'Trend sensitivity'

def mult = SensitivityOfSpikes;
def mult2 = SensitivityOfContractions;
def maLength = VolumeMovAvgLength;
def bullSensitivity = bull_Sensitivity + 1;
def bearSensitivity = bear_Sensitivity + 1;
def na = Double.NaN;
def last = IsNaN(close);

#barssince(Condition) =>
script barssince {
    input Condition = 0;
    def barssince = if Condition then 0 else barssince[1] + 1;
    plot return = barssince;
}
#// Algorithm {
#// Bullish Divergence
def priceLowerLows    = Lowest(low, priceLookBack) == Lowest(low, priceLookBack * bullSensitivity);
def lowVol1 = Lowest(volume, priceLookBack);
def lowVol2 = Lowest(volume, priceLookBack * bullSensitivity);
def volumeHigherLows  = lowVol1 > lowVol2;
def bulDiv = priceLowerLows and volumeHigherLows;
def bullishDivergence = priceLowerLows and volumeHigherLows and !bulDiv[1];

#// Bearish Divergence
def priceHigherHighs  = Highest(high, priceLookBack) == Highest(high, priceLookBack * bearSensitivity);
def highVol1 = Highest(volume, priceLookBack);
def highVol2 = Highest(volume, priceLookBack * bearSensitivity);
def volumeLowerHighs  = highVol1 < highVol2;
def bearDiv = priceHigherHighs and volumeLowerHighs;
def bearishDivergence = priceHigherHighs and volumeLowerHighs and !bearDiv[1];

#// Volume Spike:
def volMA = Average(volume, maLength);
def volDev = mult * StDev(volume, maLength);
def volDev2 = mult2 * StDev(volume, maLength);
def upperDevVol1 =  volMA + volDev;
def volumeSpike = Crosses(volume, upperDevVol1, CrossingDirection.ABOVE);

#// Volume Contraction:
def lowerDevVol1 =  volMA - volDev2;
def volumeContraction = Crosses(volume, lowerDevVol1, CrossingDirection.BELOW) and !last;

#// Volume Trend:
def increasingTrend = barssince(volume < volMA) > trendSensitivity;
def decreasingTrend = barssince(volume > volMA) > trendSensitivity;

plot pvolMA = volMA;#, 'Volume MA',
pvolMA.SetLineWeight(2);
pvolMA.AssignValueColor(if increasingTrend then Color.GREEN else
                        if decreasingTrend then color.red else Color.WHITE);

plot pdevVol1 = upperDevVol1;#, 'stdevVol1',
pdevVol1.SetLineWeight(2);
pdevVol1.SetDefaultColor(Color.DARK_GREEN);
plot pdevVol2 = lowerDevVol1;#, 'stdevVol2',color.rgb(255, 82, 82, 50),
pdevVol2.SetLineWeight(2);
pdevVol2.SetDefaultColor(Color.DARK_RED);

AddCloud(pdevVol1,pvolMA, Color.DARK_GREEN);
AddCloud(pvolMA,pdevVol2, Color.DARK_RED);

plot vol = Volume;
vol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
vol.AssignValueColor(if bullishDivergence then color.lime else
                     if bearishDivergence then color.red else       
                     if volumeSpike then color.yellow else
                     if volumeContraction then color.CYAN else CreateColor(119, 119, 119));



#--- 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
367 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