This Tradingview indicator will be of interest to Gann Square of Nine followers- would really appreciate conversion to MT4. It plots the reliable daily Gann based levels automatically :
Code: Select all
//@version=5
indicator("Gann Calculations", overlay=true, max_bars_back = 1000, max_labels_count = 500, max_lines_count = 500)
//---LINES
show_Buy = input.bool(true, "Buy", group = "Levels")
show_Sell = input.bool(true, "Sell", group = "Levels")
show_Sup = input.bool(true, "Support", group = "Levels")
show_Res = input.bool(true, "Resistance", group = "Levels")
//---LABELS
show_Buy_lb = input.bool(false, "Buy Label", group = "Labels")
show_Sell_lb = input.bool(false, "Sell Label", group = "Labels")
show_Sup_lb = input.bool(false, "Support Label", group = "Labels")
show_Res_lb = input.bool(false, "Resistance Label", group = "Labels")
//---SIGNALS
show_Signal_Buy = input.bool(false, "Signal Buy", group = "Signals")
show_Signal_Sell = input.bool(false, "Signal Sell", group = "Signals")
show_DO = input.bool(true, "Show Daily Open", group = "Daily Open")
//----
var line open_price_line = na
var line buyLine = na
var line sellLine = na
var label buyLabel = na
var label sellLabel = na
is_new_day = timeframe.change("D")
if (is_new_day)
// Delete old lines
line.delete(open_price_line)
line.delete(buyLine)
line.delete(sellLine)
// Create new open price line
open_price_line := line.new(na, na, na, na, color=color.white, style=line.style_dashed)
line.set_xy1(open_price_line, bar_index, open)
line.set_xy2(open_price_line, bar_index + 1, open)
else
if show_DO
line.set_x2(open_price_line, bar_index)
//----
num = line.get_y1(open_price_line)
root = math.sqrt(num)
minus_two = root - 2
rounded = math.round(minus_two)
result = math.pow(rounded, 2)
var float[] x = array.new_float(24)
var float[] sqr_x = array.new_float(24)
var float[] sqr_x_rounded = array.new_float(24)
var float[] sqr_x_rounded_root = array.new_float(24)
for i = 0 to 23
array.set(x, i, rounded + 0.125 * (i + 1))
array.set(sqr_x, i, array.get(x, i) * array.get(x, i))
array.set(sqr_x_rounded, i, math.round(array.get(sqr_x, i) * 100) / 100)
array.set(sqr_x_rounded_root, i, math.round((num - array.get(sqr_x_rounded, i)) * 100) / 100)
min_positive_index = -1
for i = 0 to 23
if array.get(sqr_x_rounded_root, i) < 0
min_positive_index := i
break
var float buy_above = na
var float sell_below = na
if min_positive_index >= 0
buy_above := array.get(sqr_x_rounded, min_positive_index)
sell_below := array.get(sqr_x_rounded, min_positive_index - 1)
// Delete old lines
line.delete(buyLine)
line.delete(sellLine)
// Create new buy and sell lines
if min_positive_index >= 0 and show_Buy
buyLine := line.new(na, na, na, na, color=color.aqua, width=1)
line.set_xy1(buyLine, bar_index, buy_above)
line.set_xy2(buyLine, line.get_x1(open_price_line), buy_above)
if min_positive_index >= 0 and show_Sell
sellLine := line.new(na, na, na, na, color=color.yellow, width=1)
line.set_xy1(sellLine, bar_index, sell_below)
line.set_xy2(sellLine, line.get_x1(open_price_line), sell_below)
// Delete old labels
label.delete(buyLabel)
label.delete(sellLabel)
// Create new labels
buyLabel := label.new(na, na, "")
sellLabel := label.new(na, na, "")
// Print the values of buyLine and sellLine
buyLineValues = line.get_y1(buyLine)
sellLineValues = line.get_y1(sellLine)
if show_Buy_lb
// Set label text and style
label.set_text(buyLabel, "BUY: " + str.tostring(buyLineValues))
label.set_textcolor(buyLabel, color.black) // Text color
label.set_color(buyLabel, color.new(color.aqua, 0)) // Background color
label.set_xy(buyLabel, line.get_x1(open_price_line) - 1, line.get_y1(buyLine)) // At the beginning of the buy line
label.set_style(buyLabel, style=label.style_label_right)
if show_Sell_lb
label.set_text(sellLabel, "SELL: " + str.tostring(sellLineValues))
label.set_textcolor(sellLabel, color.black) // Text color
label.set_color(sellLabel, color.new(color.yellow, 0)) // Background color
label.set_xy(sellLabel, line.get_x1(open_price_line) - 1, line.get_y1(sellLine)) // At the beginning of the sell line
label.set_style(sellLabel, style=label.style_label_right)
// Create new labels for buy and sell signals
var label buySignalLabel = na
var label sellSignalLabel = na
//-----
show_startHour = input.bool(false, "Start Hour", group = "Signals Time Window")
show_endHour = input.bool(false, "End Hour", group = "Signals Time Window")
// Define the target hour and minute for the line
targetHour = input.int(15, title="Start Hour", minval=0, maxval=24, group = "START")
targetMinute = input.int(0, title="Minute", minval=0, maxval=60, group = "START")
// Function to check if the current bar is at the target time using timestamp
isTargetTime() =>
targetTimestamp = timestamp(year(time), month(time), dayofmonth(time), targetHour, targetMinute)
time[1] < targetTimestamp and time >= targetTimestamp
plotshape( isTargetTime() ? show_startHour : na, style = shape.triangleup, location = location.bottom, color = color.new(color.gray,0), text = "in", textcolor = color.new(color.white,0), size = size.tiny)
// Define the target hour and minute for the line
targetHour1 = input.int(23, title="End Hour", minval=0, maxval=24, group = "END")
targetMinute1 = input.int(00, title="Minute", minval=0, maxval=60, group = "END")
// Function to check if the current bar is at the target time using timestamp
isTargetTime1() =>
targetTimestamp1 = timestamp(year(time), month(time), dayofmonth(time), targetHour1, targetMinute1)
time[1] < targetTimestamp1 and time >= targetTimestamp1
plotshape( isTargetTime1() ? show_endHour : na, style = shape.triangledown, location = location.bottom, color = color.new(color.gray,0), text = "out", textcolor = color.new(color.white,0), size = size.tiny)
// Check if the current bar's time is within the specified range
isWithinTimeRange = hour >= targetHour and hour <= targetHour1
// // Plot xcross shape when price crosses the buy line
// plotshape(series=ta.crossover(close, buy_above), title="Buy Signal", color=color.aqua, style=shape.circle , size=size.tiny)
// Place label on the close of the candle that breaks above the buy line
if show_Signal_Buy and ta.crossover(close, buy_above) and (close > buy_above) and isWithinTimeRange
buySignalLabel := label.new(x=bar_index, y=close, text="", color=color.aqua, style=label.style_label_down, textcolor=color.white, size=size.tiny)
// // Plot xcross shape when price crosses the sell line
// plotshape(series=ta.crossunder(close, sell_below), title="Sell Signal", color=color.yellow, style=shape.circle, size=size.tiny)
if show_Signal_Sell and ta.crossunder(close, sell_below) and (close < sell_below) and isWithinTimeRange
sellSignalLabel := label.new(x=bar_index, y=close, text="", color=color.yellow, style=label.style_label_up, textcolor=color.white, size=size.tiny)
//-------------
var line supportLine = na // Declare supportLine outside the block
var line resistanceLine = na // Declare resistanceLine outside the block
// Delete old lines
line.delete(supportLine)
line.delete(resistanceLine)
// Calculate and draw the 1st support and first resistance lines
if min_positive_index >= 0 and show_Sup
firstSupport = array.get(sqr_x_rounded, min_positive_index - 2)
// Draw the 1st support line
// supportLine := line.new(bar_index, firstSupport, bar_index + 1, firstSupport, color=color.yellow, style=line.style_dashed)
supportLine := line.new(na, na, na, na, color=color.yellow, width=1)
line.set_xy1(supportLine, bar_index, firstSupport)
line.set_xy2(supportLine, line.get_x1(open_price_line), firstSupport)
if min_positive_index >= 0 and show_Res
firstResistance = array.get(sqr_x_rounded, min_positive_index + 1)
// Draw the first resistance line
// resistanceLine := line.new(bar_index, firstResistance, bar_index + 1, firstResistance, color=color.aqua, style=line.style_dashed)
resistanceLine := line.new(na, na, na, na, color=color.aqua, width=1)
line.set_xy1(resistanceLine, bar_index, firstResistance)
line.set_xy2(resistanceLine, line.get_x1(open_price_line), firstResistance)
var label supportLabel = na
var label resistanceLabel = na
// Delete old labels for support and resistance
label.delete(supportLabel)
label.delete(resistanceLabel)
// Create new labels for support and resistance
supportLabel := label.new(na, na, "")
resistanceLabel := label.new(na, na, "")
// Print the values of supportLine and resistanceLine
supportLineValues = line.get_y1(supportLine)
resistanceLineValues = line.get_y1(resistanceLine)
if show_Sup_lb
// Set label text and style for support
label.set_text(supportLabel, "S: " + str.tostring(supportLineValues))
label.set_textcolor(supportLabel, color.black) // Text color
label.set_color(supportLabel, color.new(color.yellow, 0)) // Background color
label.set_xy(supportLabel, line.get_x1(open_price_line) - 1, line.get_y1(supportLine)) // At the beginning of the support line
label.set_style(supportLabel, style=label.style_label_right)
if show_Res_lb
// Set label text and style for resistance
label.set_text(resistanceLabel, "R: " + str.tostring(resistanceLineValues))
label.set_textcolor(resistanceLabel, color.black) // Text color
label.set_color(resistanceLabel, color.new(color.aqua, 0)) // Background color
label.set_xy(resistanceLabel, line.get_x1(open_price_line) - 1, line.get_y1(resistanceLine)) // At the beginning of the resistance line
label.set_style(resistanceLabel, style=label.style_label_right)
//------------Gann
show_squares = input.bool(false, title = "Squares lines", group = "Gann Square Lines")
show_diagonals1 = input.bool(false, title = "Diagonals Type 1 lines", group = "Diagonals Lines")
show_diagonals2 = input.bool(false, title = "Diagonals Type 2 lines", group = "Diagonals Lines")
show_midlines_type1 = input.bool(false, title = "Mid Lines Type 1 lines", group = "Mid-Lines Lines")
show_midlines_type2 = input.bool(false, title = "Mid Lines Type 2 lines", group = "Mid-Lines Lines")
show_insquare_type1 = input.bool(false, title = "Inside Square Type 1 lines", group = "Inside Square Lines")
show_insquare_type2 = input.bool(false, title = "Inside Square Type 2 lines", group = "Inside Square Lines")
quarter = input.bool(false, title = "1/4 lines", group = "Lines")
sixth = input.bool(false, title = "1/6 lines", group = "Lines")
eighth = input.bool(false, title = "1/8 lines", group = "Lines")
outsiteLines = input.color(color.new(color.white,30), title = "Frame Lines Color", group = "Style", inline = "colors")
insideLines = input.color(color.new(color.white,50), title = "Inside Lines Color", group = "Style", inline = "colors")
widthLines = input.int(1, title = "Width", group = "Style")
//----
show_startHour_S = input.bool(false, "Start Hour", group = "Gann Squares Time Window")
show_endHour_S = input.bool(false, "End Hour", group = "Gann Squares Time Window")
// Define the target hour and minute for the line
targetHour_S = input.int(0, title="Start Hour", minval=0, maxval=24, group = "Gann Squares Start")
targetMinute_S = input.int(0, title="Minute", minval=0, maxval=60, group = "Gann Squares Start")
// Function to check if the current bar is at the target time using timestamp
isTargetTime_S() =>
targetTimestamp = timestamp(year(time), month(time), dayofmonth(time), targetHour_S, targetMinute_S)
time[1] < targetTimestamp and time >= targetTimestamp
plotshape( isTargetTime_S() ? show_startHour_S : na, style = shape.triangleup, location = location.bottom, color = color.new(color.gray,0), text = "in", textcolor = color.new(color.white,0), size = size.tiny)
// Define the target hour and minute for the line
targetHour_S1 = input.int(24, title="End Hour", minval=0, maxval=24, group = "Gann Squares End")
targetMinute_S1 = input.int(00, title="Minute", minval=0, maxval=60, group = "Gann Squares End")
// Function to check if the current bar is at the target time using timestamp
isTargetTime_S1() =>
targetTimestamp1 = timestamp(year(time), month(time), dayofmonth(time), targetHour_S1, targetMinute_S1)
time[1] < targetTimestamp1 and time >= targetTimestamp1
plotshape( isTargetTime_S1() ? show_endHour_S : na, style = shape.triangledown, location = location.bottom, color = color.new(color.gray,0), text = "out", textcolor = color.new(color.white,0), size = size.tiny)
//----
calculations_day_back = input.int(1, title="Start Calculation in Days", minval=0, maxval = 3, group = "Gann Days Calculations")
//quarter - sixth - eighth
start = timestamp(year, month, dayofmonth - calculations_day_back, targetHour_S, targetMinute_S)
end = timestamp(year, month, dayofmonth - calculations_day_back, targetHour_S1, targetMinute_S1)
barsFromStart = (time - start)/(time - time[1]) // From start to real time
barsFromEnd = (time - end)/(time - time[1]) // From end to real time
bars = barsFromStart - barsFromEnd
var lines = array.new_line()
getHighestValue(lookback, beginning) =>
float higher = 0.0
for i = 0 to lookback - 1
value = nz(high[beginning - i], higher[1])
if value > higher
higher := value
higher
getLowestValue(lookback, beginning) =>
float lowest = 10000000000000
for i = 0 to lookback - 1
value = nz(low[beginning - i], lowest[1])
if value < lowest
lowest := value
lowest
drawSquares(startTime, endTime, highValue, lowValue, midValue) =>
//DRAW LINES
if show_squares
array.push(lines, line.new(startTime, highValue, endTime, highValue, xloc = xloc.bar_time, color = outsiteLines, width = widthLines))
array.push(lines, line.new(startTime, lowValue, endTime, lowValue, xloc = xloc.bar_time, color = outsiteLines, width = widthLines))
array.push(lines, line.new(startTime, highValue, startTime, lowValue, xloc = xloc.bar_time, color = outsiteLines, width = widthLines))
array.push(lines, line.new(endTime, highValue, endTime, lowValue, xloc = xloc.bar_time, color = outsiteLines, width = widthLines))
if show_insquare_type1
//## X inside square ##
array.push(lines, line.new(startTime, lowValue, endTime, highValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
if show_insquare_type2
array.push(lines, line.new(startTime, highValue, endTime, lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
var float upperY = na
var float lowerY = na
var float upperY1 = na
var float lowerY1 = na
var int upperX = na
var int lowerX = na
var int upperX1 = na
var int lowerX1 = na
var line diagonal_line = na
//## Vertical mid start and end ##
// Draw Diagonals and Place Labels on Price Cross
if show_diagonals1
upperDiagonalLine = line.new(startTime, midValue, endTime, highValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines)
lowerDiagonalLine = line.new(startTime, midValue, endTime, lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines)
// Retrieve y-coordinates
upperY := line.get_y1(upperDiagonalLine)
lowerY := line.get_y1(lowerDiagonalLine)
// Retrieve y-coordinates
upperY1 := line.get_y2(upperDiagonalLine)
lowerY1 := line.get_x2(lowerDiagonalLine)
// Retrieve y-coordinates
upperX := line.get_x1(upperDiagonalLine)
lowerX := line.get_x1(lowerDiagonalLine)
// Retrieve y-coordinates
upperX1 := line.get_x2(upperDiagonalLine)
lowerX1 := line.get_x2(lowerDiagonalLine)
// // Place label when price crosses above the upper diagonal line
// if ta.cross(close, upperY)
// label.new(bar_index, close, text="Price Crossed Above Diagonal", color=color.green, style=label.style_label_up)
// // Place label when price crosses below the lower diagonal line
// if lowerY
// label.new(bar_index, lowerY, text="Price Crossed Below Diagonal", color=color.red, style=label.style_label_down)
if show_diagonals2
array.push(lines, line.new(endTime, midValue, startTime, highValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
array.push(lines, line.new(endTime, midValue, startTime, lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
// ... (remaining code)
//## Horizontal mid start and end ##
if show_midlines_type1
array.push(lines, line.new((startTime + ((endTime - startTime) / 2)), lowValue, endTime, highValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
array.push(lines, line.new((startTime + ((endTime - startTime) / 2)), lowValue, startTime, highValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
if show_midlines_type2
array.push(lines, line.new((startTime + ((endTime - startTime) / 2)), highValue, endTime, lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
array.push(lines, line.new((startTime + ((endTime - startTime) / 2)), highValue, startTime, lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
//Quarter lines
if quarter
sectionPriceInterval = ((highValue - lowValue) / 4)
sectionTimeInterval = ((endTime - startTime) / 4)
for i = 1 to 3
array.push(lines, line.new(startTime, (lowValue + (sectionPriceInterval * i)), endTime, (lowValue + (sectionPriceInterval * i)), xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
array.push(lines, line.new((startTime + (sectionTimeInterval * i)), highValue, (startTime + (sectionTimeInterval * i)), lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
//Sixth lines
if sixth
sectionPriceInterval = ((highValue - lowValue) / 6)
sectionTimeInterval = ((endTime - startTime) / 6)
for i = 1 to 5
array.push(lines, line.new(startTime, (lowValue + (sectionPriceInterval * i)), endTime, (lowValue + (sectionPriceInterval * i)), xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
array.push(lines, line.new((startTime + (sectionTimeInterval * i)), highValue, (startTime + (sectionTimeInterval * i)), lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
//Eighth lines
if eighth
sectionPriceInterval = ((highValue - lowValue) / 8)
sectionTimeInterval = ((endTime - startTime) / 8)
for i = 1 to 7
array.push(lines, line.new(startTime, (lowValue + (sectionPriceInterval * i)), endTime, (lowValue + (sectionPriceInterval * i)), xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
array.push(lines, line.new((startTime + (sectionTimeInterval * i)), highValue, (startTime + (sectionTimeInterval * i)), lowValue, xloc = xloc.bar_time, style = line.style_dashed, color = insideLines, width = widthLines))
if barstate.islast
highestValuePrincipal = getHighestValue(bars, barsFromStart)
lowestValuePrincipal = getLowestValue(bars, barsFromStart)
midValuePrincipal = lowestValuePrincipal + ((highestValuePrincipal - lowestValuePrincipal) / 2)
intervalTime = end - start
//Draw principal square
drawSquares(start, end, highestValuePrincipal, lowestValuePrincipal, midValuePrincipal)
//Right Square
drawSquares(end, (end + intervalTime), highestValuePrincipal, lowestValuePrincipal, midValuePrincipal)
//Top Right Square
drawSquares(end, (end + intervalTime), (highestValuePrincipal + (highestValuePrincipal - lowestValuePrincipal)), highestValuePrincipal, (midValuePrincipal + (highestValuePrincipal - lowestValuePrincipal)))
//Bottom Right Square
drawSquares(end, (end + intervalTime), lowestValuePrincipal, lowestValuePrincipal - (highestValuePrincipal - lowestValuePrincipal), (midValuePrincipal - (highestValuePrincipal - lowestValuePrincipal)))
//Right Right Square
drawSquares((end + intervalTime), (end + (intervalTime * 2)), highestValuePrincipal, lowestValuePrincipal, midValuePrincipal)
//Top Right Right Square
drawSquares((end + intervalTime), (end + (intervalTime * 2)), (highestValuePrincipal + (highestValuePrincipal - lowestValuePrincipal)), highestValuePrincipal, (midValuePrincipal + (highestValuePrincipal - lowestValuePrincipal)))
//Bottom Right Right Square
drawSquares((end + intervalTime), (end + (intervalTime * 2)), lowestValuePrincipal, lowestValuePrincipal - (highestValuePrincipal - lowestValuePrincipal), (midValuePrincipal - (highestValuePrincipal - lowestValuePrincipal)))