OPEN-SOURCE SCRIPT

new_youtube_strategy

81
//version=5
strategy("Dow + Homma 1m Scalper (15m filter)", overlay=true, margin_long=100, margin_short=100, initial_capital=10000)

//===== INPUTS =====
maLen = input.int(50, "Trend SMA Length", minval=5)
htf_tf = input.timeframe("15", "Higher TF")
priceTolPct = input.float(0.05, "SR tolerance %", step=0.01)
wickFactor = input.float(2.0, "Hammer/ShootingStar wick factor", step=0.1)
dojiThresh = input.float(0.1, "Doji body % of range", step=0.01)
risk_RR = input.float(2.0, "Reward:Risk", step=0.1)
capitalRiskPct = input.float(1.0, "Risk % of equity per trade", step=0.1)

//===== 1m TREND (SMA) =====
sma1 = ta.sma(close, maLen)
sma1Up = sma1 > sma1[1]
sma1Down = sma1 < sma1[1]
uptrend1 = close > sma1 and sma1Up
downtrend1 = close < sma1 and sma1Down

//===== 15m TREND VIA request.security =====
sma15 = request.security(syminfo.tickerid, htf_tf, ta.sma(close, maLen), lookahead=barmerge.lookahead_off)
sma15Up = sma15 > sma15[1]
sma15Down = sma15 < sma15[1]
uptrend15 = close > sma15 and sma15Up
downtrend15 = close < sma15 and sma15Down

//===== SWING HIGHS/LOWS (LOCAL EXTREMA) =====
var int left = 3
var int right = 3

swHigh = ta.pivothigh(high, left, right)
swLow = ta.pivotlow(low, left, right)

//===== SR FLIP LEVELS =====
var float srSupport = na
var float srResistance = na

// when a swing high is broken -> new support
if not na(swHigh)
if close > swHigh
srSupport := swHigh
// when a swing low is broken -> new resistance
if not na(swLow)
if close < swLow
srResistance := swLow

//===== CANDLE METRICS =====
body = math.abs(close - open)
cRange = high - low
upperW = high - math.max(open, close)
lowerW = math.min(open, close) - low

isBull() => close > open
isBear() => close < open

bullHammer() =>
cRange > 0 and
isBull() and
lowerW >= wickFactor * body and
upperW <= body

bearShootingStar() =>
cRange > 0 and
isBear() and
upperW >= wickFactor * body and
lowerW <= body

isDoji() =>
cRange > 0 and body <= dojiThresh * cRange

bullEngulfing() =>
isBear()[1] and isBull() and
open <= close[1] and close >= open[1]

bearEngulfing() =>
isBull()[1] and isBear() and
open >= close[1] and close <= open[1]

//===== SR PROXIMITY =====
tol = priceTolPct * 0.01 * close
nearSupport = not na(srSupport) and math.abs(close - srSupport) <= tol
nearResistance = not na(srResistance) and math.abs(close - srResistance) <= tol

//===== SIGNAL CONDITIONS =====
bullCandle = bullHammer() or isDoji() or bullEngulfing()
bearCandle = bearShootingStar() or isDoji() or bearEngulfing()

longTrendOK = uptrend1 and uptrend15
shortTrendOK = downtrend1 and downtrend15

longSignal = longTrendOK and nearSupport and bullCandle
shortSignal = shortTrendOK and nearResistance and bearCandle

//===== POSITION SIZING (IN RISK UNITS) =====
var float lastEquity = strategy.equity
riskCapital = strategy.equity * (capitalRiskPct * 0.01)

//===== ENTRY / EXIT PRICES =====
longStop = math.min(low, nz(srSupport, low))
longRisk = close - longStop
longTP = close + risk_RR * longRisk

shortStop = math.max(high, nz(srResistance, high))
shortRisk = shortStop - close
shortTP = close - risk_RR * shortRisk

// qty in contracts (approx; assumes price * qty ≈ capital used)
longQty = longRisk > 0 ? riskCapital / longRisk : 0.0
shortQty = shortRisk > 0 ? riskCapital / shortRisk : 0.0

//===== EXECUTION =====
if longSignal and longRisk > 0 and longQty > 0
strategy.entry("Long", strategy.long, qty=longQty)
strategy.exit("Long TP/SL", from_entry="Long", stop=longStop, limit=longTP)

if shortSignal and shortRisk > 0 and shortQty > 0
strategy.entry("Short", strategy.short, qty=shortQty)
strategy.exit("Short TP/SL", from_entry="Short", stop=shortStop, limit=shortTP)

//===== PLOTS =====
plot(sma1, color=color.orange, title="SMA 1m")
plot(sma15, color=color.blue, title="HTF SMA (15m)")

plot(srSupport, "SR Support", color=color.new(color.green, 50), style=plot.style_linebr)
plot(srResistance,"SR Resistance",color=color.new(color.red, 50), style=plot.style_linebr)

// Visual debug for signals
plotshape(longSignal, title="Long Signal", style=shape.triangleup, location=location.belowbar, color=color.lime, size=size.tiny)
plotshape(shortSignal, title="Short Signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny)

Penafian

Maklumat dan penerbitan adalah tidak bertujuan, dan tidak membentuk, nasihat atau cadangan kewangan, pelaburan, dagangan atau jenis lain yang diberikan atau disahkan oleh TradingView. Baca lebih dalam Terma Penggunaan.