0% found this document useful (0 votes)
156 views5 pages

Gann SR

This document contains code for calculating Gann support and resistance levels on multiple timeframes. It retrieves historical price data, calculates the daily volatility and pivot point, then uses these values along with Gann angle mathematics to determine buy/sell zones and price targets for the first 8 cycles. The calculated support and resistance levels are conditionally plotted based on timeframe, with options to offset and set the line width of the plotted Gann lines.

Uploaded by

Bipin
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)
156 views5 pages

Gann SR

This document contains code for calculating Gann support and resistance levels on multiple timeframes. It retrieves historical price data, calculates the daily volatility and pivot point, then uses these values along with Gann angle mathematics to determine buy/sell zones and price targets for the first 8 cycles. The calculated support and resistance levels are conditionally plotted based on timeframe, with options to offset and set the line width of the plotted Gann lines.

Uploaded by

Bipin
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/ 5

//////////////////////////////////////

//// gann daily support resistance


/////////////////////////////////////
var totLn = 0.0
var totLnSqr = 0.0
var lnAvg = 0.0
var lnSqrAvg = 0.0
var variance = 0.0
var dailyVolatility = 0.0
var price = 0.0
var priceRange = 0.0

[o,ph1,pl1,pc] = request.security(syminfo.tickerid, 'D',


[open,high[1],low[1],close[1]], lookahead=barmerge.lookahead_on)
//ph1 = request.security(syminfo.tickerid, 'D', high[1],
lookahead=barmerge.lookahead_on)
//pl1 = request.security(syminfo.tickerid, 'D', low[1],
lookahead=barmerge.lookahead_on)
//pc = request.security(syminfo.tickerid, 'D', close[1],
lookahead=barmerge.lookahead_on)

//lowerTimeframe = timeframe.period == '1' or timeframe.period == '2' or


timeframe.period == '30' or timeframe.period == '5' or timeframe.period == '10' or
timeframe.period == '15'
lowerTimeframe = input.bool(true, title="Plot Gann s/r ?")

c1 = o > ph1 or o < pl1 ? o : request.security(syminfo.tickerid, 'D', close[1],


lookahead=barmerge.lookahead_on)
[c2, c3, c4, c5, c6, c7, c8, c9, c10] = request.security(syminfo.tickerid, 'D',
[close[2], close[3], close[4], close[5], close[6], close[7], close[8], close[9],
close[10]], lookahead=barmerge.lookahead_on)

truncate(number, decimals) =>


factor = math.pow(10, decimals)
int(number * factor) / factor

calculateLN() =>
tot = 0.0
tot := math.log(c1 / c2) + math.log(c2 / c3) + math.log(c3 / c4) +
math.log(c4 / c5) + math.log(c5 / c6) + math.log(c6 / c7) + math.log(c7 / c8) +
math.log(c8 / c9) + math.log(c9 / c10)
tot

calculateLNSquare() =>
tot = 0.0
tot := math.pow(math.log(c1 / c2), 2) + math.pow(math.log(c2 / c3), 2) +
math.pow(math.log(c3 / c4), 2) + math.pow(math.log(c4 / c5), 2) +
math.pow(math.log(c5 / c6), 2) + math.pow(math.log(c6 / c7), 2) +
math.pow(math.log(c7 / c8), 2) + math.pow(math.log(c8 / c9), 2) +
math.pow(math.log(c9 / c10), 2)
tot

calculatePivot() =>
(ph1 + pl1 + pc) / 3

degreeFactor(angle) =>
if angle == 1
3.75 / 180
else
if angle == 2
7.5 / 180
else
if angle == 3
15.00 / 180
else
if angle == 4
18.75 / 180
else
if angle == 5
26.25 / 180
else
if angle == 6
45.00 / 180
else
if angle == 7
63.75 / 180
else
if angle == 8
71.25 / 180
else
if angle == 9
75.00 / 180
else
if angle == 10
82.50 / 180
else
86.25 / 180

fac1 = input(0,title="degree factor 1")


fac2 = input(6,title="degree factor 2")
getCycleLevels(cycleNo, prevCycleHigh, prevCycleLow, pRange) =>
if cycleNo == 1
buyAbove = math.pow(math.sqrt(pRange) + degreeFactor(3), 2) *
degreeFactor(3) + prevCycleHigh
majorResistance = math.pow(math.sqrt(pRange) + degreeFactor(fac1), 2) *
degreeFactor(fac1) + prevCycleHigh
buyTarget = math.pow(math.sqrt(pRange) + degreeFactor(fac2), 2) *
degreeFactor(fac2) + prevCycleHigh
sellBelow = prevCycleHigh - math.pow(math.sqrt(pRange) - degreeFactor(3),
2) * degreeFactor(3)
majorSupport = prevCycleHigh - math.pow(math.sqrt(pRange) -
degreeFactor(fac1), 2) * degreeFactor(fac1)
sellTarget = prevCycleHigh - math.pow(math.sqrt(pRange) -
degreeFactor(fac2), 2) * degreeFactor(fac2)
[buyAbove, majorResistance, buyTarget, sellBelow, majorSupport, sellTarget]
else
majorResistance = math.pow(math.sqrt(pRange) + degreeFactor(fac1), 2) *
degreeFactor(fac1) + prevCycleHigh
buyTarget = math.pow(math.sqrt(pRange) + degreeFactor(fac2), 2) *
degreeFactor(fac2) + prevCycleHigh
majorSupport = prevCycleLow - math.pow(math.sqrt(pRange) -
degreeFactor(fac1), 2) * degreeFactor(fac1)
sellTarget = prevCycleLow - math.pow(math.sqrt(pRange) -
degreeFactor(fac2), 2) * degreeFactor(fac2)
[majorResistance, buyTarget, majorSupport, sellTarget, 0.0, 0.0]
pivot = calculatePivot()

totLn := calculateLN()
totLnSqr := calculateLNSquare()
lnAvg := totLn / 9
lnSqrAvg := totLnSqr / 9
variance := lnSqrAvg - math.pow(lnAvg, 2)
dailyVolatility := math.sqrt(variance)
price := c1
priceRange := dailyVolatility * price

//Cycle One
[buyAbove, majorResistane1, buytarget1, sellBelow, majorSupport1, sellTarget1] =
getCycleLevels(1, price, 0.0, priceRange)

//Cycle Tw0
[majorResistane2, buytarget2, majorSupport2, sellTarget2, dummy1, dummy2] =
getCycleLevels(2, buytarget1, sellTarget1, priceRange)

//Cycle Three
[majorResistane3, buytarget3, majorSupport3, sellTarget3, dummy3, dummy4] =
getCycleLevels(3, buytarget2, sellTarget2, priceRange)

//Cycle Four
[majorResistane4, buytarget4, majorSupport4, sellTarget4, dummy5, dummy6] =
getCycleLevels(4, buytarget3, sellTarget3, priceRange)

//Cycle Five
[majorResistane5, buytarget5, majorSupport5, sellTarget5, dummy7, dummy8] =
getCycleLevels(5, buytarget4, sellTarget4, priceRange)

//Cycle six
[majorResistane6, buytarget6, majorSupport6, sellTarget6, dummy9, dummy10] =
getCycleLevels(6, buytarget5, sellTarget5, priceRange)

//Cycle seven
[majorResistane7, buytarget7, majorSupport7, sellTarget7, dummy11, dummy12] =
getCycleLevels(7, buytarget6, sellTarget6, priceRange)

//Cycle eight
[majorResistane8, buytarget8, majorSupport8, sellTarget8, dummy13, dummy14] =
getCycleLevels(8, buytarget7, sellTarget7, priceRange)

offset_gann = input.int(0,title="Gann s/r offset")


glwidth = input.int(1,title="Gann sr linwwidth")
plot(series=lowerTimeframe ? (buyAbove+sellBelow)/2 : na, title='Gann pivot',
color=color.rgb(247, 1, 132), style=plot.style_linebr, linewidth=glwidth,
transp=0,offset=offset_gann)
plot(series=lowerTimeframe ? buyAbove : na, title='Buy Above', color=pivot[1] !=
pivot and lowerTimeframe ? na : color.blue, style=plot.style_linebr,
linewidth=glwidth, transp=0)
//plot(series=lowerTimeframe ? majorResistane1 : na, title='Major Resistance1',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? buytarget1 : na, title='TGT1', color= color.rgb(108,
182, 242), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
plot(series=lowerTimeframe ? sellBelow : na, title='Sell Below', color=pivot[1] !=
pivot and lowerTimeframe ? na : color.yellow, style=plot.style_linebr,
linewidth=glwidth, transp=0)
//plot(series=lowerTimeframe ? majorSupport1 : na, title='Major Support1',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? sellTarget1 : na, title='TGT1', color= color.rgb(239,
113, 113), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorResistane2 : na, title='Major Resistance2',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? buytarget2 : na, title='TGT2', color=color.rgb(108,
182, 242), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorSupport2 : na, title='Major Support2',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? sellTarget2 : na, title='TGT2', color= color.rgb(239,
113, 113), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorResistane3 : na, title='Major Resistance3',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? buytarget3 : na, title='TGT3', color= color.rgb(108,
182, 242), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorSupport3 : na, title='Major Support3',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? sellTarget3 : na, title='TGT3', color=color.rgb(239,
113, 113), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorResistane4 : na, title='Major Resistance4',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? buytarget4 : na, title='TGT4', color=color.rgb(108,
182, 242), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorSupport4 : na, title='Major Support4',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? sellTarget4 : na, title='TGT4', color=color.rgb(239,
113, 113), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorResistane5 : na, title='Major Resistance5',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? buytarget5 : na, title='TGT5', color=color.rgb(108,
182, 242), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorSupport5 : na, title='Major Support5',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? sellTarget5 : na, title='TGT5', color=color.rgb(239,
113, 113), style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
plot(series=lowerTimeframe ? buytarget6 : na, title='TGT5', color=color.teal,
style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)
//plot(series=lowerTimeframe ? majorSupport5 : na, title='Major Support5',
color=pivot[1] != pivot and lowerTimeframe ? na : color.gray,
style=plot.style_linebr, linewidth=1, transp=0)
plot(series=lowerTimeframe ? sellTarget6 : na, title='TGT5', color=color.fuchsia,
style=plot.style_linebr, linewidth=glwidth, transp=0,offset=offset_gann)

ll_offset = bar_index+2
l_size = size.tiny

disp_g_label = input.bool(false,title="display gann SR labels?")


label_1 = label.new(x=ll_offset, y=buyAbove, text='D Buy abv ║ ' +
str.tostring(buyAbove), style=label.style_label_left, textcolor=color.white,
size=l_size,color=color.new(color.green, 100))
label.delete(label_1[1])
label_2 = label.new(x=ll_offset, y=sellBelow, text='D Sell below ║ ' +
str.tostring(sellBelow), style=label.style_label_left, textcolor=color.white,
size=l_size,color=color.new(color.green, 100))
label.delete(label_2[1])

label_3 = disp_g_label ? label.new(x=ll_offset, y=buytarget1, text='buy Tgt1 ║ ' +


str.tostring(buytarget1), style=label.style_label_left, textcolor=color.green,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_3[1])
label_4 = disp_g_label ? label.new(x=ll_offset, y=buytarget2, text='buy Tgt2 ║ ' +
str.tostring(buytarget2), style=label.style_label_left, textcolor=color.green,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_4[1])
label_5 = disp_g_label ? label.new(x=ll_offset, y=buytarget3, text='buy Tgt3 ║ ' +
str.tostring(buytarget3), style=label.style_label_left, textcolor=color.green,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_5[1])
label_6 = disp_g_label ? label.new(x=ll_offset, y=buytarget4, text='buy Tgt4 ║ ' +
str.tostring(buytarget4), style=label.style_label_left, textcolor=color.green,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_6[1])
label_7 = disp_g_label ? label.new(x=ll_offset, y=buytarget5, text='buy Tgt5 ║ ' +
str.tostring(buytarget5), style=label.style_label_left, textcolor=color.green,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_7[1])

label_8 = disp_g_label ? label.new(x=ll_offset, y=sellTarget1, text='sell Tgt1 ║ '


+ str.tostring(sellTarget1), style=label.style_label_left, textcolor=color.red,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_8[1])
label_9 = disp_g_label ? label.new(x=ll_offset, y=sellTarget2, text='sell Tgt2 ║ '
+ str.tostring(sellTarget2), style=label.style_label_left, textcolor=color.red,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_9[1])
label_10 = disp_g_label ? label.new(x=ll_offset, y=sellTarget3, text='sell Tgt3 ║ '
+ str.tostring(sellTarget3), style=label.style_label_left, textcolor=color.red,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_10[1])
label_11 = disp_g_label ? label.new(x=ll_offset, y=sellTarget4, text='sell Tgt4 ║ '
+ str.tostring(sellTarget4), style=label.style_label_left, textcolor=color.red,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_11[1])
label_12 = disp_g_label ? label.new(x=ll_offset, y=sellTarget5, text='sell Tgt5 ║ '
+ str.tostring(sellTarget5), style=label.style_label_left, textcolor=color.red,
size=l_size,color=color.new(color.green, 100)):na
label.delete(label_12[1])

You might also like