
Đây là một chiến lược giao dịch định lượng kết hợp theo dõi xu hướng và phân tích kỹ thuật. Chiến lược này xác nhận tín hiệu giao dịch thông qua nhiều chỉ số kỹ thuật, sử dụng hệ thống quản lý vị trí dừng hàng loạt và động, nhằm mục đích nắm bắt xu hướng chính của thị trường và kiểm soát rủi ro. Chiến lược này tích hợp nhiều chỉ số kỹ thuật như EMA, MACD và RSI để xác định cơ hội giao dịch tiềm năng thông qua giao dịch chéo và lệch giữa các chỉ số.
Lập luận giao dịch cốt lõi của chiến lược dựa trên các yếu tố then chốt sau:
Chiến lược này xây dựng một hệ thống giao dịch tương đối hoàn hảo thông qua việc sử dụng kết hợp nhiều chỉ số kỹ thuật, kết hợp với việc quản lý vị trí dừng và vị trí động. Ưu điểm của chiến lược là kiểm soát rủi ro toàn diện, tín hiệu giao dịch có độ tin cậy cao, nhưng cũng có một nhược điểm có thể bỏ lỡ một số tình huống.
/*backtest
start: 2024-02-10 00:00:00
end: 2025-02-08 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("Hang Strategy Aggressive", overlay=true, initial_capital=1000, currency=currency.USDT, default_qty_type=strategy.cash, default_qty_value=100)
// === 参数设置 ===
fastLength = input.int(5, "快速EMA长度")
slowLength = input.int(15, "慢速EMA长度")
rsiLength = input.int(7, "RSI长度")
atrPeriod = input.int(10, "ATR周期")
leverageMultiple = input.float(3.0, "杠杆倍数", minval=1.0, step=0.5)
// === 止盈止损参数 ===
stopLossPercent = input.float(5.0, "止损百分比", minval=1.0, step=0.5)
firstTakeProfitPercent = input.float(8.0, "第一止盈点百分比", minval=1.0, step=0.5)
secondTakeProfitPercent = input.float(12.0, "第二止盈点百分比", minval=1.0, step=0.5)
firstTakeProfitQtyPercent = input.float(50.0, "第一止盈仓位百分比", minval=1.0, maxval=100.0, step=5.0)
// === 技术指标 ===
fastEMA = ta.ema(close, fastLength)
slowEMA = ta.ema(close, slowLength)
superFastEMA = ta.ema(close, 3)
rsi = ta.rsi(close, rsiLength)
atr = ta.atr(atrPeriod)
// === 趋势判断 ===
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
macdCross = (macdLine > signalLine) and (macdLine[1] < signalLine[1])
macdCrossDown = (macdLine < signalLine) and (macdLine[1] > signalLine[1])
// === 交易信号 ===
longCondition = (fastEMA > slowEMA) and macdCross and (rsi < 70)
shortCondition = (fastEMA < slowEMA) and macdCrossDown and (rsi > 30)
// === 平仓信号 ===
exitLong = shortCondition or (fastEMA < slowEMA)
exitShort = longCondition or (fastEMA > slowEMA)
// === 仓位管理 ===
maxRiskPerTrade = 0.05
basePosition = strategy.equity * maxRiskPerTrade
atrAmount = atr * close
riskPosition = basePosition / atrAmount * leverageMultiple
positionSize = math.min(riskPosition, strategy.equity * 0.4 / close)
// === 交易状态变量 ===
var isLong = false
var isShort = false
var partialTpTriggered = false
var float stopPrice = na
var float firstTpPrice = na
var float secondTpPrice = na
var float firstTpQty = na
// === 交易执行 ===
// 多头入场
if (longCondition and not isLong and not isShort)
strategy.entry("多", strategy.long, qty=positionSize)
isLong := true
partialTpTriggered := false
// 空头入场
if (shortCondition and not isShort and not isLong)
strategy.entry("空", strategy.short, qty=positionSize)
isShort := true
partialTpTriggered := false
// === 止盈止损逻辑 ===
if (strategy.position_size > 0) // 多仓
stopPrice := strategy.position_avg_price * (1 - stopLossPercent/100)
firstTpPrice := strategy.position_avg_price * (1 + firstTakeProfitPercent/100)
// 只在未触发第一止盈时计算第二止盈价格
if not partialTpTriggered
secondTpPrice := strategy.position_avg_price * (1 + secondTakeProfitPercent/100)
if (close[1] <= stopPrice or low <= stopPrice)
strategy.close_all("多止损")
isLong := false
partialTpTriggered := false
if (not partialTpTriggered and (close[1] >= firstTpPrice or high >= firstTpPrice))
strategy.order("多第一止盈", strategy.short, qty=firstTpQty)
partialTpTriggered := true
// 在这里重新计算第二止盈价格
secondTpPrice := high * (1 + 0.04) // 基于当前最高价再上涨4%
if (close[1] >= secondTpPrice or high >= secondTpPrice)
strategy.close_all("多第二止盈")
isLong := false
partialTpTriggered := false
if (strategy.position_size < 0) // 空仓
stopPrice := strategy.position_avg_price * (1 + stopLossPercent/100)
firstTpPrice := strategy.position_avg_price * (1 - firstTakeProfitPercent/100)
// 只在未触发第一止盈时计算第二止盈价格
if not partialTpTriggered
secondTpPrice := strategy.position_avg_price * (1 - secondTakeProfitPercent/100)
if (close[1] >= stopPrice or high >= stopPrice)
strategy.close_all("空止损")
isShort := false
partialTpTriggered := false
if (not partialTpTriggered and (close[1] <= firstTpPrice or low <= firstTpPrice))
strategy.order("空第一止盈", strategy.long, qty=firstTpQty)
partialTpTriggered := true
// 在这里重新计算第二止盈价格
secondTpPrice := low * (1 - 0.04) // 基于当前最低价再下跌4%
if (close[1] <= secondTpPrice or low <= secondTpPrice)
strategy.close_all("空第二止盈")
isShort := false
partialTpTriggered := false
// === 其他平仓条件 ===
if (exitLong and isLong)
strategy.close_all("多平仓")
isLong := false
partialTpTriggered := false
if (exitShort and isShort)
strategy.close_all("空平仓")
isShort := false
partialTpTriggered := false
// === 绘图 ===
plot(fastEMA, "快速EMA", color=color.blue)
plot(slowEMA, "慢速EMA", color=color.red)
plot(superFastEMA, "超快EMA", color=color.green)
// 绘制止盈止损线
plot(strategy.position_size != 0 ? strategy.position_avg_price : na, "开仓价", color=color.yellow)
plot(strategy.position_size != 0 ? stopPrice : na, "止损线", color=color.red)
plot(strategy.position_size != 0 ? firstTpPrice : na, "第一止盈线", color=color.green)
plot(strategy.position_size != 0 ? secondTpPrice : na, "第二止盈线", color=color.blue)