
Chiến lược này là một hệ thống giao dịch theo dõi xu hướng kết hợp nhiều chỉ số kỹ thuật. Nó giao dịch khi xu hướng thị trường rõ ràng bằng cách tích hợp nhiều chỉ số kỹ thuật như RSI (chỉ số tương đối mạnh), MACD (chỉ số trung bình di chuyển) và SMA (chỉ số di chuyển đơn giản). Chiến lược cũng bao gồm các cơ chế quản lý rủi ro như dừng, dừng lỗ và theo dõi dừng lỗ để quản lý tài chính tốt hơn.
Chiến lược này được sử dụng để giao dịch dựa trên các điều kiện cốt lõi sau:
Khi các điều kiện trên được đáp ứng cùng một lúc, hệ thống sẽ phát ra nhiều tín hiệu. Trong khi đó, chiến lược đặt mục tiêu dừng lại 5%, giới hạn dừng lỗ 3%, và dừng theo dõi 2% để bảo vệ lợi nhuận đã đạt được. Thiết kế điều kiện giao dịch nhiều tầng này giúp tăng độ chính xác và an toàn của giao dịch.
Chiến lược này tạo ra một hệ thống giao dịch tương đối hoàn hảo bằng cách sử dụng kết hợp nhiều chỉ số kỹ thuật. Nó không chỉ chứa các logic cốt lõi để theo dõi xu hướng mà còn tích hợp các cân nhắc quản lý rủi ro. Mặc dù có một số nơi cần được tối ưu hóa, nhưng khung tổng thể có khả năng mở rộng và thích ứng tốt. Việc sử dụng chiến lược thành công đòi hỏi các nhà giao dịch tối ưu hóa tham số và cải tiến chiến lược theo tình hình thị trường thực tế.
/*backtest
start: 2024-12-03 00:00:00
end: 2024-12-10 00:00:00
period: 45m
basePeriod: 45m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Flexible Swing Trading Strategy with Trailing Stop and Date Range", overlay=true)
// Input parameters
rsiPeriod = input.int(14, title="RSI Period")
macdFastLength = input.int(12, title="MACD Fast Length")
macdSlowLength = input.int(26, title="MACD Slow Length")
macdSignalSmoothing = input.int(9, title="MACD Signal Smoothing")
smaShortPeriod = input.int(20, title="Short-term SMA Period")
smaLongPeriod = input.int(50, title="Long-term SMA Period")
takeProfitPercent = input.float(5.0, title="Take Profit Percentage")
stopLossPercent = input.float(3.0, title="Stop Loss Percentage")
trailingStopPercent = input.float(2.0, title="Trailing Stop Percentage")
// Date range inputs
startDate = input(timestamp("2023-01-01 00:00"), title="Start Date")
endDate = input(timestamp("2023-12-31 23:59"), title="End Date")
// Calculate RSI
rsi = ta.rsi(close, rsiPeriod)
// Calculate MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalSmoothing)
// Calculate SMAs
smaShort = ta.sma(close, smaShortPeriod)
smaLong = ta.sma(close, smaLongPeriod)
// Buy condition
buyCondition = ta.crossover(macdLine, signalLine) and rsi < 70 and close > smaShort and smaShort > smaLong
// Execute buy orders within the date range
if (buyCondition )
strategy.entry("Buy", strategy.long)
// Calculate take profit and stop loss levels
takeProfitLevel = strategy.position_avg_price * (1 + takeProfitPercent / 100)
stopLossLevel = strategy.position_avg_price * (1 - stopLossPercent / 100)
// Set take profit, stop loss, and trailing stop
strategy.exit("Take Profit", "Buy", limit=takeProfitLevel)
strategy.exit("Stop Loss", "Buy", stop=stopLossLevel)
strategy.exit("Trailing Stop", "Buy", trail_price=close * (1 - trailingStopPercent / 100), trail_offset=trailingStopPercent / 100)
// Plot Buy signals
plotshape(series=buyCondition, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
// Plot SMAs
plot(smaShort, color=color.blue, title="20 SMA")
plot(smaLong, color=color.red, title="50 SMA")
// Plot MACD and Signal Line
plot(macdLine, color=color.blue, title="MACD Line")
plot(signalLine, color=color.orange, title="Signal Line")
// Plot RSI
hline(70, "Overbought", color=color.red)
hline(30, "Oversold", color=color.green)
plot(rsi, color=color.purple, title="RSI")
// Debugging plots
plotchar(buyCondition , char='B', location=location.belowbar, color=color.green, size=size.small)
plotchar(strategy.opentrades > 0, char='T', location=location.abovebar, color=color.blue, size=size.small)
plot(stopLossLevel, color=color.red, title="Stop Loss Level")
plot(takeProfitLevel, color=color.green, title="Take Profit Level")