ATR + Volume Spikes For ThinkOrSwim

OGK

Member
VIP
The author states:
The Average True Range (ATR) indicator is a technical analysis tool that measures the volatility of an asset. It can be used to create a trading strategy by identifying periods of high volatility and making trades based on those conditions.

Here is an example of a simple ATR trading strategy:

Calculate the ATR for the asset you are trading. This can typically be done using a charting platform or software.

Identify the average ATR over a period of time (such as 14 days). This will be your "threshold" for determining high volatility.

When the current ATR is above the threshold, enter a long position (buy) in the asset.

When the current ATR is below the threshold, exit the long position (sell) and wait for the next period of high volatility.

Repeat the process for the next period of time.

This is a basic example of an ATR strategy and can be adjusted as per one's preference, you can add other indicators or market conditions to filter out trades and also use different time frame to check the ATR values. ATR can also be used in combination with other indicators and strategies to improve the accuracy of your trades.
3hBAaif.png


Here is the original Tradingview code:
https://in.tradingview.com/script/h1Eek2pP-RAHUL-ATR-Volume-Spikes/

For the new ThinkOrSwim code, you must scroll down to the next post
@antwerks
 
Last edited by a moderator:
I have been trying for the longest time to convert this to TOS and apparently something about array function (I have been using chatgpt, deepseek, gemini ) and no luck in getting it to look like the TV version. mine looks most like a regular ATR and does not have the same curvy trails like the TV version. I am praying to God that samer800 may have a solution :)

samer800 thanks in advance.​


TradingView Publication Link
check the below:

CSS:
# // Indciator for TOS
#//@jayalekshmiaishu
#study(title="RAHUL ATR + Volume Spikes", overlay=true)
# Converted by Sam4Cok@Samer800    - 04/2025

input unusualVolumeBarColor = yes;
input atrPeriod = 20; #, "Period")
input atrMultiplier = 4.0; #, "Multiplier"
input Source = close; #, title="Source")
input movAvgLength = 20; #, minval=1, title="Smooth")
input VolumeSmaLength = 20; #, "Volume SMA length", minval=1)

def na = Double.NaN;
def last = IsNaN(close);

#vwma(source, length)
script VWMA {
input src = close;
input len = 14;
    def vol = volume/1000;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    Plot result = VWMA;
}

def atr = ATR(Length = atrPeriod);
def nLoss = atrMultiplier * atr;
def xATRTrailingStop = If(close > xATRTrailingStop[1] and close[1] > xATRTrailingStop[1], Max(xATRTrailingStop[1], close - nLoss), If(close < xATRTrailingStop[1] and close[1] < xATRTrailingStop[1], Min(xATRTrailingStop[1], close + nLoss), If(close > xATRTrailingStop[1], close - nLoss, close + nLoss)));

def pos = If(close[1] < xATRTrailingStop[1] and close > xATRTrailingStop[1], 1, If(close[1] > xATRTrailingStop[1] and close < xATRTrailingStop[1], -1, pos[1]));

def isLong;
def isShort;
def LONG = !isLong[1] and pos == 1;
def SHORT = !isShort[1] and pos == -1;

if LONG {
    isLong = yes;
    isShort = no;
} else if SHORT {
    isLong = no;
    isShort = yes;
} else {
    isLong = isLong[1];
    isShort = isShort[1];
}
def vol = volume / 1000;
def vwma = vwma(Source, movAvgLength);
def avg1 = (vwma + xATRTrailingStop) / 2;
def vol_avg = Average(vol, VolumeSmaLength);
def unusual_vol_down = unusualVolumeBarColor and vol > vol_avg * 1.2 and close < open;
def unusual_vol_up = unusualVolumeBarColor and vol > vol_avg * 1.2 and close > open;
def barHighlightColor = if unusual_vol_down then -1 else
                        if unusual_vol_up then 1 else 0;

#-- Plot
plot avgLine = if avg1 then avg1 else na;
avgLine.SetLineWeight(2);
avgLine.SetDefaultColor(Color.LIGHT_ORANGE);

#-- Signals
AddChartBubble(SHORT, high, "S", Color.RED);
AddChartBubble(LONG, low, "B", Color.GREEN, no);

#-- Bar Color
AssignPriceColor(if barHighlightColor > 0 then Color.CYAN else
                 if barHighlightColor < 0 then Color.MAGENTA else Color.CURRENT);


#-- END of CODE
 
Last edited by a moderator:
check the below:

CSS:
# // Indciator for TOS
#//@jayalekshmiaishu
#study(title="RAHUL ATR + Volume Spikes", overlay=true)
# Converted by Sam4Cok@Samer800    - 04/2025

input unusualVolumeBarColor = yes;
input atrPeriod = 20; #, "Period")
input atrMultiplier = 4.0; #, "Multiplier"
input Source = close; #, title="Source")
input movAvgLength = 20; #, minval=1, title="Smooth")
input VolumeSmaLength = 20; #, "Volume SMA length", minval=1)

def na = Double.NaN;
def last = IsNaN(close);

#vwma(source, length)
script VWMA {
input src = close;
input len = 14;
    def vol = volume/1000;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    Plot result = VWMA;
}

def atr = ATR(Length = atrPeriod);
def nLoss = atrMultiplier * atr;
def xATRTrailingStop = If(close > xATRTrailingStop[1] and close[1] > xATRTrailingStop[1], Max(xATRTrailingStop[1], close - nLoss), If(close < xATRTrailingStop[1] and close[1] < xATRTrailingStop[1], Min(xATRTrailingStop[1], close + nLoss), If(close > xATRTrailingStop[1], close - nLoss, close + nLoss)));

def pos = If(close[1] < xATRTrailingStop[1] and close > xATRTrailingStop[1], 1, If(close[1] > xATRTrailingStop[1] and close < xATRTrailingStop[1], -1, pos[1]));

def isLong;
def isShort;
def LONG = !isLong[1] and pos == 1;
def SHORT = !isShort[1] and pos == -1;

if LONG {
    isLong = yes;
    isShort = no;
} else if SHORT {
    isLong = no;
    isShort = yes;
} else {
    isLong = isLong[1];
    isShort = isShort[1];
}
def vol = volume / 1000;
def vwma = vwma(Source, movAvgLength);
def avg1 = (vwma + xATRTrailingStop) / 2;
def vol_avg = Average(vol, VolumeSmaLength);
def unusual_vol_down = unusualVolumeBarColor and vol > vol_avg * 1.2 and close < open;
def unusual_vol_up = unusualVolumeBarColor and vol > vol_avg * 1.2 and close > open;
def barHighlightColor = if unusual_vol_down then -1 else
                        if unusual_vol_up then 1 else 0;

#-- Plot
plot avgLine = if avg1 then avg1 else na;
avgLine.SetLineWeight(2);
avgLine.SetDefaultColor(Color.LIGHT_ORANGE);

#-- Signals
AddChartBubble(SHORT, high, "S", Color.RED);
AddChartBubble(LONG, low, "B", Color.GREEN, no);

#-- Bar Color
AssignPriceColor(if barHighlightColor > 0 then Color.CYAN else
                 if barHighlightColor < 0 then Color.MAGENTA else Color.CURRENT);


#-- END of CODE
Thanks again Samer. I put in your coding and used it for several stocks. I didn't see any signals just the line. Where in the chart do the signals show up?
 
Thanks again Samer. I put in your coding and used it for several stocks. I didn't see any signals just the line. Where in the chart do the signals show up?
This study signals when ATR crosses above/below its threshold on the recommended 15min timeframe
Here are several charts that had signals today:
shared grid link: http://tos.mx/!7MQ2ev9x
TBdcYFu.png


As the OP recommends using on a 15min chart. It is advised to first become familiar with this study on that aggregation. Because of the noise found on lower timeframes; trend indicators may provide sub-optimal or no results on lower aggregations.
 
Last edited by a moderator:
This study signals when ATR crosses above/below its threshold on the recommended 15min timeframe
Here are several charts that had signals today:
shared grid link: http://tos.mx/!7MQ2ev9x
TBdcYFu.png


As the OP recommends using on a 15min chart. It is advised to first become familiar with this study on that aggregation. Because of the noise found on lower timeframes; trend indicators may provide sub-optimal or no results on lower aggregations.
Thanks. I appreciate your fast and excellent response. Charts were helpful to show how the scan works and timeframe suggested.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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