
Chiến lược này là một hệ thống theo dõi xu hướng thích ứng dựa trên việc lọc nhiều chỉ báo kỹ thuật. Nó kết hợp nhiều chỉ báo kỹ thuật như đường trung bình động hàm mũ (EMA), đường trung bình động đơn giản (SMA) và đường trung bình động hội tụ phân kỳ (MACD), và điều chỉnh các thông số một cách linh hoạt để thích ứng với các môi trường thị trường khác nhau nhằm nắm bắt xu hướng hiệu quả và kiểm soát rủi ro. Chiến lược này áp dụng cơ chế lọc theo lớp và cải thiện đáng kể độ tin cậy của tín hiệu giao dịch thông qua sự phối hợp của nhiều chỉ báo kỹ thuật.
Logic cốt lõi của chiến lược này dựa trên cơ chế lọc ba lớp:
Việc tạo ra tín hiệu giao dịch đòi hỏi phải đáp ứng tất cả các điều kiện lọc: thay đổi xu hướng, xác nhận hướng SMA và hỗ trợ đường tín hiệu MACD. Chiến lược này cũng bao gồm hệ thống quản lý vị thế năng động dựa trên vốn chủ sở hữu tài khoản, tự động điều chỉnh quy mô vị thế thông qua các yếu tố đòn bẩy.
Chiến lược này đạt được hiệu quả theo dõi xu hướng đáng tin cậy hơn thông qua cơ chế lọc nhiều lớp và điều chỉnh tham số động. Mặc dù có một số rủi ro về độ trễ và phụ thuộc vào tham số, hiệu suất ổn định vẫn có thể đạt được trong các giao dịch thực tế thông qua các biện pháp tối ưu hóa tham số và kiểm soát rủi ro hợp lý. Nhà giao dịch được khuyến nghị nên tiến hành kiểm tra ngược đầy đủ trước khi sử dụng theo thời gian thực và điều chỉnh cài đặt thông số theo khả năng chịu rủi ro cá nhân của họ.
/*backtest
start: 2024-12-29 00:00:00
end: 2025-01-05 00:00:00
period: 45m
basePeriod: 45m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("Adaptive Trend Flow Strategy with Filters for SPX", overlay=true, max_labels_count=500,
initial_capital=1000, commission_type=strategy.commission.cash_per_order, commission_value=0.01, slippage=2,
margin_long=20, margin_short=20, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// User-defined inputs for trend logic
atr = input.int(14, "Main Length", minval=2, group = "Find more strategies like this on pineindicators.com")
length = input.int(2, "Main Length", minval=2)
smooth_len = input.int(2, "Smoothing Length", minval=2)
sensitivity = input.float(2.0, "Sensitivity", step=0.1)
// User-defined inputs for SMA filter
use_sma_filter = input.bool(true, "Enable SMA Filter?")
sma_length = input.int(4, "SMA Length", minval=1)
// User-defined inputs for MACD filter
use_macd_filter = input.bool(true, "Enable MACD Filter?")
macd_fast_length = input.int(2, "MACD Fast Length", minval=1)
macd_slow_length = input.int(7, "MACD Slow Length", minval=1)
macd_signal_length = input.int(2, "MACD Signal Length", minval=1)
// User-defined inputs for leverage
leverage_factor = input.float(4.5, "Leverage Factor", minval=1.0, step=0.1)
id = input("besttrader123", title= "Your TradingView username", group = "Automate this strategy with plugpine.com")
key = input("nc739ja84gf", title= "Unique identifier (UID)")
ticker = input("SPX", title= "Ticker/symbol of your broker")
bullcolor = #0097a7
bearcolor = #ff195f
showbars = input.bool(true, "Color Bars?")
showbg = input.bool(true, "Background Color?")
showsignals = input.bool(true, "Show Signals?")
// Trend calculation functions
calculate_trend_levels() =>
typical = hlc3
fast_ema = ta.ema(typical, length)
slow_ema = ta.ema(typical, length * 2)
basis = (fast_ema + slow_ema) / 2
vol = ta.stdev(typical, length)
smooth_vol = ta.ema(vol, smooth_len)
upper = basis + (smooth_vol * sensitivity)
lower = basis - (smooth_vol * sensitivity)
[basis, upper, lower]
get_trend_state(upper, lower, basis) =>
var float prev_level = na
var int trend = 0
if na(prev_level)
trend := close > basis ? 1 : -1
prev_level := trend == 1 ? lower : upper
if trend == 1
if close < lower
trend := -1
prev_level := upper
else
prev_level := lower
else
if close > upper
trend := 1
prev_level := lower
else
prev_level := upper
[trend, prev_level]
[basis, upper, lower] = calculate_trend_levels()
[trend, level] = get_trend_state(upper, lower, basis)
// SMA filter
sma_value = ta.sma(close, sma_length)
sma_condition = use_sma_filter ? close > sma_value : true
// MACD filter
[macd_line, signal_line, _] = ta.macd(close, macd_fast_length, macd_slow_length, macd_signal_length)
macd_condition = use_macd_filter ? macd_line > signal_line : true
// Signal detection with filters
long_signal = trend == 1 and trend[1] == -1 and sma_condition and macd_condition
short_signal = trend == -1 and trend[1] == 1
// Plotting visuals
p2 = plot(basis, color=trend == 1 ? bullcolor : bearcolor, linewidth=2)
p1 = plot(level, color=close > level ? bullcolor : bearcolor, linewidth=2, style=plot.style_linebr)
// if showsignals and ta.crossover(close, level)
// label.new(bar_index, level, "▲", color=bullcolor, textcolor=chart.bg_color, style=label.style_label_upper_right)
// if showsignals and ta.crossunder(close, level)
// label.new(bar_index, level, "▼", color=bearcolor, textcolor=chart.fg_color, style=label.style_label_lower_right)
qty = strategy.equity / close * leverage_factor
// Automated alerts
if long_signal
alert('{"AccountID": "' + id + '","Key": "' + key + '", "symbol": "' + ticker + '", "action": "long", "volume": ' + str.tostring(qty) + '}', alert.freq_once_per_bar)
if short_signal
alert('{"AccountID": "' + id + '","Key": "' + key + '", "symbol": "' + ticker + '", "action": "closelong"}', alert.freq_once_per_bar)
// Strategy entries and exits
if long_signal
strategy.entry("Long", strategy.long, qty=qty)
if short_signal
strategy.close("Long")
// Optional SMA and MACD plot
plot(use_sma_filter ? sma_value : na, color=color.new(color.blue, 80), title="SMA")
plot(use_macd_filter ? macd_line : na, color=color.new(color.orange, 80), title="MACD Line")
plot(use_macd_filter ? signal_line : na, color=color.new(color.red, 80), title="Signal Line")