PROTECTED SOURCE SCRIPT

Profit Trailing BBands

67
Profit Trailing Trend BBands v4.7.5 with Double Trailing SL
A TradingView Pine Script Strategy

Created by Kevin Bourn and refined with the help of Grok 3 (xAI)

Overview

Welcome to Profit Trailing Trend BBands v4.7.5, a dynamic trading strategy designed to ride trends and lock in profits with a unique double trailing stop-loss mechanism. Built for TradingView’s Pine Script v6, this strategy combines Bollinger Bands for trend detection with a smart trailing system that doubles down on profit protection. Whether you’re trading XRP or any other asset, this tool aims to maximize gains while keeping risk in check—all with a clean, visual interface.


What It Does

Identifies Trends: Uses Bollinger Bands to spot uptrends (price crossing above the upper band) and downtrends (price crossing below the lower band).
Enters Positions: Opens long or short trades based on trend signals, with customizable position sizing and leverage.
Trails Profits: Employs a two-stage trailing stop-loss:
Initial Trailing SL: Acts as a take-profit level, set as a percentage (%) or dollar ($) distance from the entry price.
Tightened Trailing SL: Once the initial profit target is hit, the stop-loss tightens to half the initial distance, locking in gains as the trend continues.
Manages Risk: Includes a margin call feature to exit losing positions before they blow up your account.
Visualizes Everything: Plots Bollinger Bands (blue upper, orange lower) and a red stepped trailing stop-loss line for easy tracking.


Why Built It?

Captures Trends: Bollinger Bands are a proven way to catch momentum, and we tuned them for responsiveness (short length, moderate multiplier).
Secures Profits: Traditional trailing stops often leave money on the table or exit too early. The double trailing SL first takes a chunk of profit, then tightens up to ride the rest of the move.
Stays Flexible: Traders can tweak price sources, stop-loss types (% or $), and position sizing to fit their style.
Looks Good: Clear visuals help you see the strategy in action without cluttering your chart.
Originally refined for XRP, it’s versatile enough for most markets — crypto, forex, stocks, you name it.


How It Works

Core Components

Bollinger Bands:
  1. Calculated using a simple moving average (SMA) and standard deviation.
  2. Default settings: 6-period length, 1.66 multiplier.
  3. Upper Band (blue): SMA + (1.66 × StdDev).
  4. Lower Band (orange): SMA - (1.66 × StdDev).
  5. Trend signals: Price crossing above the upper band triggers a long, below the lower band triggers a short.


Double Trailing Stop-Loss:
  1. Initial SL: Set via "Trailing Stop-Loss Value" (default 6% or $6). Trails the price at this distance and doubles as the first profit target.
  2. Tightened SL: Once price hits the initial SL distance in profit (e.g., +6%), the SL tightens to half (e.g., 3%) and continues trailing, locking in gains.
  3. Visualized as a red stepped line, only visible during active positions.


Position Sizing:
  1. Choose "% of Equity" (default 30%) or "Amount in $" to set trade size.
  2. Leverage (default 10x) amplifies positions, capped by available equity to avoid overexposure.


Margin Call:
  1. Exits positions if drawdown exceeds the "Margin %" (default 10%) to protect your account.


Backtesting Filter:
  1. Starts trading after a user-defined date (default: Jan 1, 2020) for focused historical analysis.


Trade Logic
  1. Long Entry: Price crosses above the upper Bollinger Band → Closes any short position, opens a long.
  2. Short Entry: Price crosses below the lower Bollinger Band → Closes any long position, opens a short.
  3. Exit: Position closes when price hits the trailing stop-loss or triggers a margin call.



How to Use It

Setup
  1. Add to TradingView:
  2. Open TradingView, go to the Pine Editor, paste the script, and click "Add to Chart."
  3. Ensure you’re using Pine Script v6 (the script includes version=6).
  4. Configure Inputs:
  5. Start Date for Backtesting: Set the date to begin historical testing (default: Jan 1, 2020).
  6. BB Length & Mult: Adjust Bollinger Band sensitivity (default: 6, 1.66).
  7. BB Price Source: Choose the price for BBands (default: Close).
  8. Trend Price Source: Choose the price for trend detection (default: Close).
  9. Trailing Stop-Loss Type: Pick "%" or "$" (default: Trailing SL %).
  10. Trailing Stop-Loss Value: Set the initial SL distance (default: 6).
  11. Margin %: Define the max drawdown before exit (default: 10%).
  12. Order Size Type & Value: Set position size as % of equity (default: 30%) or $ amount.
  13. Leverage: Adjust leverage (default: 10x).
  14. Run It:
  15. Use the Strategy Tester tab to backtest on your chosen asset and timeframe.
  16. Watch the chart for blue/orange Bollinger Bands and the red trailing SL line.



Tips for Traders

Timeframes: Works on any timeframe, but test 1H or 4H for XRP—great balance of signals and noise.
Assets: Optimized for XRP, but tweak slValue and mult for other markets (e.g., tighter SL for low-volatility pairs).
Risk Management: Keep marginPercent low (5-10%) for volatile assets; adjust leverage based on your risk tolerance.
Visuals: The red stepped SL line shows only during trades—zoom in to see its tightening in action.
Visuals on the Chart
Blue Line: Upper Bollinger Band (trend entry for longs).
Orange Line: Lower Bollinger Band (trend entry for shorts).
Red Stepped Line: Trailing Stop-Loss (shifts tighter after the first profit target).
Order Labels: Short tags like "OL" (Open Long), "CS" (Close Short), "LSL" (Long Stop-Loss), etc., mark trades.


Disclaimer
Trading involves risk. This strategy is for educational and experimental use—backtest thoroughly and use at your own risk. Past performance doesn’t guarantee future results. Not financial advice—just a tool from traders, for traders.

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.