Fibonacci Retracement Strategy
Discover our Fibonacci Retracement Strategy Pine Script template, designed for traders seeking to use Fibonacci levels. Easily backtest on TradingView and live execute this template on AlgoTest, adjusting inputs like Number of Historical Candles, target points, and stop loss points to suit your trading preferences.
info
To learn how to add this pinescript in TradingView, click here.
Fibonacci Pinescript​
warning
These strategies are for demonstration purposes only and are not intended for actual trading. AlgoTest is not responsible for any profit or loss arising from the use of these sample strategies.
//@version=5
strategy("Fibonacci Retracement Strategy", overlay=true)
// Input parameters
fib_levels = input.bool(true, title="Show Fibonacci Levels")
n = input.int(20, title="Number of Historical Candles")
target_points = input.int(100, title="Target Points")
stop_loss_points = input.int(50, title="Stop Loss Points")
// Calculate Fibonacci levels
high_price = ta.highest(close, 20)
low_price = ta.lowest(close, 20)
range_ = high_price - low_price
fib618 = high_price - range_ * 0.618
fib382 = high_price - range_ * 0.382
// Strategy logic
long_condition = ta.crossover(close, fib618)
short_condition = ta.crossunder(close, fib382)
// Plot Fibonacci levels
plot(fib_levels ? fib618 : na , "61.8%", color=color.blue, trackprice=true)
plot(fib_levels ? fib382 : na , "38.2%", color=color.red, trackprice=true)
// Strategy entry and exit
if long_condition
strategy.entry("Long", strategy.long)
if short_condition
strategy.entry("Short", strategy.short)
// Calculate target and stop loss levels
long_target = strategy.position_avg_price + target_points
long_stop_loss = strategy.position_avg_price - stop_loss_points
short_target = strategy.position_avg_price - target_points
short_stop_loss = strategy.position_avg_price + stop_loss_points
// Strategy exit
strategy.exit("Long Exit", "Long", limit=long_target, stop=long_stop_loss)
strategy.exit("Short Exit", "Short", limit=short_target, stop=short_stop_loss)