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

Gann-Angles-Indicator.png

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;

def Nadir = LowestAll(lData);
def Nadir_Bar = if low == Nadir
               then bar
               else Double.NaN;

def FirstBar = Min(HighestAll(Apex_Bar), HighestAll(Nadir_Bar));
def FirstValue = if HighestAll(Apex_Bar) == FirstBar
                 then Apex
                 else if HighestAll(Nadir_Bar) == FirstBar
                      then Nadir
                      else FirstValue[1];


def LastBar = Max(HighestAll(Apex_Bar), HighestAll(Nadir_Bar));
def LastValue = if HighestAll(Apex_Bar) == LastBar
                then Apex
                else if HighestAll(Nadir_Bar) == LastBar
                     then Nadir
                else LastValue[1];

# 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);
def StartOfTrading = secondsFromTime(OpeningTime);

# 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:
Any way to modify this to draw a -45 degree angle from the HOD and a 45 degree angle from the LOD?
 
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
def Nadir = LowestAll(lData);

# if current bar is the lowest low, then set the barnumber
def Nadir_Bar = if low == Nadir
               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
def FirstBar = Min(HighestAll(Apex_Bar), HighestAll(Nadir_Bar));

# set the price level of the firstbar
def FirstValue = if HighestAll(Apex_Bar) == FirstBar
                 then Apex
                 else if HighestAll(Nadir_Bar) == FirstBar
                      then Nadir
                      else FirstValue[1];
 
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)
maybe it can help you or someone to put them together.

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[1] > 0 then mo_hibn[1]
# else if mo_hibar then bn
# else mo_hibn[1];

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

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;
addchartbubble(test_bubbles, low, 
 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.
NBGw6Su.jpg
 
Sort of what PennyFlipper is asking for..
Find-Highest-Close-Lowest-Close.png

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:
Sort of what PennyFlipper is asking for..
Find-Highest-Close-Lowest-Close.png

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
 
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[1] > 0 then mo_hibn[1]
# else if mo_hibar then bn
# else mo_hibn[1];

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

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;
addchartbubble(test_bubbles, low,
bn + "\n" +
y1 + " y1\n" +
mo_hibn + " Hi\n" +
mo_lobn + " Lo\n" +
startbn + " lowest"
,color.yellow, no);
#
 
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)
maybe it can help you or someone to put them together.

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[1] > 0 then mo_hibn[1]
# else if mo_hibar then bn
# else mo_hibn[1];

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

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;
addchartbubble(test_bubbles, low,
 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.
NBGw6Su.jpg
Need the codes for scanning the pivot high or pivot low. Can someone please help?
 
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?
 
Is there a way to control the focus point of the existing code? After putting up this indicator, the extended line messed the zoom. Hope someone can help to address it! Thank you.
 
Last edited by a moderator:

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
294 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