本策略是一个基于斐波那契回撤和延展水平,结合EMA均线趋势判断的复合型量化交易系统。策略通过识别市场的重要支撑阻力位,结合趋势信号进行交易。系统使用20周期和50周期EMA均线判断市场趋势,并在此基础上利用斐波那契回撤水平寻找最佳交易机会。
策略的核心逻辑包含三个主要部分:首先计算近10个周期的最高价和最低价,用于确定价格波动区间;其次基于该区间计算五个关键的斐波那契回撤水平(0.236、0.382、0.5、0.618、0.786);最后通过20和50周期EMA的交叉确定趋势方向。在上升趋势中,当价格突破回撤水平时发出做多信号;在下降趋势中,当价格跌破回撤水平时发出做空信号。
该策略通过结合经典的技术分析工具,构建了一个相对完整的交易系统。虽然存在一些需要优化的地方,但整体框架具有良好的市场适应性。通过持续优化和改进,该策略有望在实际交易中取得更好的表现。建议在实盘交易前,进行充分的历史数据回测和参数优化。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-11 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Fibonacci Retracement and Extension Strategy", overlay=true)
// Define the Fibonacci levels for retracement and extension
fibRetracementLevels = array.new_float(5)
array.set(fibRetracementLevels, 0, 0.236)
array.set(fibRetracementLevels, 1, 0.382)
array.set(fibRetracementLevels, 2, 0.5)
array.set(fibRetracementLevels, 3, 0.618)
array.set(fibRetracementLevels, 4, 0.786)
fibExtensionLevels = array.new_float(5)
array.set(fibExtensionLevels, 0, 1.618)
array.set(fibExtensionLevels, 1, 2.618)
array.set(fibExtensionLevels, 2, 3.618)
array.set(fibExtensionLevels, 3, 4.236)
array.set(fibExtensionLevels, 4, 5.618)
// Calculate the high and low prices for the last 10 bars
highPrice = ta.highest(high, 10)
lowPrice = ta.lowest(low, 10)
// Calculate the Fibonacci retracement levels
fibRetracement = array.new_float(5)
for i = 0 to 4
array.set(fibRetracement, i, highPrice - (highPrice - lowPrice) * array.get(fibRetracementLevels, i))
// Calculate the trend using the Exponential Moving Average (EMA)
shortEMA = ta.ema(close, 20)
longEMA = ta.ema(close, 50)
// Define the trend conditions
isUptrend = shortEMA > longEMA
isDowntrend = shortEMA < longEMA
// Generate buy and sell signals
var float lastFibRetracementLevel = na
var float lastFibExtensionLevel = na
// Buy condition: price crosses above the highest retracement level
if (isUptrend)
for i = 0 to 4
if (close > array.get(fibRetracement, i))
lastFibRetracementLevel := array.get(fibRetracement, i)
strategy.entry("Buy", strategy.long)
// Sell condition: price crosses below the lowest retracement level
if (isDowntrend)
for i = 0 to 4
if (close < array.get(fibRetracement, i))
lastFibRetracementLevel := array.get(fibRetracement, i)
strategy.entry("Sell", strategy.short)
// Plotting the Fibonacci levels on the chart
// for i = 0 to 4
// line.new(bar_index[10], array.get(fibRetracement, i), bar_index, array.get(fibRetracement, i), color=color.new(color.blue, 70), width=1)
// Plot the EMAs
plot(shortEMA, color=color.red, title="Short EMA")
plot(longEMA, color=color.blue, title="Long EMA")