# 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"));