Fibonacci Trading for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
SCnaXjv.png


This simple script draw Fibonacci Retracement to define pullback level and draw Fibonacci Extension to define target level of a upward wave or doward wave

CODE:
CSS:
# https://www.tradingview.com/v/kLsYz4jj/
#//@inno14
#//Fibonacci Trading
# Converted by Sam4Cok@Samer800    - 10/2023
#indicator('Fibonacci Trading', overlay=true, max_l
input FillBackground = yes;
input showBubbles = {Default "Information", "Price Value", "Don't Show"};
input extendLineBy = 5;
input ShowMovAvg = yes;
input maCycleSetup = {Default "EMA/SMA", "EMA/RMA"};
input source = close;
input MovAvgPeriod = 50;
input trading_setup = {Default "MA Cycle", "Long Only","Short Only","Both"};
input showFractals = yes;
input NoOfBarsFromTheLeftOfFractal = 6;
input NoOfBarsFromTheRightOfFractal = 6;

def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def left = NoOfBarsFromTheLeftOfFractal;
def right = NoOfBarsFromTheRightOfFractal;
def info = showBubbles==showBubbles."Information";
def noLab = showBubbles!=showBubbles."Don't Show";
def emasma = maCycleSetup==maCycleSetup."EMA/SMA";

DefineGlobalColor("bgUp", CreateColor(0, 0, 127));
DefineGlobalColor("bgDn", CreateColor(127, 0, 127));

def ema = ExpAverage(source, MovAvgPeriod);
def sma = Average(source, MovAvgPeriod);
def rma = WildersAverage(source, MovAvgPeriod);
def ma2 = if emasma then sma else rma;
def _c = ema - ma2;
def bgCol = if !FillBackground then na else
            if _c > 0 then 1 else -1;

plot movAvg1 = if ShowMovAvg then ema else na;
plot movAvg2 = if ShowMovAvg then ma2 else na;
movAvg1.SetDefaultColor(Color.CYAN);
movAvg2.SetDefaultColor(GetColor(2));

AddCloud(if bgCol > 0 then pos else neg, if bgCol > 0 then neg else pos,
         GlobalColor("bgUp"), GlobalColor("bgDn"));

script isFractal {
input x = close;
input Left = 6;
input Right = 6;
    def hiBar = GetMaxValueOffset(x, Left + Right + 1);
    def isFractal = hibar == Right;
    plot out = isFractal;
}
script Lab {
input val = close;
    def cond = isNaN(val) and !isNaN(val[1]);
    def lab1 = if cond then val[1] else Double.NaN;
    def lab2 = Round(lab1, 2);
    plot out = lab2;
}
Script DrawLine {
input fromCond = no;
input toCond = no;
input endVal = close;
input extend = 6;
    def last = isNaN(close);
    def bar = AbsValue(BarNumber());
    def fromBar = highestAll(fromCond);
    def toVal = endVal;
    def line1 = inertiaAll(toVal,2);
    def line = highestAll(line1);
    plot out = if bar > fromBar-1 and !last[extend] then line else Double.NaN;
}
def long_; def short_;
Switch (trading_setup) {
Case "Long Only" :
    long_ = yes;
    short_ = no;
Case "Short Only" :
    long_ = no;
    short_ = yes;
Case "Both" :
    long_ = yes;
    short_ = yes;
Default :
    if _c > 0 {
    long_ = yes;
    short_ = no;
    } else {
    long_ = no;
    short_ = yes;
    }
}
def long = long_;
def short = short_;

def sF = isFractal(-low, Left, Right);
def rF = isFractal(high, Left, Right);
def support = if !support[1] then low else
              if sF then low[Right] else support[1];
def resistance = if !resistance[1] then high else
                 if rF then high[Right] else resistance[1];
def fraLo = if sF then low[Right] else na;
def fraHi = if rF then high[Right] else na;


plot FractalLo = if showFractals then fraLo[-Right] else na;    # "Fractal Low"
plot FractalHi = if showFractals then fraHi[-Right] else na;    # "Fractal High"
FractalLo.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
FractalHi.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_Up);

FractalLo.SetDefaultColor(Color.CYAN);
FractalHi.SetDefaultColor(Color.RED);

#/Value & Index
def bar_index = AbsValue(BarNumber());
def ph = rF;
def ph_val = if ph then resistance else ph_val[1];

def res = ph_val;
def ph_index = if ph then bar_index - Right else ph_index[1];

def pl = sF;
def pl_val = if pl then support else pl_val[1];
def sup = pl_val;
def pl_index = if pl then bar_index - Right else pl_index[1];

#//Long setup
def long_sup_index; def long_sup_val; def long_res_val;
if pl_index < ph_index {
    long_sup_index = pl_index;
    long_sup_val = pl_val;
    long_res_val = ph_val;
    } else {
    long_sup_index = long_sup_index[1];
    long_sup_val = long_sup_val[1];
    long_res_val = if long_sup_index==long_sup_index[1] then long_res_val[1] else na;
}
#//Short setup
def short_res_index; def short_res_val; def short_sup_val;
if pl_index > ph_index {
    short_res_index = ph_index;
    short_res_val = ph_val;
    short_sup_val = pl_val;
    } else {
    short_res_index = short_res_index[1];
    short_res_val = short_res_val[1];
    short_sup_val = if short_res_index==short_res_index[1] then short_sup_val[1] else na;
}
#//Short Condition
def short_cond = (source < short_res_val and Short);
#//Long Condition
def long_cond = (source > long_sup_val and Long);
#//Resistance Line
def long_res_x1 = long_sup_index;
def long_res_y1 = if long_cond then long_res_val else na;
def long_res_x2 = bar_index;
def long_res_y2 = long_res_y1;
#//Support Line
def long_sup_x1 = long_sup_index;
def long_sup_y1 = if Long then long_sup_val else na;
def long_sup_x2 = bar_index;
def long_sup_y2 = long_sup_y1;
#//Fib50 Line
def long_fib50_x1 = long_sup_index;
def long_fib50_y1 = if long_cond then (long_res_y1 + long_sup_y1) *0.5 else na;
def long_fib50_x2 = bar_index;
def long_fib50_y2 = long_fib50_y1;
#//Fib618 Line
def long_fib618_x1 = long_sup_index;
def long_fib618_y1 = if long_cond then
                     long_res_y1-(long_res_y1-long_sup_y1)*0.618 else na;
def long_fib618_x2 = bar_index;
def long_fib618_y2 = long_fib618_y1;
#//tp1
def long_tp1_x1 = long_sup_index;
def long_tp1_y1 = if long_cond then
                  long_res_y1+(long_res_y1-long_sup_y1)*0.382 else na;
def long_tp1_x2 = bar_index;
def long_tp1_y2 = long_tp1_y1;
#//tp2
def long_tp2_x1 = long_sup_index;
def long_tp2_y1 = if long_cond then
                  long_res_y1+(long_res_y1-long_sup_y1) else na;
def long_tp2_x2 = bar_index;
def long_tp2_y2 = long_tp2_y1;
#// Short Resistance Line
def short_res_x1 = short_res_index;
def short_res_y1 = if Short then short_res_val else na;
def short_res_x2 = bar_index;
def short_res_y2 = short_res_y1;
#// Short Support Line
def short_sup_x1 = short_res_index;
def short_sup_y1 = if short_cond then short_sup_val else na;
def short_sup_x2 = bar_index;
def short_sup_y2 = short_sup_y1;
#//Short Fib50 Line
def short_fib50_x1 = short_res_index;
def short_fib50_y1 = if short_cond then (short_res_y1+short_sup_y1)*0.5 else na;
def short_fib50_x2 = bar_index;
def short_fib50_y2 = short_fib50_y1;
#//Short Fib618 Line
def short_fib618_x1 = short_res_index;
def short_fib618_y1 = if short_cond then
                      short_sup_y1+(short_res_y1-short_sup_y1)*0.618 else na;
def short_fib618_x2 = bar_index;
def short_fib618_y2 = short_fib618_y1;
#// Short tp1
def short_tp1_x1 = short_res_index;
def short_tp1_y1 = if short_cond then
                   short_sup_y1-(short_res_y1-short_sup_y1)*0.382 else na;
def short_tp1_x2 = bar_index;
def short_tp1_y2 = short_tp1_y1;
#// Short tp2
def short_tp2_x1 = short_res_index;
def short_tp2_y1 = if short_cond then
                  short_sup_y1-(short_res_y1-short_sup_y1) else na;
def short_tp2_x2 = bar_index;
def short_tp2_y2 = short_tp2_y1;

#-- Long
def BreakEve = DrawLine(long_res_x1, long_res_x2, long_res_y2, extendLineby);
def Inval    = DrawLine(long_sup_x1, long_sup_x2, long_sup_y2, extendLineby);
def lvl500   = DrawLine(long_fib50_x1, long_fib50_x2, long_fib50_y2, extendLineby);
def lvl618   = DrawLine(long_fib618_x1,long_fib618_x2, long_fib618_y2, extendLineby);
def tp1      = DrawLine(long_tp1_x1, long_tp1_x2, long_tp1_y2, extendLineby);
def tp2      = DrawLine(long_tp2_x1, long_tp2_x2, long_tp2_y2, extendLineby);
#-- Short
def sBreakEve = DrawLine(short_sup_x1, short_sup_x2, short_sup_y2, extendLineby);
def sInval    = DrawLine(short_res_x1, short_res_x2, short_res_y2, extendLineby);
def slvl500   = DrawLine(short_fib50_x1, short_fib50_x2, short_fib50_y2, extendLineby);
def slvl618   = DrawLine(short_fib618_x1,short_fib618_x2,short_fib618_y2, extendLineby);
def stp1      = DrawLine(short_tp1_x1, short_tp1_x2, short_tp1_y2, extendLineby);
def stp2      = DrawLine(short_tp2_x1, short_tp2_x2, short_tp2_y2, extendLineby);

#-- Long
plot BreakEven = BreakEve;
plot Invalid = Inval;
plot Level500 = lvl500;
plot Level618 = lvl618;
plot ProfLong1 = tp1;
plot profLong2 = tp2;
#-- Short
plot sBreakEven = sBreakEve;
plot sInvalid = sInval;
plot sLevel500 = slvl500;
plot sLevel618 = slvl618;
plot sProfLong1 = stp1;
plot sprofLong2 = stp2;

BreakEven.SetDefaultColor(Color.LIGHT_GREEN);
Invalid.SetDefaultColor(Color.VIOLET);
Level500.SetDefaultColor(Color.VIOLET);
Level618.SetDefaultColor(Color.VIOLET);
ProfLong1.SetDefaultColor(Color.CYAN);
ProfLong2.SetDefaultColor(Color.CYAN);

sBreakEven.SetDefaultColor(Color.LIGHT_RED);
sInvalid.SetDefaultColor(Color.PLUM);
sLevel500.SetDefaultColor(Color.PLUM);
sLevel618.SetDefaultColor(Color.PLUM);
sProfLong1.SetDefaultColor(Color.MAGENTA);
sProfLong2.SetDefaultColor(Color.MAGENTA);

AddCloud(Level500, Invalid, GlobalColor("bgUp"));
AddCloud(sInvalid , sLevel500 , GlobalColor("bgDn"));


#-- LAbel
AddChartBubble(noLab and Lab(ProfLong2), Lab(ProfLong2),
            if info then "TP2" else "$" + Lab(ProfLong2), Color.CYAN);
AddChartBubble(noLab and Lab(ProfLong1), Lab(ProfLong1),
            if info then "TP1" else "$" + Lab(ProfLong1), Color.CYAN);
AddChartBubble(noLab and Lab(BreakEven), Lab(BreakEven),
            if info then "Long" else "$" + Lab(BreakEven), Color.LIGHT_GREEN, no);
AddChartBubble(noLab and Lab(Level500), Lab(Level500),
            if info then "0.50" else "$" + Lab(Level500), Color.VIOLET, no);
AddChartBubble(noLab and Lab(Level618), Lab(Level618),
            if info then "0.61" else "$" + Lab(Level618), Color.VIOLET, no);
AddChartBubble(noLab and Lab(Invalid), Lab(Invalid),
            if info then "Stop" else "$" + Lab(Invalid), Color.VIOLET, no);

AddChartBubble(noLab and Lab(sInvalid), Lab(sInvalid),
            if info then "Stop" else "$" + Lab(sInvalid), Color.PLUM);
AddChartBubble(noLab and Lab(sLevel618), Lab(sLevel618),
            if info then "0.61" else "$" + Lab(sLevel618), Color.PLUM);
AddChartBubble(noLab and Lab(sLevel500), Lab(sLevel500),
            if info then "0.50" else "$" + Lab(sLevel500), Color.PLUM);
AddChartBubble(noLab and Lab(sBreakEven), Lab(sBreakEven),
            if info then "Short" else "$" + Lab(sBreakEven), Color.LIGHT_RED);
AddChartBubble(noLab and Lab(sProfLong1), Lab(sProfLong1),
            if info then "TP1" else "$" + Lab(sProfLong1), Color.MAGENTA, no);
AddChartBubble(noLab and Lab(sProfLong2), Lab(sProfLong2),
            if info then "TP2" else "$" + Lab(sProfLong2), Color.MAGENTA, no);


#-- END of CODE
 

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

Thanks for this script. Question though, from the screenshot, why go long at the high anchor? Why not long at the 0.61 retracement and then be scaling at the high anchor?
 
@samer800 - I was reviewing some charts with this setup, and the support above the LONG (50%) and breakdown below are undeniable. Looking at the Fib levels (23.6%, 38.2%): Any stock above that continues to find support and move up. I was using the Daily and Weekly timeframes

Wondering if there is any way to scan for the stocks above these levels. I am not super familiar with coding here, but will continue to look as well

Thanks again for the great work/support to the community.

Cheers!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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