本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("PaBuy/PaSell + EMA 11 + RSI + H1/H4 Filter", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// --- INPUTS ---
emaLength = input.int(11, title="EMA Length")
rsiLength = input.int(14, title="RSI Length")
rsiSellLevel = input.float(45.0, title="RSI Sell Threshold (<)")
rsiBuyLevel = input.float(55.0, title="RSI Buy Threshold (>)")
// Risk Management
rrRatio = input.float(1.5, title="Risk : Reward Ratio", step=0.1)
slBufferPips = input.float(50.0, title="SL Buffer (Points/Pips)", step=10)
// --- CALCULATIONS ---
ema11 = ta.ema(close, emaLength)
rsi14 = ta.rsi(close, rsiLength)
// -------------------------------------------------------------
// HTF CANDLE FILTERS (H1 & H4 Previous Candle Color Check)
// -------------------------------------------------------------
// ดึงข้อมูลแท่งเทียนก่อนหน้า [1] ของ H1 และ H4 (ใช้ lookahead=barmerge.lookahead_on เพื่อดึงค่าแท่งที่ปิดสมบูรณ์แล้ว)
h1_prev_close = request.security(syminfo.tickerid, "60", close[1], barmerge.gaps_off, barmerge.lookahead_on)
h1_prev_open = request.security(syminfo.tickerid, "60", open[1], barmerge.gaps_off, barmerge.lookahead_on)
h4_prev_close = request.security(syminfo.tickerid, "240", close[1], barmerge.gaps_off, barmerge.lookahead_on)
h4_prev_open = request.security(syminfo.tickerid, "240", open[1], barmerge.gaps_off, barmerge.lookahead_on)
// ตรวจสอบสีแท่งเทียนก่อนหน้าของ H1 และ H4
isH1Red = h1_prev_close < h1_prev_open
isH1Green = h1_prev_close > h1_prev_open
isH4Red = h4_prev_close < h4_prev_open
isH4Green = h4_prev_close > h4_prev_open
// -------------------------------------------------------------
// PRICE ACTION PATTERNS (M5 Timeframe)
// -------------------------------------------------------------
// PaSell: แท่งปัจจุบันแดง + แท่งก่อนหน้าแดง + ปิดต่ำกว่าเนื้อแท่งก่อนหน้า
isCurrentRed = close < open
isPrevRed = close[1] < open[1]
paSellPattern = isCurrentRed and isPrevRed and (close < close[1])
// PaBuy: แท่งปัจจุบันเขียว + แท่งก่อนหน้าเขียว + ปิดสูงกว่าเนื้อแท่งก่อนหน้า
isCurrentGreen = close > open
isPrevGreen = close[1] > open[1]
paBuyPattern = isCurrentGreen and isPrevGreen and (close > close[1])
// -------------------------------------------------------------
// ENTRY CONDITIONS
// -------------------------------------------------------------
// Sell Condition: PaSell + ใต้ EMA11 + RSI < 45 + H1 ก่อนหน้าแดง + H4 ก่อนหน้าแดง
shortCondition = paSellPattern and (close < ema11) and (rsi14 < rsiSellLevel) and isH1Red and isH4Red
// Buy Condition: PaBuy + เหนือ EMA11 + RSI > 55 + H1 ก่อนหน้าเขียว + H4 ก่อนหน้าเขียว
longCondition = paBuyPattern and (close > ema11) and (rsi14 > rsiBuyLevel) and isH1Green and isH4Green
// -------------------------------------------------------------
// TRADING LOGIC
// -------------------------------------------------------------
if (longCondition)
longSL = math.min(low, low[1]) - (slBufferPips * syminfo.mintick)
longTP = close + ((close - longSL) * rrRatio)
strategy.entry("Long", strategy.long)
strategy.exit("Exit Long", "Long", stop=longSL, limit=longTP)
if (shortCondition)
shortSL = math.max(high, high[1]) + (slBufferPips * syminfo.mintick)
shortTP = close - ((shortSL - close) * rrRatio)
strategy.entry("Short", strategy.short)
strategy.exit("Exit Short", "Short", stop=shortSL, limit=shortTP)
// --- PLOTTING ---
plot(ema11, color=color.blue, linewidth=2, title="EMA 11")