# Fibonacci SuperTrend
# converted from www.FreeIndicators.com
#
# 20200615 - barbaros
input length = 14;
input retrace = 38.2;
input useHighLow = yes;
input showArrows = yes;
input colorBars = yes;
input audioAlerts = yes;
def h = if useHighLow then high else close;
def l = if useHighLow then low else close;
def minL = Lowest(l, length);
def maxH = Highest(h, length);
def hh = if h > maxH[1] then h else hh[1];
def ll = if l < minL[1] then l else ll[1];
def trend = if h > maxH[1] then 1 else if l < minL[1] then -1 else trend[1];
def isTrend = trend == 1;
def MyRange = hh-ll;
def Retracement = retrace/100;
def BuyPrice = ll + (Retracement * MyRange);
def SellPrice = hh - (Retracement * MyRange);
def UpTrend = if isTrend and !isTrend[1] then SellPrice else if isTrend and SellPrice > UpTrend[1] then SellPrice else Uptrend[1];
def DownTrend = if !isTrend && isTrend[1] then BuyPrice else if !isTrend and BuyPrice < DownTrend[1] then BuyPrice else DownTrend[1];
# Plots
plot TrendLine = if isTrend then UpTrend else DownTrend;
TrendLine.assignValueColor(if isTrend then Color.GREEN else Color.RED);
plot UpArrow = if showArrows and isTrend and !isTrend[1] then yes else no;
UpArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot DownArrow = if showArrows and !isTrend && isTrend[1] then yes else no;
DownArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
# Color Bars
AssignPriceColor(if colorBars and isTrend then Color.GREEN else if colorBars then Color.RED else Color.CURRENT);
# Alerts
Alert(isTrend and !isTrend[1], "Up Trend", Alert.BAR, Sound.Ding);
Alert(!isTrend && isTrend[1], "Down Trend", Alert.BAR, Sound.Ding);