OPEN-SOURCE SCRIPT
Sonic R 89 - NY SL Custom Fixed

//version=5
indicator("Sonic R 89 - NY SL Custom Fixed", overlay=true, max_lines_count=500)
// --- 0. TÙY CHỈNH THÔNG SỐ ---
group_session = "Cài đặt Phiên Giao Dịch (Giờ New York)"
use_session = input.bool(true, "Chỉ giao dịch theo khung giờ", group=group_session)
session_time = input.session("0800-1200", "Khung giờ NY 1", group=group_session)
session_time2 = input.session("1300-1700", "Khung giờ NY 2", group=group_session)
max_trades_per_session = input.int(1, "Số lệnh tối đa/mỗi khung giờ", minval=1, group=group_session)
group_risk = "Quản lý Rủi ro (Dashboard)"
risk_usd = input.float(100.0, "Số tiền rủi ro mỗi lệnh ($)", minval=1.0, group=group_risk)
group_sl_custom = "Cấu hình Stop Loss (SL)"
sl_mode = input.string("Dragon", "Chế độ SL", options=["Dragon", "Swing H/L"], group=group_sl_custom)
lookback_x = input.int(5, "Số nến (X) cho Swing SL", minval=1, group=group_sl_custom)
group_htf = "Lọc Đa khung thời gian (MTF)"
htf_res = input.timeframe("30", "Chọn khung HTF", group=group_htf)
group_sonic = "Cấu hình Sonic R"
vol_mult = input.float(1.5, "Đột biến Volume", minval=1.0)
max_waves = input.int(4, "Ưu tiên n nhịp đầu", minval=1)
trade_cd = input.int(5, "Khoảng cách lệnh (nến)", minval=1)
group_tp = "Quản lý SL/TP & Dòng kẻ"
rr_tp1 = input.float(1.0, "TP1 (RR)", step=0.1)
rr_tp2 = input.float(2.0, "TP2 (RR)", step=0.1)
rr_tp3 = input.float(3.0, "TP3 (RR)", step=0.1)
rr_tp4 = input.float(4.0, "TP4 (RR)", step=0.1)
line_len = input.int(15, "Chiều dài dòng kẻ", minval=1)
// --- 1. KIỂM TRA PHIÊN & HTF ---
is_in_sess1 = not na(time(timeframe.period, session_time, "America/New_York"))
is_in_sess2 = not na(time(timeframe.period, session_time2, "America/New_York"))
is_in_session = use_session ? (is_in_sess1 or is_in_sess2) : true
var int trades_count = 0
is_new_session = is_in_session and not is_in_session[1]
if is_new_session
trades_count := 0
htf_open = request.security(syminfo.tickerid, htf_res, open, lookahead=barmerge.lookahead_on)
htf_close = request.security(syminfo.tickerid, htf_res, close, lookahead=barmerge.lookahead_on)
is_htf_trend = htf_close >= htf_open ? 1 : -1
// --- 2. TÍNH TOÁN CHỈ BÁO ---
ema89 = ta.ema(close, 89)
ema34H = ta.ema(high, 34)
ema34L = ta.ema(low, 34)
atr = ta.atr(14)
avgVol = ta.sma(volume, 20)
slope89 = (ema89 - ema89[5]) / atr
hasSlope = math.abs(slope89) > 0.12
isSqueezed = math.abs(ta.ema(close, 34) - ema89) < (atr * 0.5)
var int waveCount = 0
if not hasSlope
waveCount := 0
newWave = hasSlope and ((low <= ema34H and close > ema34H) or (high >= ema34L and close < ema34L))
if newWave and not newWave[1]
waveCount := waveCount + 1
// --- 3. LOGIC VÀO LỆNH ---
isMarubozu = math.abs(close - open) / (high - low) > 0.8
highVol = volume > avgVol * vol_mult
buyCondition = is_in_session and (trades_count < max_trades_per_session) and waveCount <= max_waves and is_htf_trend == 1 and
(isMarubozu or highVol) and close > ema34H and low >= ema89 and
(slope89 > 0.1 or isSqueezed[1]) and close > open
sellCondition = is_in_session and (trades_count < max_trades_per_session) and waveCount <= max_waves and is_htf_trend == -1 and
(isMarubozu or highVol) and close < ema34L and high <= ema89 and
(slope89 < -0.1 or isSqueezed[1]) and close < open
// --- 4. QUẢN LÝ LỆNH ---
var float last_entry = na
var float last_sl = na
var float last_tp1 = na
var float last_tp2 = na
var float last_tp3 = na
var float last_tp4 = na
var string last_type = "NONE"
var int lastBar = 0
trigger_buy = buyCondition and (bar_index - lastBar > trade_cd)
trigger_sell = sellCondition and (bar_index - lastBar > trade_cd)
// --- 5. TÍNH TOÁN SL & LOT SIZE ---
float contract_size = 1.0
if str.contains(syminfo.ticker, "XAU") or str.contains(syminfo.ticker, "GOLD")
contract_size := 100
// Logic tính SL linh hoạt
float swing_low = ta.lowest(low, lookback_x)
float swing_high = ta.highest(high, lookback_x)
float temp_sl_calc = na
if trigger_buy
temp_sl_calc := (sl_mode == "Dragon") ? ema34L : swing_low
if trigger_sell
temp_sl_calc := (sl_mode == "Dragon") ? ema34H : swing_high
float sl_dist_calc = math.abs(close - temp_sl_calc)
float calc_lots = (sl_dist_calc > 0) ? (risk_usd / (sl_dist_calc * contract_size)) : 0
if (trigger_buy or trigger_sell)
trades_count := trades_count + 1
lastBar := bar_index
last_type := trigger_buy ? "BUY" : "SELL"
last_entry := close
last_sl := temp_sl_calc
float riskAmt = math.abs(last_entry - last_sl)
last_tp1 := trigger_buy ? last_entry + (riskAmt * rr_tp1) : last_entry - (riskAmt * rr_tp1)
last_tp2 := trigger_buy ? last_entry + (riskAmt * rr_tp2) : last_entry - (riskAmt * rr_tp2)
last_tp3 := trigger_buy ? last_entry + (riskAmt * rr_tp3) : last_entry - (riskAmt * rr_tp3)
last_tp4 := trigger_buy ? last_entry + (riskAmt * rr_tp4) : last_entry - (riskAmt * rr_tp4)
// Vẽ dòng kẻ
line.new(bar_index, last_entry, bar_index + line_len, last_entry, color=color.new(color.gray, 50), width=2)
line.new(bar_index, last_sl, bar_index + line_len, last_sl, color=color.red, width=2, style=line.style_dashed)
line.new(bar_index, last_tp1, bar_index + line_len, last_tp1, color=color.green, width=1)
line.new(bar_index, last_tp2, bar_index + line_len, last_tp2, color=color.lime, width=1)
line.new(bar_index, last_tp3, bar_index + line_len, last_tp3, color=color.aqua, width=1)
line.new(bar_index, last_tp4, bar_index + line_len, last_tp4, color=color.blue, width=2)
// KÍCH HOẠT ALERT()
string alert_msg = (trigger_buy ? "BUY " : "SELL ") + syminfo.ticker + " at " + str.tostring(close) + " | SL Mode: " + sl_mode + " | Lot: " + str.tostring(calc_lots, "#.##") + " | SL: " + str.tostring(last_sl, format.mintick)
alert(alert_msg, alert.freq_once_per_bar_close)
// --- 6. CẢNH BÁO CỐ ĐỊNH ---
alertcondition(trigger_buy, title="Sonic R BUY Alert", message="Sonic R BUY Signal Detected")
alertcondition(trigger_sell, title="Sonic R SELL Alert", message="Sonic R SELL Signal Detected")
// --- 7. DASHBOARD & PLOT ---
var table sonic_table = table.new(position.top_right, 2, 10, bgcolor=color.new(color.black, 70), border_width=1, border_color=color.gray)
if barstate.islast
table.cell(sonic_table, 0, 0, "NY SESSION", text_color=color.white), table.cell(sonic_table, 1, 0, last_type, text_color=(last_type == "BUY" ? color.lime : color.red))
table.cell(sonic_table, 0, 1, "SL Mode:", text_color=color.white), table.cell(sonic_table, 1, 1, sl_mode, text_color=color.orange)
table.cell(sonic_table, 0, 2, "Trades this Sess:", text_color=color.white), table.cell(sonic_table, 1, 2, str.tostring(trades_count) + "/" + str.tostring(max_trades_per_session), text_color=color.yellow)
table.cell(sonic_table, 0, 3, "LOT SIZE:", text_color=color.orange), table.cell(sonic_table, 1, 3, str.tostring(calc_lots, "#.##"), text_color=color.orange)
table.cell(sonic_table, 0, 4, "Entry:", text_color=color.white), table.cell(sonic_table, 1, 4, str.tostring(last_entry, format.mintick), text_color=color.yellow)
table.cell(sonic_table, 0, 5, "SL:", text_color=color.white), table.cell(sonic_table, 1, 5, str.tostring(last_sl, format.mintick), text_color=color.red)
table.cell(sonic_table, 0, 6, "TP1:", text_color=color.gray), table.cell(sonic_table, 1, 6, str.tostring(last_tp1, format.mintick), text_color=color.green)
table.cell(sonic_table, 0, 7, "TP2:", text_color=color.gray), table.cell(sonic_table, 1, 7, str.tostring(last_tp2, format.mintick), text_color=color.lime)
table.cell(sonic_table, 0, 8, "TP3:", text_color=color.gray), table.cell(sonic_table, 1, 8, str.tostring(last_tp3, format.mintick), text_color=color.aqua)
table.cell(sonic_table, 0, 9, "TP4:", text_color=color.gray), table.cell(sonic_table, 1, 9, str.tostring(last_tp4, format.mintick), text_color=color.blue)
plot(ema89, color=slope89 > 0.1 ? color.lime : slope89 < -0.1 ? color.red : color.gray, linewidth=2)
p_high = plot(ema34H, color=color.new(color.blue, 80))
p_low = plot(ema34L, color=color.new(color.blue, 80))
fill(p_high, p_low, color=color.new(color.blue, 96))
plotshape(trigger_buy, "BUY", shape.triangleup, location.belowbar, color=color.green, size=size.small)
plotshape(trigger_sell, "SELL", shape.triangledown, location.abovebar, color=color.red, size=size.small)
bgcolor(isSqueezed ? color.new(color.yellow, 92) : na)
bgcolor(not is_in_session ? color.new(color.gray, 96) : na)
indicator("Sonic R 89 - NY SL Custom Fixed", overlay=true, max_lines_count=500)
// --- 0. TÙY CHỈNH THÔNG SỐ ---
group_session = "Cài đặt Phiên Giao Dịch (Giờ New York)"
use_session = input.bool(true, "Chỉ giao dịch theo khung giờ", group=group_session)
session_time = input.session("0800-1200", "Khung giờ NY 1", group=group_session)
session_time2 = input.session("1300-1700", "Khung giờ NY 2", group=group_session)
max_trades_per_session = input.int(1, "Số lệnh tối đa/mỗi khung giờ", minval=1, group=group_session)
group_risk = "Quản lý Rủi ro (Dashboard)"
risk_usd = input.float(100.0, "Số tiền rủi ro mỗi lệnh ($)", minval=1.0, group=group_risk)
group_sl_custom = "Cấu hình Stop Loss (SL)"
sl_mode = input.string("Dragon", "Chế độ SL", options=["Dragon", "Swing H/L"], group=group_sl_custom)
lookback_x = input.int(5, "Số nến (X) cho Swing SL", minval=1, group=group_sl_custom)
group_htf = "Lọc Đa khung thời gian (MTF)"
htf_res = input.timeframe("30", "Chọn khung HTF", group=group_htf)
group_sonic = "Cấu hình Sonic R"
vol_mult = input.float(1.5, "Đột biến Volume", minval=1.0)
max_waves = input.int(4, "Ưu tiên n nhịp đầu", minval=1)
trade_cd = input.int(5, "Khoảng cách lệnh (nến)", minval=1)
group_tp = "Quản lý SL/TP & Dòng kẻ"
rr_tp1 = input.float(1.0, "TP1 (RR)", step=0.1)
rr_tp2 = input.float(2.0, "TP2 (RR)", step=0.1)
rr_tp3 = input.float(3.0, "TP3 (RR)", step=0.1)
rr_tp4 = input.float(4.0, "TP4 (RR)", step=0.1)
line_len = input.int(15, "Chiều dài dòng kẻ", minval=1)
// --- 1. KIỂM TRA PHIÊN & HTF ---
is_in_sess1 = not na(time(timeframe.period, session_time, "America/New_York"))
is_in_sess2 = not na(time(timeframe.period, session_time2, "America/New_York"))
is_in_session = use_session ? (is_in_sess1 or is_in_sess2) : true
var int trades_count = 0
is_new_session = is_in_session and not is_in_session[1]
if is_new_session
trades_count := 0
htf_open = request.security(syminfo.tickerid, htf_res, open, lookahead=barmerge.lookahead_on)
htf_close = request.security(syminfo.tickerid, htf_res, close, lookahead=barmerge.lookahead_on)
is_htf_trend = htf_close >= htf_open ? 1 : -1
// --- 2. TÍNH TOÁN CHỈ BÁO ---
ema89 = ta.ema(close, 89)
ema34H = ta.ema(high, 34)
ema34L = ta.ema(low, 34)
atr = ta.atr(14)
avgVol = ta.sma(volume, 20)
slope89 = (ema89 - ema89[5]) / atr
hasSlope = math.abs(slope89) > 0.12
isSqueezed = math.abs(ta.ema(close, 34) - ema89) < (atr * 0.5)
var int waveCount = 0
if not hasSlope
waveCount := 0
newWave = hasSlope and ((low <= ema34H and close > ema34H) or (high >= ema34L and close < ema34L))
if newWave and not newWave[1]
waveCount := waveCount + 1
// --- 3. LOGIC VÀO LỆNH ---
isMarubozu = math.abs(close - open) / (high - low) > 0.8
highVol = volume > avgVol * vol_mult
buyCondition = is_in_session and (trades_count < max_trades_per_session) and waveCount <= max_waves and is_htf_trend == 1 and
(isMarubozu or highVol) and close > ema34H and low >= ema89 and
(slope89 > 0.1 or isSqueezed[1]) and close > open
sellCondition = is_in_session and (trades_count < max_trades_per_session) and waveCount <= max_waves and is_htf_trend == -1 and
(isMarubozu or highVol) and close < ema34L and high <= ema89 and
(slope89 < -0.1 or isSqueezed[1]) and close < open
// --- 4. QUẢN LÝ LỆNH ---
var float last_entry = na
var float last_sl = na
var float last_tp1 = na
var float last_tp2 = na
var float last_tp3 = na
var float last_tp4 = na
var string last_type = "NONE"
var int lastBar = 0
trigger_buy = buyCondition and (bar_index - lastBar > trade_cd)
trigger_sell = sellCondition and (bar_index - lastBar > trade_cd)
// --- 5. TÍNH TOÁN SL & LOT SIZE ---
float contract_size = 1.0
if str.contains(syminfo.ticker, "XAU") or str.contains(syminfo.ticker, "GOLD")
contract_size := 100
// Logic tính SL linh hoạt
float swing_low = ta.lowest(low, lookback_x)
float swing_high = ta.highest(high, lookback_x)
float temp_sl_calc = na
if trigger_buy
temp_sl_calc := (sl_mode == "Dragon") ? ema34L : swing_low
if trigger_sell
temp_sl_calc := (sl_mode == "Dragon") ? ema34H : swing_high
float sl_dist_calc = math.abs(close - temp_sl_calc)
float calc_lots = (sl_dist_calc > 0) ? (risk_usd / (sl_dist_calc * contract_size)) : 0
if (trigger_buy or trigger_sell)
trades_count := trades_count + 1
lastBar := bar_index
last_type := trigger_buy ? "BUY" : "SELL"
last_entry := close
last_sl := temp_sl_calc
float riskAmt = math.abs(last_entry - last_sl)
last_tp1 := trigger_buy ? last_entry + (riskAmt * rr_tp1) : last_entry - (riskAmt * rr_tp1)
last_tp2 := trigger_buy ? last_entry + (riskAmt * rr_tp2) : last_entry - (riskAmt * rr_tp2)
last_tp3 := trigger_buy ? last_entry + (riskAmt * rr_tp3) : last_entry - (riskAmt * rr_tp3)
last_tp4 := trigger_buy ? last_entry + (riskAmt * rr_tp4) : last_entry - (riskAmt * rr_tp4)
// Vẽ dòng kẻ
line.new(bar_index, last_entry, bar_index + line_len, last_entry, color=color.new(color.gray, 50), width=2)
line.new(bar_index, last_sl, bar_index + line_len, last_sl, color=color.red, width=2, style=line.style_dashed)
line.new(bar_index, last_tp1, bar_index + line_len, last_tp1, color=color.green, width=1)
line.new(bar_index, last_tp2, bar_index + line_len, last_tp2, color=color.lime, width=1)
line.new(bar_index, last_tp3, bar_index + line_len, last_tp3, color=color.aqua, width=1)
line.new(bar_index, last_tp4, bar_index + line_len, last_tp4, color=color.blue, width=2)
// KÍCH HOẠT ALERT()
string alert_msg = (trigger_buy ? "BUY " : "SELL ") + syminfo.ticker + " at " + str.tostring(close) + " | SL Mode: " + sl_mode + " | Lot: " + str.tostring(calc_lots, "#.##") + " | SL: " + str.tostring(last_sl, format.mintick)
alert(alert_msg, alert.freq_once_per_bar_close)
// --- 6. CẢNH BÁO CỐ ĐỊNH ---
alertcondition(trigger_buy, title="Sonic R BUY Alert", message="Sonic R BUY Signal Detected")
alertcondition(trigger_sell, title="Sonic R SELL Alert", message="Sonic R SELL Signal Detected")
// --- 7. DASHBOARD & PLOT ---
var table sonic_table = table.new(position.top_right, 2, 10, bgcolor=color.new(color.black, 70), border_width=1, border_color=color.gray)
if barstate.islast
table.cell(sonic_table, 0, 0, "NY SESSION", text_color=color.white), table.cell(sonic_table, 1, 0, last_type, text_color=(last_type == "BUY" ? color.lime : color.red))
table.cell(sonic_table, 0, 1, "SL Mode:", text_color=color.white), table.cell(sonic_table, 1, 1, sl_mode, text_color=color.orange)
table.cell(sonic_table, 0, 2, "Trades this Sess:", text_color=color.white), table.cell(sonic_table, 1, 2, str.tostring(trades_count) + "/" + str.tostring(max_trades_per_session), text_color=color.yellow)
table.cell(sonic_table, 0, 3, "LOT SIZE:", text_color=color.orange), table.cell(sonic_table, 1, 3, str.tostring(calc_lots, "#.##"), text_color=color.orange)
table.cell(sonic_table, 0, 4, "Entry:", text_color=color.white), table.cell(sonic_table, 1, 4, str.tostring(last_entry, format.mintick), text_color=color.yellow)
table.cell(sonic_table, 0, 5, "SL:", text_color=color.white), table.cell(sonic_table, 1, 5, str.tostring(last_sl, format.mintick), text_color=color.red)
table.cell(sonic_table, 0, 6, "TP1:", text_color=color.gray), table.cell(sonic_table, 1, 6, str.tostring(last_tp1, format.mintick), text_color=color.green)
table.cell(sonic_table, 0, 7, "TP2:", text_color=color.gray), table.cell(sonic_table, 1, 7, str.tostring(last_tp2, format.mintick), text_color=color.lime)
table.cell(sonic_table, 0, 8, "TP3:", text_color=color.gray), table.cell(sonic_table, 1, 8, str.tostring(last_tp3, format.mintick), text_color=color.aqua)
table.cell(sonic_table, 0, 9, "TP4:", text_color=color.gray), table.cell(sonic_table, 1, 9, str.tostring(last_tp4, format.mintick), text_color=color.blue)
plot(ema89, color=slope89 > 0.1 ? color.lime : slope89 < -0.1 ? color.red : color.gray, linewidth=2)
p_high = plot(ema34H, color=color.new(color.blue, 80))
p_low = plot(ema34L, color=color.new(color.blue, 80))
fill(p_high, p_low, color=color.new(color.blue, 96))
plotshape(trigger_buy, "BUY", shape.triangleup, location.belowbar, color=color.green, size=size.small)
plotshape(trigger_sell, "SELL", shape.triangledown, location.abovebar, color=color.red, size=size.small)
bgcolor(isSqueezed ? color.new(color.yellow, 92) : na)
bgcolor(not is_in_session ? color.new(color.gray, 96) : na)
Script open-source
Dans l'esprit TradingView, le créateur de ce script l'a rendu open source afin que les traders puissent examiner et vérifier ses fonctionnalités. Bravo à l'auteur! Bien que vous puissiez l'utiliser gratuitement, n'oubliez pas que la republication du code est soumise à nos Règles.
Clause de non-responsabilité
Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.
Script open-source
Dans l'esprit TradingView, le créateur de ce script l'a rendu open source afin que les traders puissent examiner et vérifier ses fonctionnalités. Bravo à l'auteur! Bien que vous puissiez l'utiliser gratuitement, n'oubliez pas que la republication du code est soumise à nos Règles.
Clause de non-responsabilité
Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.