Skip to content

Instantly share code, notes, and snippets.

@SurajBahadur
Created April 16, 2025 21:05
Show Gist options
  • Save SurajBahadur/d85aef774d9bd55e0c0ca258de8c52ce to your computer and use it in GitHub Desktop.
Save SurajBahadur/d85aef774d9bd55e0c0ca258de8c52ce to your computer and use it in GitHub Desktop.
3 Candle Formation Trading Strategy
//@version=5
indicator("3-Candle Pattern with Horizontal Rectangle Box", overlay=true)
// === Pattern Detection ===
isValidBullishPattern() =>
close[2] > open[2] and close[1] > open[1] and close > open and low[1] > low[2] and low > low[1]
isValidBearishPattern() =>
close[2] < open[2] and close[1] < open[1] and close < open and high[1] < high[2] and high < high[1]
// === State Variables ===
var box bullBox = na
var box bearBox = na
var int bullStartBar = na
var int bearStartBar = na
// === Detect Patterns ===
bullishPattern = isValidBullishPattern()
bearishPattern = isValidBearishPattern()
// === Start Bullish Rectangle Box ===
if bullishPattern
if not na(bearBox)
box.set_right(bearBox, bar_index)
bearBox := na
bearStartBar := na
if na(bullBox)
bullStartBar := bar_index[2]
bullBox := box.new(bullStartBar, close[2], bar_index, low[2], border_color=color.green, bgcolor=color.new(color.green, 85))
// === Extend Bullish Box ===
if not na(bullBox) and not bullishPattern
box.set_right(bullBox, bar_index)
// === Start Bearish Rectangle Box ===
if bearishPattern
if not na(bullBox)
box.set_right(bullBox, bar_index)
bullBox := na
bullStartBar := na
if na(bearBox)
bearStartBar := bar_index[2]
bearBox := box.new(bearStartBar, high[2], bar_index, close[2], border_color=color.red, bgcolor=color.new(color.red, 85))
// === Extend Bearish Box ===
if not na(bearBox) and not bearishPattern
box.set_right(bearBox, bar_index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment