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
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:
# 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: