Fear/Greed Zone Reversal For ThinkOrSwim

The author states: The "Fear/Greed Zone Reversals [UAlgo]" aims at identifying potential reversal points in the market based on sentiment zones characterized by fear and greed.

This indicator utilizes a combination of moving averages, standard deviations, and price action to detect when the market transitions from extreme fear to greed or vice versa. By identifying these critical turning points, traders can gain insights into potential buy or sell opportunities.

A Greed Zone is when the price deviates significantly above the chosen moving average. This suggests market sentiment might be leaning towards greed, potentially indicating a selling opportunity.

A Fear Zone is when the price deviates significantly below the chosen moving average of the selected price source. This suggests market sentiment might be leaning towards fear, potentially indicating a buying opportunity.

Sgh3Uqt.png


Original Tradingview code:
https://www.tradingview.com/v/6c5OEsJF/
ThinkOrSwim code can be found in the next post
 
Last edited by a moderator:

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

https://www.tradingview.com/v/6c5OEsJF/

Code:
// This Pine Script™ code is subject to the terms of the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) [URL]https://creativecommons.org/licenses/by-nc-sa/4.0/[/URL]
// © UAlgo
//@version=5
indicator("Fear/Greed Zone Reversals [UAlgo]", overlay=true, shorttitle="Fear/Greed Zone Reversals [UAlgo]")
// Grouped Settings
group_general = "General Settings"
group_fear = "Fear Zone Settings"
group_greed = "Greed Zone Settings"
group_colors = "Colors"
// Moving Average Type Selection
ma_type_fear = input.string("SMA", title="Moving Average Type for Fear", options=["SMA", "EMA", "WMA", "VWMA", "HMA"], group=group_fear)
ma_type_greed = input.string("SMA", title="Moving Average Type for Greed", options=["SMA", "EMA", "WMA", "VWMA", "HMA"], group=group_greed)
// FearZone Settings
src_fear = input.source(close, title="Fear Source", group=group_fear)
len_high_fear = input.int(50, title="Fear Period", group=group_fear)
stdev_len_fear = input.int(50, title="Fear Stdev Period", group=group_fear)
// GreedZone Settings
src_greed = input.source(close, title="Greed Source", group=group_greed)
len_low_greed = input.int(50, title="Greed Period", group=group_greed)
stdev_len_greed = input.int(50, title="Greed Stdev Period", group=group_greed)
// Color Settings
color_fear_label = input.color(color.teal, title="Fear Label Color", group=group_colors)
color_greed_label = input.color(color.red, title="Greed Label Color", group=group_colors)
text_color = input.color(color.white, title="Label Text Color", group=group_colors)
// Function to calculate the selected moving average
f_moving_average(ma_type, src, length) =>
    ma = switch ma_type
        "SMA" => ta.sma(src, length)
        "EMA" => ta.ema(src, length)
        "WMA" => ta.wma(src, length)
        "VWMA" => ta.vwma(src, length)
        "HMA" => ta.hma(src, length)
        => ta.sma(src, length)  // Default to SMA if something goes wrong
    ma
// Function to calculate the first zone condition and limit
f_zone_condition_1(src, len_high, stdev_len, ma_type, is_fear) =>
    cond_1 = is_fear ? (ta.highest(src, len_high) - src) / ta.highest(src, len_high) : (ta.lowest(src, len_high) - src) / ta.lowest(src, len_high)
    avg_1 = f_moving_average(ma_type, cond_1, stdev_len)
    stdev_1 = ta.stdev(cond_1, stdev_len)
    limit_1 = is_fear ? avg_1 + stdev_1 : avg_1 - stdev_1
    [cond_1, limit_1]
// Function to calculate the second zone condition and limit
f_zone_condition_2(src, len_high, stdev_len, ma_type, is_fear) =>
    cond_2 = f_moving_average(ma_type, src, len_high)
    avg_2 = f_moving_average(ma_type, cond_2, stdev_len)
    stdev_2 = ta.stdev(cond_2, stdev_len)
    limit_2 = is_fear ? avg_2 - stdev_2 : avg_2 + stdev_2
    [cond_2, limit_2]
// Function to check zone activity
f_zone_active(cond_1, cond_2, limit_1, limit_2, is_fear) =>
    is_fear ? (cond_1 > limit_1 and cond_2 < limit_2) : (cond_1 < limit_1 and cond_2 > limit_2)
// FearZone Calculations
[fear_cond_1, fear_limit_1] = f_zone_condition_1(src_fear, len_high_fear, stdev_len_fear, ma_type_fear, true)
[fear_cond_2, fear_limit_2] = f_zone_condition_2(src_fear, len_high_fear, stdev_len_fear, ma_type_fear, true)
fear_zone_active = f_zone_active(fear_cond_1, fear_cond_2, fear_limit_1, fear_limit_2, true)
// GreedZone Calculations
[greed_cond_1, greed_limit_1] = f_zone_condition_1(src_greed, len_low_greed, stdev_len_greed, ma_type_greed, false)
[greed_cond_2, greed_limit_2] = f_zone_condition_2(src_greed, len_low_greed, stdev_len_greed, ma_type_greed, false)
greed_zone_active = f_zone_active(greed_cond_1, greed_cond_2, greed_limit_1, greed_limit_2, false)
// Buy/Sell Signals
if greed_zone_active[1] and not greed_zone_active
    label.new(bar_index, high+ta.atr(14)*0.5, "▼", style=label.style_label_down, color=color_greed_label, textcolor=text_color, size=size.small)
if fear_zone_active[1] and not fear_zone_active
    label.new(bar_index, low-ta.atr(14)*0.5, "▲", style=label.style_label_up, color=color_fear_label, textcolor=text_color, size=size.small)
// Alert Conditions
alertcondition(greed_zone_active[1] and not greed_zone_active, title="Greed Zone Reversal", message="Greed Zone Reversal detected")
alertcondition(fear_zone_active[1] and not fear_zone_active, title="Fear Zone Reversal", message="Fear Zone Reversal detected")
check the below:

CSS:
#// Indicator for TOS
#// © UAlgo
#indicator("Fear/Greed Zone Reversals [UAlgo]", overlay=true, shorttitle="Fear/Greed Zone Reversals [UAlgo]")
# Converted by Sam4Cok@Samer800    - 08/2024 - Request from UseThinkScript.com member

#// Moving Average Type Selection
input timeframe = AggregationPeriod.MIN;
input fearMovAvg = AverageType.SIMPLE;  # "Moving Average Type for Fear"
input FearSrc = FundamentalType.CLOSE; #, title="Fear Source", group=group_fear)
input FearPeriod = 50; #, title="Fear Period", group=group_fear)
input FearStdevPeriod = 50; #, title="Fear Stdev Period", group=group_fear)
input greedMovAvg = AverageType.SIMPLE; # "Moving Average Type for Greed"
input GreedSrc = FundamentalType.CLOSE; #, title="Greed Source", group=group_greed)
input GreedPeriod = 50; #, title="Greed Period", group=group_greed)
input GreedStdevPeriod = 50; #, title="Greed Stdev Period", group=group_greed)

def current = getAggregationPeriod();
def tf = Max(current, timeframe);
def FearSource = Fundamental(FundamentalType = FearSrc, Period = tf);
def GreedSource = Fundamental(FundamentalType = GreedSrc, Period = tf);
#// Function to calculate the first zone condition and limit
script f_zone_condition_1 {
    input src = close;
    input len_high = 50;
    input stdev_len = 50;
    input ma_type = AverageType.SIMPLE;
    input is_fear = yes;
    def hh = Highest(src, len_high);
    def ll = Lowest(src, len_high);
    def fear = (hh - src) / hh;
    def greed = (ll - src) / ll;
    def cond_1 = if is_fear then fear else greed;
    def avg_1 = MovingAverage(ma_type, cond_1, stdev_len);
    def stdev_1 = StDev(cond_1, stdev_len);
    def limit_1 = if is_fear then avg_1 + stdev_1 else avg_1 - stdev_1;
    plot cond = cond_1;
    plot limit = limit_1;
}
#// Function to calculate the second zone condition and limit
script f_zone_condition_2 {
    input src = close;
    input len_high = 50;
    input stdev_len = 50;
    input ma_type = AverageType.SIMPLE;
    input is_fear = yes;
    def cond_2 = MovingAverage(ma_type, src, len_high);
    def avg_2 = MovingAverage(ma_type, cond_2, stdev_len);
    def stdev_2 = StDev(cond_2, stdev_len);
    def limit_2 = if is_fear then avg_2 - stdev_2 else avg_2 + stdev_2;
    plot cond = cond_2;
    plot limit = limit_2;
}
#// Function to check zone activity
script f_zone_active {
    input cond_1 = close;
    input cond_2 = close;
    input limit_1 = 0;
    input limit_2 = 0;
    input is_fear = yes;
    def fear = (cond_1 > limit_1 and cond_2 < limit_2);
    def greed = (cond_1 < limit_1 and cond_2 > limit_2);
    def f_zone_active = if is_fear then fear else greed;
    plot out = f_zone_active;
}
#// FearZone Calculations
def fear_cond_1  = f_zone_condition_1(FearSource, FearPeriod, FearStdevPeriod, fearMovAvg, yes).cond;
def fear_limit_1 = f_zone_condition_1(FearSource, FearPeriod, FearStdevPeriod, fearMovAvg, yes).limit;
def fear_cond_2 = f_zone_condition_2(FearSource, FearPeriod, FearStdevPeriod, fearMovAvg, yes).cond;
def fear_limit_2 = f_zone_condition_2(FearSource, FearPeriod, FearStdevPeriod, fearMovAvg, yes).limit;
def fear_zone_active = f_zone_active(fear_cond_1, fear_cond_2, fear_limit_1, fear_limit_2, yes);

#// GreedZone Calculations
def greed_cond_1 = f_zone_condition_1(GreedSource, GreedPeriod, GreedStdevPeriod, greedMovAvg, no).cond;
def greed_limit_1 = f_zone_condition_1(GreedSource, GreedPeriod, GreedStdevPeriod, greedMovAvg, no).limit;
def greed_cond_2 = f_zone_condition_2(GreedSource, GreedPeriod, GreedStdevPeriod, greedMovAvg, no).cond;
def greed_limit_2 = f_zone_condition_2(GreedSource, GreedPeriod, GreedStdevPeriod, greedMovAvg, no).limit;
def greed_zone_active = f_zone_active(greed_cond_1, greed_cond_2, greed_limit_1, greed_limit_2, no);

#// Buy/Sell Signals
def sigUp = fear_zone_active[1] and !fear_zone_active;
def sigDn = greed_zone_active[1] and !greed_zone_active;

AddChartBubble(sigUp and !sigUp[1], low, "UP", Color.GREEN, no);
AddChartBubble(sigDn and !sigDn[1], high, "DN", Color.RED);

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