ATR-Based Support & Resistance for ThinkorSwim

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

Ill urge people to try to use this on a two hour time frame on the SPY, i cant speak for other commodities, but it seems to do a great job on a two hour time frame, is there a way that we can combine standard error channels to this?
 
@BenTen Cool script. How can I predict or plot what the ATR clouds will be prior to market open? Currently, I'm using 14/Day Length/AggPeriod and I notice the ATR projection extends into after hours, but stops a few hours prior to market open. When I'm preparing for market open, I notice the ATR projection does not print until after the opening bell. For example, with GAPS (up or down) the ATR extremes (red/green clouds) would change once the market opens. I'd like to guestimate the projection for ATR clouds will be prior to market open. So, if the price of $XYZ drops from $50 to $45 I'd like to visualize where the new ATR clouds will print.
 
@Cecil If you disable extended hours and expand your chart, you'll see the zones being projected into the next day.

lcJgABd.png
 
@BenTen Thank you for the quick response. I accomplished this with your script, however, I've encountered another issue. I adopted a modified ATR calculation and successfully plotted the same. However, with this script I cannot get the extension to work.

I'm not quite sure why the following code won't allow for the zone projections:

Code:
input PercentPastFullATR = 0.5;
input PercentShortFullATR = 0.1;

input ATRAvgLength = 14;
input AggPeriod = AggregationPeriod.DAY;


def h = high(GetSymbol(), period = AggPeriod)[0];
def c = close(GetSymbol(), period = AggPeriod)[0];
def l = low(GetSymbol(), period = AggPeriod)[0];
def o = open(GetSymbol(), period = AggPeriod)[0];

def ATR = WildersAverage(TrueRange(high(period = AggPeriod), close(period = AggPeriod), low(period = AggPeriod)), ATRAvgLength);
def day_atr = ATR;

def ATRSatisfied = if h-l > ATR then 1 else 0;
def ATRAnchorLowFlag = if atrsatisfied == 1 and absValue(l-o) < absValue (h-o) then 1 else 0;
def day_low_calculated = if ATRsatisfied == 0 then l else if atrsatisfied == 1 and atranchorLowFlag == 1 then l else if atrsatisfied == 1 and atranchorLowFlag == 0 then h-atr else l;
def day_high_calculated = if ATRsatisfied == 0 then h else if atrsatisfied == 1 and atranchorLowFlag == 0 then h else if atrsatisfied == 1 and atranchorLowFlag == 1 then l+atr else l;

# #PLOT_SUPPORT AND RESISTANCE
#PLOT_RESISTANCE(RED)
plot ExcessPercentageOfFullATRAboveOpen = day_low_calculated + (day_atr * (1+PercentPastFullATR));
ExcessPercentageOfFullATRAboveOpen.assignValueColor(COLOR.DARK_RED);
ExcessPercentageOfFullATRAboveOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot FullATRAboveOpen = day_low_calculated + atr;
FullATRAboveOpen.assignValueColor(COLOR.DARK_RED);
AddCloud(ExcessPercentageOfFullATRAboveOpen, FullATRAboveOpen, COLOR.DARK_RED, COLOR.DARK_RED);
FullATRAboveOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot PercentageShortOfFullATRAboveOpen = day_low_calculated + (day_atr * (1-PercentShortFullATR));
PercentageShortOfFullATRAboveOpen.assignValueColor(COLOR.DARK_RED);
AddCloud(FullATRAboveOpen, PercentageShortOfFullATRAboveOpen, COLOR.DARK_RED, COLOR.DARK_RED);
PercentageShortOfFullATRAboveOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#PLOT_SUPPORT(GREEN)
plot PercentShortOfFullATRBelowOpen = day_high_calculated - (day_atr * (1-PercentShortFullATR));
PercentShortOfFullATRBelowOpen.assignValueColor(COLOR.LIGHT_GREEN);
PercentShortOfFullATRBelowOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot FullATRBelowOpen = day_high_calculated - day_atr;
FullATRBelowOpen.assignValueColor(COLOR.LIGHT_GREEN);
AddCloud(FullATRBelowOpen, PercentShortOfFullATRBelowOpen, COLOR.LIGHT_GREEN, COLOR.LIGHT_GREEN);
FullATRBelowOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot ExcessPercentageOfFullATRBelowOpen = day_high_calculated - (day_atr * (1+PercentPastFullATR));
ExcessPercentageOfFullATRBelowOpen.assignValueColor(COLOR.LIGHT_GREEN);
AddCloud(ExcessPercentageOfFullATRBelowOpen, FullATRBelowOpen, COLOR.LIGHT_GREEN, COLOR.LIGHT_GREEN);
ExcessPercentageOfFullATRBelowOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
@Cecil That's because it's using values of the current day instead of the previous day. No way it's going to project the zones forward.
 
@BenTen Forgive me, but which line and value are you referring to? And secondly, is there a way to keep my ATR calculations (which I find to be more accurate in my strategy) and project a zone for them?
 
@Cecil Compare this part of your code to mine:

Code:
def h = high(GetSymbol(), period = AggPeriod)[0];
def c = close(GetSymbol(), period = AggPeriod)[0];
def l = low(GetSymbol(), period = AggPeriod)[0];
def o = open(GetSymbol(), period = AggPeriod)[0];
 
@BenTen I changed the [0] to [1] on all these but it didn't make the desire projection. I'm assuming because of this section of code:

def ATRSatisfied = if h-l > ATR then 1 else 0;
def ATRAnchorLowFlag = if atrsatisfied == 1 and absValue(l-o) < absValue (h-o) then 1 else 0;
def day_low_calculated = if ATRsatisfied == 0 then l else if atrsatisfied == 1 and atranchorLowFlag == 1 then l else if atrsatisfied == 1 and atranchorLowFlag == 0 then h-atr else l;
def day_high_calculated = if ATRsatisfied == 0 then h else if atrsatisfied == 1 and atranchorLowFlag == 0 then h else if atrsatisfied == 1 and atranchorLowFlag == 1 then l+atr else l;


@vmseller You may recognize this code. Wondering if you're familiar with a way to project the zone calculation into the upcoming market open?
 
Last edited:
Ultimate ATR Combo
This has
  • 2 ATR's with values 10,3 & 30,9.
  • I find the 10,3 vs 10, 1.75 holds me in trades a little longer,
  • but I do like the 1.75 for shorter trades.
a1.png

Here is my code that colors the bars.
Ruby:
# Ultimate ATR Combo
# Investing to Give
declare upper;
input averageType = AverageType.EXPONENTIAL;

# Fast ATR TS
input trailType = {default modified, unmodified};
input ATRPeriod = 10;
input ATRFactor = 3.0;
input firstTrade = {default long, short};

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
then high - close[1]
else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);

def trueRange;
switch (trailType) {
case modified:
trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
trueRange = TrueRange(high, close, low);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
switch (firstTrade) {
case long:
state = state.long;
trail = close - loss;
case short:
state = state.short;
trail = close + loss;
}
} else {
state = state.init;
trail = Double.NaN;
}
case long:
if (close > trail[1]) {
state = state.long;
trail = Max(trail[1], close - loss);
} else {
state = state.short;
trail = close + loss;
}
case short:
if (close < trail[1]) {
state = state.short;
trail = Min(trail[1], close + loss);
} else {
state = state.long;
trail = close - loss;
}
}

def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);

def Buy_Zone = state == state.long;
def Sell_Zone = state == state.short;

# plot Buy = Buy_Zone[1] == 0 and Buy_Zone;
# plot Sell = Sell_Zone[1] == 0 and Sell_Zone;

plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.Line);
TrailingStop.DefineColor("Buy", GetColor(5));
TrailingStop.DefineColor("Sell", GetColor(6));
TrailingStop.SetLineWeight(3);
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")
else TrailingStop.Color("Buy"));


# Slow ATR TS
input trailType_Slow = {default modified_Slow, unmodified_Slow};
input ATRPeriod_Slow = 30;
input ATRFactor_Slow = 10.0;
input firstTrade_Slow = {default long_Slow, short_Slow};

Assert(ATRFactor_Slow > 0, "'atr factor' must be positive: " + ATRFactor_Slow);

def HiLo_Slow = Min(high - low, 1.5 * Average(high - low, ATRPeriod_Slow));
def HRef_Slow = if low <= high[1]
then high - close[1]
else (high - close[1]) - 0.5 * (low - high[1]);
def LRef_Slow = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);

def trueRange_Slow;
switch (trailType_Slow) {
case modified_Slow:
trueRange_Slow = Max(HiLo_Slow, Max(HRef_Slow, LRef_Slow));
case unmodified_Slow:
trueRange_Slow = TrueRange(high, close, low);
}
def loss_Slow = ATRFactor_Slow * MovingAverage(averageType, trueRange_Slow, ATRPeriod_Slow);

def state_Slow = {default init_Slow, long_Slow, short_Slow};
def trail_Slow;
switch (state_Slow[1]) {
case init_Slow:
if (!IsNaN(loss_Slow)) {
switch (firstTrade_Slow) {
case long_Slow:
state_Slow = state_Slow.long_Slow;
trail_Slow = close - loss_Slow;
case short_Slow:
state_Slow = state_Slow.short_Slow;
trail_Slow = close + loss_Slow;
}
} else {
state_Slow = state_Slow.init_Slow;
trail_Slow = Double.NaN;
}
case long_Slow:
if (close > trail_Slow[1]) {
state_Slow = state_Slow.long_Slow;
trail_Slow = Max(trail_Slow[1], close - loss_Slow);
} else {
state_Slow = state_Slow.short_Slow;
trail_Slow = close + loss_Slow;
}
case short_Slow:
if (close < trail_Slow[1]) {
state_Slow = state_Slow.short_Slow;
trail_Slow = Min(trail_Slow[1], close + loss_Slow);
} else {
state_Slow = state_Slow.long_Slow;
trail_Slow = close - loss_Slow;
}
}

def BuySignal_Slow = Crosses(state_Slow == state_Slow.long_Slow, 0, CrossingDirection.ABOVE);
def SellSignal_Slow = Crosses(state_Slow == state_Slow.short_Slow, 0, CrossingDirection.ABOVE);

def Buy_Zone_Slow = state_Slow == state_Slow.long_Slow;
def Sell_Zone_Slow = state_Slow == state_Slow.short_Slow;

plot TrailingStop_Slow = trail_Slow;

TrailingStop_Slow.SetPaintingStrategy(PaintingStrategy.Line);
TrailingStop_Slow.DefineColor("Buy", GetColor(5));
TrailingStop_Slow.DefineColor("Sell", GetColor(6));
TrailingStop_Slow.SetLineWeight(3);
TrailingStop_Slow.AssignValueColor(if state_Slow == state_Slow.long_Slow
then TrailingStop_Slow.Color("Sell")
else TrailingStop_Slow.Color("Buy"));




def Buy = Buy_Zone and Buy_Zone_Slow;
def Sell = Sell_Zone and Sell_Zone_Slow;

AssignPriceColor (if Buy then Color.GREEN else if Sell then Color.RED else Color.YELLOW);
# End
 
Last edited by a moderator:
Found this awesome script with cool labels and great support resistance lines/clouds but I want to see only the lines for today. I would love to have the lines up but its just too cluttered showing every day. Please take a look for me to show "today only". Thank you in advance.
Ruby:
# Saty_ATR_Levels
# Copyright (C) 2022 Saty Mahajan
# Author is not responsible for your trading using this script.
# Data provided in this script is not financial advice.
#
# Special thanks to Gabriel Viana.
# Based on my own ideas and ideas from Ripster, drippy2hard,
# Adam Sliver, and others.
#
# Features:
# - Day, Multiday, Swing, Position trading modes
# - Range against ATR for each period
# - Put and call trigger levels
# - Mid-range level
# - Full-range levels
# - Optional Extension Level

input trading_type = {default "Day", "Multiday", "Swing", "Position"};
input use_options_labels = yes;
input atr_Length = 14;
input trigger_percentage = 0.236;
input use_current_close = no;
input cloud_size = 1.0;
input close_cloud_on = yes;
input lower_trigger_cloud_on = yes;
input lower_middle_cloud_on = yes;
input lower_atr_cloud_on = yes;
input lower_extension_cloud_on = no;
input upper_trigger_cloud_on = yes;
input upper_middle_cloud_on = yes;
input upper_atr_cloud_on = yes;
input upper_extension_cloud_on = no;
input fluid_trend_low_ema = 5;
input fluid_trend_high_ema = 12;
input trend_low_ema = 34;
input trend_high_ema = 50;
input show_only_current_trading_type = yes;
input priceclose = close;

DefineGlobalColor("Close Cloud", Color.WHITE);
DefineGlobalColor("Lower Trigger Cloud", color.light_red);
DefineGlobalColor("Lower Middle Cloud", Color.GRAY);
DefineGlobalColor("Lower ATR Cloud", Color.WHITE);
DefineGlobalColor("Lower Extension Cloud", Color.GRAY);
DefineGlobalColor("Upper Trigger Cloud", Color.uptick);
DefineGlobalColor("Upper Middle Cloud", Color.GRAY);
DefineGlobalColor("Upper ATR Cloud", Color.WHITE);
DefineGlobalColor("Upper Extension Cloud", Color.GRAY);

def trading_period;
switch (trading_type) {
case "Day":
    trading_period = AggregationPeriod.DAY;
case "Multiday":
    trading_period = AggregationPeriod.WEEK;
case "Swing":
    trading_period = AggregationPeriod.MONTH;
case "Position":
    trading_period = AggregationPeriod.QUARTER;
}

# Trend
def price = close;
def fluid_trend_low = MovAvgExponential(price, fluid_trend_low_ema);
def fluid_trend_high = MovAvgExponential(price, fluid_trend_high_ema);
def fluid_bullish = price >= fluid_trend_low and price >= fluid_trend_high;
def fluid_bearish = price <= fluid_trend_low and price <= fluid_trend_high;
def trend_low = MovAvgExponential(price, trend_low_ema);
def trend_high = MovAvgExponential(price, trend_high_ema);
def bullish = price >= trend_low and price >= trend_high;
def bearish = price <= trend_low and price <= trend_high;

# Data
def previous_close = close(period = trading_period)[if use_current_close then 0 else 1];
def atr = Round(WildersAverage(TrueRange(high(period = trading_period), close(period = trading_period), low(period = trading_period)), atr_Length)[if use_current_close then 0 else 1], 2);
def period_high = Highest(high(period = trading_period), 1);
def period_low = Lowest(low(period = trading_period), 1);
def tr = period_high - period_low;
def tr_percent_of_atr = Round((tr / atr) * 100, 0);
def lower_trigger = previous_close - (trigger_percentage * atr);
def upper_trigger = previous_close + (trigger_percentage * atr);
def lower_atr = previous_close - atr;
def upper_atr = previous_close + atr;
def lower_middle = previous_close - (atr * 0.618);
def upper_middle = previous_close + (atr * 0.618);
def lower_extension = (lower_atr) - (atr * 0.618);
def upper_extension = (upper_atr) + (atr * 0.618);

def rangeNotAvailable = IsNaN(tr);
def atrNotAvailable = IsNaN(atr);







# Labels
#AddLabel(yes,"Trend ",if (fluid_bullish and bullish) then Color.GREEN else if (fluid_bearish and bearish) then Color.RED else Color.ORANGE);
#AddLabel (yes, (if trading_period == AggregationPeriod.QUARTER then "Position "  else if trading_period == AggregationPeriod.MONTH then "Swing " else if trading_period == AggregationPeriod.WEEK then "Multiday " else "Day ") + (if (rangeNotAvailable) then "Range N/A Pre-Market" else if (atrNotAvailable) then " Range - Not Enough Data" else "Range ($" + Round (tr , 2) + ") is " + Round (tr_percent_of_atr, 0) + "% of ATR ($" + Round (atr, 2) + ")   ") , (if (rangeNotAvailable or atrNotAvailable) then Color.GRAY else (if tr_percent_of_atr <= 70 then Color.GREEN else if tr_percent_of_atr >= 90 then Color.RED else Color.ORANGE)));





#AddLabel (yes, (if use_options_labels then "Calls" else "Long") + " > $" + Round (upper_trigger, 2) + " | +1 ATR: $" + Round (upper_atr, 2) + "   ", GlobalColor("Upper Trigger Cloud"));

#addlabel(yes,"$" + Round(previous_close,2), (if priceclose > previous_close then Color.uptick else if priceclose < previous_close then Color.light_red else Color.LIGHT_GRAY));

#AddLabel (yes, (if use_options_labels then "Puts" else "Short") + " < $" + Round (lower_trigger, 2) + " | -1 ATR: $" +  Round (lower_atr, 2) + "   ", GlobalColor("Lower Trigger Cloud"));




######AddLabel(priceclose, "50 EMA— " + Round (upper_trigger,2), (if priceclose > upper_trigger then Color.uptick else if priceclose < upper_trigger then Color.light_red else Color.LIGHT_GRAY));



AddLabel(priceclose, (if use_options_labels then "Calls" else "Long") + " above $" + Round (upper_trigger, 2) + "  |  +1 ATR: $" + Round (upper_atr, 2) + "   ", (if priceclose > upper_trigger then Color.uptick else if priceclose < upper_trigger then Color.gray else Color.LIGHT_GRAY));


addlabel(yes,"$" + Round(previous_close,2), (if priceclose > previous_close then Color.uptick else if priceclose < previous_close then Color.light_red else Color.LIGHT_GRAY));



AddLabel(priceclose, (if use_options_labels then "Puts" else "Short") + " below $" + Round (lower_trigger, 2) + "  |  -1 ATR: $" +  Round (lower_atr, 2) + "   ", (if priceclose < lower_trigger then Color.light_red else if priceclose > lower_trigger then Color.gray else Color.LIGHT_GRAY));






def cloud_size_factor = 0.01 * cloud_size;

# Previous Close cloud
AddCloud(if close_cloud_on then (previous_close - (atr * cloud_size_factor / 2)) else Double.NaN, (previous_close + (atr * cloud_size_factor / 2)), GlobalColor("Close Cloud"), GlobalColor("Close Cloud"));

# Trigger clouds
AddCloud(if lower_trigger_cloud_on then lower_trigger else Double.NaN, (lower_trigger - (atr * cloud_size_factor)), GlobalColor("Lower Trigger Cloud"), GlobalColor("Lower Trigger Cloud"));
AddCloud(if upper_trigger_cloud_on then upper_trigger else Double.NaN, (upper_trigger + (atr * cloud_size_factor)), GlobalColor("Upper Trigger Cloud"), GlobalColor("Upper Trigger Cloud"));

# Middle clouds
AddCloud(if lower_middle_cloud_on then lower_middle else Double.NaN, (lower_middle - (atr * cloud_size_factor)), GlobalColor("Lower Middle Cloud"), GlobalColor("Lower Middle Cloud"));
AddCloud(if upper_middle_cloud_on then upper_middle else Double.NaN, (upper_middle + (atr * cloud_size_factor)), GlobalColor("Upper Middle Cloud"), GlobalColor("Upper Middle Cloud"));

# +/- 1 ATR clouds
AddCloud(if lower_atr_cloud_on then lower_atr else Double.NaN, (lower_atr - (atr * cloud_size_factor)), GlobalColor("Lower ATR Cloud"), GlobalColor("Lower ATR Cloud"));
AddCloud(if upper_atr_cloud_on then upper_atr else Double.NaN, (upper_atr + (atr * cloud_size_factor)), GlobalColor("Upper ATR Cloud"), GlobalColor("Upper ATR Cloud"));

# Fib Extension clouds
AddCloud(if lower_extension_cloud_on then lower_extension else Double.NaN, (lower_extension - (atr * cloud_size_factor)), GlobalColor("Lower Extension Cloud"), GlobalColor("Lower Extension Cloud"));
AddCloud(if upper_extension_cloud_on then upper_extension else Double.NaN, (upper_extension + (atr * cloud_size_factor)), GlobalColor("Upper Extension Cloud"), GlobalColor("Upper Extension Cloud"));
 
Found this awesome script with cool labels and great support resistance lines/clouds but I want to see only the lines for today. I would love to have the lines up but its just too cluttered showing every day. Please take a look for me to show "today only". Thank you in advance.
Ruby:
# Saty_ATR_Levels
# Copyright (C) 2022 Saty Mahajan
# Author is not responsible for your trading using this script.
# Data provided in this script is not financial advice.
#
# Special thanks to Gabriel Viana.
# Based on my own ideas and ideas from Ripster, drippy2hard,
# Adam Sliver, and others.
#
# Features:
# - Day, Multiday, Swing, Position trading modes
# - Range against ATR for each period
# - Put and call trigger levels
# - Mid-range level
# - Full-range levels
# - Optional Extension Level

input trading_type = {default "Day", "Multiday", "Swing", "Position"};
input use_options_labels = yes;
input atr_Length = 14;
input trigger_percentage = 0.236;
input use_current_close = no;
input cloud_size = 1.0;
input close_cloud_on = yes;
input lower_trigger_cloud_on = yes;
input lower_middle_cloud_on = yes;
input lower_atr_cloud_on = yes;
input lower_extension_cloud_on = no;
input upper_trigger_cloud_on = yes;
input upper_middle_cloud_on = yes;
input upper_atr_cloud_on = yes;
input upper_extension_cloud_on = no;
input fluid_trend_low_ema = 5;
input fluid_trend_high_ema = 12;
input trend_low_ema = 34;
input trend_high_ema = 50;
input show_only_current_trading_type = yes;
input priceclose = close;

DefineGlobalColor("Close Cloud", Color.WHITE);
DefineGlobalColor("Lower Trigger Cloud", color.light_red);
DefineGlobalColor("Lower Middle Cloud", Color.GRAY);
DefineGlobalColor("Lower ATR Cloud", Color.WHITE);
DefineGlobalColor("Lower Extension Cloud", Color.GRAY);
DefineGlobalColor("Upper Trigger Cloud", Color.uptick);
DefineGlobalColor("Upper Middle Cloud", Color.GRAY);
DefineGlobalColor("Upper ATR Cloud", Color.WHITE);
DefineGlobalColor("Upper Extension Cloud", Color.GRAY);

def trading_period;
switch (trading_type) {
case "Day":
    trading_period = AggregationPeriod.DAY;
case "Multiday":
    trading_period = AggregationPeriod.WEEK;
case "Swing":
    trading_period = AggregationPeriod.MONTH;
case "Position":
    trading_period = AggregationPeriod.QUARTER;
}

# Trend
def price = close;
def fluid_trend_low = MovAvgExponential(price, fluid_trend_low_ema);
def fluid_trend_high = MovAvgExponential(price, fluid_trend_high_ema);
def fluid_bullish = price >= fluid_trend_low and price >= fluid_trend_high;
def fluid_bearish = price <= fluid_trend_low and price <= fluid_trend_high;
def trend_low = MovAvgExponential(price, trend_low_ema);
def trend_high = MovAvgExponential(price, trend_high_ema);
def bullish = price >= trend_low and price >= trend_high;
def bearish = price <= trend_low and price <= trend_high;

# Data
def previous_close = close(period = trading_period)[if use_current_close then 0 else 1];
def atr = Round(WildersAverage(TrueRange(high(period = trading_period), close(period = trading_period), low(period = trading_period)), atr_Length)[if use_current_close then 0 else 1], 2);
def period_high = Highest(high(period = trading_period), 1);
def period_low = Lowest(low(period = trading_period), 1);
def tr = period_high - period_low;
def tr_percent_of_atr = Round((tr / atr) * 100, 0);
def lower_trigger = previous_close - (trigger_percentage * atr);
def upper_trigger = previous_close + (trigger_percentage * atr);
def lower_atr = previous_close - atr;
def upper_atr = previous_close + atr;
def lower_middle = previous_close - (atr * 0.618);
def upper_middle = previous_close + (atr * 0.618);
def lower_extension = (lower_atr) - (atr * 0.618);
def upper_extension = (upper_atr) + (atr * 0.618);

def rangeNotAvailable = IsNaN(tr);
def atrNotAvailable = IsNaN(atr);







# Labels
#AddLabel(yes,"Trend ",if (fluid_bullish and bullish) then Color.GREEN else if (fluid_bearish and bearish) then Color.RED else Color.ORANGE);
#AddLabel (yes, (if trading_period == AggregationPeriod.QUARTER then "Position "  else if trading_period == AggregationPeriod.MONTH then "Swing " else if trading_period == AggregationPeriod.WEEK then "Multiday " else "Day ") + (if (rangeNotAvailable) then "Range N/A Pre-Market" else if (atrNotAvailable) then " Range - Not Enough Data" else "Range ($" + Round (tr , 2) + ") is " + Round (tr_percent_of_atr, 0) + "% of ATR ($" + Round (atr, 2) + ")   ") , (if (rangeNotAvailable or atrNotAvailable) then Color.GRAY else (if tr_percent_of_atr <= 70 then Color.GREEN else if tr_percent_of_atr >= 90 then Color.RED else Color.ORANGE)));





#AddLabel (yes, (if use_options_labels then "Calls" else "Long") + " > $" + Round (upper_trigger, 2) + " | +1 ATR: $" + Round (upper_atr, 2) + "   ", GlobalColor("Upper Trigger Cloud"));

#addlabel(yes,"$" + Round(previous_close,2), (if priceclose > previous_close then Color.uptick else if priceclose < previous_close then Color.light_red else Color.LIGHT_GRAY));

#AddLabel (yes, (if use_options_labels then "Puts" else "Short") + " < $" + Round (lower_trigger, 2) + " | -1 ATR: $" +  Round (lower_atr, 2) + "   ", GlobalColor("Lower Trigger Cloud"));




######AddLabel(priceclose, "50 EMA— " + Round (upper_trigger,2), (if priceclose > upper_trigger then Color.uptick else if priceclose < upper_trigger then Color.light_red else Color.LIGHT_GRAY));



AddLabel(priceclose, (if use_options_labels then "Calls" else "Long") + " above $" + Round (upper_trigger, 2) + "  |  +1 ATR: $" + Round (upper_atr, 2) + "   ", (if priceclose > upper_trigger then Color.uptick else if priceclose < upper_trigger then Color.gray else Color.LIGHT_GRAY));


addlabel(yes,"$" + Round(previous_close,2), (if priceclose > previous_close then Color.uptick else if priceclose < previous_close then Color.light_red else Color.LIGHT_GRAY));



AddLabel(priceclose, (if use_options_labels then "Puts" else "Short") + " below $" + Round (lower_trigger, 2) + "  |  -1 ATR: $" +  Round (lower_atr, 2) + "   ", (if priceclose < lower_trigger then Color.light_red else if priceclose > lower_trigger then Color.gray else Color.LIGHT_GRAY));






def cloud_size_factor = 0.01 * cloud_size;

# Previous Close cloud
AddCloud(if close_cloud_on then (previous_close - (atr * cloud_size_factor / 2)) else Double.NaN, (previous_close + (atr * cloud_size_factor / 2)), GlobalColor("Close Cloud"), GlobalColor("Close Cloud"));

# Trigger clouds
AddCloud(if lower_trigger_cloud_on then lower_trigger else Double.NaN, (lower_trigger - (atr * cloud_size_factor)), GlobalColor("Lower Trigger Cloud"), GlobalColor("Lower Trigger Cloud"));
AddCloud(if upper_trigger_cloud_on then upper_trigger else Double.NaN, (upper_trigger + (atr * cloud_size_factor)), GlobalColor("Upper Trigger Cloud"), GlobalColor("Upper Trigger Cloud"));

# Middle clouds
AddCloud(if lower_middle_cloud_on then lower_middle else Double.NaN, (lower_middle - (atr * cloud_size_factor)), GlobalColor("Lower Middle Cloud"), GlobalColor("Lower Middle Cloud"));
AddCloud(if upper_middle_cloud_on then upper_middle else Double.NaN, (upper_middle + (atr * cloud_size_factor)), GlobalColor("Upper Middle Cloud"), GlobalColor("Upper Middle Cloud"));

# +/- 1 ATR clouds
AddCloud(if lower_atr_cloud_on then lower_atr else Double.NaN, (lower_atr - (atr * cloud_size_factor)), GlobalColor("Lower ATR Cloud"), GlobalColor("Lower ATR Cloud"));
AddCloud(if upper_atr_cloud_on then upper_atr else Double.NaN, (upper_atr + (atr * cloud_size_factor)), GlobalColor("Upper ATR Cloud"), GlobalColor("Upper ATR Cloud"));

# Fib Extension clouds
AddCloud(if lower_extension_cloud_on then lower_extension else Double.NaN, (lower_extension - (atr * cloud_size_factor)), GlobalColor("Lower Extension Cloud"), GlobalColor("Lower Extension Cloud"));
AddCloud(if upper_extension_cloud_on then upper_extension else Double.NaN, (upper_extension + (atr * cloud_size_factor)), GlobalColor("Upper Extension Cloud"), GlobalColor("Upper Extension Cloud"));

add this code to your code, before the addclouds.
change the if then in each cloud to include
x and

Code:
# pick, just today or all

input today_only = yes;

def istoday = if GetLastDay() == GetDay() then 1 else 0;

def x = if today_only then istoday else 1;


this modified line has x added to it, to control when the cloud is drawn.

# Previous Close cloud
AddCloud(if x and close_cloud_on then (previous_close - (atr * cloud_size_factor / 2)) else Double.NaN, (previous_close + (atr * cloud_size_factor / 2)), GlobalColor("Close Cloud"), GlobalColor("Close Cloud"));
 
By default, the indicator uses the "DAY" option. However, if you would like to play around with other timeframe aggregation, then you can do so from the indicator's setting. I found it interesting when applying the Monthly or OPT_EXP (aggregation period equal to option expiration) onto the Daily chart.

T4KVQhs.png

pz4G1gS.png


thinkScript Code

Code:
# ATR Grid (ATR-Based Support & Resistance)
# Assembled by BenTen and @diazlaz at useThinkScript.com
# Converted from https://www.tradingview.com/script/EDN4oFyQ-ATR-0-5-0-7-ranges/

input lvl1 = 0.5;
input lvl2 = 0.7;
input daily_atr_len = 15;

input length = 15;
input AggPeriod = AggregationPeriod.DAY;
input averageType = AverageType.WILDERS;

def h = high(GetSymbol(), period = AggPeriod)[1];
def c = close(GetSymbol(), period = AggPeriod)[1];
def l = low(GetSymbol(), period = AggPeriod)[1];

def atr_func = MovingAverage(averageType, TrueRange(h, c, l), length);
def day_atr = atr_func;
def day_close = c;

plot p1 = day_close + day_atr;
p1.assignValueColor(COLOR.DARK_RED);
p1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot p7 = day_close + day_atr * lvl2;
p7.assignValueColor(COLOR.RED);
AddCloud(p1, p7, COLOR.DARK_RED, COLOR.DARK_RED);
p7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot p5 = day_close + day_atr * lvl1;
p5.assignValueColor(COLOR.RED);
AddCloud(p7, p5, COLOR.RED, COLOR.RED);
p5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot m5 = day_close - day_atr * lvl1;
m5.assignValueColor(COLOR.GREEN);
#AddCloud(p7,p5,COLOR.GREEN, COLOR.GREEN);
m5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot m7 = day_close - day_atr * lvl2;
m7.assignValueColor(COLOR.GREEN);
AddCloud(m7, m5, COLOR.GREEN, COLOR.GREEN);
m7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot m1 = day_close - day_atr;
m1.assignValueColor(COLOR.DARK_GREEN);
AddCloud(m1, m7, COLOR.DARK_GREEN, COLOR.DARK_GREEN);
m1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Hi @BenTen thanks for this awesome indicator its really superb and have been doing successful trades with this indicator. I have added extra piece of code from other indicators to show the projected ATR levels as bubblecharts and they are working fine. The code is below.

def cond = showBubble and IsNaN(close[ShiftBubble]) and !IsNaN(close[n1]) ;
AddChartBubble(cond, p1, Concat("100% BullTarget: ", Round(p1)), Color.MAGENTA);
AddChartBubble(cond, p9, Concat("125% BullTarget: ", Round(p9)), Color.MAGENTA);
AddChartBubble(cond, p7, Concat("75% BullTarget: ", Round(p7)), Color.MAGENTA);
AddChartBubble(cond, p5, Concat("50% BullTarget: ", Round(p5)), Color.MAGENTA);
AddChartBubble(cond, m1, Concat("100% BearTarget: ", Round(m1)), Color.MAGENTA);
AddChartBubble(cond, m9, Concat("125% BearTarget: ", Round(m9)), Color.MAGENTA);
AddChartBubble(cond, m7, Concat("75% BearTarget: ", Round(m7)), Color.MAGENTA);
AddChartBubble(cond, m5, Concat("50% BearTarget: ", Round(m5)), Color.MAGENTA);

However, the challenge I am facing the levels of these targets are plotted for the previous days as well which makes my chart very cluttered. Is there a way to add the show only function to your code wherein if the condition is set to Yes then only the projected levels for tomorrow are shown and not the previous days levels as those become irrelevant once the day is over.

Would highly appreciate your help to solve the problem.
 
What I am asking about this indicator is would the previous ATR levels plotted for a stock would change or they would remain static?.
 
Last edited by a moderator:
@Ben thanks for that. Do you know if there is a pivot script out there that would put up weekly pivots on day chart that works for mobile (iOS)?
@jeremyadorna This one works for me on TOS mobile for Android, try it.
https://usethinkscript.com/threads/...ort-resistance-indicator-for-thinkorswim.158/

add this code to your code, before the addclouds.
change the if then in each cloud to include
x and

Code:
# pick, just today or all

input today_only = yes;

def istoday = if GetLastDay() == GetDay() then 1 else 0;

def x = if today_only then istoday else 1;
I added this and it works perfectly. I changed the clouds to plots to make it work on mobile.

I was able to get it to work on mobile
How?
 
@jeremyadorna This one works for me on TOS mobile for Android, try it.
https://usethinkscript.com/threads/...ort-resistance-indicator-for-thinkorswim.158/


I added this and it works perfectly. I changed the clouds to plots to make it work on mobile.


How?
I added this and it works perfectly. I changed the clouds to plots to make it work on mobile.

@wado729
Hello, Could you please explain or show what did you change in the codes in SATY ATR levels that works in mobile or could you please share the link if possible, you said you changed the cloud to plot, i am not finding any option in settings to change plot, if we want to update all clouds to plot in the complete code it is completely mess for me, I really appreciate if you could share the link that works in mobile complete code.

Thanks in Advance.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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