PROTECTED SOURCE SCRIPT

Trend Master

127
Overview

The Strategy is a trend-following trading system designed for forex, stocks, or other markets on TradingView. It uses pivot points to identify support and resistance levels, combined with a 200-period Exponential Moving Average (EMA) to filter trades. The strategy enters long or short positions based on trend reversals during specific trading sessions (London or New York). It incorporates robust risk management, including position sizing based on risk percentage or fixed amount, trailing stop-losses, breakeven moves, and weekly/monthly profit/loss limits to prevent overtrading.

This script is ideal for traders who want a semi-automated approach with visual aids like colored session backgrounds, support/resistance lines, and a performance dashboard. It supports backtesting from a custom start date and can limit trades to one per session for discipline. Alerts are built-in for entries, exits, and stop-loss adjustments, making it compatible with automated trading bots.

Key Benefits:

  • Trend Reversal Detection: Spots higher highs/lows and lower highs/lows to confirm trend changes.
  • Session Filtering: Trades only during high-liquidity sessions to avoid choppy markets.
  • Risk Control: Automatically calculates position sizes to risk only a set percentage or dollar amount per trade.
  • Performance Tracking: Displays a table of weekly or monthly P&L (profit and loss) with color-coded heatmaps for easy review.
  • Customizable: Adjust trade direction, risk levels, take-profit ratios, and more via inputs.
  • The strategy uses a 1:1.2 risk-reward ratio by default but can be tweaked.
  • How It Works


Trend Identification:

  • The script calculates pivot highs and lows using left (4) and right (2) bars to detect swing points.
  • It identifies patterns like Higher Highs (HH), Higher Lows (HL), Lower Highs (LH), and Lower Lows (LL) to determine the trend direction (uptrend if above resistance, downtrend if below support).
  • Support (green dotted lines) and resistance (red dotted lines) are drawn dynamically and update on trend changes.
  • Bars are colored blue (uptrend) or black (downtrend) for visual clarity.



Entry Signals:

  • Long Entry: Price closes above the 200 EMA, trend shifts from down to up (e.g., breaking resistance), during an active session (London or NY), and no trade has been taken that session (if enabled).
  • Short Entry: Price closes below the 200 EMA, trend shifts from up to down (e.g., breaking support), during an active session, and no prior trade that session.
  • Trades can be restricted to "Long Only," "Short Only," or "Both."
  • Entries are filtered by a start date (e.g., from January 2022) and optional month-specific testing.



Position Sizing and Risk:

  • Risk per trade: Either a fixed dollar amount (e.g., $500) or percentage of equity (e.g., 1%).
  • Quantity is calculated as: Risk Amount / (Entry Price - Stop-Loss Price).
  • This ensures you never risk more than intended, regardless of market volatility.



Stop-Loss (SL) and Take-Profit (TP):

  • SL for Longs: Set below the recent support level, adjustable by a "reduce value" (e.g., tighten by 0-90%) and gap (e.g., add a buffer).
  • SL for Shorts: Set above the recent resistance level, with similar adjustments.
  • TP: Based on risk-reward ratio (default 1.2:1), so if SL is 100 pips away, TP is 120 pips in profit.
  • Visual boxes show SL (red) and TP (green) on the chart for the next 4 bars after entry.



Trade Management:

  • Trailing SL: Automatically moves SL to the new support (longs) or resistance (shorts) if it tightens the stop without increasing risk.
  • Breakeven Move: If enabled, SL moves to entry price once profit reaches a set ratio of initial risk (default 1:1). For example, if risk was 1%, SL moves to breakeven at 1% profit.
  • One Trade Per Session: Prevents multiple entries in the same London or NY session to avoid overtrading.
  • Sessions include optional weekend inclusion and are highlighted (blue for London, green for NY).


Risk Limits (Weekly/Monthly):

  • Monitors P&L for the current week or month.
  • Stops trading if losses hit a limit (e.g., -3%) or profits reach a target (e.g., +7%).
  • Resets at the start of each new week/month.
  • Alerts notify when limits are hit.



Exits:

  • Trades exit at TP, SL, or manually via alerts.
  • No time-based exits; relies on price action.



Performance Dashboard:

  • A customizable table (position, size, colors) shows P&L percentages for each week/month in a grid.
  • Rows = Years, Columns = Weeks (1-52) or Months (1-12).
  • Color scaling: Green for profits (darker for bigger wins), red for losses (darker for bigger losses).
  • Yearly totals in the last column.
  • Helps visualize strategy performance over time without manual calculations.




Input Parameters Explained
Here's a breakdown of the main inputs for easy customization:

  • Trade Direction: "Both" (default), "Long Only," or "Short Only" – Controls allowed trade types.
  • Test Only Selected Month: If true, backtests only the specified month from the start year.
  • Start Year/Month: Sets the backtest start date (default: Jan 2022).
  • Include Weekends: If true, sessions can include weekends (rarely useful for forex).
  • Only One Trade Per Session: Limits to one entry per London/NY session (default: true).
  • Risk Management Time Frame: "Weekly" or "Monthly" – For P&L limits.
  • Enable Limits: Toggle weekly/monthly stop trading on loss/profit thresholds.
  • Loss Limit (%)/Profit Target (%): Stops trading if P&L hits these (e.g., -3% loss or +7% profit).
  • London/New York Session: Enable/disable, with time ranges (e.g., London: 0800-1300 UTC).
  • Left/Right Bars: For pivot detection (default: 4 left, 2 right) – Higher values smooth signals.
  • Support/Resistance: Toggle lines, colors, style, width.
  • Change Bar Color: Colors bars based on trend.
  • TP RR: Take-profit risk-reward (default: 1.2).
  • Stoploss Reduce Value: Tightens SL (negative values widen it, 0-0.9 range).
  • Stoploss Gap: Adds a buffer to SL (e.g., 0.1% away from support).
  • Move to Breakeven: Enables SL move to entry at a profit ratio (default: true, 1:1).
  • Use Risk Amount $: If true, risks fixed $ (e.g., 500); else, % of equity (default: 1%).
  • EMA 3: The slow EMA period (default: 200) for trend filter.
  • Performance Display: Toggle table, location (e.g., Bottom Right), size, colors, scaling for heatmaps.


Setup and Usage Tips

  • Add to Chart: Copy the script into TradingView's Pine Editor, compile, and add to your chart.
  • Backtesting: Use the Strategy Tester tab. Adjust inputs and test on historical data.
  • Live Trading: Connect alerts to a broker or bot (e.g., via webhook). The script sends JSON-formatted alerts for entry, exit, SL moves, and limits.
  • Best Markets: Works well on crypto pairs like SOLUSD or RUNEUSD on 4H timeframes.
  • Risk Warning: This is not financial advice. Always use demo accounts first. Past performance doesn't guarantee future results. Commission is set to 0.05% by default – adjust for your broker.

Customization: Experiment with EMA length or RR ratio for your style.

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.