//@version=5 strategy("WD Gann Natural Square Intraday System", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10) // ========================================== // Inputs // ========================================== tolerancePct = input.float(0.4, title="Gann Level Proximity Tolerance (%)", minval=0.05, maxval=2.0, step=0.05) / 100.0 rrRatio = input.float(1.5, title="Risk/Reward Ratio", minval=0.5, step=0.1) useATRBuffer = input.bool(true, title="Use ATR Buffer for Stop Loss") atrLength = input.int(14, title="ATR Length", minval=1) atrMult = input.float(0.5, title="ATR Buffer Multiplier", minval=0.1, step=0.1) atrVal = ta.atr(atrLength) // ========================================== // Gann Natural Square Calculation // ========================================== // Formula: Square natural number m. If result is even, add 1 to make it odd. f_gann_level(int m) => sq = m * m sq % 2 == 0 ? sq + 1 : sq // Get base integer square root of current price baseM = math.round(math.sqrt(close)) // Generate surrounding Gann Levels (-2 to +2 offsets) gann0 = f_gann_level(baseM - 2) gann1 = f_gann_level(baseM - 1) gann2 = f_gann_level(baseM) gann3 = f_gann_level(baseM + 1) gann4 = f_gann_level(baseM + 2) // Function to check if a price value is within tolerance of any Gann level f_is_near_gann(float priceVal) => isNear = false nearestLvl = 0.0 levels = array.new_float(5) array.set(levels, 0, gann0) array.set(levels, 1, gann1) array.set(levels, 2, gann2) array.set(levels, 3, gann3) array.set(levels, 4, gann4) for i = 0 to 4 lvl = array.get(levels, i) if math.abs(priceVal - lvl) / lvl <= tolerancePct isNear := true nearestLvl := lvl break [isNear, nearestLvl] // Determine proximity to Gann Support or Resistance [nearLow, lvlLow] = f_is_near_gann(low) [nearHigh, lvlHigh] = f_is_near_gann(high) [nearClose, lvlClose] = f_is_near_gann(close) isNearGannSupport = nearLow or nearClose isNearGannResist = nearHigh or nearClose // ========================================== // Candlestick Pattern Logic // ========================================== cRange = high - low body = math.abs(close - open) upperWick = high - math.max(open, close) lowerWick = math.min(open, close) - low // Bullish Reversals isHammer = cRange > 0 and lowerWick >= 2.0 * body and upperWick <= 0.2 * cRange isBullishEngulfing = close[1] < open[1] and close > open and close >= open[1] and open <= close[1] // Bearish Reversals isInvertedHammer = cRange > 0 and upperWick >= 2.0 * body and lowerWick <= 0.2 * cRange isBearishEngulfing = close[1] > open[1] and close < open and close <= open[1] and open >= close[1] // Trading Signals longCondition = isNearGannSupport and (isHammer or isBullishEngulfing) shortCondition = isNearGannResist and (isInvertedHammer or isBearishEngulfing) // ========================================== // Visualization // ========================================== plot(gann2, "Gann Main Level", color=color.new(color.blue, 20), linewidth=2) plot(gann1, "Gann Level Below", color=color.new(color.gray, 50), linewidth=1) plot(gann3, "Gann Level Above", color=color.new(color.gray, 50), linewidth=1) plotshape(longCondition, title="Long Signal", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small) plotshape(shortCondition, title="Short Signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small) // ========================================== // Order Execution // ========================================== var float longSL = na var float longTP = na var float shortSL = na var float shortTP = na if longCondition and strategy.position_size == 0 buffer = useATRBuffer ? atrVal * atrMult : 0.0 longSL := low - buffer risk = close - longSL if risk > 0 longTP := close + (risk * rrRatio) strategy.entry("Gann_Long", strategy.long) strategy.exit("Exit_Long", "Gann_Long", stop=longSL, limit=longTP) if shortCondition and strategy.position_size == 0 buffer = useATRBuffer ? atrVal * atrMult : 0.0 shortSL := high + buffer risk = shortSL - close if risk > 0 shortTP := close - (risk * rrRatio) strategy.entry("Gann_Short", strategy.short) strategy.exit("Exit_Short", "Gann_Short", stop=shortSL, limit=shortTP)