PROTECTED SOURCE SCRIPT
Turtle Long & Short (Donchian + N-Stop)

. Overview and Core Functionality
The indicator implements the classic Turtle Trading System rules. It uses two sets of Donchian Channels for generating entry and exit signals, and the Average True Range (ATR), referred to as N, to calculate a dynamic, volatility-adjusted initial stop-loss.
The script simulates a position's life cycle (entry, holding the fixed initial stop, and exiting) and only conditionally displays the calculated initial stop-loss price on the chart when a trade signal is active.
2. Key Input Parameters (Adjustable Settings)
The script provides detailed input groups for customization:
A. Signal Settings:
len_entry (Default: 20): Period for the Entry Donchian Channel (20-day high/low breakout).
len_exit (Default: 10): Period for the Exit Donchian Channel (10-day low/high trailing stop).
B. Risk Settings (N):
len_atr (Default: 20): Period used to calculate the Average True Range (N), which determines volatility.
stop_loss_multiplier (Default: 2.0): The factor applied to N to calculate the initial stop-loss (e.g., 2.0×N=2N).
C. Label Display: Controls the appearance of the entry labels.
label_background_color_long / label_background_color_short: Background color for Long/Short entry labels.
label_text_color: Text color for the labels.
label_size_input: Size control for the label (tiny, small, normal, large, huge).
3. Trading Logic and State Management
A. Entry and Exit Conditions
Trade Type Entry Condition Trailing Exit Condition Stop-Loss (SL)
Long Close > 20-period High Close < 10-period Low Fixed Entry Price−(Multiplier×N)
Short Close < 20-period Low Close > 10-period High Fixed Entry Price+(Multiplier×N)
In Google Sheets exportieren
B. Position State Management
The script uses persistent var float variables (fixed_long_stop_price and fixed_short_stop_price) to maintain the state:
Upon an Entry signal, the calculated stop-loss price is fixed and assigned to the respective var variable.
The variable holds this fixed price on subsequent bars.
The price is reset to na (Not Applicable) only when an Exit condition (10-period trailing exit, fixed stop-loss hit, or reverse entry signal) is met.
This logic ensures the initial stop-loss line is plotted only when a simulated trade is active.
4. Visual Elements and Alerts
Donchian Channels: Plotted as two lines (Entry High/Exit Low) with a fill for visualization.
N-Stop-Loss Lines: Two lines (fixed_long_stop_price in Fuchsia and fixed_short_stop_price in Orange) are plotted using plot.style_linebr, ensuring they appear only after a trade signal fires and disappear on exit.
Signal Shapes (plotshape):
Long Entry: Green triangle below the bar.
Short Entry: Red triangle above the bar.
Long/Short Exits: Diamond shapes indicating the trailing stop exit.
Entry Labels (label.new): Custom-colored labels appear at the point of entry, displaying the current N value and the exact calculated N-Stop price.
Alerts (alertcondition): Alerts are set up for both Long Entry and Short Entry conditions.
The indicator implements the classic Turtle Trading System rules. It uses two sets of Donchian Channels for generating entry and exit signals, and the Average True Range (ATR), referred to as N, to calculate a dynamic, volatility-adjusted initial stop-loss.
The script simulates a position's life cycle (entry, holding the fixed initial stop, and exiting) and only conditionally displays the calculated initial stop-loss price on the chart when a trade signal is active.
2. Key Input Parameters (Adjustable Settings)
The script provides detailed input groups for customization:
A. Signal Settings:
len_entry (Default: 20): Period for the Entry Donchian Channel (20-day high/low breakout).
len_exit (Default: 10): Period for the Exit Donchian Channel (10-day low/high trailing stop).
B. Risk Settings (N):
len_atr (Default: 20): Period used to calculate the Average True Range (N), which determines volatility.
stop_loss_multiplier (Default: 2.0): The factor applied to N to calculate the initial stop-loss (e.g., 2.0×N=2N).
C. Label Display: Controls the appearance of the entry labels.
label_background_color_long / label_background_color_short: Background color for Long/Short entry labels.
label_text_color: Text color for the labels.
label_size_input: Size control for the label (tiny, small, normal, large, huge).
3. Trading Logic and State Management
A. Entry and Exit Conditions
Trade Type Entry Condition Trailing Exit Condition Stop-Loss (SL)
Long Close > 20-period High Close < 10-period Low Fixed Entry Price−(Multiplier×N)
Short Close < 20-period Low Close > 10-period High Fixed Entry Price+(Multiplier×N)
In Google Sheets exportieren
B. Position State Management
The script uses persistent var float variables (fixed_long_stop_price and fixed_short_stop_price) to maintain the state:
Upon an Entry signal, the calculated stop-loss price is fixed and assigned to the respective var variable.
The variable holds this fixed price on subsequent bars.
The price is reset to na (Not Applicable) only when an Exit condition (10-period trailing exit, fixed stop-loss hit, or reverse entry signal) is met.
This logic ensures the initial stop-loss line is plotted only when a simulated trade is active.
4. Visual Elements and Alerts
Donchian Channels: Plotted as two lines (Entry High/Exit Low) with a fill for visualization.
N-Stop-Loss Lines: Two lines (fixed_long_stop_price in Fuchsia and fixed_short_stop_price in Orange) are plotted using plot.style_linebr, ensuring they appear only after a trade signal fires and disappear on exit.
Signal Shapes (plotshape):
Long Entry: Green triangle below the bar.
Short Entry: Red triangle above the bar.
Long/Short Exits: Diamond shapes indicating the trailing stop exit.
Entry Labels (label.new): Custom-colored labels appear at the point of entry, displaying the current N value and the exact calculated N-Stop price.
Alerts (alertcondition): Alerts are set up for both Long Entry and Short Entry conditions.
Скрипт с защищённым кодом
Этот скрипт опубликован с закрытым исходным кодом. However, you can use it freely and without any limitations – learn more here.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.
Скрипт с защищённым кодом
Этот скрипт опубликован с закрытым исходным кодом. However, you can use it freely and without any limitations – learn more here.
Отказ от ответственности
Информация и публикации не предназначены для предоставления и не являются финансовыми, инвестиционными, торговыми или другими видами советов или рекомендаций, предоставленных или одобренных TradingView. Подробнее читайте в Условиях использования.