OPEN-SOURCE SCRIPT

Adaptive Sharpe Regime [forexobroker]

306
Adaptive Sharpe Regime [forexobroker] computes a rolling Sharpe ratio of bar returns and sorts the result into directional regime tiers, then times entries with a reclaim-EMA cross. The unique angle is a Newey-West lag-1 autocovariance correction that stops serial correlation in returns from artificially inflating the Sharpe, making the regime read self-normalising and comparable across instruments and timeframes.

🔶 ALGORITHM

1. Compute per-bar returns (log returns by default, or simple returns) and their rolling mean and variance over the Sharpe window.
2. Estimate the lag-1 autocovariance of demeaned returns over the same window.
3. Apply the Newey-West correction: corrected variance = variance + 2 x autocovariance, then take the square root for the corrected volatility.
4. Sharpe = mean return / corrected volatility (guarded against zero volatility).
5. Sort the Sharpe into five regime tiers using the Strong and Mild thresholds: strong+ (+2), mild+ (+1), flat (0), mild- (-1), strong- (-2). The tier sign is the directional bias.
6. With a bias set, a close crossover (long) or crossunder (short) of the reclaim EMA times the actual entry. Signals respect an optional strong-only filter, a bar cooldown, position-lock, and bar-close confirmation. Non-repainting.

🔶 SIGNAL LOGIC

- Buy: in-session AND tier is tradable AND bias = +1 AND close crosses over the reclaim EMA AND position not already long AND cooldown elapsed AND barstate.isconfirmed (then position locks long).
- Sell: in-session AND tier is tradable AND bias = -1 AND close crosses under the reclaim EMA AND position not already short AND cooldown elapsed AND barstate.isconfirmed (then position locks short).
- Only fires when the Sharpe tier is tradable (mild or stronger; strong-only if "Require Strong Tier" is on).

🔶 INPUTS

- Sharpe group: window length over which returns and Sharpe are measured; default Sharpe Window 40.
- Returns mode: log vs simple returns toggle; default Log Returns on.
- Newey-West: toggle the lag-1 autocovariance variance inflation; default on.
- Regime tiers: Strong and Mild absolute-Sharpe thresholds; default Strong Sharpe 0.8.
- Mild threshold: lower band for the mild tier; default Mild Sharpe 0.2.
- Volatility reference: ATR length used for the dashboard reference; default ATR Length 14.
- Signal Logic group: entry reclaim EMA, strong-only filter, cooldown; default Entry Reclaim EMA 9.
- Cooldown: minimum bars between signals; default Cooldown Bars 5.
- Filters group: optional session restriction and session window; default Restrict to Session off.
- Visual group: dashboard, 3-layer glow, reclaim EMA, buy/sell colors, dashboard background.

🔶 ALERTS

ASH Buy, ASH Sell, ASH Any Signal, ASH Strong Plus, ASH Strong Minus, ASH Bias Bull, ASH Bias Bear, ASH EMA Up, ASH EMA Down, ASH Sharpe Zero, ASH Tier Change, ASH Armed, ASH Webhook JSON.

🔶 LIMITATIONS

- The Sharpe ratio needs a full window of returns to stabilise, so the first Sharpe Window bars are warm-up and tiers should not be trusted there.
- The Newey-West correction only addresses lag-1 serial correlation; higher-order autocorrelation is not modelled and the corrected variance is clamped to a small positive floor.
- Regime tiers are statistical classifications, not forecasts; a strong tier describes recent risk-adjusted drift, not future returns.
- Default thresholds are tuned for liquid instruments; thin or gappy symbols can produce unstable Sharpe values.
- Entries confirm on bar close, so the reclaim-EMA cross is acknowledged with one-bar lag versus intrabar price.

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

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