it is not drawing fib properly .. dont draw fib on small pivots use a Swing then draw fib on that swing .. entry at fib retracement 0.618. stoploss at 0.886 and take profit is RR3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You're absolutely right; drawing Fibonacci retracements on small pivots isn't ideal. Instead, we should identify significant swings to draw more meaningful Fibonacci retracement levels. Below is an improved Pine Script that identifies significant swings and then draws Fibonacci retracements on those swings. The script will trigger a trade when the price retraces to the 0.618 Fibonacci level, set a stop loss at the 0.886 level, and place a take profit at a 3:1 risk-reward ratio.
Here is the updated Pine Script:
pinescript
//@version=5
strategy("Fib Retracement Swing-Based Entry with Stop @ 0.886 and RR3 TP", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// ================ INPUTS ================= //
lookback = input.int(50, title="Lookback Period for Swing Detection", minval=1)
tolerance = input.float(0.5, title="Price Tolerance for Entry Trigger", step=0.1)
swingThreshold = input.float(1.0, title="Minimum Swing Threshold (% of ATR)", step=0.1)
// ================ ATR CALCULATION ================= //
atr = ta.atr(14)
swingThresholdValue = atr * swingThreshold / 100
// ================ SWING DETECTION ================= //
var float swingHigh = na
var float swingLow = na
var int swingBar = na
if (ta.highest(high, lookback)[1] < high and high[1] < high)
swingHigh := high
swingBar := bar_index
if (ta.lowest(low, lookback)[1] > low and low[1] > low)
swingLow := low
swingBar := bar_index
// Ensure the swing is significant
significantSwing = not na(swingHigh) and not na(swingLow) and math.abs(swingHigh - swingLow) >= swingThresholdValue
// ================ FIB LEVEL CALCULATIONS ================= //
fib_618 = na
fib_886 = na
takeProfit = na
if (significantSwing)
fib_618 := swingHigh - (swingHigh - swingLow) * 0.618
fib_886 := swingHigh - (swingHigh - swingLow) * 0.886
risk = fib_618 - fib_886
takeProfit := fib_618 + risk * 3
// ================ ENTRY CONDITION ================= //
enterLong = (strategy.position_size == 0) and (math.abs(close - fib_618) < tolerance) and (significantSwing)
// If the condition is met, then enter long with an attached exit order (stop loss & take profit).
if enterLong
strategy.entry("Long", strategy.long, comment="Entry @ Fib 0.618")
// Use strategy.exit to attach a stop-loss at fib_886 and a take-profit at the RR3 target.
strategy.exit("TP/SL", from_entry="Long", stop=fib_886, limit=takeProfit)
// ================ PLOTTING ================= //
plotshape(series=swingHigh, location=location.belowbar, color=color.green, style=shape.labelup, text="Swing High", offset = -lookback)
plotshape(series=swingLow, location=location.abovebar, color=color.red, style=shape.labeldown, text="Swing Low", offset = -lookback)
plot(significantSwing ? fib_618 : na, color=color.blue, title="Fib 0.618 (Entry)")
plot(significantSwing ? fib_886 : na, color=color.orange, title="Fib 0.886 (Stop)")
plot(significantSwing ? takeProfit : na, color=color.purple, title="Take Profit (RR3)")
Explanation:
ATR Calculation:
The script calculates the Average True Range (ATR) to determine a threshold for significant swings. The swingThreshold input allows you to set the minimum swing size as a percentage of the ATR.
Swing Detection:
The script identifies potential swing highs and lows within the lookback period.
It then checks if the swing (the difference between the swing high and low) is significant by comparing it to the swingThresholdValue, which is a percentage of the ATR. This ensures that only meaningful swings are considered.
Fibonacci Level Calculations:
If a significant swing is detected, the script calculates the 0.618 and 0.886 Fibonacci retracement levels based on the identified swing high and low.
It also calculates the take profit level using a 3:1 risk-reward ratio.
Entry Condition:
A long position is entered if the close price is within the specified tolerance of the 0.618 Fibonacci level and a significant swing has been detected.
When entering a long position the script automatically sets a stop loss at the 0.886 level and a take profit at the calculated RR3 level.
Plotting:
The script plots the identified swing highs and lows, as well as the Fibonacci levels (0.618, 0.886) and the take profit level, but only if a significant swing is detected.
Swing points are marked with labels for clarity.
This script should now properly identify significant swings before drawing Fibonacci retracement levels and executing trades based on those levels. Be sure to backtest and further customise the script to fit your specific trading strategy and risk management rules.