0% found this document useful (0 votes)
11 views

gann fib lines

This document is a Pine Script code for a TradingView indicator that calculates and displays Gann Fibonacci levels on a price chart. It allows users to customize various parameters such as time frame, method of price calculation, number of levels, and whether to show labels or mid-levels. The script dynamically updates the levels and labels based on price movements and user inputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

gann fib lines

This document is a Pine Script code for a TradingView indicator that calculates and displays Gann Fibonacci levels on a price chart. It allows users to customize various parameters such as time frame, method of price calculation, number of levels, and whether to show labels or mid-levels. The script dynamically updates the levels and labels based on price movements and user inputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

//@version=5

indicator("Gann Fib Levels)", overlay=true, max_lines_count=500)

// === Inputs ===


gftf = input.timeframe("D", title="Gann Fib TF", group="Gann Fib")
met = input.string("C-Open", options=["C-Open", "P-Close", "Price @"],
title="Method")
pr = input.float(1, title="@ Price", minval=0.0001)
wi = input(1, title="Line Width")
levels = input.int(3, title="Number of Levels Above/Below", minval=1, maxval=12)
smid = input(true, title="Show Mids")
slab = input(false, title = "Show Labels")
pslab = input(false, title = "Show Previous Labels")

// === HTF session source ===


[op, cl1, tim] = request.security(syminfo.tickerid, gftf, [open, close[1], time],
barmerge.gaps_off, barmerge.lookahead_on)
dec = met == "P-Close" ? cl1 : met == "C-Open" ? op : pr
newbar = ta.change(tim)
tfLabel = "[" + gftf + "]"

// === Base Gann Levels ===


ab = math.pow(math.sqrt(dec) + 0.7389, 2)
be = math.pow(math.sqrt(dec) - 0.7389, 2)
ab_gap = ab - dec
be_gap = dec - be
uma = ab_gap * 0.25
umb = ab_gap * 0.75
lma = be_gap *0.25
lmb = be_gap *0.75
// === Historical levels ===
var float decp = na
var int prev_time = na

if newbar
if not na(prev_time)
line.new(prev_time, decp, tim, decp, xloc.bar_time, color=#030713,
width=wi)
if pslab
label.new(prev_time, decp, text="Pivot :" + str.tostring(decp,
"#.####") + " " + tfLabel,style=label.style_label_right, color =color.white,
textcolor=#02091c, size=size.small, xloc=xloc.bar_time)
for i = 1 to levels
upper = decp + ab_gap * i
lower = decp - be_gap * i
ul1 = decp + uma
ul2 = decp + umb
ll1 = decp - lma
ll2 = decp - lmb
line.new(prev_time, upper, tim, upper, xloc.bar_time, color=#cf7d0a,
width=wi, style=line.style_solid)
line.new(prev_time, lower, tim, lower, xloc.bar_time, color=#cf7d0a,
width=wi, style=line.style_solid)
if pslab
label.new(prev_time, upper, text="R" + str.tostring(i) + ":" +
str.tostring(upper, "#.####") + " " + tfLabel, style=label.style_label_right,color
=color.white, textcolor=#02091c, size=size.small, xloc=xloc.bar_time)
label.new(prev_time, lower, text="S" + str.tostring(i) + ":" +
str.tostring(lower, "#.####") + " " + tfLabel, style=label.style_label_right,color
=color.white, textcolor=#02091c, size=size.small, xloc=xloc.bar_time)

if i == 1
up1 = decp + ab_gap / 2
dw1 = decp - be_gap / 2
line.new(prev_time, up1 , tim, up1, xloc.bar_time, color=#10b36f,
width=wi)
line.new(prev_time, dw1 , tim, dw1, xloc.bar_time, color=#10b36f,
width=wi)
if smid
line.new(prev_time, ul1 , tim, ul1, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
line.new(prev_time, ul2 , tim, ul2, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
line.new(prev_time, ll1 , tim, ll1, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
line.new(prev_time, ll2 , tim, ll2, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
if pslab
label.new(prev_time, up1, text=str.tostring(up1, "#.####"),
style=label.style_label_right, color =color.white, textcolor=#02091c,
size=size.small, xloc=xloc.bar_time)
label.new(prev_time, dw1, text=str.tostring(dw1, "#.####"),
style=label.style_label_right, color =color.white, textcolor=#02091c,
size=size.small, xloc=xloc.bar_time)
else
mid_upper = decp + ab_gap * (i - 0.5)
mid_lower = decp - be_gap * (i - 0.5)
ul12 = decp + ab_gap * (i - 0.75)
ul22 = decp + ab_gap * (i - 0.25)
ll12 = decp - be_gap * (i - 0.75)
ll22 = decp - be_gap * (i - 0.25)
line.new(prev_time, mid_upper, tim, mid_upper, xloc.bar_time,
color=#10b36f, width=wi)
line.new(prev_time, mid_lower, tim, mid_lower, xloc.bar_time,
color=#10b36f, width=wi)
if smid
line.new(prev_time, ul12 , tim, ul12, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
line.new(prev_time, ul22 , tim, ul22, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
line.new(prev_time, ll12 , tim, ll12, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
line.new(prev_time, ll22 , tim, ll22, xloc.bar_time,
color=#2114d7, width=wi, style = line.style_dotted)
if pslab
label.new(prev_time, mid_upper, text=str.tostring(mid_upper,
"#.####"), style=label.style_label_right, color =color.white, textcolor=#02091c,
size=size.small, xloc=xloc.bar_time)
label.new(prev_time, mid_lower, text=str.tostring(mid_lower,
"#.####"), style=label.style_label_right, color =color.white, textcolor=#02091c,
size=size.small, xloc=xloc.bar_time)
decp := dec
prev_time := tim

////=====Current Session ===//////////


// === Live Level Drawing
updateLine(line l, float y, color col, _style) =>
if na(l)
line.new(tim, y, time, y, xloc.bar_time, color=col, width=wi, style =
_style)
else
line.set_xy1(l, tim, y)
line.set_xy2(l, time, y)
l

draw_label(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size,


_textalign) =>
dlabel = label.new(x=_x, y=_y, text=_text, xloc=_xloc, yloc=_yloc,
color=_color, style=_style, textcolor=_textcolor, size=_size,
textalign=_textalign)
label.delete(dlabel[1])

// === Setup ===


var line decli = na
var label declab = na
decli := updateLine(decli, dec, #030713, line.style_solid)
if slab
draw_label(bar_index + 10, dec, met + "(" + gftf + "): " + str.tostring(dec,
' : 0.00'), xloc.bar_index, yloc.price, color.white, label.style_text_outline,
#04011a, size.normal, text.align_right)

var line abli = na


var line beli = na
abli := updateLine(abli, ab, #cf7d0a, line.style_solid)
beli := updateLine(beli, be, #cf7d0a, line.style_solid)
if slab
draw_label(bar_index + 10, ab, "R1(" + gftf + "): " + str.tostring(ab, ' :
0.00'), xloc.bar_index, yloc.price, color.white, label.style_text_outline, #04011a,
size.normal, text.align_right)
draw_label(bar_index + 10, be, "S1(" + gftf + "): " + str.tostring(be, ' :
0.00'), xloc.bar_index, yloc.price, color.white, label.style_text_outline, #04011a,
size.normal, text.align_right)

// === Line Arrays ===


var line[] abLines = array.new_line()
var line[] beLines = array.new_line()
var line[] abMidLines = array.new_line()
var line[] beMidLines = array.new_line()
var line[] q1abLines = array.new_line()
var line[] q2abLines = array.new_line()
var line[] q3beLines = array.new_line()
var line[] q4beLines = array.new_line()

// === Label Arrays ===


var label[] abLabels = array.new_label()
var label[] beLabels = array.new_label()
var label[] abMidLabels = array.new_label()
var label[] beMidLabels = array.new_label()
var label[] qu1Labels = array.new_label()
var label[] qu2Labels = array.new_label()
var label[] qu3Labels = array.new_label()
var label[] qu4Labels = array.new_label()

// === Ensure array sizes match levels ===


while array.size(abLines) < levels
array.push(abLines, na)
while array.size(beLines) < levels
array.push(beLines, na)
while array.size(abMidLines) < levels
array.push(abMidLines, na)
while array.size(beMidLines) < levels
array.push(beMidLines, na)
while array.size(q1abLines) < levels
array.push(q1abLines, na)
while array.size(q2abLines) < levels
array.push(q2abLines, na)
while array.size(q3beLines) < levels
array.push(q3beLines, na)
while array.size(q4beLines) < levels
array.push(q4beLines, na)
while array.size(abLabels) < levels
array.push(abLabels, na)
while array.size(beLabels) < levels
array.push(beLabels, na)
while array.size(abMidLabels) < levels
array.push(abMidLabels, na)
while array.size(beMidLabels) < levels
array.push(beMidLabels, na)
while array.size(qu1Labels) < levels
array.push(qu1Labels, na)
while array.size(qu2Labels) < levels
array.push(qu2Labels, na)
while array.size(qu3Labels) < levels
array.push(qu3Labels, na)
while array.size(qu4Labels) < levels
array.push(qu4Labels, na)

// === Level Drawing Loop ===


for i = 1 to levels
ab_y = dec + ab_gap * i
be_y = dec - be_gap * i

// Update main R/S lines


ab_line = updateLine(array.get(abLines, i - 1), ab_y, #cf7d0a,
line.style_solid)
be_line = updateLine(array.get(beLines, i - 1), be_y, #cf7d0a,
line.style_solid)
array.set(abLines, i - 1, ab_line)
array.set(beLines, i - 1, be_line)

// Delete old labels


old_ab_label = array.get(abLabels, i - 1)
old_be_label = array.get(beLabels, i - 1)
if not na(old_ab_label)
label.delete(old_ab_label)
if not na(old_be_label)
label.delete(old_be_label)

if slab
new_ab_label = label.new(x=bar_index + 10, y=ab_y, text="R" +
str.tostring(i) + "(" + gftf + "): " + str.tostring(ab_y, ' : 0.00'),
xloc=xloc.bar_index, yloc=yloc.price, color=color.white,
style=label.style_text_outline, textcolor=#04011a, size=size.normal,
textalign=text.align_right)
new_be_label = label.new(x=bar_index + 10, y=be_y, text="S" +
str.tostring(i) + "(" + gftf + "): " + str.tostring(be_y, ' : 0.00'),
xloc=xloc.bar_index, yloc=yloc.price, color=color.white,
style=label.style_text_outline, textcolor=#04011a, size=size.normal,
textalign=text.align_right)
array.set(abLabels, i - 1, new_ab_label)
array.set(beLabels, i - 1, new_be_label)

// Mid levels
mid_ab_y = dec + ab_gap * (i - 0.5)
qur1 = dec + ab_gap * (i - 0.25)
qur2 = dec + ab_gap * (i - 0.75)
mid_be_y = dec - be_gap * (i - 0.5)
qur3 = dec - be_gap * (i - 0.25)
qur4 = dec - be_gap * (i - 0.75)

abm_line = updateLine(array.get(abMidLines, i - 1), mid_ab_y, #10b36f,


line.style_solid)
bem_line = updateLine(array.get(beMidLines, i - 1), mid_be_y, #10b36f,
line.style_solid)
array.set(abMidLines, i - 1, abm_line)
array.set(beMidLines, i - 1, bem_line)

if smid
q1_line = updateLine(array.get(q1abLines, i - 1), qur1, #2114d7,
line.style_dotted)
array.set(q1abLines, i - 1, q1_line)
q2_line = updateLine(array.get(q2abLines, i - 1), qur2, #2114d7,
line.style_dotted)
array.set(q2abLines, i - 1, q2_line)
q3_line = updateLine(array.get(q3beLines, i - 1), qur3, #2114d7,
line.style_dotted)
array.set(q3beLines, i - 1, q3_line)
q4_line = updateLine(array.get(q4beLines, i - 1), qur4, #2114d7,
line.style_dotted)
array.set(q4beLines, i - 1, q4_line)
// Delete old mid-level labels
old_abm_label = array.get(abMidLabels, i - 1)
old_bem_label = array.get(beMidLabels, i - 1)
old_qu1_label = array.get(qu1Labels, i - 1)
old_qu2_label = array.get(qu2Labels, i - 1)
old_qu3_label = array.get(qu3Labels, i - 1)
old_qu4_label = array.get(qu4Labels, i - 1)

if not na(old_abm_label)
label.delete(old_abm_label)
if not na(old_bem_label)
label.delete(old_bem_label)
if not na(old_qu1_label)
label.delete(old_qu1_label)
if not na(old_qu2_label)
label.delete(old_qu2_label)
if not na(old_qu3_label)
label.delete(old_qu3_label)
if not na(old_qu4_label)
label.delete(old_qu4_label)

// Create new mid-level value-only labels


if slab
new_abm_label = label.new(x=bar_index + 10, y=mid_ab_y,
text=str.tostring(mid_ab_y, 'Mid : 0.00'), xloc=xloc.bar_index, yloc=yloc.price,
color=color.white, style=label.style_label_right, textcolor=#04011a,
size=size.small, textalign=text.align_right)
new_bem_label = label.new(x=bar_index + 10, y=mid_be_y,
text=str.tostring(mid_be_y, 'Mid : 0.00'), xloc=xloc.bar_index, yloc=yloc.price,
color=color.white, style=label.style_label_right, textcolor=#04011a,
size=size.small, textalign=text.align_right)
array.set(abMidLabels, i - 1, new_abm_label)
array.set(beMidLabels, i - 1, new_bem_label)
if smid
new_qu1_label = label.new(x=bar_index + 10, y=qur1,
text=str.tostring(qur1, '0.00'), xloc=xloc.bar_index, yloc=yloc.price,
color=color.white, style=label.style_label_right, textcolor=#04011a,
size=size.small, textalign=text.align_right)
new_qu2_label = label.new(x=bar_index + 10, y=qur2,
text=str.tostring(qur2, '0.00'), xloc=xloc.bar_index, yloc=yloc.price,
color=color.white, style=label.style_label_right, textcolor=#04011a,
size=size.small, textalign=text.align_right)
new_qu3_label = label.new(x=bar_index + 10, y=qur3,
text=str.tostring(qur3, '0.00'), xloc=xloc.bar_index, yloc=yloc.price,
color=color.white, style=label.style_label_right, textcolor=#04011a,
size=size.small, textalign=text.align_right)
new_qu4_label = label.new(x=bar_index + 10, y=qur4,
text=str.tostring(qur4, '0.00'), xloc=xloc.bar_index, yloc=yloc.price,
color=color.white, style=label.style_label_right, textcolor=#04011a,
size=size.small, textalign=text.align_right)

array.set(qu1Labels, i - 1, new_qu1_label)
array.set(qu2Labels, i - 1, new_qu2_label)
array.set(qu3Labels, i - 1, new_qu3_label)
array.set(qu4Labels, i - 1, new_qu4_label)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy