//@version=5
indicator("Supply Demand Zones Pro", overlay=true, max_boxes_count=500)
// COLORS
demandColor = color.new(color.green,85)
supplyColor = color.new(color.red,85)
// CANDLE TYPES
isRally(i)=>
close[i] > open[i]
isDrop(i)=>
close[i] < open[i]
// STRONG CANDLE
isStrong(i)=>
body = math.abs(close[i]-open[i])
rng = high[i]-low[i]
body >= rng * 0.60
// BASE CANDLE
isBase(i)=>
body = math.abs(close[i]-open[i])
rng = high[i]-low[i]
body <= rng * 0.70
// ARRAYS
var demandZones = array.new_box()
var supplyZones = array.new_box()
var demandBars = array.new_int()
var supplyBars = array.new_int()
// CREATE DEMAND
createDemand(startPrice,endPrice)=>
top = math.max(startPrice,endPrice)
bottom = math.min(startPrice,endPrice)
b = box.new(bar_index-1,top,bar_index+10,bottom,
bgcolor=demandColor,border_color=color.green)
array.push(demandZones,b)
array.push(demandBars,bar_index)
// CREATE SUPPLY
createSupply(startPrice,endPrice)=>
top = math.max(startPrice,endPrice)
bottom = math.min(startPrice,endPrice)
b = box.new(bar_index-1,top,bar_index+10,bottom,
bgcolor=supplyColor,border_color=color.red)
array.push(supplyZones,b)
array.push(supplyBars,bar_index)
// REMOVE DEMAND
removeDemand()=>
i = array.size(demandZones)-1
while i >= 0
z = array.get(demandZones,i)
created = array.get(demandBars,i)
zoneHigh = box.get_top(z)
if bar_index > created + 2 and low <= zoneHigh
box.delete(z)
array.remove(demandZones,i)
array.remove(demandBars,i)
i := i-1
// REMOVE SUPPLY
removeSupply()=>
i = array.size(supplyZones)-1
while i >= 0
z = array.get(supplyZones,i)
created = array.get(supplyBars,i)
zoneLow = box.get_bottom(z)
if bar_index > created + 2 and high >= zoneLow
box.delete(z)
array.remove(supplyZones,i)
array.remove(supplyBars,i)
i := i-1
// PATTERN DETECTION
if bar_index > 3
// DBR DEMAND
if isDrop(2) and isBase(1) and isRally(0) and isStrong(0)
start = math.max(open[1],close[1])
endp = math.min(low[2],low[1])
createDemand(start,endp)
// RBR DEMAND
if isRally(2) and isBase(1) and isRally(0) and isStrong(0)
start = math.max(open[1],close[1])
endp = low[1]
createDemand(start,endp)
// RBD SUPPLY
if isRally(2) and isBase(1) and isDrop(0) and isStrong(0)
start = math.min(open[1],close[1])
endp = math.max(high[2],high[1])
createSupply(start,endp)
// DBD SUPPLY
if isDrop(2) and isBase(1) and isDrop(0) and isStrong(0)
start = math.min(open[1],close[1])
endp = high[1]
createSupply(start,endp)
// DELETE USED ZONES
removeDemand()
removeSupply()
//@version=5
strategy("Swing Trend Strategy PRO (Sideways Filtered) | Nifty 500",
overlay=true,
initial_capital=100000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
pyramiding=0,
process_orders_on_close=true)
// INPUTS
stopLossPercent = input.float(8.0, "Stop Loss %", minval=1)
rsiLength = input.int(14, "RSI Length")
emaFastLength = input.int(20, "Fast EMA")
emaMidLength = input.int(50, "Mid EMA")
emaLongLength = input.int(200, "Long EMA")
adxLength = input.int(14, "ADX Length")
adxThreshold = input.float(20, "ADX Minimum Trend Strength")
// INDICATORS
emaFast = ta.ema(close, emaFastLength)
emaMid = ta.ema(close, emaMidLength)
emaLong = ta.ema(close, emaLongLength)
rsiVal = ta.rsi(close, rsiLength)
[diplus, diminus, adxVal] = ta.dmi(adxLength, adxLength)
// TREND CONDITIONS
trendUp = close > emaLong and emaMid > emaLong
strongTrend = adxVal > adxThreshold
// ENTRY CONDITION
entryCondition = trendUp and strongTrend and close > emaFast and ta.crossover(rsiVal, 55)
if entryCondition
strategy.entry("LONG", strategy.long)
// STOP LOSS
stopPrice = strategy.position_avg_price * (1 - stopLossPercent / 100)
if strategy.position_size > 0
strategy.exit("SL", from_entry="LONG", stop=stopPrice)
// TREND EXIT
trendExit = close < emaMid
if strategy.position_size > 0 and trendExit
strategy.close("LONG")
// PLOTS
plot(emaFast, title="20 EMA", color=color.orange, linewidth=2)
plot(emaMid, title="50 EMA", color=color.blue, linewidth=2)
plot(emaLong, title="200 EMA", color=color.red, linewidth=2)
// Execution markers
longOpened = strategy.position_size > 0 and strategy.position_size[1] == 0
longClosed = strategy.position_size == 0 and strategy.position_size[1] > 0
plotshape(longOpened,
title="BUY",
style=shape.labelup,
location=location.belowbar,
color=color.green,
text="BUY")
plotshape(longClosed,
title="SELL",
style=shape.labeldown,
location=location.abovebar,
color=color.red,
text="SELL")