# Gann Angles Indicator for ThinkorSwim

#### Picard

##### Member
Here's code for Gann Angles originally coded by Mobius and modified for highs and lows by someone else. I posted this for those who already know how to use Gann's Angles. There are courses in Udemy.com, if you don't know already. Also look here: The Best Gann Fan Trading Strategy Code:
``````# Gann Angles by Mobius
# Modified
# V02.03.03.2019
#hint: The primary Gann angles: 1X2, 1X1 and 2X1.  1X1 is moving one unit of price with one unit of time. Additional angles 1X8, 1X4, 4X1 and 8X1.
def Coefficient_1 = .5;
def Coefficient_2 = 1;
def Coefficient_3 = 2;
def Coefficient_4 = .125;
def Coefficient_5 = .25;
def Coefficient_6 = 4;
def bar = BarNumber();

input numMonths = 1;
input lineWeight = 3;

def numBars     = 21 * numMonths;
def barNum       = if IsNaN( close ) then Double.NaN else BarNumber();
def endBar      = HighestAll( barNum );
def startBar    = if endBar <= numBars then 1 else endBar - numBars;

def hData       = If( barNum < startBar, Double.NaN, high );
def lData       = If( barNum < startBar, Double.NaN, low );

def peak = HighestAll(hData);

def Apex = HighestAll(hData);
def Apex_Bar = if high == Apex
then bar
else Double.NaN;

then bar
else Double.NaN;

def FirstValue = if HighestAll(Apex_Bar) == FirstBar
then Apex
else FirstValue;

def LastValue = if HighestAll(Apex_Bar) == LastBar
then Apex
else LastValue;

# Gann Line Algorithm
def x = AbsValue(HighestAll(Apex_Bar) - HighestAll(Nadir_Bar));
def slope_1 = (AbsValue(Apex - Nadir) * Coefficient_1) / x;
def slope_2 = (AbsValue(Apex - Nadir) * Coefficient_2) / x;
def slope_3 = (AbsValue(Apex - Nadir) * Coefficient_3) / x;
def slope_4 = (AbsValue(Apex - Nadir) * Coefficient_4) / x;
def slope_5 = (AbsValue(Apex - Nadir) * Coefficient_5) / x;
def slope_6 = (AbsValue(Apex - Nadir) * Coefficient_6) / x;

plot G1 = if bar >= FirstBar and HighestAll(Apex_Bar) == FirstBar
then FirstValue - (slope_1 * (bar - FirstBar))
else if bar >= FirstBar and HighestAll(Nadir_Bar) == FirstBar
then FirstValue + (slope_1 * (bar - FirstBar))
else Double.NaN;
plot G2 = if bar >= FirstBar and HighestAll(Apex_Bar) == FirstBar
then FirstValue - (slope_2 * (bar - FirstBar))
else if bar >= FirstBar and HighestAll(Nadir_Bar) == FirstBar
then FirstValue + (slope_2 * (bar - FirstBar))
else Double.NaN;
plot G3 = if bar >= FirstBar and HighestAll(Apex_Bar) == FirstBar
then FirstValue - (slope_3 * (bar - FirstBar))
else if bar >= FirstBar and HighestAll(Nadir_Bar) == FirstBar
then FirstValue + (slope_3 * (bar - FirstBar))
else Double.NaN;
plot G4 = if bar >= FirstBar and HighestAll(Apex_Bar) == FirstBar
then FirstValue - (slope_4 * (bar - FirstBar))
else if bar >= FirstBar and HighestAll(Nadir_Bar) == FirstBar
then FirstValue + (slope_4 * (bar - FirstBar))
else Double.NaN;
plot G5 = if bar >= FirstBar and HighestAll(Apex_Bar) == FirstBar
then FirstValue - (slope_5 * (bar - FirstBar))
else if bar >= FirstBar and HighestAll(Nadir_Bar) == FirstBar
then FirstValue + (slope_5 * (bar - FirstBar))
else Double.NaN;
plot G6 = if bar >= FirstBar and HighestAll(Apex_Bar) == FirstBar
then FirstValue - (slope_6 * (bar - FirstBar))
else if bar >= FirstBar and HighestAll(Nadir_Bar) == FirstBar
then FirstValue + (slope_6 * (bar - FirstBar))
else Double.NaN;
# End Code Gann Angles``````

The following is from the thinkScript Lounge:

I'm attempting to develop a custom quote column for the Gann Angle so that I can watch the rate of increase or decrease of prices in a gap as a quick reference to selecting a stock to gap trade. There's no errors in the code; however, I'd appreciate any feedback or ideas on this. I've updated my Gann Angle code so that all time units are in seconds. So hopefully this should be working correctly; however, I'm not sure about the units of the angles. Are they in radians or in decimal degrees?

Code:
``````script cq_gannangle {
# CQ_GannAngle

def StartOfDay = 0000;
def OpeningTime = 0930;

# Time Values
def CurrentTime = secondsFromTime(StartOfDay);

# Price Values
def CurrentPrice = hl2;
def OpeningPrice = open(period = "DAY"); # Today's Opening Price

plot GannAngle = (CurrentPrice - OpeningPrice)/(CurrentTime -StartOfTrading);
}
# CQ_GannAngleCOLUMN
def _GannAngle = CQ_GannAngle().GannAngle;
plot GannAngle = roundUp((Atan(_GannAngle) * 180 / Double.Pi),2);#This is in degrees``````

Last edited:

#### TonXas

##### New member
Any way to modify this to draw a -45 degree angle from the HOD and a 45 degree angle from the LOD?

#### cswu1211

##### Member
VIP
@Picard How to "explain" the GANN angles straight lines were distorted/bended with TSLA • germanburrito

#### cswu1211

VIP

##### New member
VIP
@cswu1211 Are you using a log price scale for your charts? Just a guess why the lines are slightly curved.

#### cswu1211

##### Member
VIP
@cswu1211 Are you using a log price scale for your charts? Just a guess why the lines are slightly curved.
NO, I use the regular ones. some other stocks show the straight Gann lines, except for this (TSLA) one. I just wondering why?! Might happen for other stocks or timeframe?!

#### ext99k

##### Member
How does this determine where the fan begins?

#### halcyonguy

##### Well-known member
VIP
How does this determine where the fan begins?
Ruby:
``````# fan starting point ?
#   find the highest high and lowest low.
#   then determine which one occurred first.

# highest price level of high
def Apex = HighestAll(hData);

# if current bar is the highest high, then set the barnumber
def Apex_Bar = if high == Apex
then bar
else Double.NaN;

# lowest price level of low

# if current bar is the lowest low, then set the barnumber
then bar
else Double.NaN;

# use highestall() to get the barnumbers for highest high and lowest low.
#   only 1 bar for each variable (Apex_Bar, Nadir_Bar) will have a number.
#   the other bars will be double.nan, so ignored by highestall()
# use min() to determine which one came first, which one has a lower barnumber

# set the price level of the firstbar
def FirstValue = if HighestAll(Apex_Bar) == FirstBar
then Apex
else FirstValue;``````

#### PennyFlipper101

##### New member
how do I set it so the fan is plotted on the monthly low or the monthly high

#### halcyonguy

##### Well-known member
VIP
how do I set it so the fan is plotted on the monthly low or the monthly high

how do I set it so the fan is plotted on the monthly low or the monthly high
i'm not sure, i don't have my mind in tune with this now,
but i made this to find the first of monthly high or monthly low. (the lowest barnumber)

Ruby:
``````# test3_month_hilo

# find the monthly high or low, with the lowest bar number.
#  draw a vertical line before it. draw a white triangle between the high/low lines

def na = double.nan;
def bn = barnumber();

# find the monthly low and high
input agg = AggregationPeriod.month;
def mo_hi = high(period = agg);
def mo_lo = low(period = agg);

input show_hilo_lines = yes;
input show_monthly_hilo_arrows = yes;

plot zhiline = if show_hilo_lines then mo_hi else na;
zhiline.setdefaultcolor(color.cyan);
plot zloline = if show_hilo_lines then mo_lo else na;
zloline.setdefaultcolor(color.cyan);

def mo_hibar = if (high == mo_hi) then 1 else 0;
def mo_lobar = if (low == mo_lo) then 1 else 0;

plot zhi_arrow = if (show_monthly_hilo_arrows and mo_hibar) then mo_hi*1.003 else na;
zhi_arrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi_arrow.SetDefaultColor(Color.red);
zhi_arrow.setlineweight(3);
zhi_arrow.hidebubble();

plot zlo_arrow = if (show_monthly_hilo_arrows and mo_lobar) then mo_lo*0.997 else na;
zlo_arrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo_arrow.SetDefaultColor(Color.green);
zlo_arrow.setlineweight(3);
zlo_arrow.hidebubble();

# find first monthly arrow , either the hi or low
def mo_hibn = lowestall(if mo_hibar then bn else na);
def mo_lobn = lowestall(if mo_lobar then bn else na);

#def mo_hibn = if bn == 1 then 0
# else if mo_hibn > 0 then mo_hibn
# else if mo_hibar then bn
# else mo_hibn;

#def mo_lobn = if bn == 1 then 0
# else if mo_lobn > 0 then mo_lobn
# else if  mo_lobar then bn
# else mo_lobn;

def y1 = (mo_hi + mo_lo)/2;

def startbn = if (mo_hibn == 0 or mo_lobn == 0) then na else if mo_hibn < mo_lobn then mo_hibn else mo_lobn;

# draw a vertical line before the hi or low with the lowest bar number
addverticalline( bn == startbn , "lowest hilo", color.yellow);

plot z3 = if bn == startbn then y1 else na;
z3.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
z3.SetDefaultColor(Color.white);
z3.setlineweight(4);
z3.hidebubble();

input test_bubbles = no;
bn + "\n" +
y1 + " y1\n" +
mo_hibn + " Hi\n" +
mo_lobn + " Lo\n" +
startbn + " lowest"
,color.yellow, no);
#``````

AXP 2 hour chart
monthly high / low lines
vertical yellow line at the first high or low.
white triangle drawn also at that time.
arrows drawn for each month, for high and low. #### IHopeToLearn

##### New member
Sort of what PennyFlipper is asking for.. I don't care about the wicks I want the lines to be on the close. Along with the difference in percentage and price between the two.
The attached screenshot shows the white lines representing where I prefer your lines to be. I don't want the Highs and Lows but the lowest close and highest close of a give period, also what would be great is a percentage and price difference between them.
Is that possible? If you can tell me what to replace I can try, but newbie

Last edited by a moderator:

#### majidg

##### Member
Sort of what PennyFlipper is asking for.. I don't care about the wicks I want the lines to be on the close. Along with the difference in percentage and price between the two.
The attached screenshot shows the white lines representing where I prefer your lines to be. I don't want the Highs and Lows but the lowest close and highest close of a give period, also what would be great is a percentage and price difference between them.
Is that possible? If you can tell me what to replace I can try, but newbie
Can you please provide the codes for the above chart? Thanks

#### IHopeToLearn

##### New member
Can you please provide the codes for the above chart? Thanks
Please understand that the white lines were hand drawn and that is my preference as opposed to lines hitting the wicks.
But here is the code, which is the same as halcyonguy posted, I just changed the colors:

#### ​

# test3_month_hilo

# find the monthly high or low, with the lowest bar number.
# draw a vertical line before it. draw a white triangle between the high/low lines

def na = double.nan;
def bn = barnumber();

# find the monthly low and high
input agg = AggregationPeriod.month;
def mo_hi = high(period = agg);
def mo_lo = low(period = agg);

input show_hilo_lines = yes;
input show_monthly_hilo_arrows = yes;

plot zhiline = if show_hilo_lines then mo_hi else na;
zhiline.setdefaultcolor(color.cyan);
plot zloline = if show_hilo_lines then mo_lo else na;
zloline.setdefaultcolor(color.cyan);

def mo_hibar = if (high == mo_hi) then 1 else 0;
def mo_lobar = if (low == mo_lo) then 1 else 0;

plot zhi_arrow = if (show_monthly_hilo_arrows and mo_hibar) then mo_hi*1.003 else na;
zhi_arrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi_arrow.SetDefaultColor(Color.red);
zhi_arrow.setlineweight(3);
zhi_arrow.hidebubble();

plot zlo_arrow = if (show_monthly_hilo_arrows and mo_lobar) then mo_lo*0.997 else na;
zlo_arrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo_arrow.SetDefaultColor(Color.green);
zlo_arrow.setlineweight(3);
zlo_arrow.hidebubble();

# find first monthly arrow , either the hi or low
def mo_hibn = lowestall(if mo_hibar then bn else na);
def mo_lobn = lowestall(if mo_lobar then bn else na);

#def mo_hibn = if bn == 1 then 0
# else if mo_hibn > 0 then mo_hibn
# else if mo_hibar then bn
# else mo_hibn;

#def mo_lobn = if bn == 1 then 0
# else if mo_lobn > 0 then mo_lobn
# else if mo_lobar then bn
# else mo_lobn;

def y1 = (mo_hi + mo_lo)/2;

def startbn = if (mo_hibn == 0 or mo_lobn == 0) then na else if mo_hibn < mo_lobn then mo_hibn else mo_lobn;

# draw a vertical line before the hi or low with the lowest bar number
addverticalline( bn == startbn , "lowest hilo", color.yellow);

plot z3 = if bn == startbn then y1 else na;
z3.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
z3.SetDefaultColor(Color.white);
z3.setlineweight(4);
z3.hidebubble();

input test_bubbles = no;
bn + "\n" +
y1 + " y1\n" +
mo_hibn + " Hi\n" +
mo_lobn + " Lo\n" +
startbn + " lowest"
,color.yellow, no);
#

#### majidg

##### Member
i'm not sure, i don't have my mind in tune with this now,
but i made this to find the first of monthly high or monthly low. (the lowest barnumber)

Ruby:
``````# test3_month_hilo

# find the monthly high or low, with the lowest bar number.
#  draw a vertical line before it. draw a white triangle between the high/low lines

def na = double.nan;
def bn = barnumber();

# find the monthly low and high
input agg = AggregationPeriod.month;
def mo_hi = high(period = agg);
def mo_lo = low(period = agg);

input show_hilo_lines = yes;
input show_monthly_hilo_arrows = yes;

plot zhiline = if show_hilo_lines then mo_hi else na;
zhiline.setdefaultcolor(color.cyan);
plot zloline = if show_hilo_lines then mo_lo else na;
zloline.setdefaultcolor(color.cyan);

def mo_hibar = if (high == mo_hi) then 1 else 0;
def mo_lobar = if (low == mo_lo) then 1 else 0;

plot zhi_arrow = if (show_monthly_hilo_arrows and mo_hibar) then mo_hi*1.003 else na;
zhi_arrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi_arrow.SetDefaultColor(Color.red);
zhi_arrow.setlineweight(3);
zhi_arrow.hidebubble();

plot zlo_arrow = if (show_monthly_hilo_arrows and mo_lobar) then mo_lo*0.997 else na;
zlo_arrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo_arrow.SetDefaultColor(Color.green);
zlo_arrow.setlineweight(3);
zlo_arrow.hidebubble();

# find first monthly arrow , either the hi or low
def mo_hibn = lowestall(if mo_hibar then bn else na);
def mo_lobn = lowestall(if mo_lobar then bn else na);

#def mo_hibn = if bn == 1 then 0
# else if mo_hibn > 0 then mo_hibn
# else if mo_hibar then bn
# else mo_hibn;

#def mo_lobn = if bn == 1 then 0
# else if mo_lobn > 0 then mo_lobn
# else if  mo_lobar then bn
# else mo_lobn;

def y1 = (mo_hi + mo_lo)/2;

def startbn = if (mo_hibn == 0 or mo_lobn == 0) then na else if mo_hibn < mo_lobn then mo_hibn else mo_lobn;

# draw a vertical line before the hi or low with the lowest bar number
addverticalline( bn == startbn , "lowest hilo", color.yellow);

plot z3 = if bn == startbn then y1 else na;
z3.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
z3.SetDefaultColor(Color.white);
z3.setlineweight(4);
z3.hidebubble();

input test_bubbles = no;
bn + "\n" +
y1 + " y1\n" +
mo_hibn + " Hi\n" +
mo_lobn + " Lo\n" +
startbn + " lowest"
,color.yellow, no);
#``````

AXP 2 hour chart
monthly high / low lines
vertical yellow line at the first high or low.
white triangle drawn also at that time.
arrows drawn for each month, for high and low. #### MerryDay

Staff member
Staff
VIP
Code:
``````zhi_arrow is true
#or
zlo_arrow is true``````

#### khpro59

##### Member
VIP
how do I set it so the fan is plotted on the monthly low or the monthly high
Did you ever figure this out? Im also trying to get this but maybe 4 hour instead. I usually plot a Gann Fan on the 4 hour, trace over them with trend lines so that it shows up on my intraday charts. Its very effective, but sometimes im rushing to do it (Daughter loves to grab my attention pre market). If anyone could help that would be awesome.

In short : I am looking for a Gann Fan Angle study that I can choose my aggregation period for. I.e. id like to have Gann Angle values from the 4 hour chart, show up on my 2-15 min chart, is that possible?

### Not the exact question you're looking for?

87k+ Posts
303 Online ## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?