该策略是一个结合了传统技术分析和现代人工智能方法的趋势跟踪系统。它主要利用指数移动平均线(EMA)和简单移动平均线(SMA)作为趋势过滤器,同时引入预测模型来优化入场时机。策略专门针对日线级别进行了优化,旨在捕捉中长期市场趋势。
策略的核心逻辑包含三个主要组成部分: 1. 趋势判断系统 - 使用200周期的EMA和SMA作为主要趋势过滤器,通过价格与均线的位置关系判断当前趋势方向 2. 预测模块 - 采用可扩展的预测组件,目前使用模拟预测,后续可替换为机器学习模型 3. 仓位管理 - 设定固定的4根K线持仓周期,用于控制持仓时间和风险
交易信号的产生需要同时满足趋势方向和预测信号的一致性,即: - 多头信号:价格位于EMA和SMA之上,且预测值为正 - 空头信号:价格位于EMA和SMA之下,且预测值为负
该策略通过结合传统技术分析和现代预测方法,构建了一个稳健的趋势跟踪系统。其主要优势在于逻辑清晰、风险可控和较强的可扩展性。通过策略优化,特别是在预测模型和风险控制方面的改进,有望进一步提升策略的稳定性和盈利能力。策略适合追求中长期稳定收益的投资者使用。
/*backtest
start: 2024-02-21 00:00:00
end: 2025-02-18 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("My Strategy", overlay=true)
// Parameters (adjust as needed)
neighborsCount = 8
maxBarsBack = 2000
featureCount = 5
useDynamicExits = true
useEmaFilter = true
emaPeriod = 200
useSmaFilter = true
smaPeriod = 200
// Moving Average Calculations
ema = ta.ema(close, emaPeriod)
sma = ta.sma(close, smaPeriod)
// Trend Conditions
isEmaUptrend = close > ema
isEmaDowntrend = close < ema
isSmaUptrend = close > sma
isSmaDowntrend = close < sma
// Model Prediction (Replace with your real model)
// Here a simulation is used, replace it with real predictions
prediction = math.random() * 2 - 1 // Random value between -1 and 1
// Entry Signals
isNewBuySignal = prediction > 0 and isEmaUptrend and isSmaUptrend
isNewSellSignal = prediction < 0 and isEmaDowntrend and isSmaDowntrend
// Exit Signals
var int barsHeld = 0
var bool in_position = false
var int entry_bar = 0
if isNewBuySignal and not in_position
in_position := true
entry_bar := bar_index
barsHeld := 1
else if isNewSellSignal and not in_position
in_position := true
entry_bar := bar_index
barsHeld := 1
else if in_position
barsHeld := barsHeld + 1
if barsHeld == 4
in_position := false
endLongTradeStrict = barsHeld == 4 and isNewBuySignal[1]
endShortTradeStrict = barsHeld == 4 and isNewSellSignal[1]
// Backtest Logic
var float totalProfit = 0
var float entryPrice = na
var int tradeDirection = 0
if isNewBuySignal and tradeDirection <= 0
entryPrice := close
tradeDirection := 1
strategy.entry("Long", strategy.long)
if isNewSellSignal and tradeDirection >= 0
entryPrice := close
tradeDirection := -1
strategy.entry("Short", strategy.short)
if (endLongTradeStrict and tradeDirection == 1) or (endShortTradeStrict and tradeDirection == -1)
exitPrice = close
profit = (exitPrice - entryPrice) / entryPrice
if tradeDirection == -1
profit := (entryPrice - exitPrice) / entryPrice
totalProfit := totalProfit + profit
tradeDirection := 0
strategy.close_all()
plot(close, color=color.blue)
plot(ema, color=color.orange)
plot(sma, color=color.purple)