
이 전략은 돈치안 채널 (Donchian Channel) 과 평균 실제 파도 (ATR) 를 기반으로 한 트렌드 추적 거래 시스템이다. 이 전략은 4시간 K 라인 사이클의 돈치안 채널 중간 궤도와 현재 가격의 편차 정도를 활용하고, ATR을 동적 변동의 측정 지표로 결합하여 시장 변동에서 입점과 출구를 찾는다. 이 전략은梯度加仓 및 중지 메커니즘을 채택하고, 고정 거래 금액 (USDT) 을 통해 포지션 관리를 하며, 큰 변동 상황에서의 효과적인 자금 사용을 실현한다.
전략의 핵심 논리는 다음과 같은 핵심 요소에 기반합니다.
다중 주기 동치안 통로와 ATR 동적 간격 변동 추적 거래 전략은 기술 분석과 위험 관리를 결합한 정량 거래 시스템이다. 전략은 1분 K 라인에서 4 시간 주기 데이터를 사용하여 결정을 수행함으로써 중기 경향을 효과적으로 추적하고 ATR을 동적으로 사용하여 다른 시장 환경에 맞게 거래 간격을 조정한다. 고정 금액 거래와 계단 창고 구축 메커니즘은 위험을 제어하고 비용을 평형화하는 데 도움이됩니다.
이 전략은 특히 변동성이 높은 시장 환경에 적합하지만, 트렌드 반전 위험과 자금 관리 문제에 주의를 기울여야 합니다. 시장 환경 필터, 동적 파라미터 조정 및 중지 장치와 같은 최적화 조치를 추가함으로써 전략의 안정성과 장기적인 수익성을 더욱 향상시킬 수 있습니다. 실제 적용에서 충분한 재검토를 수행하고 특정 거래 품종에 대한 파라미터 최적화를 수행하고 자금을 안전하게 보장하기 위해 엄격한 위험 제어 조치를 시행하는 것이 좋습니다.
/*backtest
start: 2024-04-02 00:00:00
end: 2025-04-01 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BNB_USDT"}]
*/
//@version=5
strategy("Donchian Channel and ATR Strategy", overlay=true, currency="USDT", commission_type=strategy.commission.percent, commission_value=0.1)
// 用pine编写策略,实时执行。
// 获得suiusdt合约的4小时K线,用4小时k线20周期,计算唐奇安通道 和ATR。
// 最新的 ATR * 2 为间隔。每次买卖金额都是5.1usdt,根据金额计算交易数量。
// 策略基于1分钟k线执行。
// 开始时,baseprice为空。
// 如果 唐奇安通道中轨 - 当前价格 > 间隔, 则市价买入5.1usdt,记录成交价格为 baseprice。
// 重新计算计算唐奇安通道 和ATR, 还是使用4小时K线的20个周期。
// baseprice不为空,
// 如果 baseprice - 当前价格 > 间隔, 则市价买入5.1usdt,记录成交价格为 baseprice;
// 如果 当前价格 - baseprice > 间隔, 则市价卖出5.1usdt,记录成交价格为 baseprice。
// 卖出时,判断仓位是否足够卖出,不够则卖出剩余的仓位。卖出后,如果没有仓位了,设置baseprice为空。
// 标签和日志记录:买还是卖(buy/sell),初次买入标记为initBuy,价格,间隔,买入的数量,买入后仓位的总数量。
// 用不同颜色区分买卖。
// 获取4小时K线数据
resolution_4h = "240" // 4小时K线
high_4h = request.security(syminfo.tickerid, resolution_4h, high)
low_4h = request.security(syminfo.tickerid, resolution_4h, low)
close_4h = request.security(syminfo.tickerid, resolution_4h, close)
// 计算唐奇安通道和ATR(基于4小时K线)
length = 20
donchian_upper = ta.highest(high_4h, length)
donchian_lower = ta.lowest(low_4h, length)
donchian_middle = ta.sma(close_4h, length)
atr_value = ta.atr(length) // 使用4小时K线计算ATR
// 设置交易参数
trade_amount = 5.1 // 每次交易金额(USDT)
var float baseprice = na // 当前基准价格
var float total_position_qty = 0 // 总仓位数量
// 日志记录函数
log_trade(action, price, interval, qty, total_qty, color) =>
log_text = str.format("{0} @ {1} | Interval: {2} | Qty: {3} | Total Qty: {4}",
action, str.tostring(price), str.tostring(interval),
str.tostring(qty), str.tostring(total_qty))
// 策略逻辑
interval = atr_value * 2 // 间隔 = ATR * 2
if (na(baseprice))
if (donchian_middle - close > interval) // 使用1分钟K线的close价格判断
qty = trade_amount / close // 计算交易数量
strategy.entry("Buy", strategy.long, qty=qty)
baseprice := close // 更新基准价格
total_position_qty += qty // 更新总仓位数量
log_trade("initBuy", baseprice, interval, qty, total_position_qty, color.green) // 记录日志和标签
else
if (baseprice - close > interval) // 使用1分钟K线的close价格判断
qty = trade_amount / close
strategy.entry("Buy", strategy.long, qty=qty)
baseprice := close
total_position_qty += qty
log_trade("Buy", baseprice, interval, qty, total_position_qty, color.blue)
else if (close - baseprice > interval)
if (total_position_qty > 0)
qty = trade_amount / close
if (total_position_qty >= qty)
strategy.close("Buy", qty=qty)
total_position_qty -= qty
log_trade("Sell", baseprice, interval, qty, total_position_qty, color.red)
else
strategy.close("Buy")
total_position_qty := 0
log_trade("Sell (Full Position)", baseprice, interval, total_position_qty, 0, color.red)
baseprice := na // 清空基准价格
// 绘制唐奇安通道和ATR(基于4小时K线)
plot(donchian_upper, title="Donchian Upper", color=color.blue, linewidth=2)
plot(donchian_middle, title="Donchian Middle", color=color.orange, linewidth=2)
plot(donchian_lower, title="Donchian Lower", color=color.blue, linewidth=2)
plot(atr_value, title="ATR", color=color.red, linewidth=2)