
The Dynamic Fibonacci Retracement Trend-Following Quantitative Strategy is a technical analysis trading system based on Fibonacci retracement levels, specifically designed to identify potential buy and sell signals in trending markets. This strategy calculates Fibonacci retracement levels (23.6%, 38.2%, 50%, and 61.8%) between price highs and lows, treating these levels as potential support and resistance zones, generating trading signals when price interacts with these key levels. The core advantage of this strategy lies in its flexibility, allowing traders to select trend direction (“From Top to Bottom” or “From Bottom to Top”) based on market conditions, and customize entry levels, take profit, and stop loss parameters to adapt to different market environments and risk preferences.
The working principle of this strategy revolves around the application of the Fibonacci sequence, a mathematical relationship widely used in financial markets. The specific implementation steps are as follows:
Historical Analysis: The strategy first identifies the highest and lowest prices within a user-defined lookback period (default 144 periods) as the basis for calculating Fibonacci retracement levels.
Direction Selection: Depending on the user-selected Fibonacci direction (“From Top to Bottom” or “From Bottom to Top”), the strategy employs different calculation methods. If “From Top to Bottom” is selected, the highest point is set as the 0% level and the lowest point as the 100% level; if “From Bottom to Top” is selected, the opposite applies.
Level Calculation: Based on identified highs and lows and the selected direction, the strategy calculates four key Fibonacci retracement levels: 23.6%, 38.2%, 50%, and 61.8%.
Signal Generation:
Risk Management: The strategy automatically sets take profit and stop loss when trade signals are triggered, with default values of 24 pips for take profit and 4 pips for stop loss, converted to price units using syminfo.mintick multiplied by 10.
Visualization: The strategy plots all Fibonacci levels, highest and lowest points, and buy/sell signals on the chart, providing intuitive visual analysis assistance.
High Adaptability: The strategy allows users to select the Fibonacci direction based on current market trends, making it effective in both uptrends and downtrends, enhancing the flexibility and adaptability of the strategy.
Customizable Parameters: Users can customize entry levels, lookback periods, take profit, and stop loss parameters according to personal trading style and risk preference, increasing the personalization of the strategy.
Solid Technical Foundation: The strategy is based on the widely recognized Fibonacci retracement theory, which has a solid theoretical foundation and practical verification in the field of technical analysis, enhancing the reliability of the strategy.
Clear Visual Assistance: By intuitively displaying Fibonacci levels, high/low points, and trading signals on the chart, traders can more easily understand market structure and strategy logic, aiding the decision-making process.
Integrated Risk Management: The strategy has built-in stop loss and take profit mechanisms, automatically setting risk parameters for each trade, helping to maintain consistent risk management rules and protect capital safety.
Real-time Dynamic Calculation: The strategy continuously updates Fibonacci levels, ensuring calculations are always based on recent highs and lows, keeping the analysis relevant to current market conditions.
Lookback Period Sensitivity: The strategy relies on the lookback period to determine highs and lows, and different lookback periods may lead to significantly different results. Too short a period may result in too many noise signals, while too long a period may miss important market turning points. Solution: It is recommended to backtest different lookback periods to find the optimal settings for specific markets and timeframes.
False Signals in Oscillating Markets: In sideways or oscillating markets, prices may frequently cross Fibonacci levels, generating excessive trading signals, increasing trading costs, and potentially leading to consecutive losses. Solution: Consider adding additional filtering conditions, such as trend confirmation indicators (like moving averages or ADX) to reduce false signals.
Limitations of Fixed Pip Stop Loss and Take Profit: The strategy uses fixed pips for stop loss and take profit, which may not be suitable for all market conditions, especially when volatility changes. Solution: Consider using dynamic stop loss and take profit based on ATR (Average True Range) to adapt to current market volatility.
Single Indicator Dependency: Relying solely on Fibonacci retracements for trading decisions ignores other important market factors and indicators, potentially leading to insufficient signal quality. Solution: Combine the strategy with other technical indicators or price action analysis to build a multiple confirmation system.
Delayed Recognition of Trend Changes: The strategy may react slowly to trend changes because it calculates levels based on past highs and lows. Solution: Reduce the lookback period or add trend change early warning mechanisms, such as momentum indicators.
Integrate Multiple Timeframe Analysis: The current strategy runs on a single timeframe only. Consider integrating multiple timeframe analysis, such as confirming trend direction on a larger timeframe and then executing entry signals on a smaller timeframe to improve the robustness of the strategy. Rationale: This can reduce false signals and ensure trading direction aligns with the larger trend.
Introduce Dynamic Risk Management: Replace fixed pip stop loss and take profit with dynamic parameters based on ATR, allowing risk management to self-adapt to market volatility. Rationale: ATR can measure market volatility, automatically widening stop loss ranges during high volatility and narrowing them during low volatility, better matching market reality.
Add Volume Confirmation: Incorporate volume analysis when generating signals to ensure price breakouts are supported by sufficient trading volume. Rationale: Breakouts supported by volume are more reliable, reducing losses from false breakouts.
Implement Adaptive Fibonacci Calculation: Instead of basing calculations solely on a fixed lookback period, automatically adjust the lookback period according to market volatility, using longer periods during high volatility and shorter periods during low volatility. Rationale: This adaptive approach can better capture genuine market turning points.
Add Market State Classifier: Incorporate functionality that can identify the current market state (trending, ranging, or transitioning) and adopt different trading rules for different market states. Rationale: Different market states are suitable for different trading strategies; trending markets are suitable for trend following, while ranging markets are suitable for range trading.
Optimize Entry Timing: Building on the current foundation, add candlestick patterns or price action analysis to seek more precise entry timing near Fibonacci levels. Rationale: This can improve entry precision and enhance risk-reward ratios.
The Dynamic Fibonacci Retracement Trend-Following Quantitative Strategy is a systematic trading method based on classic technical analysis theory, providing traders with objective entry signals and a risk management framework by identifying the support and resistance effects of Fibonacci retracement levels. The main advantages of this strategy lie in its adaptability and customizability, allowing traders to adjust parameter settings for different market environments. However, it also faces challenges such as potential false signals in oscillating markets and dependency on a single indicator. There is significant room for optimization through integrating multiple timeframe analysis, introducing dynamic risk management, and adding additional confirmation indicators. For traders, understanding Fibonacci principles and their market applications is key to effectively using this strategy; combined with appropriate risk management and market analysis, this strategy can be a powerful weapon in the trading toolbox.
/*backtest
start: 2025-05-18 00:00:00
end: 2025-05-19 16:00:00
period: 2m
basePeriod: 2m
exchanges: [{"eid":"Futures_Binance","currency":"SOL_USDT"}]
*/
//@version=5
strategy("简单斐波那契回撤策略", overlay=true, initial_capital=1000,default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// 输入回看周期以识别高点和低点
lookback = input.int(144, title="回看周期", minval=10)
// 输入选择斐波那契计算方向
fib_direction = input.string(title="斐波那契方向", defval="从上到下", options=["从上到下", "从下到上"])
// 输入斐波那契水平
fib_level_236 = input.float(0.236, title="斐波那契 23.6% 水平")
fib_level_382 = input.float(0.382, title="斐波那契 38.2% 水平")
fib_level_50 = input.float(0.5, title="斐波那契 50% 水平")
fib_level_618 = input.float(0.618, title="斐波那契 61.8% 水平")
// 输入选择买入和卖出信号的水平
buy_entry_level = input.string(title="买入入场水平", defval="斐波那契 61.8%", options=["斐波那契 23.6%", "斐波那契 38.2%", "斐波那契 50%", "斐波那契 61.8%"])
sell_entry_level = input.string(title="卖出入场水平", defval="斐波那契 38.2%", options=["斐波那契 23.6%", "斐波那契 38.2%", "斐波那契 50%", "斐波那契 61.8%"])
// 输入止盈和止损(以点数为单位)
take_profit_pips = input.int(24, title="止盈(点数)")
stop_loss_pips = input.int(4, title="止损(点数)")
// 识别回看周期内的高点和低点
highestHigh = ta.highest(high, lookback)
lowestLow = ta.lowest(low, lookback)
// 根据选择的方向计算斐波那契水平
var float fib_0 = na
var float fib_100 = na
var float fib_236 = na
var float fib_382 = na
var float fib_50 = na
var float fib_618 = na
if fib_direction == "从上到下"
fib_0 := highestHigh
fib_100 := lowestLow
fib_236 := highestHigh - (highestHigh - lowestLow) * fib_level_236
fib_382 := highestHigh - (highestHigh - lowestLow) * fib_level_382
fib_50 := highestHigh - (highestHigh - lowestLow) * fib_level_50
fib_618 := highestHigh - (highestHigh - lowestLow) * fib_level_618
else
fib_0 := lowestLow
fib_100 := highestHigh
fib_236 := lowestLow + (highestHigh - lowestLow) * fib_level_236
fib_382 := lowestLow + (highestHigh - lowestLow) * fib_level_382
fib_50 := lowestLow + (highestHigh - lowestLow) * fib_level_50
fib_618 := lowestLow + (highestHigh - lowestLow) * fib_level_618
// 根据用户输入确定买入和卖出信号的水平
var float buy_fib_level = na
var float sell_fib_level = na
if buy_entry_level == "斐波那契 23.6%"
buy_fib_level := fib_236
if buy_entry_level == "斐波那契 38.2%"
buy_fib_level := fib_382
if buy_entry_level == "斐波那契 50%"
buy_fib_level := fib_50
if buy_entry_level == "斐波那契 61.8%"
buy_fib_level := fib_618
if sell_entry_level == "斐波那契 23.6%"
sell_fib_level := fib_236
if sell_entry_level == "斐波那契 38.2%"
sell_fib_level := fib_382
if sell_entry_level == "斐波那契 50%"
sell_fib_level := fib_50
if sell_entry_level == "斐波那契 61.8%"
sell_fib_level := fib_618
// 将点数转换为价格单位(假设1点 = 0.0001,适用于如EURUSD的货币对)
pip_value = syminfo.mintick * 10
take_profit = take_profit_pips * pip_value
stop_loss = stop_loss_pips * pip_value
// 交易信号
var bool longSignal = na
var bool shortSignal = na
if fib_direction == "从上到下"
longSignal := ta.crossover(close, buy_fib_level) and close > buy_fib_level
shortSignal := ta.crossunder(close, sell_fib_level) and close < sell_fib_level
else
longSignal := ta.crossover(close, buy_fib_level) and close > buy_fib_level
shortSignal := ta.crossunder(close, sell_fib_level) and close < sell_fib_level
// 根据信号执行交易,设置止盈和止损
if (longSignal)
strategy.entry("多头", strategy.long, comment="买入")
strategy.exit("止盈/止损", "多头", limit=close + take_profit, stop=close - stop_loss)
if (shortSignal)
strategy.entry("空头", strategy.short, comment="卖出")
strategy.exit("止盈/止损", "空头", limit=close - take_profit, stop=close + stop_loss)
// 绘制斐波那契水平
plot(fib_0, title="斐波那契 0%", color=color.blue, linewidth=1, style=plot.style_line)
plot(fib_236, title="斐波那契 23.6%", color=color.green, linewidth=1, style=plot.style_line)
plot(fib_382, title="斐波那契 38.2%", color=color.green, linewidth=1, style=plot.style_line)
plot(fib_50, title="斐波那契 50%", color=color.red, linewidth=1, style=plot.style_line)
plot(fib_618, title="斐波那契 61.8%", color=color.green, linewidth=1, style=plot.style_line)
plot(fib_100, title="斐波那契 100%", color=color.blue, linewidth=1, style=plot.style_line)
// 为斐波那契水平创建带有白色文本的标签
var label fibLabel0 = na
var label fibLabel236 = na
var label fibLabel382 = na
var label fibLabel50 = na
var label fibLabel618 = na
var label fibLabel100 = na
if (na(fibLabel0))
fibLabel0 := label.new(bar_index, fib_0, text="斐波那契 0%", color=na, textcolor=color.white, style=label.style_label_right, yloc=yloc.price)
fibLabel236 := label.new(bar_index, fib_236, text="斐波那契 23.6%", color=na, textcolor=color.white, style=label.style_label_right, yloc=yloc.price)
fibLabel382 := label.new(bar_index, fib_382, text="斐波那契 38.2%", color=na, textcolor=color.white, style=label.style_label_right, yloc=yloc.price)
fibLabel50 := label.new(bar_index, fib_50, text="斐波那契 50%", color=na, textcolor=color.white, style=label.style_label_right, yloc=yloc.price)
fibLabel618 := label.new(bar_index, fib_618, text="斐波那契 61.8%", color=na, textcolor=color.white, style=label.style_label_right, yloc=yloc.price)
fibLabel100 := label.new(bar_index, fib_100, text="斐波那契 100%", color=na, textcolor=color.white, style=label.style_label_right, yloc=yloc.price)
else
label.set_xy(fibLabel0, bar_index, fib_0)
label.set_xy(fibLabel236, bar_index, fib_236)
label.set_xy(fibLabel382, bar_index, fib_382)
label.set_xy(fibLabel50, bar_index, fib_50)
label.set_xy(fibLabel618, bar_index, fib_618)
label.set_xy(fibLabel100, bar_index, fib_100)
// 绘制信号
plotshape(series=longSignal, location=location.belowbar, color=color.green, style=shape.labelup, title="买入信号", text="买入")
plotshape(series=shortSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="卖出信号", text="卖出")
// 绘制最高点和最低点
plot(highestHigh, title="最高点", color=color.purple, linewidth=2, offset=-lookback)
plot(lowestLow, title="最低点", color=color.purple, linewidth=2, offset=-lookback)