INVITE-ONLY SCRIPT
ที่อัปเดต:

TDR (Trend Direction & Risk) Indicator

1 736
Introduction

The TDR (Trend Direction & Risk) Indicator combines two of my previous indicators into one. TDR helps to visually identify price trends and also measures the risk of entering a position during that trend. This indicator is a combination of Ichimoku cloud techniques, particularly the Kijun-Sen and the percentage price oscillator (PPO) to measure a risk score.

Using the TDR Indicator

There are two main indicator lines, the blue line represents a moving average which can act as support or resistance and the orange and red line is called the trend base. When price crosses the trend base and the moving average also crosses then a change in trend direction is confirmed. This is confirmed with either a Buy or Sell signal.

In between these signals there are also Dollar Cost Average and Take Profit signals based on the current trend direction. Each of these signals gives a warning, indicated with an x above or below the candle accompanied with a b for (buy) or an (s) for sell. The next candle will display a small triangle as confirmation of this signal. These signals are based on a cross of the Advance Signal Price which is show at the current price candle.

The future base level can be set to show a larger macro trend than the current timeframe. An example of this is the 3 day timeframe which I use to determine if Bitcoin is in either a bull or bear market.

The Dollar Cost Average and Take Profit signals recommend to buy or remove a percentage of your position at these points. This percentage is completely down to personal preference.

Risk & Information Box

At the bottom right of the chart there is an information box which contains some Information. It shows the current recommended position based on the signals mentioned above. It also shows a risk score which ranges from -100 to 100. This can be used in combination with the signals and trend above for additional entry points. There are three levels, Low, Neutral and High. Buying a percentage of a position at a low risk level and selling a percentage of a position at a high risk level in combination with the trend signals is recommended.

S/R and Base Percentage

These boxes show the price percentage distance from the blue S/R line and the Trend Base Line. These larger these percentages the more likely a snap back in price towards these lines is likely. In my testing once these get above 20% or -20% the chance the price will move back towards those lines is more likely.

Users who have access to my other scripts will be given access to this one and those old ones will no longer me maintained or updated.
เอกสารเผยแพร่
Adds a new field to the table which shows the current market condition, bull or bear market. This is calculated based on the High Timeframe Market Trend which can be changed in the settings, Default is 3Day and recommended to keep it on 3Day.
เอกสารเผยแพร่
Changed the descriptions for active position.
เอกสารเผยแพร่
Add an option to hide the Data Table on the bottom right of the chart. Show active positions as either IN or OUT
เอกสารเผยแพร่
Added a visual representation of the risk on the chart. When the risk level is very high it will show as a red circle above the candles and when very low a green circle below the candles.
เอกสารเผยแพร่
Remove most of the data from the table that is shown visually on the chart now. Show only Bull or Bear Market text instead.
เอกสารเผยแพร่
Added ATR indicator on current bar and added ability to set the risk zone value
เอกสารเผยแพร่
TDR - Trend Driven Risk

Overview:
The TDR (Trend Direction & Risk) indicator is a comprehensive trend, risk, and trade management tool designed for active traders and portfolio managers. It combines advanced trend detection, dynamic risk zoning, and allocation-based trade simulation to help you identify optimal entry and exit points, manage risk, and track performance.

Key Features
Trend Detection:
Utilizes Donchian channel baselines and EMAs to identify trend direction and key support/resistance levels.

Main Baseline: User-configurable Donchian average (default 50).
Short Baseline: User-configurable Donchian average (default 26).
EMA S/R: 21-period EMA for dynamic support/resistance.

Risk Engine:
Choose between two risk models:

TDV Extremes: Detects high/low risk zones using rolling Donchian distance and hit counting.
Classic: Uses PPO (Percentage Price Oscillator) percent rank for momentum-based risk assessment.
Visual risk zones and buffer settings help you avoid trades in dangerous conditions.
Signal Generation:

Full Entry (Buy): Fires on confirmed EMA/baseline crossovers with trend and risk filters.
Pre-Entry (DCA): Allows partial allocation before a full entry, based on short-term baseline crossovers in value zones.
Take Profit (TP): Partial exits triggered by short-term weakness within an uptrend.
Full Exit (Sell): Closes remaining allocation on confirmed trend reversal or risk triggers.
High/Low Risk Zone Actions: Special logic for entering/exiting in extreme risk zones.
Trade Simulation & Allocation:

Simulates real-world allocation: pre-entries, full entries, partial TPs, and full exits.
Allocation caps and per-trade limits prevent overexposure.
Tracks average entry/exit prices, PnL, and trade statistics.

Exit Suppression:
Prevents premature exits when EMA, baseline, or price are rising, reducing whipsaws.

Performance Tracking:
Built-in tables for portfolio value, returns, Sharpe ratio, drawdown, win rates, and more.
Buy & Hold comparison and annualized return calculations.

Customizable Settings:
Adjustable baseline lengths, EMA length, risk thresholds, allocation sizes, and more.
Date range filtering for backtesting specific periods.
Visual options for displaying tables, risk zones, and key levels.


How to Use
Configure Baselines and Risk Engine:

Set your preferred baseline lengths and risk engine (TDV Extremes or Classic).
Adjust risk thresholds and buffer as needed for your asset/timeframe.

Set Allocation and Trade Management:
Define pre-entry, TP, and high/low risk allocation percentages.
Set maximum pre-entries and TPs per trade.

Monitor Signals and Zones:
Watch for buy/sell/pre-entry/TP signals on the chart.
Use risk zone markers to avoid trading in dangerous conditions.

Review Performance:
Analyze the built-in tables for trade stats, returns, and risk metrics.
Compare strategy performance to Buy & Hold.

Visuals
Baselines:
Main Baseline (color-coded by trend)
Short Baseline (purple)
EMA S/R (blue)
Future Baseline (orange, higher timeframe)

Risk Zones:
Green/red dots for low/high risk bars

Trade Markers:
Labels for pre-entries, full entries, TPs, and exits

Tables:
Portfolio, returns, entry/exit, trade stats, and market condition
เอกสารเผยแพร่
Fixes the table to be readable in both light and dark modes
เอกสารเผยแพร่
Fixed the table colors again. Should work better now
เอกสารเผยแพร่
Fixed a bug where high risk take profit signals were not firing.
เอกสารเผยแพร่
Fixes a bug in the catch up logic that was preventing a buy signal from firing when it was supposed to in some cases.
เอกสารเผยแพร่

TDR Indicator - Major Logic Fixes & Improvements
Bug Fixes (8 Critical Issues Resolved)


1. Persistent Crossover Signal Fix

Fixed issue where 3-bar persistent crossover/crossunder signals would fire repeatedly on every bar after conditions were met

Added state tracking flags (persistent_buy_fired, persistent_sell_fired) that reset when EMA crosses back over/under baseline

Ensures persistent signals only fire once per trend phase

2. Suppress Logic Enhancement

Fixed buy signal suppression that was only checking the primary crossover condition

Now applies suppression uniformly to ALL buy signal types (primary, 2-bar catch-up, and 3-bar persistent)

Prevents false entries during weak downward momentum

3. Signal History Tracking Improvement

Replaced stale temporary variables with direct signal history references

Implemented two-phase logic to avoid circular dependencies: conditions → signal declarations → history checks → final flags

Eliminates potential for outdated signal data causing incorrect entry/exit decisions

4. Date Filter Reset Timing

Moved reset logic from end of script to immediately after date filtering (before indicator calculations)

Ensures first bar in date range starts with clean state

Prevents stale data from affecting initial calculations

5. Realized PnL Accumulation Fix

Fixed timing of realisedPnL reset that was wiping out accumulated take profit gains

Now resets only after full exit (not on new entry)

Properly accumulates PnL throughout trade including all partial exits

6. Buy/Sell Return Tracking Integration

Integrated previously unused buyReturns and sellReturns arrays into Trade Stats table

Added 2 new rows showing simple signal-based metrics:
Row 2: Buy Avg Return, Buy Win Rate, Buy Signals count
Row 3: Sell Avg Return, Sell Win Rate, Sell Signals count

Provides dual perspective: allocation-based vs. simple signal-based performance

7. Allocation Bounds Protection

Added safety bounds checking at 4 critical allocation modification points

remaining_allocation: Clamped to minimum 0.0 (prevents negative values)
exited_portion: Clamped to range [0, 100%] (prevents exceeding 100%)

Protects against edge cases with rapid pre-entries or take profits

8. Code Organization Improvement

Moved buy/sell return tracking from statistics section to trade simulation section
Better synchronization with trade state updates (inLong, inShort, entryPrice)

Improved logical flow: execution logic → state tracking → statistical calculations
เอกสารเผยแพร่
Added a trailing stop loss and max drawdown protection.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน