make me a Tradinngview Pine editor script that triggers trade when market retrace to FIB…
- User
make me a Tradinngview Pine editor script that triggers trade when market retrace to FIB 0.618 level ... use stoploss at 0.886 and use RR3 as takeprofit point
- Assistant
Below is one example of a Pine Script (version 5) strategy that attempts to do the following:
• Calculates a recent swing high and low over a user‑defined lookback period.
• Computes a Fibonacci retracement from that move where the 0.618 level is used for a potential long‑entry.
• Sets a stop loss at the 0.886 retracement level and a take‑profit order at a risk:reward ratio of 3 (i.e. take‑profit is placed 3× the distance between entry and stop loss).Please note:
• This is a basic “proof‐of‐concept” script meant for demonstration. In a live system you may need to refine the swing high/low logic and the order‐execution rules.
• Trading “signals” based on Fibonacci levels usually involve human interpretation (or more sophisticated swing‑detection methods) – so be sure to test and adapt this script to your trading style and risk management.Copy and paste the code into TradingView’s Pine Editor and click “Add to Chart.”
//@version=5 strategy("Fib Retracement 0.618 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 High/Low", minval=1) tolerance = input.float(0.5, title="Price Tolerance for Entry Trigger", step=0.1) // ================ SWING CALCULATIONS ================= // // We use the highest high and lowest low over the lookback period to approximate a recent impulse move. swingHigh = ta.highest(high, lookback) swingLow = ta.lowest(low, lookback) // ================ FIB LEVEL CALCULATIONS ================= // // Since Fibonacci retracements are typically drawn from the high to the low (for an up move), // we compute: // - The 61.8% retracement level: used here as our potential long-entry trigger. // - The 88.6% retracement level: used as our stop loss level. fib_618 = swingHigh - (swingHigh - swingLow) * 0.618 fib_886 = swingHigh - (swingHigh - swingLow) * 0.886 // ================ CALCULATE RISK & TAKE-PROFIT ================= // // For a long trade, the risk is the difference between our entry level (fib_618) and the stop level (fib_886). risk = fib_618 - fib_886 // As requested, the take profit is set at a reward ratio of 3. takeProfit = fib_618 + risk * 3 // ================ ENTRY CONDITION ================= // // This example checks if the price is near the fib_618 level. // You can adjust the condition logic (for example, waiting for a bounce or a close above the level). enterLong = (strategy.position_size == 0) and (math.abs(close - fib_618) < tolerance) // 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 ================= // plot(swingHigh, color=color.green, title="Swing High") plot(swingLow, color=color.red, title="Swing Low") plot(fib_618, color=color.blue, title="Fib 0.618 (Entry)") plot(fib_886, color=color.orange, title="Fib 0.886 (Stop)") plot(takeProfit, color=color.purple, title="Take Profit (RR3)")
How the Script Works
-
Swing High/Low Calculation:
The script looks back over the pastlookbackbars and picks the highest high (swingHigh) and the lowest low (swingLow). This is assumed to be the recent impulse move. -
Fibonacci Levels:
The 0.618 level is calculated from the swing high to the swing low. This level is our target for a potential long‑entry. The script also calculates the 0.886 level to set a stop loss. -
Risk and Reward Calculation:
The "risk" is defined as the vertical distance between the 0.618 level and the 0.886 level. The take‑profit is placed 3 times that distance above the entry level (i.e. risk reward 3:1). -
Trade Trigger:
A long position is triggered when the price is within a user‑defined tolerance of the fib_618 level. You might adjust or extend this condition (for example, waiting for a close above that level) depending on your strategy. -
Order Management:
When the long position is entered, a corresponding exit order is attached that automatically sets the stop loss at fib_886 and takes profit at the RR3 target.
Be sure to backtest and modify further to match your own precise trading rules and risk management requirements. Happy coding and trading!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.