#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
#// © Zeiierman
#//Copyright by Zeiierman.
#//The information contained in my scripts/indicators/ideas does not constitute financial advice or a solicitation to buy or sell any securities of any type.
#//I will not accept liability for any loss or damage, including without limitation any loss of profit, which may arise directly or indirectly from use of or reliance on such information.
#//All investments involve risk, and the past performance of a security, industry, sector, market, financial product, trading strategy, or individual’s trading does not guarantee future results or returns.
#//Investors are fully responsible for any investment decisions they make. Such decisions should be based solely on an evaluation of their financial circumstances, investment objectives, risk tolerance, and liquidity needs.
#//My scripts/indicators/ideas are only for educational purposes!
#study("Fearzone (Expo) - Contrarian Indicator", overlay=true, shorttitle="FearZone (Expo)")
#study("GreedZone indicator ", overlay=true, shorttitle="GreedZone (Expo)")
# Converted and mod by Sam4Cok@Samer800 - 06/2023
input ShowZonezStyle = {Default "Candle Sticks", "Wedges", "Don't Show"};
input ZonesStartAlert = {Default Dots, Arrows, "Don't Show"};
input source = ohlc4; # "Source"
input FearzoneMovAvgType = AverageType.SIMPLE;
input FearzonePeriod = 30; # "Fearzone Period"
input FearzoneStdevPeriod = 50; # "Stdev Period Fearzone"
input GreedZoneMovAvgType = AverageType.SIMPLE;
input GreedZonePeriod = 30; # "GreedZone Period"
input GreedZoneStdevPeriod = 50; # "Stdev Period GreedZone"
def na = Double.NaN;
def tr = TrueRange(high, close, low);
def Candlesticks = ShowZonezStyle==ShowZonezStyle."Candle Sticks";
def Wedges = ShowZonezStyle==ShowZonezStyle."Wedges";
def dots = ZonesStartAlert==ZonesStartAlert.Dots;
def AlertCircle = ZonesStartAlert!=ZonesStartAlert."Don't Show";
DefineGlobalColor("up", CreateColor(38,166,154));
DefineGlobalColor("dn", CreateColor(239,83,80));
#// Condition One - Fear
def HZone = Highest(source, FearzonePeriod);
def FZ1 = (HZone - source) / HZone;
def FAVG1 = MovingAverage(FearzoneMovAvgType, FZ1, FearzoneStdevperiod);
def FSTDEV1 = StDev(FZ1, FearzoneStdevperiod);
def FZ1Limit = FAVG1 + FSTDEV1;
#// Condition One - Greed
def LZone = lowest(source, GreedZonePeriod);
def GZ1 = (LZone - source)/ LZone;
def GAVG1 = MovingAverage(GreedZoneMovAvgType, GZ1, GreedZoneStdevperiod);
def GSTDEV1 = StDev(GZ1, GreedZoneStdevperiod);
def GZ1Limit = GAVG1 - GSTDEV1;
#// Condition Two - Fear
def FZ2 = MovingAverage(FearzoneMovAvgType, source, FearzonePeriod);
def FAVG2 = MovingAverage(FearzoneMovAvgType, FZ2, FearzoneStdevperiod);
def FSTDEV2 = StDev(FZ2, FearzoneStdevperiod);
def FZ2Limit = FAVG2 - FSTDEV2;
#// Condition Two - Greed
def GZ2 = MovingAverage(GreedZoneMovAvgType, source, GreedZonePeriod);
def GAVG2 = MovingAverage(GreedZoneMovAvgType, GZ2, GreedZoneStdevperiod);
def GSTDEV2 = StDev(GZ2, GreedZoneStdevperiod);
def GZ2Limit = GAVG2 + GSTDEV2;
#// FearZone
def Fearzone_Con = FZ1 > FZ1Limit and FZ2 < FZ2Limit;
def FearZoneOpen = if Fearzone_Con then source - tr else na;
def FearZoneClose = if Fearzone_Con then source - 2 * tr else na;
#// GreedZone
def Greedzone_Con = GZ1 < GZ1Limit and GZ2 > GZ2Limit;
def GreedZoneOpen = if Greedzone_Con then source + tr else na;
def GreedZoneClose = if Greedzone_Con then source + 2 * tr else na;
#/ Alerts
def FearAler = AlertCircle and Fearzone_Con and !Fearzone_Con[1];#? #FC6C85 : na
def GreedAlert = AlertCircle and Greedzone_Con and !Greedzone_Con[1];#? #FC6C85 : na
#-- Plots
plot FearZone = if FearAler then low - tr/3 else na; # "Alert Circles"
FearZone.SetPaintingStrategy(if dots then PaintingStrategy.POINTS else PaintingStrategy.ARROW_UP);
FearZone.SetDefaultColor(Color.RED);#GlobalColor("dn"));
FearZone.SetLineWeight(2);
plot GreedZone = if GreedAlert then high + tr/3 else na; # "Alert Circles"
GreedZone.SetPaintingStrategy(if dots then PaintingStrategy.POINTS else PaintingStrategy.ARROW_DOWN);
GreedZone.SetDefaultColor(Color.GREEN);#GlobalColor("dn"));
GreedZone.SetLineWeight(2);
plot FearWedges = if Wedges and Fearzone_Con then low else na; # "Alert Circles"
FearWedges.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
FearWedges.SetDefaultColor(GlobalColor("dn"));#GlobalColor("dn"));
FearWedges.SetLineWeight(2);
plot GreedWedges = if Wedges and Greedzone_Con then high else na; # "Alert Circles"
GreedWedges.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
GreedWedges.SetDefaultColor(GlobalColor("up"));#GlobalColor("dn"));
GreedWedges.SetLineWeight(2);
#-- Candle Sticks
AddChart(high = if Candlesticks then FearZoneOpen else na,
low = FearZoneClose, open = FearZoneOpen, close = FearZoneClose,
type = ChartType.CANDLE, growcolor = GlobalColor("dn"));
AddChart(high = if Candlesticks then GreedZoneOpen else na ,
low = GreedZoneClose , open = GreedZoneClose, close = GreedZoneOpen,
type = ChartType.CANDLE, growcolor = GlobalColor("up"));
#---- END of CODE