KAMA Fibonacci Bands For ThinkOrSwim

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

Hi,
Anyone able to convert this into TOS? Thank you!

https://www.tradingview.com/v/s5nMUzqE/

//@version=2
study(title="KAMA Fibonacci Bands [DW]", shorttitle="KFB [DW]", overlay=true)
//This study is an experimental combination of the Kaufman Adaptive Moving Average with ATR and Fibonacci percentages.
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Updates:
//After some careful review, I noticed the previous version was actually reflecting OHLC/4 SMA values, rather than KAMA values. This version actually reflects KAMA values.
//ATR sampling period is now linked to the sampling period parameter.
//Bar colors are now included. The color scheme is based on the direction of the KAMA.
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Source
src = input(defval=hlc3, title="Source")
//Sampling Period
per = input(defval=34, minval=1, title="Sampling Period")
//Smoothing Constant
fast = input(defval=0.666, step=0.001, title="Smoothing Constant Fast End")
slow = input(defval=0.0645, step=0.0001, title="Smoothing Constant Slow End")
//ATR Multiplier
mult = input(defval=2, minval=1, title="ATR Multiplier")
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//KAMA
kama(a, b)=>
dist = abs(a[0] - a[1])
signal = abs(a - a)
noise = sum(dist, b)
effr = noise!=0 ? signal/noise : 1
sc = pow(effr*(fast - slow) + slow,2)
kama = nz(kama[1], a) + sc*(a - nz(kama[1], a))
kma = kama(src, per)
//Bands
b382h = kma + mult*atr(per)*0.382
b50h = kma + mult*atr(per)*0.5
b618h = kma + mult*atr(per)*0.618
b764h = kma + mult*atr(per)*0.764
b1h = kma + mult*atr(per)
b1272h = kma + mult*atr(per)*1.272
b1414h = kma + mult*atr(per)*1.414
b1618h = kma + mult*atr(per)*1.618
b382l = kma - mult*atr(per)*0.382
b50l = kma - mult*atr(per)*0.5
b618l = kma - mult*atr(per)*0.618
b764l = kma - mult*atr(per)*0.764
b1l = kma - mult*atr(per)
b1272l = kma - mult*atr(per)*1.272
b1414l = kma - mult*atr(per)*1.414
b1618l = kma - mult*atr(per)*1.618
//Color
kcolor = kma > kma[1] ? lime : kma < kma[1] ? red : black
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//----------------------------------------------------------------------------------------------------------------------------------------------------------------
//KAMA Plot
plot(kma, color=kcolor, linewidth=2, style=linebr, transp=0, title="KAMA")
//Band Plots
b1618hplot = plot(b1618h, color=fuchsia, style=linebr, transp=0, title="161.8% Positive Band")
b1414hplot = plot(b1414h, color=purple, style=linebr, transp=0, title="141.4% Positive Band")
b1272hplot = plot(b1272h, color=blue, style=linebr, transp=0, title="127.2% Positive Band")
b1hplot = plot(b1h, color=black, style=linebr, transp=0, title="100% Positive Band")
b764hplot = plot(b764h, color=lime, style=linebr, transp=0, title="76.4% Positive Band")
b618hplot = plot(b618h, color=green, style=linebr, transp=0, title="61.8% Positive Band")
b50hplot = plot(b50h, color=yellow, style=linebr, transp=0, title="50% Positive Band")
b382hplot = plot(b382h, color=orange, style=linebr, transp=0, title="38.2% Positive Band")
b382lplot = plot(b382l, color=orange, style=linebr, transp=0, title="38.2% Negative Band")
b50lplot = plot(b50l, color=yellow, style=linebr, transp=0, title="50% Negative Band")
b618lplot = plot(b618l, color=green, style=linebr, transp=0, title="61.8% Negative Band")
b764lplot = plot(b764l, color=lime, style=linebr, transp=0, title="76.4% Negative Band")
b1lplot = plot(b1l, color=black, style=linebr, transp=0, title="100% Negative Band")
b1272lplot = plot(b1272l, color=blue, style=linebr, transp=0, title="127.2% Negative Band")
b1414lplot = plot(b1414l, color=purple, style=linebr, transp=0, title="141.4% Negative Band")
b1618lplot = plot(b1618l, color=fuchsia, style=linebr, transp=0, title="161.8% Negative Band")
//Band Fills
fill(b1618hplot, b1414hplot, color=fuchsia)
fill(b1414hplot, b1272hplot, color=purple)
fill(b1272hplot, b1hplot, color=blue)
fill(b1hplot, b764hplot, color=lime)
fill(b764hplot, b618hplot, color=green)
fill(b618hplot, b50hplot, color=yellow)
fill(b50hplot, b382hplot, color=orange)
fill(b50lplot, b382lplot, color=orange)
fill(b618lplot, b50lplot, color=yellow)
fill(b764lplot, b618lplot, color=green)
fill(b1lplot, b764lplot, color=lime)
fill(b1272lplot, b1lplot, color=blue)
fill(b1414lplot, b1272lplot, color=purple)
fill(b1618lplot, b1414lplot, color=fuchsia)
//Bar Color
barcolor(kcolor)
try this. I added stDev option as well.

CSS:
#https://www.tradingview.com/v/s5nMUzqE/
#@DonovanWall
#study(title="KAMA Fibonacci Bands [DW]", shorttitle="KFB [DW]", overlay=true)
# Converted and mod by Sam4Cok@Samer800    - 05/2023

input BarColors = yes;
input src = hlc3;#, title="Source")
input per = 34;#, minval=1, title="Sampling Period")
input CalcMethod = {Default "ATR", "StDev"};
input fibBandOption = {Default "Extended Fibonacci Levels", "Fibonacci Retracement Levels", "Don't show Fibonacci Levels"};
input fast = 0.666;#,  step=0.001,  title="Smoothing Constant Fast End")
input slow = 0.0645;#, step=0.0001, title="Smoothing Constant Slow End")
input mult = 2;#, minval=1, title="ATR Multiplier")

def na = Double.NaN;
def atrCalc = CalcMethod==CalcMethod."ATR";
def NoLevel = fibBandOption==fibBandOption."Don't show Fibonacci Levels";
def AllLevel = fibBandOption==fibBandOption."Extended Fibonacci Levels";
#-----Color
DefineGlobalColor("fuchsia", CreateColor(255, 0, 255));
DefineGlobalColor("purple" , CreateColor(128, 0, 128));
DefineGlobalColor("blue"   , CreateColor(0, 0, 255));
DefineGlobalColor("black"  , Color.DARK_GRAY);#CreateColor(238,242,17));
DefineGlobalColor("lime"   , CreateColor(0, 255, 0));
DefineGlobalColor("green"  , CreateColor(0, 128, 0));
DefineGlobalColor("yellow" , CreateColor(255, 255, 0));
DefineGlobalColor("orange" , CreateColor(255, 127, 0));

#//KAMA
#kama(a, b)=>
script kama {
    input a = close;
    input b = 34;
    input fast = 0.666;
    input slow = 0.0645;
    def dist   = AbsValue(a[0] - a[1]);
    def signal = AbsValue(a - a[b]);
    def noise  = Sum(dist, b - 1);
    def effr   = if noise != 0 then signal / noise else 1;
    def sc     = Power(effr * (fast - slow) + slow, 2);
    def kama   = CompoundValue(1, kama[1] + sc * (a - kama[1]), a);
    plot out = kama;
}

def kma = kama(src, per, fast, slow);
def nATR = ATR(LENGTH = per) * mult;
def sdev   = StDev(src, per) * mult;

def dev = if atrCalc then nATR else sdev;

#//Bands
def b382h  = kma + dev * 0.382;
def b50h   = kma + dev * 0.5;
def b618h  = kma + dev * 0.618;
def b764h  = kma + dev * 0.786;
def b1h    = kma + dev;
def b1272h = kma + dev * 1.272;
def b1414h = kma + dev * 1.414;
def b1618h = kma + dev * 1.618;
def b382l  = kma - dev * 0.382;
def b50l   = kma - dev * 0.5;
def b618l  = kma - dev * 0.618;
def b764l  = kma - dev * 0.786;
def b1l    = kma - dev;
def b1272l = kma - dev * 1.272;
def b1414l = kma - dev * 1.414;
def b1618l = kma - dev * 1.618;

#//Color
def kcol = if kma > kma[1] then 1 else if kma < kma[1] then -1 else 0;

#//-------------------------------------------------------
#/KAMA Plot
plot kamaPlot = kma;    # "KAMA"
kamaPlot.SetLineWeight(2);
kamaPlot.AssignValueColor(if kcol > 0 then Color.GREEN else
                          if kcol < 0 then Color.RED else Color.GRAY);
#//Band Plots
plot b1618hplot = if !AllLevel then na else b1618h;  # "161.8% Positive Band"
plot b1414hplot = if !AllLevel then na else b1414h;  # "141.4% Positive Band"
plot b1272hplot = if !AllLevel then na else b1272h;  # "127.2% Positive Band"
plot b1hplot    = if !AllLevel then na else b1h;     # "100% Positive Band"
plot b764hplot  = if Nolevel then na else b764h;     # "78.6% Positive Band"
plot b618hplot  = if Nolevel then na else b618h;     # "61.8% Positive Band"
plot b50hplot   = if !AllLevel then na else b50h;    # "50% Positive Band"
plot b382hplot  = if Nolevel then na else b382h;     # "38.2% Positive Band"
plot b382lplot  = if Nolevel then na else b382l;     # "38.2% Negative Band"
plot b50lplot   = if !AllLevel then na else b50l;    # "50% Negative Band"
plot b618lplot  = if Nolevel then na else b618l;     # "61.8% Negative Band"
plot b764lplot  = if Nolevel then na else b764l;     # "78.6% Negative Band"
plot b1lplot    = if !AllLevel then na else b1l;     # "100% Negative Band"
plot b1272lplot = if !AllLevel then na else b1272l;  # "127.2% Negative Band"
plot b1414lplot = if !AllLevel then na else b1414l;  # "141.4% Negative Band"
plot b1618lplot = if !AllLevel then na else b1618l;  # "161.8% Negative Band"

b1618hplot.SetDefaultColor(GlobalColor("fuchsia"));
b1414hplot.SetDefaultColor(GlobalColor("purple"));
b1272hplot.SetDefaultColor(GlobalColor("blue"));
b1hplot.SetDefaultColor(GlobalColor("black"));
b764hplot.SetDefaultColor(GlobalColor("lime"));
b618hplot.SetDefaultColor(GlobalColor("green"));
b50hplot.SetDefaultColor(GlobalColor("yellow"));
b382hplot.SetDefaultColor(GlobalColor("orange"));
b382lplot.SetDefaultColor(GlobalColor("orange"));
b50lplot.SetDefaultColor(GlobalColor("yellow"));
b618lplot.SetDefaultColor(GlobalColor("green"));
b764lplot.SetDefaultColor(GlobalColor("lime"));
b1lplot.SetDefaultColor(GlobalColor("black"));
b1272lplot.SetDefaultColor(GlobalColor("blue"));
b1414lplot.SetDefaultColor(GlobalColor("purple"));
b1618lplot.SetDefaultColor(GlobalColor("fuchsia"));

#//Band Fills
AddCloud(b382hplot, b382lplot, Color.DARK_GRAY);

#//Bar Color
def ExtUp = src>b618h and kcol>0;
def Up    = kcol>0;
def ExtDn = src<b618l and kcol<0;
def Dn    = kcol<0;

AssignPriceColor(if !BarColors then Color.CURRENT else
                 if ExtUp then Color.GREEN else
                 if Up    then Color.DARK_GREEN else
                 if ExtDn then Color.RED else
                 if Dn    then Color.DARK_RED else Color.GRAY);


#-- END OF CODE
 
try this. I added stDev option as well.

CSS:
#https://www.tradingview.com/v/s5nMUzqE/
#@DonovanWall
#study(title="KAMA Fibonacci Bands [DW]", shorttitle="KFB [DW]", overlay=true)
# Converted and mod by Sam4Cok@Samer800    - 05/2023

input BarColors = yes;
input src = hlc3;#, title="Source")
input per = 34;#, minval=1, title="Sampling Period")
input CalcMethod = {Default "ATR", "StDev"};
input fibBandOption = {Default "Extended Fibonacci Levels", "Fibonacci Retracement Levels", "Don't show Fibonacci Levels"};
input fast = 0.666;#,  step=0.001,  title="Smoothing Constant Fast End")
input slow = 0.0645;#, step=0.0001, title="Smoothing Constant Slow End")
input mult = 2;#, minval=1, title="ATR Multiplier")

def na = Double.NaN;
def atrCalc = CalcMethod==CalcMethod."ATR";
def NoLevel = fibBandOption==fibBandOption."Don't show Fibonacci Levels";
def AllLevel = fibBandOption==fibBandOption."Extended Fibonacci Levels";
#-----Color
DefineGlobalColor("fuchsia", CreateColor(255, 0, 255));
DefineGlobalColor("purple" , CreateColor(128, 0, 128));
DefineGlobalColor("blue"   , CreateColor(0, 0, 255));
DefineGlobalColor("black"  , Color.DARK_GRAY);#CreateColor(238,242,17));
DefineGlobalColor("lime"   , CreateColor(0, 255, 0));
DefineGlobalColor("green"  , CreateColor(0, 128, 0));
DefineGlobalColor("yellow" , CreateColor(255, 255, 0));
DefineGlobalColor("orange" , CreateColor(255, 127, 0));

#//KAMA
#kama(a, b)=>
script kama {
    input a = close;
    input b = 34;
    input fast = 0.666;
    input slow = 0.0645;
    def dist   = AbsValue(a[0] - a[1]);
    def signal = AbsValue(a - a[b]);
    def noise  = Sum(dist, b - 1);
    def effr   = if noise != 0 then signal / noise else 1;
    def sc     = Power(effr * (fast - slow) + slow, 2);
    def kama   = CompoundValue(1, kama[1] + sc * (a - kama[1]), a);
    plot out = kama;
}

def kma = kama(src, per, fast, slow);
def nATR = ATR(LENGTH = per) * mult;
def sdev   = StDev(src, per) * mult;

def dev = if atrCalc then nATR else sdev;

#//Bands
def b382h  = kma + dev * 0.382;
def b50h   = kma + dev * 0.5;
def b618h  = kma + dev * 0.618;
def b764h  = kma + dev * 0.786;
def b1h    = kma + dev;
def b1272h = kma + dev * 1.272;
def b1414h = kma + dev * 1.414;
def b1618h = kma + dev * 1.618;
def b382l  = kma - dev * 0.382;
def b50l   = kma - dev * 0.5;
def b618l  = kma - dev * 0.618;
def b764l  = kma - dev * 0.786;
def b1l    = kma - dev;
def b1272l = kma - dev * 1.272;
def b1414l = kma - dev * 1.414;
def b1618l = kma - dev * 1.618;

#//Color
def kcol = if kma > kma[1] then 1 else if kma < kma[1] then -1 else 0;

#//-------------------------------------------------------
#/KAMA Plot
plot kamaPlot = kma;    # "KAMA"
kamaPlot.SetLineWeight(2);
kamaPlot.AssignValueColor(if kcol > 0 then Color.GREEN else
                          if kcol < 0 then Color.RED else Color.GRAY);
#//Band Plots
plot b1618hplot = if !AllLevel then na else b1618h;  # "161.8% Positive Band"
plot b1414hplot = if !AllLevel then na else b1414h;  # "141.4% Positive Band"
plot b1272hplot = if !AllLevel then na else b1272h;  # "127.2% Positive Band"
plot b1hplot    = if !AllLevel then na else b1h;     # "100% Positive Band"
plot b764hplot  = if Nolevel then na else b764h;     # "78.6% Positive Band"
plot b618hplot  = if Nolevel then na else b618h;     # "61.8% Positive Band"
plot b50hplot   = if !AllLevel then na else b50h;    # "50% Positive Band"
plot b382hplot  = if Nolevel then na else b382h;     # "38.2% Positive Band"
plot b382lplot  = if Nolevel then na else b382l;     # "38.2% Negative Band"
plot b50lplot   = if !AllLevel then na else b50l;    # "50% Negative Band"
plot b618lplot  = if Nolevel then na else b618l;     # "61.8% Negative Band"
plot b764lplot  = if Nolevel then na else b764l;     # "78.6% Negative Band"
plot b1lplot    = if !AllLevel then na else b1l;     # "100% Negative Band"
plot b1272lplot = if !AllLevel then na else b1272l;  # "127.2% Negative Band"
plot b1414lplot = if !AllLevel then na else b1414l;  # "141.4% Negative Band"
plot b1618lplot = if !AllLevel then na else b1618l;  # "161.8% Negative Band"

b1618hplot.SetDefaultColor(GlobalColor("fuchsia"));
b1414hplot.SetDefaultColor(GlobalColor("purple"));
b1272hplot.SetDefaultColor(GlobalColor("blue"));
b1hplot.SetDefaultColor(GlobalColor("black"));
b764hplot.SetDefaultColor(GlobalColor("lime"));
b618hplot.SetDefaultColor(GlobalColor("green"));
b50hplot.SetDefaultColor(GlobalColor("yellow"));
b382hplot.SetDefaultColor(GlobalColor("orange"));
b382lplot.SetDefaultColor(GlobalColor("orange"));
b50lplot.SetDefaultColor(GlobalColor("yellow"));
b618lplot.SetDefaultColor(GlobalColor("green"));
b764lplot.SetDefaultColor(GlobalColor("lime"));
b1lplot.SetDefaultColor(GlobalColor("black"));
b1272lplot.SetDefaultColor(GlobalColor("blue"));
b1414lplot.SetDefaultColor(GlobalColor("purple"));
b1618lplot.SetDefaultColor(GlobalColor("fuchsia"));

#//Band Fills
AddCloud(b382hplot, b382lplot, Color.DARK_GRAY);

#//Bar Color
def ExtUp = src>b618h and kcol>0;
def Up    = kcol>0;
def ExtDn = src<b618l and kcol<0;
def Dn    = kcol<0;

AssignPriceColor(if !BarColors then Color.CURRENT else
                 if ExtUp then Color.GREEN else
                 if Up    then Color.DARK_GREEN else
                 if ExtDn then Color.RED else
                 if Dn    then Color.DARK_RED else Color.GRAY);


#-- END OF CODE
@samer800 thank you very much for this great system, your work is so good. thank you ! a quick favor samer, do you think it is possible to add arrow for the signal, example green arrow for long and red arrow for short. thank you in advance
 
@samer800 thank you very much for this great system, your work is so good. thank you ! a quick favor samer, do you think it is possible to add arrow for the signal, example green arrow for long and red arrow for short. thank you in advance
pls provide the condition. When to plot green/red arrow
 
@samer800 thank you for the reply. condition: when changes from red to green, a green arrow with the green candle, and a red arrow with a red candle. thank you in advance!
add this at the end of the code:

CSS:
input ShowSignals = yes;
def UpSig = kcol>0 and kcol!=kcol[1];
def DnSig = kcol<0 and kcol!=kcol[1];
plot sigUp = if showSignals then if upSig then low else na else na;
sigUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot sigDn = if showSignals then if dnSig then high else na else na;
sigDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.LIGHT_RED);
 
add this at the end of the code:

CSS:
input ShowSignals = yes;
def UpSig = kcol>0 and kcol!=kcol[1];
def DnSig = kcol<0 and kcol!=kcol[1];
plot sigUp = if showSignals then if upSig then low else na else na;
sigUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot sigDn = if showSignals then if dnSig then high else na else na;
sigDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.LIGHT_RED);
@samer800 thank you very much!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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