Fibonacci Fans indicator for ThinkorSwim

chewie76

Well-known member
VIP
VIP Enthusiast
Can you develop an indicator that automatically plots Fibonacci Fans? Similar to this?

 
Last edited by a moderator:

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

Give this a try

Rich (BB code):
#hint: <b>Fibonacci Fan lines</b>
Fibonacci Fan lines are trend lines based on Fibonacci retracement points. Rising fan lines extend up from a trough and pass through retracement based on the advance (lowest low to highest high or on the falling fan lines: highest high to lowest low). These fan lines can then be used to estimate support levels or potential reversal zones. Falling fan lines can then be used to estimate resistance levels or potential reversal zones. 

#hint Price: Price used in the alerts on crossing of the fans. <b>(Default is Close)</b> 

#hint onExpansion: Determines if the fan lines are projected past the current bar into the right side expansion <b>(Default is Yes)</b> 

#hint Coefficient1: Fan Line 1: Trough to 38.2% retracement on rising fans.  
Peak to 38.2% Coefficient on falling fans. <b>(Default is 38.2%)</b> 

#hint Coefficient_2: Fan Line 2: 
Trough to 50% retracement on rising fans.  
Peak to 50% Coefficient on falling fans. <b>(Default is 50%)</b> 

#hint Coefficient_3: Fan Line 3: 
Trough to 61.8% retracement on rising fans.  
Peak to 61.8% Coefficient on falling fans. <b>(Default is 61.8%)</b> 

#wizard input: Price 

#wizard text: Inputs: Price: 

#wizard input: onExpansion 

#wizard text: onExpansion: 

#wizard input: Coefficient1  

#wizard text: Coefficient1 : 

#wizard input: Coefficient_2  

#wizard text: Coefficient_2: 

#wizard input: Coefficient_3   

#wizard text: Coefficient_3: 

  

input price = close; 

input high = high; 

input low = low; 

input onExpansion = Yes; 

input Coefficient1 = .382; 

input Coefficient_2 = .5; 

input Coefficient_3 = .618; 

def a = HighestAll(high); 

def b = LowestAll(low); 

def barnumber = barNumber(); 

def c = if high == a then barnumber else double.nan; 

def d = if low == b then barnumber else double.nan; 

rec highnumber = compoundValue(1, if IsNaN(c) then highnumber[1] else c, c); 

def highnumberall = HighestAll(highnumber); 

rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] else d, d); 

def lownumberall = LowestAll(lownumber); 

def upward = highnumberall > lownumberall; 

def downward = highnumberall < lownumberall; 

def low382 =  b + ((b - a) * Coefficient1); 

def low5 =  b + ((b - a) * Coefficient_2); 

def low618 = b + ((b - a) * Coefficient_3); 

  

def high382 =  a - ((a - b) * Coefficient1); 

def high5 =  a - ((a - b) * Coefficient_2); 

def high618 = a - ((a - b) * Coefficient_3); 

  

def x = AbsValue(lownumberall - highnumberall ); 

  

def slope = (a - b) / x; 

def slope382 = (high382 - b) / x; 

def slope5 = (high5 - b) / x; 

def slope618 = (high618 - b) / x; 

  

def slopelow = (b - a) / x; 

def slopelow382 = (low382 - b) / x; 

def slopelow5 = (low5 - b) / x; 

def slopelow618 = (low618 - b) / x; 

  

def day = getDay(); 

def month = getMonth(); 

def year = getYear(); 

def lastDay = getLastDay(); 

def lastmonth = getLastMonth(); 

def lastyear = getLastYear(); 

def isToday = if(day == lastDay and month == lastmonth and year == lastyear, 1, 0); 

def istodaybarnumber = HighestAll(if isToday then barnumber else double.nan); 

  

def line = b + (slope * (barnumber - lownumber)); 

def line382 = b + (slope382 * (barnumber - lownumber)); 

def line5 = b + (slope5 * (barnumber - lownumber)); 

def line618 = b + (slope618 * (barnumber - lownumber)); 

  

def linelow = a + (slopelow * (barnumber - highnumber)); 

def line382low = a + (slopelow382 * (barnumber - highnumber)); 

def line5low = a + (slopelow5 * (barnumber - highnumber)); 

def line618low = a + (slopelow618 * (barnumber - highnumber)); 

  

def currentlinelow = if barnumber <= istodaybarnumber then linelow else double.nan; 

def currentline382low = if barnumber <= istodaybarnumber then line382low else double.nan; 

def currentline5low = if barnumber <= istodaybarnumber then line5low else double.nan; 

def currentline618low = if barnumber <= istodaybarnumber then line618low else double.nan; 

  

def currentline = if barnumber <= istodaybarnumber then line else double.nan; 

def currentline382 = if barnumber <= istodaybarnumber then line382 else double.nan; 

def currentline5 = if barnumber <= istodaybarnumber then line5 else double.nan; 

def currentline618 = if barnumber <= istodaybarnumber then line618 else double.nan; 

  

Plot FibFan =  if  downward and onExpansion then linelow else if downward then currentlinelow else if upward and onExpansion then line else if upward then currentline else double.nan; 

FibFan.SetStyle(Curve.SHORT_DASH); 

FibFan.AssignValueColor(color.red); 

  

Plot "Coefficient 1" =  if (downward and onExpansion) then line382low else if downward then currentline382low else if upward and onExpansion then line382 else if upward then currentline382 else double.nan; 

"Coefficient 1".SetStyle(Curve.Firm); 

"Coefficient 1".AssignValueColor(color.red); 

Plot "Coefficient 2" = if downward and onExpansion then line5low else if downward then currentline5low else if upward and onExpansion then line5 else if upward then currentline5 else double.nan; 

"Coefficient 2".AssignValueColor(color.red); 

"Coefficient 2".SetStyle(Curve.Firm); 

Plot "Coefficient 3" =  if downward and onExpansion then line618low else if downward then currentline618low else if upward and onExpansion then line618 else if upward then currentline618  else double.nan; 

"Coefficient 3".AssignValueColor(color.red); 

"Coefficient 3".SetStyle(Curve.Firm); 

alert((price crosses below "Coefficient 1") , "Price crosses below Coefficient 1"); 

alert((price crosses below "Coefficient 2") , "Price crosses below Coefficient 2"); 

alert((price crosses below "Coefficient 3") , "Price crosses below Coefficient 3"); 

alert((price crosses above "Coefficient 1") , "Price crosses above Coefficient 1"); 

alert((price crosses above "Coefficient 2") , "Price crosses above Coefficient 2"); 

alert((price crosses above "Coefficient 3") , "Price crosses above Coefficient 3"); 

End
 
Last edited:
Here is a Fib Fibonacci Fan
Code:
By Jesse
##Begin##

#hint: <b>Fibonacci Fan lines</b>\nFibonacci Fan lines are trendlines based on Fibonacci retracement points. Rising fan lines extend up from a trough and pass through retracement based on the advance (lowest low to highest high or on the falling fan lines: highest high to lowest low). These fan lines can then be used to estimate support levels or potential reversal zones. Falling fan lines can then be used to estimate resistance levels or potential reversal zones.

#hint Price: Price used in the alerts on crossing of the fans. <b>(Default is Close)</b>
#hint onExpansion: Determines if the fan lines are projected past the current bar into the right side expansion <b>(Default is Yes)</b>
#hint Coefficient1: Fan Line 1: Trough to 38.2% retracement on rising fans.  \nPeak to 38.2% Coefficient on falling fans. <b>(Default is 38.2%)</b>
#hint Coefficient_2: Fan Line 2: \nTrough to 50% retracement on rising fans.  \nPeak to 50% Coefficient on falling fans. <b>(Default is 50%)</b>
#hint Coefficient_3: Fan Line 3: \nTrough to 61.8% retracement on rising fans.  \nPeak to 61.8% Coefficient on falling fans. <b>(Default is 61.8%)</b>

#wizard input: Price
#wizard text: Inputs: Price:
#wizard input: onExpansion
#wizard text: onExpansion:
#wizard input: Coefficient1
#wizard text: Coefficient1 :
#wizard input: Coefficient_2
#wizard text: Coefficient_2:
#wizard input: Coefficient_3
#wizard text: Coefficient_3:

input price = close;
input high = high;
input low = low;
input onExpansion = Yes;
input Coefficient1 = .382;
input Coefficient_2 = .5;
input Coefficient_3 = .618;

def a = HighestAll(high);
def b = LowestAll(low);
def barnumber = barNumber();
def c = if high == a then barnumber else double.nan;
def d = if low == b then barnumber else double.nan;
rec highnumber = compoundValue(1, if IsNaN(c) then highnumber[1] else c, c);
def highnumberall = HighestAll(highnumber);
rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] else d, d);
def lownumberall = LowestAll(lownumber);

def upward = highnumberall > lownumberall;
def downward = highnumberall < lownumberall;

def low382 =  b + ((b - a) * Coefficient1);
def low5 =  b + ((b - a) * Coefficient_2);
def low618 = b + ((b - a) * Coefficient_3);

def high382 =  a - ((a - b) * Coefficient1);
def high5 =  a - ((a - b) * Coefficient_2);
def high618 = a - ((a - b) * Coefficient_3);

def x = AbsValue(lownumberall - highnumberall );

def slope = (a - b) / x;
def slope382 = (high382 - b) / x;
def slope5 = (high5 - b) / x;
def slope618 = (high618 - b) / x;

def slopelow = (b - a) / x;
def slopelow382 = (low382 - b) / x;
def slopelow5 = (low5 - b) / x;
def slopelow618 = (low618 - b) / x;

def day = getDay();
def month = getMonth();
def year = getYear();
def lastDay = getLastDay();
def lastmonth = getLastMonth();
def lastyear = getLastYear();
def isToday = if(day == lastDay and month == lastmonth and year == lastyear, 1, 0);
def istodaybarnumber = HighestAll(if isToday then barnumber else double.nan);

def line = b + (slope * (barnumber - lownumber));
def line382 = b + (slope382 * (barnumber - lownumber));
def line5 = b + (slope5 * (barnumber - lownumber));
def line618 = b + (slope618 * (barnumber - lownumber));

def linelow = a + (slopelow * (barnumber - highnumber));
def line382low = a + (slopelow382 * (barnumber - highnumber));
def line5low = a + (slopelow5 * (barnumber - highnumber));
def line618low = a + (slopelow618 * (barnumber - highnumber));

def currentlinelow = if barnumber <= istodaybarnumber then linelow else double.nan;
def currentline382low = if barnumber <= istodaybarnumber then line382low else double.nan;
def currentline5low = if barnumber <= istodaybarnumber then line5low else double.nan;
def currentline618low = if barnumber <= istodaybarnumber then line618low else double.nan;

def currentline = if barnumber <= istodaybarnumber then line else double.nan;
def currentline382 = if barnumber <= istodaybarnumber then line382 else double.nan;
def currentline5 = if barnumber <= istodaybarnumber then line5 else double.nan;
def currentline618 = if barnumber <= istodaybarnumber then line618 else double.nan;

Plot FibFan =  if  downward and onExpansion then linelow else if downward then currentlinelow else if upward and onExpansion then line else if upward then currentline else double.nan;
FibFan.SetStyle(Curve.SHORT_DASH);
FibFan.AssignValueColor(color.red);

Plot "Coefficient 1" =  if (downward and onExpansion) then line382low else if downward then currentline382low else if upward and onExpansion then line382 else if upward then currentline382 else double.nan;
"Coefficient 1".SetStyle(Curve.Firm);
"Coefficient 1".AssignValueColor(color.red);
Plot "Coefficient 2" = if downward and onExpansion then line5low else if downward then currentline5low else if upward and onExpansion then line5 else if upward then currentline5 else double.nan;
"Coefficient 2".AssignValueColor(color.red);
"Coefficient 2".SetStyle(Curve.Firm);
Plot "Coefficient 3" =  if downward and onExpansion then line618low else if downward then currentline618low else if upward and onExpansion then line618 else if upward then currentline618  else double.nan;
"Coefficient 3".AssignValueColor(color.red);
"Coefficient 3".SetStyle(Curve.Firm);

alert((price crosses below "Coefficient 1") , "Price crosses below Coefficient 1");
alert((price crosses below "Coefficient 2") , "Price crosses below Coefficient 2");
alert((price crosses below "Coefficient 3") , "Price crosses below Coefficient 3");
alert((price crosses above "Coefficient 1") , "Price crosses above Coefficient 1");
alert((price crosses above "Coefficient 2") , "Price crosses above Coefficient 2");
alert((price crosses above "Coefficient 3") , "Price crosses above Coefficient 3");
##End##
 
Last edited by a moderator:

Above what you see is an aggregate version of auto Fib Fans that measures the high and low of price of the previous aggregation be it the month quarter year etc. I have found it to be an amazing indicator but my only problem is that the code is not inputting the correct highs and lows of the previous aggregation due to incorporating "highestall" and "barnumber" as part of the code. Been trying to find a workaround this but everytime I mess with the code it goes form angled to horizontal lines. Was hoping the community would be able to assist in this. The Fibs shown are what I found to so far be the best lines to use, but if anyone finds a better set of lines then by all means would love to hear it.

Also as a side note from my experience is that aggregations of quarters like the quarter and yearly seem to be the best aggregations since I personally believe and from what I have read is that the quarterly is when algos and hedge funds seem to reset or adjust positions. May be totally wrong but from what I have seen it seems to be logical.

Ruby:
input aggregationAF = AggregationPeriod.quARTER;

def high = high(period = aggregationAF)[1];
def low = low(period = aggregationAF)[1];
def close = low(period = aggregationaf)[1];

def a = high;
def b = low;


def barnumber = (BarNumber());


def c = if high == a then barnumber else Double.NaN;
def d = if low == b then barnumber else Double.Nan;


def highnumberall = HighestAll(c);
def lownumberall = LowestAll(d);
input factoraf = 1;
input lengthaf = 1;
def ATR = FactorAF * MovingAverage( AverageType.SIMPLE, TrueRange(high, close, low), lengthAF);

def low1 =  b + ((b-a) * -2.386);
def low2 =  b + ((b-a) * .114);
def low3 = b + ((b-a) * -2.618);
def low4 = b + ((b-a) * .236);
def low5 = b + ((b-a) * -1.3);
def low6 = b + ((b-a) * .618);
def low7 = b + ((b-a) * 1.618);
def low8 =  b + ((b-a) * .386);
def low9 =  b + ((b-a) * 0);
def low10 = b + ((b-a) * -.236);
def low11 = b + ((b-a) * -.114);
def low12 = b + ((b-a) * -5.368);
def low13 = b + ((b-a) * -.618);
def low14 = b + ((b-a) * -.236);
def low15 = b + ((b-a) * -1.14);
def low16 = b + ((b-a) * -1.7);
def low17 = b + ((b-a) * -1.886);
def low18 = b + ((b-a) * -1);

def x = AbsValue(lownumberall - highnumberall );


def slope = (a - b) / x;
def slope2 = (low1 - b) / x;
def slope3 = (low2 - b) / x;
def slope4 = (low3 - b) / x;
def slope5 = (low4 - b) / x;
def slope6 = (low5 - b) / x;
def slope7 = (low6 - b) / x;
def slope8 = (low7 - b) / x;
def slope9 = (low8 - b) / x;
def slope10 = (low9 - b) / x;
def slope11 = (low10 - b) / x;
def slope12 = (low11 - b) / x;
def slope13 = (low12 - b) / x;
def slope14 = (low13 - b) / x;
def slope15 = (low14 - b) / x;
def slope16 = (low15 - b) / x;
def slope17 = (low16 - b) / x;
def slope18 = (low17 - b) / x;
def slope19 = (low18 - b) / x;


plot line = b + (.5*slope * (d - low));
plot line2 = b + (slope2 * (d - low));
plot line3 = b + (slope3 * (d - low));
plot line4 = b + (slope4 * (d - low));
plot line5 = b + (slope5 * (d - low));
plot line6 = b + (slope6 * (d - low));
plot line7 = b + (slope7 * (d - low));
plot line8 = b + (slope8 * (d - low));
plot line9 = b + (slope9 * (d - low));
plot line10 = b + (slope10 * (d - low));
plot line11 = b + (slope11 * (d - low));
plot line12 = b + (slope12 * (d - low));
plot line13 = b + (slope13 * (d - low));
plot line14 = b + (slope14 * (d - low));
plot line15 = b + (slope15 * (d - low));
plot line16 = b + (slope16 * (d - low));
plot line17 = b + (slope17 * (d - low));
plot line18 = b + (slope18 * (d - low));
plot line19 = b + (slope19 * (d - low));

#y=mx+b
def paintingStrategy = PaintingStrategy.HORIZONTAL ;
line.SetPaintingStrategy(paintingStrategy);
line2.SetPaintingStrategy(paintingStrategy);
line3.SetPaintingStrategy(paintingStrategy);
line4.SetPaintingStrategy(paintingStrategy);
line5.SetPaintingStrategy(paintingStrategy);
line6.SetPaintingStrategy(paintingStrategy);
line7.SetPaintingStrategy(paintingStrategy);
line8.SetPaintingStrategy(paintingStrategy);
line9.SetPaintingStrategy(paintingStrategy);
line10.SetPaintingStrategy(paintingStrategy);
line11.SetPaintingStrategy(paintingStrategy);
line12.SetPaintingStrategy(paintingStrategy);
line13.SetPaintingStrategy(paintingStrategy);
line14.SetPaintingStrategy(paintingStrategy);
line15.SetPaintingStrategy(paintingStrategy);
line16.SetPaintingStrategy(paintingStrategy);
line17.SetPaintingStrategy(paintingStrategy);
line18.SetPaintingStrategy(paintingStrategy);
line19.SetPaintingStrategy(paintingStrategy);


addcloud(line17, line18, color.green);
addcloud(line12, line3, color.green);
addcloud(line9, line7, color.cyan);
addcloud(line14, line, color.green);
addcloud(line6, line19, color.yellow);
addcloud(line4, line2, color.red);
 
Last edited:
Can you make the bottom indicator version4?
thanks

//@version=2
study(title='Fibonacci FAN', shorttitle='FibFan', overlay=true)

tf = input('W', title='Alt Timeframe')

zigzag() =>
_isUp = close >= close[1]
_isDown = close <= close[1]
_direction = _isUp[1] and _isDown ? -1 : _isDown[1] and _isUp ? 1 : nz(_direction[1])
_zigzag = _isUp[1] and _isDown and _direction[1] != -1 ? highest(2) : _isDown[1] and _isUp and _direction[1] != 1 ? lowest(2) : na

sz = (change(time(tf)) != 0 ? security(tickerid, tf, zigzag()) : na)

plot(sz, title='zigzag', color=black, linewidth=2)

x = valuewhen(sz, sz, 1)
z = valuewhen(sz, sz, 0)
n_x = valuewhen(sz, n, 1)
n_z = valuewhen(sz, n, 0)

price_range = z-x
bar_range = n_z-n_x
step = price_range/bar_range
projection = sz ? na : z + ((n-n_z)*step)
projection_n0236 = sz ? na : z + ((n-n_z)step)-0.236
projection_n0382 = sz ? na : z + ((n-n_z)step)-0.382
projection_n0500 = sz ? na : z + ((n-n_z)step)-0.500
projection_n0618 = sz ? na : z + ((n-n_z)step)-0.618
projection_n1000 = sz ? na : z + ((n-n_z)step)-1.000
projection_n1618 = sz ? na : z + ((n-n_z)step)-1.618
projection_n3141 = sz ? na : z + ((n-n_z)step)-3.141
plot(title='0.0', series=projection, style=linebr, color=blue)
plot(title='-0.236', series=projection_n0236, style=linebr, color=gray)
plot(title='-0.382', series=projection_n0382, style=linebr, color=orange)
plot(title='-0.500', series=projection_n0500, style=linebr, color=red)
plot(title='-0.618', series=projection_n0618, style=linebr, color=yellow)
plot(title='-1.000', series=projection_n1000, style=linebr, color=silver)
plot(title='-1.618', series=projection_n1618, style=linebr, color=lime)
plot(title='-3.141', series=projection_n3141, style=linebr, color=aqua)
 
Give this a try

Rich (BB code):
#hint: <b>Fibonacci Fan lines</b>
Fibonacci Fan lines are trend lines based on Fibonacci retracement points. Rising fan lines extend up from a trough and pass through retracement based on the advance (lowest low to highest high or on the falling fan lines: highest high to lowest low). These fan lines can then be used to estimate support levels or potential reversal zones. Falling fan lines can then be used to estimate resistance levels or potential reversal zones.

#hint Price: Price used in the alerts on crossing of the fans. <b>(Default is Close)</b>

#hint onExpansion: Determines if the fan lines are projected past the current bar into the right side expansion <b>(Default is Yes)</b>

#hint Coefficient1: Fan Line 1: Trough to 38.2% retracement on rising fans. 
Peak to 38.2% Coefficient on falling fans. <b>(Default is 38.2%)</b>

#hint Coefficient_2: Fan Line 2:
Trough to 50% retracement on rising fans. 
Peak to 50% Coefficient on falling fans. <b>(Default is 50%)</b>

#hint Coefficient_3: Fan Line 3:
Trough to 61.8% retracement on rising fans. 
Peak to 61.8% Coefficient on falling fans. <b>(Default is 61.8%)</b>

#wizard input: Price

#wizard text: Inputs: Price:

#wizard input: onExpansion

#wizard text: onExpansion:

#wizard input: Coefficient1 

#wizard text: Coefficient1 :

#wizard input: Coefficient_2 

#wizard text: Coefficient_2:

#wizard input: Coefficient_3  

#wizard text: Coefficient_3:

 

input price = close;

input high = high;

input low = low;

input onExpansion = Yes;

input Coefficient1 = .382;

input Coefficient_2 = .5;

input Coefficient_3 = .618;

def a = HighestAll(high);

def b = LowestAll(low);

def barnumber = barNumber();

def c = if high == a then barnumber else double.nan;

def d = if low == b then barnumber else double.nan;

rec highnumber = compoundValue(1, if IsNaN(c) then highnumber[1] else c, c);

def highnumberall = HighestAll(highnumber);

rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] else d, d);

def lownumberall = LowestAll(lownumber);

def upward = highnumberall > lownumberall;

def downward = highnumberall < lownumberall;

def low382 =  b + ((b - a) * Coefficient1);

def low5 =  b + ((b - a) * Coefficient_2);

def low618 = b + ((b - a) * Coefficient_3);

 

def high382 =  a - ((a - b) * Coefficient1);

def high5 =  a - ((a - b) * Coefficient_2);

def high618 = a - ((a - b) * Coefficient_3);

 

def x = AbsValue(lownumberall - highnumberall );

 

def slope = (a - b) / x;

def slope382 = (high382 - b) / x;

def slope5 = (high5 - b) / x;

def slope618 = (high618 - b) / x;

 

def slopelow = (b - a) / x;

def slopelow382 = (low382 - b) / x;

def slopelow5 = (low5 - b) / x;

def slopelow618 = (low618 - b) / x;

 

def day = getDay();

def month = getMonth();

def year = getYear();

def lastDay = getLastDay();

def lastmonth = getLastMonth();

def lastyear = getLastYear();

def isToday = if(day == lastDay and month == lastmonth and year == lastyear, 1, 0);

def istodaybarnumber = HighestAll(if isToday then barnumber else double.nan);

 

def line = b + (slope * (barnumber - lownumber));

def line382 = b + (slope382 * (barnumber - lownumber));

def line5 = b + (slope5 * (barnumber - lownumber));

def line618 = b + (slope618 * (barnumber - lownumber));

 

def linelow = a + (slopelow * (barnumber - highnumber));

def line382low = a + (slopelow382 * (barnumber - highnumber));

def line5low = a + (slopelow5 * (barnumber - highnumber));

def line618low = a + (slopelow618 * (barnumber - highnumber));

 

def currentlinelow = if barnumber <= istodaybarnumber then linelow else double.nan;

def currentline382low = if barnumber <= istodaybarnumber then line382low else double.nan;

def currentline5low = if barnumber <= istodaybarnumber then line5low else double.nan;

def currentline618low = if barnumber <= istodaybarnumber then line618low else double.nan;

 

def currentline = if barnumber <= istodaybarnumber then line else double.nan;

def currentline382 = if barnumber <= istodaybarnumber then line382 else double.nan;

def currentline5 = if barnumber <= istodaybarnumber then line5 else double.nan;

def currentline618 = if barnumber <= istodaybarnumber then line618 else double.nan;

 

Plot FibFan =  if  downward and onExpansion then linelow else if downward then currentlinelow else if upward and onExpansion then line else if upward then currentline else double.nan;

FibFan.SetStyle(Curve.SHORT_DASH);

FibFan.AssignValueColor(color.red);

 

Plot "Coefficient 1" =  if (downward and onExpansion) then line382low else if downward then currentline382low else if upward and onExpansion then line382 else if upward then currentline382 else double.nan;

"Coefficient 1".SetStyle(Curve.Firm);

"Coefficient 1".AssignValueColor(color.red);

Plot "Coefficient 2" = if downward and onExpansion then line5low else if downward then currentline5low else if upward and onExpansion then line5 else if upward then currentline5 else double.nan;

"Coefficient 2".AssignValueColor(color.red);

"Coefficient 2".SetStyle(Curve.Firm);

Plot "Coefficient 3" =  if downward and onExpansion then line618low else if downward then currentline618low else if upward and onExpansion then line618 else if upward then currentline618  else double.nan;

"Coefficient 3".AssignValueColor(color.red);

"Coefficient 3".SetStyle(Curve.Firm);

alert((price crosses below "Coefficient 1") , "Price crosses below Coefficient 1");

alert((price crosses below "Coefficient 2") , "Price crosses below Coefficient 2");

alert((price crosses below "Coefficient 3") , "Price crosses below Coefficient 3");

alert((price crosses above "Coefficient 1") , "Price crosses above Coefficient 1");

alert((price crosses above "Coefficient 2") , "Price crosses above Coefficient 2");

alert((price crosses above "Coefficient 3") , "Price crosses above Coefficient 3");

End
Hello. When i tried to add this Fib Fan it had invalid statements that I dont know how to solve. Would someone take a look at it? I am looking for a fib fan that I can attach to the Peak i want. Thanks
 
Give this a try

Rich (BB code):
#hint: <b>Fibonacci Fan lines</b>
Fibonacci Fan lines are trend lines based on Fibonacci retracement points. Rising fan lines extend up from a trough and pass through retracement based on the advance (lowest low to highest high or on the falling fan lines: highest high to lowest low). These fan lines can then be used to estimate support levels or potential reversal zones. Falling fan lines can then be used to estimate resistance levels or potential reversal zones.

#hint Price: Price used in the alerts on crossing of the fans. <b>(Default is Close)</b>

#hint onExpansion: Determines if the fan lines are projected past the current bar into the right side expansion <b>(Default is Yes)</b>

#hint Coefficient1: Fan Line 1: Trough to 38.2% retracement on rising fans. 
Peak to 38.2% Coefficient on falling fans. <b>(Default is 38.2%)</b>

#hint Coefficient_2: Fan Line 2:
Trough to 50% retracement on rising fans. 
Peak to 50% Coefficient on falling fans. <b>(Default is 50%)</b>

#hint Coefficient_3: Fan Line 3:
Trough to 61.8% retracement on rising fans. 
Peak to 61.8% Coefficient on falling fans. <b>(Default is 61.8%)</b>

#wizard input: Price

#wizard text: Inputs: Price:

#wizard input: onExpansion

#wizard text: onExpansion:

#wizard input: Coefficient1 

#wizard text: Coefficient1 :

#wizard input: Coefficient_2 

#wizard text: Coefficient_2:

#wizard input: Coefficient_3  

#wizard text: Coefficient_3:

 

input price = close;

input high = high;

input low = low;

input onExpansion = Yes;

input Coefficient1 = .382;

input Coefficient_2 = .5;

input Coefficient_3 = .618;

def a = HighestAll(high);

def b = LowestAll(low);

def barnumber = barNumber();

def c = if high == a then barnumber else double.nan;

def d = if low == b then barnumber else double.nan;

rec highnumber = compoundValue(1, if IsNaN(c) then highnumber[1] else c, c);

def highnumberall = HighestAll(highnumber);

rec lownumber = compoundValue(1, if IsNaN(d) then lownumber[1] else d, d);

def lownumberall = LowestAll(lownumber);

def upward = highnumberall > lownumberall;

def downward = highnumberall < lownumberall;

def low382 =  b + ((b - a) * Coefficient1);

def low5 =  b + ((b - a) * Coefficient_2);

def low618 = b + ((b - a) * Coefficient_3);

 

def high382 =  a - ((a - b) * Coefficient1);

def high5 =  a - ((a - b) * Coefficient_2);

def high618 = a - ((a - b) * Coefficient_3);

 

def x = AbsValue(lownumberall - highnumberall );

 

def slope = (a - b) / x;

def slope382 = (high382 - b) / x;

def slope5 = (high5 - b) / x;

def slope618 = (high618 - b) / x;

 

def slopelow = (b - a) / x;

def slopelow382 = (low382 - b) / x;

def slopelow5 = (low5 - b) / x;

def slopelow618 = (low618 - b) / x;

 

def day = getDay();

def month = getMonth();

def year = getYear();

def lastDay = getLastDay();

def lastmonth = getLastMonth();

def lastyear = getLastYear();

def isToday = if(day == lastDay and month == lastmonth and year == lastyear, 1, 0);

def istodaybarnumber = HighestAll(if isToday then barnumber else double.nan);

 

def line = b + (slope * (barnumber - lownumber));

def line382 = b + (slope382 * (barnumber - lownumber));

def line5 = b + (slope5 * (barnumber - lownumber));

def line618 = b + (slope618 * (barnumber - lownumber));

 

def linelow = a + (slopelow * (barnumber - highnumber));

def line382low = a + (slopelow382 * (barnumber - highnumber));

def line5low = a + (slopelow5 * (barnumber - highnumber));

def line618low = a + (slopelow618 * (barnumber - highnumber));

 

def currentlinelow = if barnumber <= istodaybarnumber then linelow else double.nan;

def currentline382low = if barnumber <= istodaybarnumber then line382low else double.nan;

def currentline5low = if barnumber <= istodaybarnumber then line5low else double.nan;

def currentline618low = if barnumber <= istodaybarnumber then line618low else double.nan;

 

def currentline = if barnumber <= istodaybarnumber then line else double.nan;

def currentline382 = if barnumber <= istodaybarnumber then line382 else double.nan;

def currentline5 = if barnumber <= istodaybarnumber then line5 else double.nan;

def currentline618 = if barnumber <= istodaybarnumber then line618 else double.nan;

 

Plot FibFan =  if  downward and onExpansion then linelow else if downward then currentlinelow else if upward and onExpansion then line else if upward then currentline else double.nan;

FibFan.SetStyle(Curve.SHORT_DASH);

FibFan.AssignValueColor(color.red);

 

Plot "Coefficient 1" =  if (downward and onExpansion) then line382low else if downward then currentline382low else if upward and onExpansion then line382 else if upward then currentline382 else double.nan;

"Coefficient 1".SetStyle(Curve.Firm);

"Coefficient 1".AssignValueColor(color.red);

Plot "Coefficient 2" = if downward and onExpansion then line5low else if downward then currentline5low else if upward and onExpansion then line5 else if upward then currentline5 else double.nan;

"Coefficient 2".AssignValueColor(color.red);

"Coefficient 2".SetStyle(Curve.Firm);

Plot "Coefficient 3" =  if downward and onExpansion then line618low else if downward then currentline618low else if upward and onExpansion then line618 else if upward then currentline618  else double.nan;

"Coefficient 3".AssignValueColor(color.red);

"Coefficient 3".SetStyle(Curve.Firm);

alert((price crosses below "Coefficient 1") , "Price crosses below Coefficient 1");

alert((price crosses below "Coefficient 2") , "Price crosses below Coefficient 2");

alert((price crosses below "Coefficient 3") , "Price crosses below Coefficient 3");

alert((price crosses above "Coefficient 1") , "Price crosses above Coefficient 1");

alert((price crosses above "Coefficient 2") , "Price crosses above Coefficient 2");

alert((price crosses above "Coefficient 3") , "Price crosses above Coefficient 3");

End
Hey Ben, Can you please create a shareable link for this study: The Fibonacci Fan Lines. I keep receiving an error message showing two invalid statements; see attached picture...Thanks
 

Attachments

  • AutoFinErrorMessageScreen Shot 2023-12-03 at 5.36.31 PM.png
    AutoFinErrorMessageScreen Shot 2023-12-03 at 5.36.31 PM.png
    91.1 KB · Views: 87
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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