本文介绍了一种结合MACD和Supertrend两个技术指标的交易策略。该策略利用MACD指标的交叉信号和Supertrend指标的趋势方向来判断进场和出场时机,以期在趋势行情中获取利润。策略的主要思路是在MACD金叉且Supertrend为绿色时做多,在MACD死叉且Supertrend为红色时做空,MACD信号线与MACD线的交叉作为平仓信号。
该策略使用MACD指标和Supertrend指标来产生交易信号。MACD由快速移动平均线(默认为12日)减去慢速移动平均线(默认为26日)得到,再计算MACD的9日移动平均线作为信号线。当MACD线上穿信号线时形成金叉,是做多信号;当MACD线下穿信号线时形成死叉,是做空信号。Supertrend指标结合ATR波动率指标,在价格高于Supertrend线且Supertrend线为绿色时表示上升趋势,在价格低于Supertrend线且Supertrend线为红色时表示下降趋势。策略在MACD金叉且Supertrend为绿色时做多,在MACD死叉且Supertrend为红色时做空,以趋势为友。同时当MACD信号线与MACD线交叉时平仓,控制回撤。
本文介绍了一个基于MACD指标和Supertrend指标的交易策略,该策略通过MACD的趋势判断和Supertrend的方向过滤,在趋势行情中进行交易,同时利用信号线交叉及时平仓,以控制回撤。策略优势在于逻辑简单,趋势把握能力强,同时也存在参数适用性、信号滞后性和频繁交易的风险。未来可以从参数优化、信号过滤、仓位管理、周期和品种选择等方面对策略进行完善,以期获得更稳健的收益。
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy(title="MACD + Supertrend Strategy", overlay=true)
// MACD Calculation
fastLength = 12
slowLength = 26
signalSmoothing = 9
macdSrc = close
// MACD Line
fastMA = ta.ema(macdSrc, fastLength)
slowMA = ta.ema(macdSrc, slowLength)
macdLine = fastMA - slowMA
// MACD Signal Line
signalMA = ta.ema(macdLine, signalSmoothing)
// MACD Histogram
histogram = macdLine - signalMA
// Supertrend Calculation
supertrendATRLength = 10
supertrendFactor = 3.0
[supertrend, _] = ta.supertrend(supertrendFactor, supertrendATRLength)
// Entry and Exit Conditions
longCondition = (macdLine > signalMA) and (supertrend < close)
shortCondition = (signalMA > macdLine) and (supertrend > close)
// Long Entry
if longCondition
strategy.entry("Long", strategy.long)
// Long Exit (Sell)
if signalMA > macdLine
strategy.close("Long")
// Short Entry
if shortCondition
strategy.entry("Short", strategy.short)
// Short Exit (Cover)
if macdLine > signalMA
strategy.close("Short")
// Close Long Position if short condition is met
if shortCondition
strategy.close("Long")
// Close Short Position if long condition is met
if longCondition
strategy.close("Short")
// Plotting
plotshape(series=longCondition, title="Long Entry Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="Long")
plotshape(series=shortCondition, title="Short Entry Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="Short")
// Alerts
alertcondition(longCondition, title='Long Entry Signal', message='MACD crossover and Supertrend below close price')
alertcondition(signalMA > macdLine, title='Long Exit Signal', message='MACD signal line crosses above MACD line')
alertcondition(shortCondition, title='Short Entry Signal', message='MACD crossunder and Supertrend above close price')
alertcondition(macdLine > signalMA, title='Short Exit Signal', message='MACD line crosses above MACD signal line')