OPEN-SOURCE SCRIPT

Fibonacci 5 Candles Retracement

297
snapshot

================================================================================
FIBONACCI 5 CANDLES RETRACEMENT - STRATEGY GUIDE
================================================================================

WHAT DOES THIS STRATEGY DO?
---------------------------

This strategy automatically identifies market trends and uses Fibonacci
retracements to find the best entry points. The idea is simple: when price
makes a strong movement (trend), it often pulls back before continuing in
the same direction. The strategy captures these "pullbacks" to enter at the
right moment.

HOW IT WORKS?
-------------

1. TREND DETECTION
The strategy looks for 5 consecutive candles of the same color:
- 5 red candles = BEARISH trend (price falls)
- 5 green candles = BULLISH trend (price rises)

2. CALCULATION OF START AND END POINTS

For a BEARISH trend (5 red candles):
- START: The highest point between the first red candle and the previous one
- END: The lowest point reached during the 5 candles (and beyond, if the
trend continues)

For a BULLISH trend (5 green candles):
- START: The lowest point between the first green candle and the previous one
- END: The highest point reached during the 5 candles (and beyond, if the
trend continues)

3. DYNAMIC UPDATE
The END point updates automatically if price continues to move in the
direction of the trend, creating new highs (for bullish trends) or new
lows (for bearish trends).

4. TREND END

Normal Mode:
- BEARISH trend ends when a candle closes above the previous candle's open
- BULLISH trend ends when a candle closes below the previous candle's open

"Extended Trend" mode (optional):
- The trend remains active until a candle closes beyond the dynamic 50%
retracement level
- When this happens, the END point "freezes" (stops updating), but the
trend can continue

5. FIBONACCI RETRACEMENT CALCULATION
Once START and END are identified, the strategy automatically calculates
Fibonacci levels. IMPORTANT: for retracements and pending orders, we
consider START as 100% and END as 0%, because we work on the part of the
trend that is recovered (the pullback).

The retracement levels are:

- 70% = level closest to START (smallest retracement)
- 60% = second level
- 50% = central level (often used for entry)
- 25% = level closest to END (largest retracement)


6. PENDING ORDER PLACEMENT
When a trend is identified and completed, the strategy automatically places
a pending order (limit order) at one of the selectable Fibonacci levels.

Available levels:
- 25%: closest to END
- 50%: central level (balanced)
- 60%: closest to START
- 70%: very close to START

The order direction depends on the trend:
- BEARISH trend → SHORT order (bet that price falls)
- BULLISH trend → LONG order (bet that price rises)

Stop Loss and Take Profit (for retracements):
- Stop Loss: always at START level
- Take Profit: always at END level

EXTENDED TAKE PROFIT:
If the order is executed (filled), the strategy can apply an "Extended
Take Profit" if configured. IMPORTANT: for the extended TP calculation,
we consider START as 0% and END as 100% (the original trend movement).
For example, if you set 3%, the Take Profit will be at 103% of the
original trend movement instead of 100%.

AVAILABLE FILTERS
-----------------

1. MINIMUM TREND (pips)
Filters trends that are too small. If a trend is below the set value:
- START and END labels become gray (instead of red/green)
- No pending order is placed
- The trend is still displayed on the chart

Useful for avoiding trading movements that are too small.

2. EMA FILTER
Uses two moving averages (EMA 50 and EMA 200) to filter direction:
- If active: places LONG orders only when EMA50 > EMA200 (uptrend)
- If active: places SHORT orders only when EMA50 < EMA200 (downtrend)

Useful for trading only in the direction of the main trend.

3. EXTENDED TREND
Modifies how the trend is considered "completed":
- If disabled: uses normal logic (opposite candle)
- If active: the trend remains in formation until a candle closes beyond
the dynamic 50%. When this happens, END freezes but the trend can continue.

Useful for capturing longer trends and extended movements.

VISUALIZATION
-------------

The strategy displays on the chart:

1. START AND END LABELS
- Red color for bearish trends
- Green color for bullish trends
- Gray color if the trend is not valid (too small)
- Remain visible even when new trends form

2. START AND END LINES
- Horizontal lines indicating the start (START) and end (END) points of the trend
- White color by default, customizable from the settings panel
- Update dynamically when the END point changes
- Can be shown or hidden via the "Show Start/End Lines" option

3. FIBONACCI LINES
The strategy shows horizontal lines at retracement levels:
- Line at 50% (yellow by default)
- Line at 25% (green by default)
- Line at 60% (azure by default)
- Line at 70% (red by default)

COLOR CUSTOMIZATION:
All line colors can be customized from the settings panel in the
"LINE COLORS" section:
- Start/End Line Color: customize the color of START and END lines
- 50% Line Color: customize the color of the 50% line
- 25% Line Color: customize the color of the 25% line
- 60% Line Color: customize the color of the 60% line
- 70% Line Color: customize the color of the 70% line

Lines update dynamically when the END point changes and can be shown or
hidden individually via options in the "VISUALIZATION" section.

4. PENDING ORDER LABELS
Show pending order information:
- Direction (LONG or SHORT)
- Entry price
- Stop Loss
- Take Profit

Positioned far from the chart to avoid cluttering the visualization.

ALERTS
------

If enabled, alerts send notifications when:

1. PENDING ORDER CREATED
When a new pending order is placed, with all information.

2. PENDING ORDER UPDATED
When the pending order is updated (for example, if the level changes or
if the END point moves).

3. ORDER OPENED
When the pending order is executed (filled) and the position is opened.

Alerts can be configured in TradingView to send notifications via email,
SMS, or other platforms.

RECOMMENDED SETTINGS
--------------------

To get started, you can use these settings:

VISUALIZATION:
- Show all lines and labels to see how it works
- Show Start/End Lines: true (to display lines at START and END points)
- Customize line colors in the "LINE COLORS" section according to your preferences

STRATEGY:
- Pending Order Level: 50% (balanced)
- Extended TP: 0% (use standard TP at 100%)

FILTERS:
- Minimum Trend: 0 pips (disabled initially)
- Use EMA Filter: false (disabled initially)
- Extended Trend: false (use normal logic)

ALERTS:
- Enable Alerts: true (if you want to receive notifications)

PRACTICAL EXAMPLE
-----------------

Scenario: Bearish Trend

1. Price forms 5 consecutive red candles
2. The strategy identifies:
- START = 1.2000 (highest point)
- END = 1.1900 (lowest point)
- Range = 100 pips

3. Calculates Fibonacci levels (for retracements: START = 100%, END = 0%):
- 100% = 1.2000 (START)
- 70% = 1.1930
- 60% = 1.1940
- 50% = 1.1950
- 25% = 1.1975
- 0% = 1.1900 (END)

4. If you set "Pending Order Level" to 50%:
- Places a SHORT pending order at 1.1950 (50% retracement)
- Stop Loss at 1.2000 (START = 100%)
- Take Profit at 1.1900 (END = 0%)

5. If price rises and touches 1.1950:
- The order is executed
- Opens a SHORT position
- If price falls to 1.1900 → Take Profit (profit)
- If price rises to 1.2000 → Stop Loss (loss)

IMPORTANT NOTE
--------------

This strategy is a technical analysis tool. Like all trading strategies,
it does not guarantee profits. Trading involves risks and you can lose money.
Always use appropriate risk management and test the strategy on historical
data before using it with real money.

LICENSE
-------

This code is open source and available for modification. You are free to
use, modify, and distribute this strategy. If you republish or share a
modified version, please kindly mention the original author.

================================================================================

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.