//@version=5
strategy(title="ETH 1HR Optimized", overlay=true, initial_capital=100,
pyramiding=1,
calc_on_order_fills=false, calc_on_every_tick=false,
default_qty_type=strategy.percent_of_equity,
default_qty_value=50, commission_value=0.04) // 恢复 50% 仓位
// 输入参数
Position = input.string("BOTH", "POSITIONS", options=["BOTH", "LONG", "SHORT"])
src = hlc3, src2 = hl2
is_Long = Position == "SHORT" ? false : true
is_Short = Position == "LONG" ? false : true
// T3
T3_len = input.int(8, "T3 LENGTH", minval=2) // 增加周期
a1 = input.float(0.5, "T3 VOLUME FACTOR", step=0.1, minval=0.1) // 平滑
T3(_src, _T3_len, _a1) =>
e1 = ta.ema(_src, _T3_len)
e2 = ta.ema(e1, _T3_len)
e3 = ta.ema(e2, _T3_len)
e4 = ta.ema(e3, _T3_len)
e5 = ta.ema(e4, _T3_len)
e6 = ta.ema(e5, _T3_len)
c1 = -_a1*_a1*_a1
c2 = 3*_a1*_a1 + 3*_a1*_a1*_a1
c3 = -6*_a1*_a1 - 3*_a1 - 3*_a1*_a1*_a1
c4 = 1 + 3*_a1 + _a1*_a1*_a1 + 3*_a1*_a1
c1*e6 + c2*e5 + c3*e4 + c4*e3
T3_Rising = T3(src, T3_len, a1) > T3(src, T3_len, a1)[1]
T3_Falling = T3(src, T3_len, a1) < T3(src, T3_len, a1)[1]
T3_Rising2 = T3(src2, T3_len, a1) > T3(src2, T3_len, a1)[1]
T3_Falling2 = T3(src2, T3_len, a1) < T3(src2, T3_len, a1)[1]
// Range Filter
per = input.int(25, "SAMPLING PERIOD", minval=1) // 增加周期
mult = input.float(2.2, "RANGE MULTIPLIER", minval=0.1, step=0.1) // 提高阈值
Range_filter(_src, _per, _mult) =>
var float upward = 0.0
var float downward = 0.0
wper = (_per*2) - 1
avrng = ta.ema(math.abs(_src - _src[1]), _per)
smoothrng = ta.ema(avrng, wper) * _mult
var float local_filt = na
local_filt := na(local_filt[1]) ? _src : _src > nz(local_filt[1]) ?
math.max(_src - smoothrng, nz(local_filt[1])) : math.min(_src + smoothrng,
nz(local_filt[1]))
upward := local_filt > local_filt[1] ? nz(upward[1]) + 1 : local_filt <
local_filt[1] ? 0 : nz(upward[1])
downward := local_filt < local_filt[1] ? nz(downward[1]) + 1 : local_filt >
local_filt[1] ? 0 : nz(downward[1])
[smoothrng, local_filt, upward, downward]
[smoothrng, filt, upward, downward] = Range_filter(src, per, mult)
[_, _, upward2, downward2] = Range_filter(src2, per, mult)
hband = filt + smoothrng
lband = filt - smoothrng
// ADX
ADX_len = input.int(14, "ADX LENGTH", minval=1)
th = input.int(20, "ADX THRESHOLD", minval=0) // 提高趋势要求
calcADX(_ADX_len) =>
var float SmoothedTrueRange = 0.0
var float SmoothedDirectionalMovementPlus = 0.0
var float SmoothedDirectionalMovementMinus = 0.0
TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))),
math.abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-
nz(high[1]), 0) : 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ?
math.max(nz(low[1])-low, 0) : 0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) -
(nz(SmoothedTrueRange[1])/_ADX_len) + TrueRange
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) -
(nz(SmoothedDirectionalMovementPlus[1])/_ADX_len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) -
(nz(SmoothedDirectionalMovementMinus[1])/_ADX_len) + DirectionalMovementMinus
DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus-DIMinus) / (DIPlus+DIMinus) * 100
ADX = ta.sma(DX, _ADX_len)
[DIPlus, DIMinus, ADX]
[DIPlus, DIMinus, ADX] = calcADX(ADX_len)
// SAR
Sst = input.float(0.02, "SAR START", step=0.01, minval=0.01)
Sinc = input.float(0.02, "SAR INC", step=0.01, minval=0.01)
Smax = input.float(0.2, "SAR MAX", step=0.01, minval=0.01)
SAR = ta.sar(Sst, Sinc, Smax)
// RSI
RSI_len = input.int(14, "RSI LENGTH", minval=1)
RSI_obos = input.int(55, "RSI CENTER LINE", minval=1) // 提高中线
RSI = ta.rsi(src, RSI_len)
// MACD
fast_length = input.int(12, "MACD FAST LENGTH", minval=1)
slow_length = input.int(26, "MACD SLOW LENGTH", minval=1)
signal_length = input.int(9, "MACD SIGNAL SMOOTHING", minval=1, maxval=50)
[macdLine, signalLine, hist] = ta.macd(src, fast_length, slow_length,
signal_length)
// EMA 趋势过滤
ema_fast_len = input.int(20, "EMA FAST LENGTH", minval=1)
ema_slow_len = input.int(50, "EMA SLOW LENGTH", minval=1)
ema_fast = ta.ema(close, ema_fast_len)
ema_slow = ta.ema(close, ema_slow_len)
ema_bull = ema_fast > ema_slow // EMA 交叉看涨
ema_bear = ema_fast < ema_slow // EMA 交叉看跌
// 入场信号(确认信号,优化条件)
longCond = ((high[1] > hband[1] and upward[1] > 0) or (high[1] > hband[1] and
upward2[1] > 0))
and (DIPlus[1] > DIMinus[1] and ADX[1] > th) and (SAR[1] < close[1])
and ((T3_Rising[1]) or (T3_Rising2[1])) and (RSI[1] > RSI_obos) and (hist[1] >
0)
and volume[1] > ta.sma(volume, 30)[1] * 0.5 and ema_bull[1]
shortCond = ((low[1] < lband[1] and downward[1] > 0) or (low[1] < lband[1] and
downward2[1] > 0))
and (DIPlus[1] < DIMinus[1] and ADX[1] > th) and (SAR[1] > close[1])
and ((T3_Falling[1]) or (T3_Falling2[1])) and (RSI[1] < (100-RSI_obos)) and
(hist[1] < 0)
and volume[1] > ta.sma(volume, 30)[1] * 0.5 and ema_bear[1]
// 简化状态逻辑
longCondition = longCond
shortCondition = shortCond
// 退出条件
tsi = input.float(3.0, "TRAILING STOP ACTIVATION %", step=0.1)
ts_offset = input.float(1.5, "TRAILING STOP OFFSET %", step=0.1) // 适中偏移
sl = input.float(1.5, "STOP LOSS %", step=0.1, minval=0.1)
profit_target = input.float(15.0, "PROFIT TARGET %", step=0.1) // 提高止盈
var float last_open_long = na
var float last_open_short = na
last_open_long := longCondition ? close : nz(last_open_long[1])
last_open_short := shortCondition ? close : nz(last_open_short[1])
// 执行交易
if longCondition and is_Long
strategy.entry("Long", strategy.long)
if shortCondition and is_Short
strategy.entry("Short", strategy.short)
// 退出逻辑
strategy.exit("Long Exit", "Long", profit=profit_target*100, loss=sl*100,
trail_points=tsi*100, trail_offset=ts_offset*100)
strategy.exit("Short Exit", "Short", profit=profit_target*100, loss=sl*100,
trail_points=tsi*100, trail_offset=ts_offset*100)
// 调试信号
plotshape(longCondition, "Long Entry", shape.triangleup, location.belowbar,
color.green, size=size.small)
plotshape(shortCondition, "Short Entry", shape.triangledown, location.abovebar,
color.red, size=size.small)
// 调试指标
plot(hband, "High Band", color=color.blue, linewidth=1)
plot(lband, "Low Band", color=color.red, linewidth=1)
plot(SAR, "SAR", color=color.yellow, style=plot.style_cross)
plot(ema_fast, "EMA Fast", color=color.orange, linewidth=1)
plot(ema_slow, "EMA Slow", color=color.purple, linewidth=1)