Fibonacci Average with Supertrended Moving Average COMBO for ThinkorSwim

chewie76

Well-known member
VIP
VIP Enthusiast
This is a combination of two indicators from this amazing forum added into one indicator with added dynamic colors.

I combined the Typical Fibonacci Average found here. https://usethinkscript.com/threads/the-typical-fibonacci-average-for-thinkorswim.12458/#post-107052

with the Supertrended Moving Averages found here. https://usethinkscript.com/threads/supertrended-moving-averages-for-thinkorswim.17207/#post-143310

and added dynamic colors red/green. I'm happy with the results!

1719496730184.png


Code:
#FIB_AVG with SUPERTRENDED_MOVING_AVERAGES COMBO
#Assembled by Chewie on 6/27/24
#FIB_Average was posted by Jman831 and can be found at https://usethinkscript.com/threads/the-typical-fibonacci-average-for-thinkorswim.12458/#post-107052

input AverageTypes = AverageType.EXPONENTIAL;
input AveragePrices = close;
input AverageLength1 = 144;
input AverageLength2 = 126;
input AverageLength3 = 108;
input AverageLength4 = 90;
input AverageLength5 = 72;
input AverageLength6 = 54;
input AverageLength7 = 27;
input AverageLength8 = 18;

def avg1 = MovingAverage(AverageTypes, AveragePrices, AverageLength1) * 34;
def avg2 = MovingAverage(AverageTypes, AveragePrices, AverageLength2) * 21;
def avg3 = MovingAverage(AverageTypes, AveragePrices, AverageLength3) * 13;
def avg4 = MovingAverage(AverageTypes, AveragePrices, AverageLength4) * 8;
def avg5 = MovingAverage(AverageTypes, AveragePrices, AverageLength5) * 5;
def avg6 = MovingAverage(AverageTypes, AveragePrices, AverageLength6) * 3;
def avg7 = MovingAverage(AverageTypes, AveragePrices, AverageLength7) * 2;
def avg8 = MovingAverage(AverageTypes, AveragePrices, AverageLength8);
def typfibavg = (avg1 + avg2 + avg3 + avg4 + avg5 + avg6 + avg7 + avg8) / 87;

plot TypicalFibAverage = typfibavg;
TypicalFibAverage.setlineWeight(3);


# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
#- Updated - minor code fix - Sam4Cok@Samer800    - 01/2024
input maType  = {SMA, default EMA, HMA, WMA, VWMA, DEMA, ZLSMA, ZLEMA, VAR, TILL, TSF};
input Source = close;
input maLength  = 135;    #100 " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"
input ShowCloud = no;
input ShowCloud2 = yes;
input showsignals = no;

def na = Double.NaN;
def avPlot = ohlc4;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#pine_linreg(src, len, offset=0) =>
Script linreg {
input src = close;
input len = 100;
input offset = 0;
    def na = Double.NaN;
    def bar_index = isNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
Script TSF {
input src = close;
input length = 100;
    def lrc = Inertia(src, length);
    def lrc1 = linreg(src, length, 1);
    def lrs = lrc - lrc1;
    def TSF = Inertia(src, length) + lrs;
    plot out = TSF;
}
script TILL {
    input src = close;
    input length = 100;
    input T3a1 = 0.7;
    def T3e1 = ExpAverage(src, length);
    def T3e2 = ExpAverage(T3e1, length);
    def T3e3 = ExpAverage(T3e2, length);
    def T3e4 = ExpAverage(T3e3, length);
    def T3e5 = ExpAverage(T3e4, length);
    def T3e6 = ExpAverage(T3e5, length);
    def T3c1 = -T3a1 * T3a1 * T3a1;
    def T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1;
    def T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1;
    def T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1;
    def T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3;
    plot out = T3;
}
script VAR {
    input src = close;
    input length = 100;
    def curClose = src;
    def prevClose = src[1];
    def valpha = 2 / (length + 1);
    def vud1 = if curClose > prevClose then curClose - prevClose else 0;
    def vdd1 = if prevClose > curClose then prevClose - curClose else 0;
    def vUD = sum(vud1, 9);
    def vDD = sum(vdd1, 9);
    def vCMO = if vUD + vDD == 0 then 0 else (vUD - vDD) / (vUD + vDD);
    def cmo = valpha * AbsValue(vCMO);
    def VAR = CompoundValue(1, cmo * curClose + (1 - cmo) * VAR[1], src);
    plot out = VAR;
}
#export zlSma(float src, simple int len) =>
script ZLSMA {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlSma(float src, simple int len) =>
script ZLEMA {
    input src = close;
    input len = 14;
    def lenR = Round(len / 2, 0);
    def zxLag = if len / 2 == lenR then len / 2 else (len - 1) / 2;
    def zxEMAData = src + src - src[zxLag];
    def ZLEMA = ExpAverage(zxEMAData, len);
    plot out = ZLEMA;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
def mov;
Switch (maType) {
Case SMA : mov = Average(source, maLength);
Case VAR : mov = VAR(source, maLength);
Case WMA : mov = WMA(source, maLength);
Case VWMA : mov = VWMA(source, maLength);
Case DEMA : mov = DEMA(source, maLength);
Case ZLSMA : mov = ZLSMA(source, maLength);
Case ZLEMA : mov = ZLEMA(source, maLength);
Case TILL : mov = TILL(source, maLength);
Case TSF : mov = TSF(source, maLength);
Case HMA : mov = HullMovingAvg(source, maLength);
Default : mov = ExpAverage(source, maLength);
}
#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
Script Supertrend {
input src = hl2;
input atrLength = 14;
input mult = 2;
input changeATR = yes;
    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = src - mult * nATR;
    def dn = src + mult * nATR;
    def lower;
    def upper;
    def up2 = if isNaN(upper[1]) then up else upper[1];
    def dn2 = if isNaN(lower[1]) then dn else lower[1];
    def up1 = if up2==0 then up else up2;
    def dn1 = if dn2==0 then dn else dn2;
        upper = if (close[1] > up1) then if up > up1 then up else up1 else up;
        lower = if (close[1] < dn1) then if dn < dn1 then dn else dn1 else dn;
    def trend;
    def trend2 = if isNaN(trend[1]) then 0 else trend[1];
    def trend1 = if trend2==0 then 1 else trend2;
    if trend1 == -1 and close > dn1 {
        trend = 1;
    } else if trend1 == 1 and close < up1 {
        trend = -1;
    } else {
        trend = trend1;
    }
    def superTrend = if trend == -1 then lower else upper;

    plot ST = superTrend;
    plot dir = trend;
}

def ma = mov;
def ST    = Supertrend(ma, atrLength, atrMultiplier,changeATR).ST;
def Trend = Supertrend(ma, atrLength, atrMultiplier,changeATR).DIR;
def change = Trend!=Trend[1];
def buySignal  = trend > 0 and change;
def sellSignal = trend < 0 and change;

#-- plts

plot upPlot = if Trend > 0 then ST else na;  # 'Up Band'
plot loPlot = if Trend < 0 then ST else na; # 'Low Band'
#upPlot.SetDefaultColor(CreateColor(76,175,80));
upPlot.SetDefaultColor(Color.dark_green);
#loPlot.SetDefaultColor(CreateColor(255,82,82));
loPlot.SetDefaultColor(Color.red);
upPlot.SetLineWeight(3);
loPlot.SetLineWeight(3);
plot upPt = if buySignal then ST else na;
plot dnPt = if sellSignal then ST else na;
upPt.SetLineWeight(2);
dnPt.SetLineWeight(2);
upPt.SetDefaultColor(CreateColor(76,175,80));
dnPt.SetDefaultColor(CreateColor(255,82,82));
upPt.setPaintingStrategy(PaintingStrategy.POINTS);
dnPt.setPaintingStrategy(PaintingStrategy.POINTS);

TypicalFibAverage.setdefaultcolor(Color.GREEN);
TypicalFibAverage.AssignValueColor(if TypicalFibAverage < loplot then Color.DARK_ORANGE else color.current);

#-- Signals and Cloud

AddChartBubble(showsignals and buySignal, ST, "B", Color.GREEN, no);
AddChartBubble(showsignals and sellSignal, ST, "S", Color.RED);

AddCloud(If(TypicalFibAverage < loplot, TypicalFibAverage, Double.NaN), loplot, Color.DARK_red, Color.DARK_red);

AddCloud(If(TypicalFibAverage > upplot, TypicalFibAverage, Double.NaN), upplot, Color.DARK_green, Color.DARK_green);

AddCloud(if ShowCloud then avPlot else na, upPlot, CreateColor(0,35,0)); # 'Upper Area'
AddCloud(if ShowCloud then loPlot else na, avPlot, CreateColor(55,0,0));  # 'Lower Area'

#--- END CODE
 
I also created a MTF version of this. 4 HR is default time. See upper lines in picture.

1719503304692.png


Code:
#MTF FIB AVG WITH SUPERTRENDED MA
#Assembled by Chewie on 6/27/24
#FIB_Average was posted by Jman831 and can be found at https://usethinkscript.com/threads/the-typical-fibonacci-average-for-thinkorswim.12458/#post-107052

input agg1 = aggregationPeriod.four_hours;
def Source = close(period = agg1);

input AverageTypes = AverageType.EXPONENTIAL;
DEF AveragePrices = source;
input AverageLength1 = 144;
input AverageLength2 = 126;
input AverageLength3 = 108;
input AverageLength4 = 90;
input AverageLength5 = 72;
input AverageLength6 = 54;
input AverageLength7 = 27;
input AverageLength8 = 18;

def avg1 = MovingAverage(AverageTypes, AveragePrices, AverageLength1) * 34;
def avg2 = MovingAverage(AverageTypes, AveragePrices, AverageLength2) * 21;
def avg3 = MovingAverage(AverageTypes, AveragePrices, AverageLength3) * 13;
def avg4 = MovingAverage(AverageTypes, AveragePrices, AverageLength4) * 8;
def avg5 = MovingAverage(AverageTypes, AveragePrices, AverageLength5) * 5;
def avg6 = MovingAverage(AverageTypes, AveragePrices, AverageLength6) * 3;
def avg7 = MovingAverage(AverageTypes, AveragePrices, AverageLength7) * 2;
def avg8 = MovingAverage(AverageTypes, AveragePrices, AverageLength8);
def typfibavg = (avg1 + avg2 + avg3 + avg4 + avg5 + avg6 + avg7 + avg8) / 87;

plot TypicalFibAverage = typfibavg;
TypicalFibAverage.setlineWeight(3);





# https://www.tradingview.com/v/sfV6H5h5/
#//@KivancOzbilgic
#indicator('SuperTrended Moving Averages', 'ST MA', overlay=true,
#Converted by Sam4Cok@Samer800    - 11/2023
#- Updated - minor code fix - Sam4Cok@Samer800    - 01/2024
input maType  = {SMA, default EMA, HMA, WMA, VWMA, DEMA, ZLSMA, ZLEMA, VAR, TILL, TSF};
#input Source = close;
input maLength  = 135;    #100 " Length MA"
input changeATR = yes;
input atrLength = 10;    # " Length Atr"
input atrMultiplier = 0.5;   # "Band Mult"
input ShowCloud = no;
input ShowCloud2 = yes;
input showsignals = no;

def na = Double.NaN;
def avPlot = ohlc4;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
#pine_linreg(src, len, offset=0) =>
Script linreg {
input src = close;
input len = 100;
input offset = 0;
    def na = Double.NaN;
    def bar_index = isNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
Script TSF {
input src = close;
input length = 100;
    def lrc = Inertia(src, length);
    def lrc1 = linreg(src, length, 1);
    def lrs = lrc - lrc1;
    def TSF = Inertia(src, length) + lrs;
    plot out = TSF;
}
script TILL {
    input src = close;
    input length = 100;
    input T3a1 = 0.7;
    def T3e1 = ExpAverage(src, length);
    def T3e2 = ExpAverage(T3e1, length);
    def T3e3 = ExpAverage(T3e2, length);
    def T3e4 = ExpAverage(T3e3, length);
    def T3e5 = ExpAverage(T3e4, length);
    def T3e6 = ExpAverage(T3e5, length);
    def T3c1 = -T3a1 * T3a1 * T3a1;
    def T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1;
    def T3c3 = -6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1;
    def T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1;
    def T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3;
    plot out = T3;
}
script VAR {
    input src = close;
    input length = 100;
    def curClose = src;
    def prevClose = src[1];
    def valpha = 2 / (length + 1);
    def vud1 = if curClose > prevClose then curClose - prevClose else 0;
    def vdd1 = if prevClose > curClose then prevClose - curClose else 0;
    def vUD = sum(vud1, 9);
    def vDD = sum(vdd1, 9);
    def vCMO = if vUD + vDD == 0 then 0 else (vUD - vDD) / (vUD + vDD);
    def cmo = valpha * AbsValue(vCMO);
    def VAR = CompoundValue(1, cmo * curClose + (1 - cmo) * VAR[1], src);
    plot out = VAR;
}
#export zlSma(float src, simple int len) =>
script ZLSMA {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlSma(float src, simple int len) =>
script ZLEMA {
    input src = close;
    input len = 14;
    def lenR = Round(len / 2, 0);
    def zxLag = if len / 2 == lenR then len / 2 else (len - 1) / 2;
    def zxEMAData = src + src - src[zxLag];
    def ZLEMA = ExpAverage(zxEMAData, len);
    plot out = ZLEMA;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * v, len) / SimpleMovingAvg(v, len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
def mov;
Switch (maType) {
Case SMA : mov = Average(source, maLength);
Case VAR : mov = VAR(source, maLength);
Case WMA : mov = WMA(source, maLength);
Case VWMA : mov = VWMA(source, maLength);
Case DEMA : mov = DEMA(source, maLength);
Case ZLSMA : mov = ZLSMA(source, maLength);
Case ZLEMA : mov = ZLEMA(source, maLength);
Case TILL : mov = TILL(source, maLength);
Case TSF : mov = TSF(source, maLength);
Case HMA : mov = HullMovingAvg(source, maLength);
Default : mov = ExpAverage(source, maLength);
}
#Supertrend(lenAtr, lenMas, mult, highP, lowP, closeP) =>
Script Supertrend {
input src = hl2;
input atrLength = 14;
input mult = 2;
input changeATR = yes;
    def tr = TrueRange(high, close, low);
    def nATR = if changeATR then ATR(Length = atrLength) else SimpleMovingAvg(tr, atrLength);
    def up = src - mult * nATR;
    def dn = src + mult * nATR;
    def lower;
    def upper;
    def up2 = if isNaN(upper[1]) then up else upper[1];
    def dn2 = if isNaN(lower[1]) then dn else lower[1];
    def up1 = if up2==0 then up else up2;
    def dn1 = if dn2==0 then dn else dn2;
        upper = if (close[1] > up1) then if up > up1 then up else up1 else up;
        lower = if (close[1] < dn1) then if dn < dn1 then dn else dn1 else dn;
    def trend;
    def trend2 = if isNaN(trend[1]) then 0 else trend[1];
    def trend1 = if trend2==0 then 1 else trend2;
    if trend1 == -1 and close > dn1 {
        trend = 1;
    } else if trend1 == 1 and close < up1 {
        trend = -1;
    } else {
        trend = trend1;
    }
    def superTrend = if trend == -1 then lower else upper;

    plot ST = superTrend;
    plot dir = trend;
}

def ma = mov;
def ST    = Supertrend(ma, atrLength, atrMultiplier,changeATR).ST;
def Trend = Supertrend(ma, atrLength, atrMultiplier,changeATR).DIR;
def change = Trend!=Trend[1];
def buySignal  = trend > 0 and change;
def sellSignal = trend < 0 and change;

#-- plts

plot upPlot = if Trend > 0 then ST else na;  # 'Up Band'
plot loPlot = if Trend < 0 then ST else na; # 'Low Band'
#upPlot.SetDefaultColor(CreateColor(76,175,80));
upPlot.SetDefaultColor(Color.dark_green);
#loPlot.SetDefaultColor(CreateColor(255,82,82));
loPlot.SetDefaultColor(Color.red);
upPlot.SetLineWeight(3);
loPlot.SetLineWeight(3);
plot upPt = if buySignal then ST else na;
plot dnPt = if sellSignal then ST else na;
upPt.SetLineWeight(2);
dnPt.SetLineWeight(2);
upPt.SetDefaultColor(CreateColor(76,175,80));
dnPt.SetDefaultColor(CreateColor(255,82,82));
upPt.setPaintingStrategy(PaintingStrategy.POINTS);
dnPt.setPaintingStrategy(PaintingStrategy.POINTS);

TypicalFibAverage.setdefaultcolor(Color.GREEN);
TypicalFibAverage.AssignValueColor(if TypicalFibAverage < loplot then Color.DARK_ORANGE else color.current);

#-- Signals and Cloud

AddChartBubble(showsignals and buySignal, ST, "B", Color.GREEN, no);
AddChartBubble(showsignals and sellSignal, ST, "S", Color.RED);

AddCloud(If(TypicalFibAverage < loplot, TypicalFibAverage, Double.NaN), loplot, Color.DARK_red, Color.DARK_red);

AddCloud(If(TypicalFibAverage > upplot, TypicalFibAverage, Double.NaN), upplot, Color.DARK_green, Color.DARK_green);

AddCloud(if ShowCloud then avPlot else na, upPlot, CreateColor(0,35,0)); # 'Upper Area'
AddCloud(if ShowCloud then loPlot else na, avPlot, CreateColor(55,0,0));  # 'Lower Area'

#--- END CODE
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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