
Chiến lược này là một hệ thống giao dịch thông minh kết hợp MACD và LRS. Chiến lược này tối ưu hóa tính toán MACD thông qua sự kết hợp của nhiều phương pháp trung bình di chuyển và đưa ra phân tích hồi quy để tăng cường độ tin cậy của tín hiệu giao dịch. Chiến lược này cho phép các nhà giao dịch có thể lựa chọn linh hoạt sử dụng chỉ số đơn hoặc kết hợp hai chỉ số để tạo tín hiệu giao dịch và được trang bị cơ chế dừng lỗ để kiểm soát rủi ro.
Trọng tâm của chiến lược là nắm bắt xu hướng thị trường thông qua MACD được tối ưu hóa và chỉ số quay ngược tuyến tính. Phần MACD sử dụng sự kết hợp của bốn phương pháp tính toán trung bình di chuyển SMA, EMA, WMA và TEMA, tăng cường độ nhạy cảm với xu hướng giá. Phần quay ngược tuyến tính đánh giá hướng và cường độ của xu hướng bằng cách tính toán độ dốc và vị trí của đường quay ngược.
Chiến lược này tạo ra một hệ thống giao dịch linh hoạt và đáng tin cậy bằng cách kết hợp các phiên bản cải tiến của các chỉ số cổ điển và các phương pháp thống kê. Thiết kế mô-đun của nó cho phép các nhà giao dịch điều chỉnh các tham số chiến lược và cơ chế xác nhận tín hiệu một cách linh hoạt theo các môi trường thị trường khác nhau.
/*backtest
start: 2024-11-10 00:00:00
end: 2024-12-09 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy('SIMPLIFIED MACD & LRS Backtest by NHBProd', overlay=false)
// Function to calculate TEMA (Triple Exponential Moving Average)
tema(src, length) =>
ema1 = ta.ema(src, length)
ema2 = ta.ema(ema1, length)
ema3 = ta.ema(ema2, length)
3 * (ema1 - ema2) + ema3
// MACD Calculation Function
macdfx(src, fast_length, slow_length, signal_length, method) =>
fast_ma = method == 'SMA' ? ta.sma(src, fast_length) :
method == 'EMA' ? ta.ema(src, fast_length) :
method == 'WMA' ? ta.wma(src, fast_length) :
tema(src, fast_length)
slow_ma = method == 'SMA' ? ta.sma(src, slow_length) :
method == 'EMA' ? ta.ema(src, slow_length) :
method == 'WMA' ? ta.wma(src, slow_length) :
tema(src, slow_length)
macd = fast_ma - slow_ma
signal = method == 'SMA' ? ta.sma(macd, signal_length) :
method == 'EMA' ? ta.ema(macd, signal_length) :
method == 'WMA' ? ta.wma(macd, signal_length) :
tema(macd, signal_length)
hist = macd - signal
[macd, signal, hist]
// MACD Inputs
useMACD = input(true, title="Use MACD for Signals")
src = input(close, title="MACD Source")
fastp = input(12, title="MACD Fast Length")
slowp = input(26, title="MACD Slow Length")
signalp = input(9, title="MACD Signal Length")
macdMethod = input.string('EMA', title='MACD Method', options=['EMA', 'SMA', 'WMA', 'TEMA'])
// MACD Calculation
[macd, signal, hist] = macdfx(src, fastp, slowp, signalp, macdMethod)
// Linear Regression Inputs
useLR = input(true, title="Use Linear Regression for Signals")
lrLength = input(24, title="Linear Regression Length")
lrSource = input(close, title="Linear Regression Source")
lrSignalSelector = input.string('Rising Linear', title='Signal Selector', options=['Price Above Linear', 'Rising Linear', 'Both'])
// Linear Regression Calculation
linReg = ta.linreg(lrSource, lrLength, 0)
linRegPrev = ta.linreg(lrSource, lrLength, 1)
slope = linReg - linRegPrev
// Linear Regression Buy Signal
lrBuySignal = lrSignalSelector == 'Price Above Linear' ? (close > linReg) :
lrSignalSelector == 'Rising Linear' ? (slope > 0 and slope > slope[1]) :
lrSignalSelector == 'Both' ? (close > linReg and slope > 0) : false
// MACD Crossover Signals
macdCrossover = ta.crossover(macd, signal)
// Buy Signals based on user choices
macdSignal = useMACD and macdCrossover
lrSignal = useLR and lrBuySignal
// Buy condition: Use AND condition if both are selected, OR condition if only one is selected
buySignal = (useMACD and useLR) ? (macdSignal and lrSignal) : (macdSignal or lrSignal)
// Plot MACD
hline(0, title="Zero Line", color=color.gray)
plot(macd, color=color.blue, title="MACD Line", linewidth=2)
plot(signal, color=color.orange, title="Signal Line", linewidth=2)
plot(hist, color=hist >= 0 ? color.green : color.red, style=plot.style_columns, title="MACD Histogram")
// Plot Linear Regression Line and Slope
plot(slope, color=slope > 0 ? color.purple : color.red, title="Slope", linewidth=2)
plot(linReg,title="lingreg")
// Signal Plot for Visualization
plotshape(buySignal, style=shape.labelup, location=location.bottom, color=color.new(color.green, 0), title="Buy Signal", text="Buy")
// Sell Signals for Exiting Long Positions
macdCrossunder = ta.crossunder(macd, signal) // MACD Crossunder for Sell Signal
lrSellSignal = lrSignalSelector == 'Price Above Linear' ? (close < linReg) :
lrSignalSelector == 'Rising Linear' ? (slope < 0 and slope < slope[1]) :
lrSignalSelector == 'Both' ? (close < linReg and slope < 0) : false
// User Input for Exit Signals: Select indicators to use for exiting trades
useMACDSell = input(true, title="Use MACD for Exit Signals")
useLRSell = input(true, title="Use Linear Regression for Exit Signals")
// Sell condition: Use AND condition if both are selected to trigger a sell at the same time, OR condition if only one is selected
sellSignal = (useMACDSell and useLRSell) ? (macdCrossunder and lrSellSignal) :
(useMACDSell ? macdCrossunder : false) or
(useLRSell ? lrSellSignal : false)
// Plot Sell Signals for Visualization (for exits, not short trades)
plotshape(sellSignal, style=shape.labeldown, location=location.top, color=color.new(color.red, 0), title="Sell Signal", text="Sell")
// Alerts
alertcondition(buySignal, title="Buy Signal", message="Buy signal detected!")
alertcondition(sellSignal, title="Sell Signal", message="Sell signal detected!")
// Take Profit and Stop Loss Inputs
takeProfit = input.float(10.0, title="Take Profit (%)") // Take Profit in percentage
stopLoss = input.float(0.10, title="Stop Loss (%)") // Stop Loss in percentage
// Backtest Date Range
startDate = input(timestamp("2024-01-01 00:00"), title="Start Date")
endDate = input(timestamp("2025-12-12 00:00"), title="End Date")
inBacktestPeriod = true
// Entry Rules (Only Long Entries)
if (buySignal and inBacktestPeriod)
strategy.entry("Buy", strategy.long)
// Exit Rules (Only for Long Positions)
strategy.exit("Exit Buy", from_entry="Buy", limit=close * (1 + takeProfit / 100), stop=close * (1 - stopLoss / 100))
// Exit Long Position Based on Sell Signals
if (sellSignal and inBacktestPeriod)
strategy.close("Buy", comment="Exit Signal")