该策略是一个基于双重时间框架的动态支撑位交易系统,通过在周线和日线时间框架上结合SMA和EMA均线的交叉信号来进行交易。系统利用了均线之间形成的支撑带来识别市场趋势和交易机会,通过两个不同时间周期的信号确认来提高交易的准确性。策略采用了百分比仓位管理方式,并考虑了交易成本和滑点因素。
策略的核心原理是通过监测两个时间周期内均线的交叉和位置关系来确定交易信号: 1. 长周期(周线)使用20周SMA和21周EMA,短周期(日线)使用50日SMA和51日EMA 2. 在长周期中,当EMA向上穿越SMA时产生做多信号,向下穿越时产生平仓信号 3. 在短周期中,当EMA向上穿越SMA且短周期EMA位于长周期EMA之上时产生做多信号 4. 当短周期出现做空信号或长周期均线交叉向下时,系统会平掉所有多单 5. 策略在指定的时间范围内运行,超出范围自动平仓
该策略通过结合不同时间周期的均线交叉信号来构建一个相对稳健的交易系统。通过支撑带的概念来识别市场趋势,利用多重确认机制来提高交易的准确性。策略的设计考虑了实际交易中的各种因素,包括交易成本、滑点和时间管理等。虽然存在一些固有的风险,但通过提供的优化方向可以进一步提升策略的稳定性和盈利能力。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-04 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Demo GPT - Bull Market Support Band", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.1, slippage=3)
start_date = input(timestamp("2018-01-01 00:00 +0000"), title="Start Date")
end_date = input(timestamp("2069-12-31 00:00 +0000"), title="End Date")
lsmaLength = input.int(20, title="Long SMA Length", minval=1)
lemaLength = input.int(21, title="Long EMA Length", minval=1)
customLongTimeframe = input.timeframe("W", title="Long Timeframe") // Khung thời gian dài
ssmaLength = input.int(50, title="Short SMA Length", minval=1)
semaLength = input.int(51, title="Short EMA Length", minval=1)
customShortTimeframe = input.timeframe("D", title="Short Timeframe") // Khung thời gian ngắn
source = close
// Tính toán SMA và EMA cho khung thời gian dài
smaLong = ta.sma(source, lsmaLength)
emaLong = ta.ema(source, lemaLength)
outSmaLong = request.security(syminfo.tickerid, customLongTimeframe, smaLong)
outEmaLong = request.security(syminfo.tickerid, customLongTimeframe, emaLong)
// Tính toán SMA và EMA cho khung thời gian ngắn
smaShort = ta.sma(source, ssmaLength)
emaShort = ta.ema(source, semaLength)
outSmaShort = request.security(syminfo.tickerid, customShortTimeframe, smaShort)
outEmaShort = request.security(syminfo.tickerid, customShortTimeframe, emaShort)
// Plot các chỉ báo trên biểu đồ
smaPlotLong = plot(outSmaLong, color=color.new(color.red, 0), title='20w SMA (Long)')
emaPlotLong = plot(outEmaLong, color=color.new(color.green, 0), title='21w EMA (Long)')
smaPlotShort = plot(outSmaShort, color=color.new(color.red, 0), title='20d SMA (Short)')
emaPlotShort = plot(outEmaShort, color=color.new(color.green, 0), title='21d EMA (Short)')
// Fill vùng giữa các đường SMA và EMA
fill(smaPlotLong, emaPlotLong, color=color.new(color.orange, 75), fillgaps=true)
fill(smaPlotShort, emaPlotShort, color=color.new(color.orange, 75), fillgaps=true)
// Điều kiện long và short cho khung thời gian dài
longConditionLong = ta.crossover(outEmaLong, outSmaLong)
shortConditionLong = ta.crossunder(outEmaLong, outSmaLong)
// Điều kiện long và short cho khung thời gian ngắn
longConditionShort = ta.crossover(outEmaShort, outSmaShort) and (outEmaShort > outEmaLong)
shortConditionShort = ta.crossunder(outEmaShort, outSmaShort) and (outEmaShort > outEmaLong) // Điều kiện short khi EMA ngắn hạn cắt xuống dưới SMA ngắn hạn và EMA ngắn hạn cao hơn EMA dài hạn
// Kiểm tra điều kiện trong khoảng thời gian được chỉ định
inDateRange = true
// Nếu khung ngắn hạn xuất hiện tín hiệu short, ưu tiên đóng tất cả các lệnh Long
if shortConditionShort and inDateRange
strategy.close_all()
// Nếu khung dài có tín hiệu short, đóng tất cả các lệnh Long
if shortConditionLong and inDateRange
strategy.close_all()
// Nếu khung ngắn hạn có tín hiệu long và không có tín hiệu short từ khung dài, vào lệnh Long
if longConditionShort and not shortConditionLong and not shortConditionShort and inDateRange
strategy.entry("Long", strategy.long)
// Đóng tất cả các lệnh khi không trong khoảng thời gian được chọn
if not inDateRange
strategy.close_all()