A Fibonacci-based trading strategy! Here's a TradingView Pine Editor script that does exactly what you asked for:
Fibonacci Retracement Trading Strategy (Long Only)
//@version=5
strategy("Fib Retracement Strategy (Long Only)", overlay=true, currency=currency.USD, initial_capital=10000)
// Parameters
length = input.int(20, "Swing Length", minval=1)
src = input.source(close, "Source")
fibLevel1 = input.float(0.618, "Fib Retracement Level (Entry)", minval=0, maxval=1)
stopLevel = input.float(0.886, "Fib Stop Level", minval=0, maxval=1)
rr = input.float(3, "Risk-Reward Ratio", minval=1)
// Calculate swing highs and lows
swHigh = ta.highest(src, length)
swLow = ta.lowest(src, length)
// Calculate Fibonacci retracement levels
diff = swHigh - swLow
fib618 = swLow + (diff * fibLevel1)
fib886 = swLow + (diff * stopLevel)
// Plot Fibonacci levels
plot(fib618, color=color.green, linewidth=2, title="Fib 0.618")
plot(fib886, color=color.red, linewidth=2, title="Fib 0.886")
// Entry condition: price retraces to Fib 0.618 level
longCondition = ta.crossover(src, fib618)
if (longCondition)
strategy.entry("Long", strategy.long)
// Calculate stop loss and take profit levels
stopLoss = fib886
takeProfit = src + ((src - stopLoss) * rr)
// Plot stop loss and take profit levels
plot(stopLoss, color=color.red, style=plot.style_linebr, title="Stop Loss")
plot(takeProfit, color=color.green, style=plot.style_linebr, title="Take Profit (RR3)")
// Exit conditions
strategy.exit("Exit Long", "Long", stop=stopLoss, limit=takeProfit)
// Optional: plot labels for easier visualization
if (longCondition)
label.new(bar_index, src, "Buy", yloc=yloc.belowbar, color=color.green)
if (strategy.position_size[1] > 0 and strategy.position_size == 0)
label.new(bar_index, src, "Sell (TP/SL)", yloc=yloc.abovebar, color=strategy.position_size[1] > strategy.position_size ? color.green : color.red)
Here's how the script works:
- Inputs:
length: Swing length to calculate highest high and lowest low (default: 20 bars).
src: Source price (default: close price).
fibLevel1: Fibonacci retracement level for entry (default: 0.618).
stopLevel: Fibonacci level for stop loss (default: 0.886).
rr: Risk-Reward ratio for take profit (default: 3, meaning 3:1 RR).
- Swing High/Low calculation:
swHigh and swLow are calculated using ta.highest() and ta.lowest() functions over the specified length.
- Fibonacci retracement levels:
fib618 (entry level) and fib886 (stop loss level) are calculated using the difference between swHigh and swLow, multiplied by the respective Fibonacci ratios.
- Entry condition: A long trade is entered when the price (
src) crosses over the fib618 level (ta.crossover() function).
- Stop Loss and Take Profit calculation:
stopLoss is set to the fib886 level.
takeProfit is calculated by adding the difference between the entry price (src) and stopLoss, multiplied by the Risk-Reward ratio (rr).
- Exit conditions:
strategy.exit() function is used to set a stop loss (stop=stopLoss) and take profit (limit=takeProfit) for the long trade.
- Optional labels: The script plots labels for Buy (entry) and Sell (exit) signals to help visualize trades.
How to use this script:
- Open TradingView and go to the Pine Editor (bottom panel).
- Delete any existing code, paste this script, and click "Add to Chart" (above the editor).
- Adjust the input parameters (e.g.,
length, fibLevel1, stopLevel, rr) as needed.
- Observe the green line (Fib 0.618 entry level), red line (Fib 0.886 stop loss), and green dotted line (Take Profit, RR3).
- When a long trade is triggered, you'll see a green "Buy" label below the bar, and a green dotted Take Profit line above.
- If the trade hits the stop loss (red line), you'll see a red "Sell (TP/SL)" label above the bar.
Disclaimer: This script is for educational purposes only. Always backtest strategies in a demo environment before using them in live markets. Manage your risk and adjust position sizes accordingly.
Happy trading!