OPEN-SOURCE SCRIPT

Market Shift Strategy

115
//version=5
strategy("Market Shift Strategy", overlay=true, margin_long=100, margin_short=100)

// Inputs
htfTimeframe = input.timeframe("4H", title="Higher Timeframe")
obLookback = input.int(50, title="Order Block Lookback Period")

// 1. Check HTF Market Structure (Bullish)
htfHigh = request.security(syminfo.tickerid, htfTimeframe, high)
htfLow = request.security(syminfo.tickerid, htfTimeframe, low)

htfBullish = htfHigh[2] < htfHigh[1] and htfHigh[1] < htfHigh[0] and
htfLow[2] < htfLow[1] and htfLow[1] < htfLow[0]

// 2. Find HTF Support (Recent Swing Low)
var float htfSupport = na
htfSwingLow = ta.pivotlow(htfLow, 2, 2)
if not na(htfSwingLow)
htfSupport := htfSwingLow

// 3. Detect Order Blocks on LTF
var float[] obHighs = array.new_float()
var float[] obLows = array.new_float()

detectOB() =>
bearishCandle = close[1] < open[1]
bullishBreak = close > high[1]
nearSupport = math.abs(low[1] - htfSupport) <= htfSupport * 0.005

if bearishCandle and bullishBreak and nearSupport and htfBullish
array.unshift(obHighs, high[1])
array.unshift(obLows, low[1])

// Keep only recent OBs
if array.size(obHighs) > obLookback
array.pop(obHighs)
array.pop(obLows)

detectOB()

// 4. Entry Conditions
var bool entrySignal = false
var float entryPrice = na
var float stopLoss = na
var float takeProfit = na

for i = 0 to array.size(obHighs)-1
obH = array.get(obHighs, i)
obL = array.get(obLows, i)

// Check price return to OB zone
if low <= obH and high >= obL and not entrySignal
entrySignal := true
entryPrice := obL
stopLoss := obL - (obL * 0.001)
takeProfit := htfHigh[0]
break

// 5. Execute Strategy
if entrySignal
strategy.entry("Long", strategy.long)
strategy.exit("Exit", "Long", stop=stopLoss, limit=takeProfit)
entrySignal := false

// Plotting
plotshape(entrySignal, style=shape.triangleup, color=color.green, location=location.belowbar)
plot(stopLoss, "Stop Loss", color=color.red, linewidth=2)
plot(takeProfit, "Take Profit", color=color.green, linewidth=2)

Clause de non-responsabilité

Les informations et les publications ne sont pas destinées à être, et ne constituent pas, des conseils ou des recommandations en matière de finance, d'investissement, de trading ou d'autres types de conseils fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.