该策略是一个结合了趋势跟踪和时间退出机制的量化交易策略。策略的核心是通过观察价格与60日移动平均线的关系来捕捉市场趋势,同时引入了年末强制平仓机制来控制风险。当收盘价突破60日均线且均线斜率为正时入场做多,并在每年最后一个交易日统一平仓出场。
策略主要基于以下几个核心要素: 1. 趋势判断:使用60日简单移动平均线(SMA)作为中期趋势的判断指标,通过计算14天均线斜率来确认趋势方向。 2. 入场信号:当价格向上突破60日均线且均线斜率为正时,表明市场可能进入上升趋势,此时产生买入信号。 3. 出场机制:策略采用固定的时间出场机制,在每年最后一个交易日平掉所有持仓。这种机制可以有效规避跨年度持仓风险。 4. 交易时间管理:策略内置了交易日期范围控制和交易日判断功能,确保只在有效的交易日进行操作。
该策略通过结合趋势跟踪和时间管理,构建了一个相对稳健的交易系统。策略逻辑简单明确,易于理解和执行,具有较好的实用性。通过合理的参数优化和风险控制措施的补充,该策略有望在实际交易中取得稳定收益。
/*backtest
start: 2025-01-09 00:00:00
end: 2025-01-16 00:00:00
period: 3m
basePeriod: 3m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":49999}]
*/
//@version=5
strategy("Buy above 60-day MA, Sell at year-end", overlay=true, pyramiding=1)
// Define inputs for start and end dates
startDate = input(defval=timestamp("2010-01-01"), title="Start Date")
endDate = input(defval=timestamp("2024-12-31"), title="End Date")
// Define 60-day moving average
length = input.int(defval=60, title="MA Length", minval=1)
ma = ta.sma(close, length)
slope = ta.sma(ma, 14) - ta.sma(ma, 14)[1]
// Check if current bar is within the specified date range
withinDateRange = true
// Function to check if a day is a trading day (Monday to Friday)
isTradingDay(day) => true
// Check if current bar is the last trading day of the year
// Check if current bar is the last trading day of the year
isLastTradingDayOfYear = false
yearNow = year(time)
if (month == 12 and dayofmonth == 31)
isLastTradingDayOfYear := isTradingDay(time)
else if (month == 12 and dayofmonth == 30)
isLastTradingDayOfYear := isTradingDay(time) and not isTradingDay(time + 86400000)
else if (month == 12 and dayofmonth == 29)
isLastTradingDayOfYear := isTradingDay(time) and not isTradingDay(time + 86400000) and not isTradingDay(time + 86400000 * 2)
// Plot moving average
plot(ma, color=color.blue, linewidth=2)
// Buy when closing price crosses above 60-day MA and up trend
if (withinDateRange and ta.crossover(close, ma) and slope > 0)
strategy.entry("Buy", strategy.long)
// Sell all positions at the last trading day of the year
if (isLastTradingDayOfYear)
strategy.close_all(comment="Sell at year-end")
// Plot buy and sell signals
//plotshape(series=ta.crossover(close, ma), location=location.belowbar, color=color.green, style=shape.labelup, text="Buy")
//plotshape(series=isLastTradingDayOfYear, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")