OPEN-SOURCE SCRIPT
AlphaTrend AutoTrade PRO v3.1

//version=5
// ---------------------------------------------------------------------------
// AlphaTrend AutoTrade PRO v3.1 (compile‑safe, dynamic lot)
// ---------------------------------------------------------------------------
// – Fixed Lot OR %Equity sizing via runtime `qty` parameter
// – ATR-based SL/TP + optional Trailing
// – Optional filters (HTF-RSI, Session, Weekday)
// – Webhook JSON + Debug Shapes
// ---------------------------------------------------------------------------
//──────────────────────── ① Order Size Mode ─────────────────────────
accountMode = input.string("Fixed", "Order Size Mode", options=["Fixed","PercentRisk"])
fixedLot = input.float(1.0, "Fixed Lot Size", step=0.1)
riskPct = input.float(1.0, "% Equity Risk per Trade (if Percent)", step=0.1)
// Strategy header must use constant; we pick FIXED 1 lot as default placeholder
strategy("AlphaTrend AutoTrade PRO v3.1", overlay=true,
default_qty_type=strategy.fixed,
default_qty_value=1,
initial_capital=10000,
currency=currency.USD,
calc_on_every_tick=false,
max_bars_back=500)
// Helper to compute dynamic qty each entry
calcQty(bool isLong) =>
accountMode == "Fixed" ? fixedLot :
// Percent equity: qty = (equity * pct) / price
(strategy.equity * (riskPct/100.0)) / close
//──────────────────────── ② AlphaTrend & Risk Inputs ───────────────
atrMult = input.float(1.0, "ATR Multiplier", step=0.05)
atrLen = input.int(14, "ATR Period")
slATR = input.float(1.2, "SL × ATR")
tpATR = input.float(1.8, "TP × ATR")
trailOn = input.bool(true, "Enable Trailing")
trailStart= input.float(1.5, "Trail Start × ATR")
trailOffset= input.float(1.0, "Trail Offset × ATR")
//──────────────────────── ③ Filters ────────────────────────────────
htfEnable = input.bool(false, "Higher‑TF RSI filter")
htfTF = input.timeframe("15", "Higher‑TF TF")
rsithreshold= input.int(50, "HTF RSI Threshold", minval=10, maxval=90)
sessEnable = input.bool(false, "Session filter (Bangkok)")
sessionStr = input.session("1300-2300", "Active HHMM‑HHMM")
dowEnable = input.bool(false, "Filter by Weekday")
tradeDays = input.string("12345", "Trade days (1=Mon)")
showDebug = input.bool(true, "Show Debug Shapes")
secretToken = input.string("MYSECRET", "Webhook Token")
//──────────────────────── ④ AlphaTrend Calc ───────────────────────
atr = ta.sma(ta.tr, atrLen)
upT = low - atr*atrMult
dnT = high + atr*atrMult
var float at = na
condUp = ta.rsi(close, atrLen) >= 50
at := condUp ? math.max(nz(at[1], upT), upT) : math.min(nz(at[1], dnT), dnT)
rawLong = ta.crossover(at, at[2])
rawShort = ta.crossunder(at, at[2])
htfPassLong = not htfEnable or request.security(syminfo.tickerid, htfTF, ta.rsi(close, atrLen) > rsithreshold)
htfPassShort = not htfEnable or request.security(syminfo.tickerid, htfTF, ta.rsi(close, atrLen) < rsithreshold)
sessPass = not sessEnable or not na(time(timeframe.period, sessionStr))
dowPass = not dowEnable or str.contains(tradeDays, str.tostring(dayofweek))
longCond = rawLong and htfPassLong and sessPass and dowPass
shortCond = rawShort and htfPassShort and sessPass and dowPass
//──────────────────────── ⑤ Risk Params ───────────────────────────
slPts = atr*slATR
tpPts = atr*tpATR
trailP = trailOn ? atr*trailStart : na
trailO = trailOn ? atr*trailOffset : na
lotDesc = accountMode=="Fixed" ? str.tostring(fixedLot) : str.tostring(riskPct)+"%"
//──────────────────────── ⑥ Execute & Webhook ─────────────────────
if longCond
lot = calcQty(true)
strategy.entry("L", strategy.long, qty=lot)
strategy.exit("XL", from_entry="L", stop=close-slPts, limit=close+tpPts, trail_points=trailP, trail_offset=trailO)
if barstate.isconfirmed and barstate.isrealtime
msg = '{"token":"'+secretToken+'","action":"buy","symbol":"'+syminfo.ticker+'","price":'+str.tostring(close)+',"sl":'+str.tostring(close-slPts)+',"tp":'+str.tostring(close+tpPts)+',"lot":"'+lotDesc+'"}'
alert(msg, alert.freq_once_per_bar_close)
if shortCond
lot = calcQty(false)
strategy.entry("S", strategy.short, qty=lot)
strategy.exit("XS", from_entry="S", stop=close+slPts, limit=close-tpPts, trail_points=trailP, trail_offset=trailO)
if barstate.isconfirmed and barstate.isrealtime
msg = '{"token":"'+secretToken+'","action":"sell","symbol":"'+syminfo.ticker+'","price":'+str.tostring(close)+',"sl":'+str.tostring(close+slPts)+',"tp":'+str.tostring(close-tpPts)+',"lot":"'+lotDesc+'"}'
alert(msg, alert.freq_once_per_bar_close)
//──────────────────────── ⑦ Debug Shapes ──────────────────────────
plotshape(rawLong and showDebug, title="RawLong", style=shape.circle, location=location.belowbar, color=color.aqua, size=size.tiny)
plotshape(rawShort and showDebug, title="RawShort", style=shape.circle, location=location.abovebar, color=color.purple, size=size.tiny)
plotshape(longCond and showDebug, title="LongOK", style=shape.arrowup, location=location.belowbar, color=color.green)
plotshape(shortCond and showDebug, title="ShortOK", style=shape.arrowdown, location=location.abovebar, color=color.red)
//──────────────────────── ⑧ Plot AlphaTrend ───────────────────────
col = at > at[2] ? color.green : color.red
plot(at, "AlphaTrend", color=col, linewidth=3)
plot(at[2], "AlphaTrendLag", color=color.new(col,70), linewidth=2)
// ---------------------------------------------------------------------------
// AlphaTrend AutoTrade PRO v3.1 (compile‑safe, dynamic lot)
// ---------------------------------------------------------------------------
// – Fixed Lot OR %Equity sizing via runtime `qty` parameter
// – ATR-based SL/TP + optional Trailing
// – Optional filters (HTF-RSI, Session, Weekday)
// – Webhook JSON + Debug Shapes
// ---------------------------------------------------------------------------
//──────────────────────── ① Order Size Mode ─────────────────────────
accountMode = input.string("Fixed", "Order Size Mode", options=["Fixed","PercentRisk"])
fixedLot = input.float(1.0, "Fixed Lot Size", step=0.1)
riskPct = input.float(1.0, "% Equity Risk per Trade (if Percent)", step=0.1)
// Strategy header must use constant; we pick FIXED 1 lot as default placeholder
strategy("AlphaTrend AutoTrade PRO v3.1", overlay=true,
default_qty_type=strategy.fixed,
default_qty_value=1,
initial_capital=10000,
currency=currency.USD,
calc_on_every_tick=false,
max_bars_back=500)
// Helper to compute dynamic qty each entry
calcQty(bool isLong) =>
accountMode == "Fixed" ? fixedLot :
// Percent equity: qty = (equity * pct) / price
(strategy.equity * (riskPct/100.0)) / close
//──────────────────────── ② AlphaTrend & Risk Inputs ───────────────
atrMult = input.float(1.0, "ATR Multiplier", step=0.05)
atrLen = input.int(14, "ATR Period")
slATR = input.float(1.2, "SL × ATR")
tpATR = input.float(1.8, "TP × ATR")
trailOn = input.bool(true, "Enable Trailing")
trailStart= input.float(1.5, "Trail Start × ATR")
trailOffset= input.float(1.0, "Trail Offset × ATR")
//──────────────────────── ③ Filters ────────────────────────────────
htfEnable = input.bool(false, "Higher‑TF RSI filter")
htfTF = input.timeframe("15", "Higher‑TF TF")
rsithreshold= input.int(50, "HTF RSI Threshold", minval=10, maxval=90)
sessEnable = input.bool(false, "Session filter (Bangkok)")
sessionStr = input.session("1300-2300", "Active HHMM‑HHMM")
dowEnable = input.bool(false, "Filter by Weekday")
tradeDays = input.string("12345", "Trade days (1=Mon)")
showDebug = input.bool(true, "Show Debug Shapes")
secretToken = input.string("MYSECRET", "Webhook Token")
//──────────────────────── ④ AlphaTrend Calc ───────────────────────
atr = ta.sma(ta.tr, atrLen)
upT = low - atr*atrMult
dnT = high + atr*atrMult
var float at = na
condUp = ta.rsi(close, atrLen) >= 50
at := condUp ? math.max(nz(at[1], upT), upT) : math.min(nz(at[1], dnT), dnT)
rawLong = ta.crossover(at, at[2])
rawShort = ta.crossunder(at, at[2])
htfPassLong = not htfEnable or request.security(syminfo.tickerid, htfTF, ta.rsi(close, atrLen) > rsithreshold)
htfPassShort = not htfEnable or request.security(syminfo.tickerid, htfTF, ta.rsi(close, atrLen) < rsithreshold)
sessPass = not sessEnable or not na(time(timeframe.period, sessionStr))
dowPass = not dowEnable or str.contains(tradeDays, str.tostring(dayofweek))
longCond = rawLong and htfPassLong and sessPass and dowPass
shortCond = rawShort and htfPassShort and sessPass and dowPass
//──────────────────────── ⑤ Risk Params ───────────────────────────
slPts = atr*slATR
tpPts = atr*tpATR
trailP = trailOn ? atr*trailStart : na
trailO = trailOn ? atr*trailOffset : na
lotDesc = accountMode=="Fixed" ? str.tostring(fixedLot) : str.tostring(riskPct)+"%"
//──────────────────────── ⑥ Execute & Webhook ─────────────────────
if longCond
lot = calcQty(true)
strategy.entry("L", strategy.long, qty=lot)
strategy.exit("XL", from_entry="L", stop=close-slPts, limit=close+tpPts, trail_points=trailP, trail_offset=trailO)
if barstate.isconfirmed and barstate.isrealtime
msg = '{"token":"'+secretToken+'","action":"buy","symbol":"'+syminfo.ticker+'","price":'+str.tostring(close)+',"sl":'+str.tostring(close-slPts)+',"tp":'+str.tostring(close+tpPts)+',"lot":"'+lotDesc+'"}'
alert(msg, alert.freq_once_per_bar_close)
if shortCond
lot = calcQty(false)
strategy.entry("S", strategy.short, qty=lot)
strategy.exit("XS", from_entry="S", stop=close+slPts, limit=close-tpPts, trail_points=trailP, trail_offset=trailO)
if barstate.isconfirmed and barstate.isrealtime
msg = '{"token":"'+secretToken+'","action":"sell","symbol":"'+syminfo.ticker+'","price":'+str.tostring(close)+',"sl":'+str.tostring(close+slPts)+',"tp":'+str.tostring(close-tpPts)+',"lot":"'+lotDesc+'"}'
alert(msg, alert.freq_once_per_bar_close)
//──────────────────────── ⑦ Debug Shapes ──────────────────────────
plotshape(rawLong and showDebug, title="RawLong", style=shape.circle, location=location.belowbar, color=color.aqua, size=size.tiny)
plotshape(rawShort and showDebug, title="RawShort", style=shape.circle, location=location.abovebar, color=color.purple, size=size.tiny)
plotshape(longCond and showDebug, title="LongOK", style=shape.arrowup, location=location.belowbar, color=color.green)
plotshape(shortCond and showDebug, title="ShortOK", style=shape.arrowdown, location=location.abovebar, color=color.red)
//──────────────────────── ⑧ Plot AlphaTrend ───────────────────────
col = at > at[2] ? color.green : color.red
plot(at, "AlphaTrend", color=col, linewidth=3)
plot(at[2], "AlphaTrendLag", color=color.new(col,70), linewidth=2)
開源腳本
本著TradingView的真正精神,此腳本的創建者將其開源,以便交易者可以查看和驗證其功能。向作者致敬!雖然您可以免費使用它,但請記住,重新發佈程式碼必須遵守我們的網站規則。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。
開源腳本
本著TradingView的真正精神,此腳本的創建者將其開源,以便交易者可以查看和驗證其功能。向作者致敬!雖然您可以免費使用它,但請記住,重新發佈程式碼必須遵守我們的網站規則。
免責聲明
這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。