
This strategy is an advanced trend-following and reversal trading system based on Fibonacci retracement levels. It dynamically identifies price highs and lows to automatically calculate and plot seven key Fibonacci retracement levels (0%, 23.6%, 38.2%, 50%, 61.8%, 78.6%, and 100%) for identifying potential support and resistance levels. The system employs a bi-directional trading mechanism that can capture both long opportunities in uptrends and short opportunities in downtrends.
The core logic is based on several key elements: 1. Dynamic High-Low Identification: Calculates highest and lowest points over a user-defined lookback period to ensure real-time updates of Fibonacci levels. 2. Bi-directional Trading Signals: Triggers long signals on breakouts above 61.8% retracement and short signals on breaks below 38.2% retracement. 3. Precise Exit Mechanism: Exits long positions at 23.6% level and short positions at 78.6% level. 4. Visual Optimization Options: Offers compact line display mode to reduce chart clutter.
The strategy combines classical Fibonacci retracement theory with modern quantitative trading techniques to create a comprehensive trading system. Its strength lies in automatic identification of key price levels and clear trading signals, while remaining mindful of market environment impacts on strategy performance. Through the suggested optimization directions, the strategy’s stability and profitability can be further enhanced.
/*backtest
start: 2024-01-06 00:00:00
end: 2025-01-05 00:00:00
period: 3h
basePeriod: 3h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Fibonacci Retracement Strategy for Crypto", overlay=true)
// Input parameters
lookback = input.int(50, title="Lookback Period", minval=1)
plotLevels = input.bool(true, title="Plot Fibonacci Levels?")
compactLines = input.bool(true, title="Compact Fibonacci Lines?")
// Calculate highest high and lowest low for the lookback period
highestHigh = ta.highest(high, lookback)
lowestLow = ta.lowest(low, lookback)
// Fibonacci retracement levels
diff = highestHigh - lowestLow
level0 = highestHigh
level23_6 = highestHigh - diff * 0.236
level38_2 = highestHigh - diff * 0.382
level50 = highestHigh - diff * 0.5
level61_8 = highestHigh - diff * 0.618
level78_6 = highestHigh - diff * 0.786
level100 = lowestLow
// Plot Fibonacci levels (compact mode to make lines shorter)
// if plotLevels
// lineStyle = compactLines ? line.style_dashed : line.style_solid
// line.new(bar_index[lookback], level0, bar_index, level0, color=color.green, width=1, style=lineStyle)
// line.new(bar_index[lookback], level23_6, bar_index, level23_6, color=color.blue, width=1, style=lineStyle)
// line.new(bar_index[lookback], level38_2, bar_index, level38_2, color=color.blue, width=1, style=lineStyle)
// line.new(bar_index[lookback], level50, bar_index, level50, color=color.orange, width=1, style=lineStyle)
// line.new(bar_index[lookback], level61_8, bar_index, level61_8, color=color.red, width=1, style=lineStyle)
// line.new(bar_index[lookback], level78_6, bar_index, level78_6, color=color.red, width=1, style=lineStyle)
// line.new(bar_index[lookback], level100, bar_index, level100, color=color.green, width=1, style=lineStyle)
// Long trade: Buy when price crosses above 61.8% retracement
longCondition = ta.crossover(close, level61_8)
if longCondition
strategy.entry("Long", strategy.long, alert_message="Price bounced off Fibonacci level - Enter Long")
// Short trade: Sell when price crosses below 38.2% retracement
shortCondition = ta.crossunder(close, level38_2)
if shortCondition
strategy.entry("Short", strategy.short, alert_message="Price crossed below Fibonacci level - Enter Short")
// Exit conditions
exitLong = close >= level23_6
if exitLong
strategy.close("Long", alert_message="Price reached 23.6% Fibonacci level - Exit Long")
exitShort = close <= level78_6
if exitShort
strategy.close("Short", alert_message="Price reached 78.6% Fibonacci level - Exit Short")