Diupdate
[blackcat] L2 Sine-Weighted Moving Average (SWMA)

Level: 2
Background
Invented by Patrick Lafferty in 1999, a Sine Weighted Moving Average (SWMA) takes its weighting from the first half of a Sine wave cycle and accordingly, the most weighting is given to the data in the middle of the data set. It is therefore very similar to the Triangular Moving Average.
Function
A sine weighted moving average (Sine-MA) applies weights to each bar in the shape of the bulge in a sine curve from 0 to pi. For an N-bar average the weightings are
/ 1 \ / 2 \ / N \
sin | --- * pi |, sin | --- * pi |, ..., sin | --- * pi |
\ N+1 / \ N+1 / \ N+1 /
The effect is that middle prices have the greatest weight (much like the TMA, Triangular Moving Average). A Sine Weighted Moving Average ( Sine WMA ) takes its weighting from the first half of a Sine wave cycle and accordingly, the most weighting is given to the data in the middle of the data set.
Key Signal
SWMA(FastLength) --> SWMA Fast Line.
SWMA(SlowLength) --> SWMA Slow Line.
Remarks
This is a Level 2 free and open source indicator.
Feedbacks are appreciated.
Background
Invented by Patrick Lafferty in 1999, a Sine Weighted Moving Average (SWMA) takes its weighting from the first half of a Sine wave cycle and accordingly, the most weighting is given to the data in the middle of the data set. It is therefore very similar to the Triangular Moving Average.
Function
A sine weighted moving average (Sine-MA) applies weights to each bar in the shape of the bulge in a sine curve from 0 to pi. For an N-bar average the weightings are
/ 1 \ / 2 \ / N \
sin | --- * pi |, sin | --- * pi |, ..., sin | --- * pi |
\ N+1 / \ N+1 / \ N+1 /
The effect is that middle prices have the greatest weight (much like the TMA, Triangular Moving Average). A Sine Weighted Moving Average ( Sine WMA ) takes its weighting from the first half of a Sine wave cycle and accordingly, the most weighting is given to the data in the middle of the data set.
Key Signal
SWMA(FastLength) --> SWMA Fast Line.
SWMA(SlowLength) --> SWMA Slow Line.
Remarks
This is a Level 2 free and open source indicator.
Feedbacks are appreciated.
Catatan Rilis
**OVERVIEW**This indicator implements Patrick Lafferty's Sine-Weighted Moving Average (SWMA), a sophisticated technical analysis tool designed to filter out noise while maintaining sensitivity to price movements 📈📉. It utilizes a unique sine-weighting algorithm to assign varying importance to different time periods within each calculation window ⚖️.
The script features dual-timeframe averaging capabilities through adjustable fast and slow periods, creating clear visual signals for trend identification and potential entry points 🔄.
FEATURES
• Customizable Price Source selection [bold]close, open, high, or low[/bold]
• Adjustable Fast & Slow Periods with default values set at 8 and 21 respectively
• Visually striking crossover/crossunder alerts using BUY/SSELL labels ❗
• Real-time histogram coloring reflecting relative position of indicators ✨
• Alert functionality for automated trade notifications 🔔
HOW TO USE
Add the indicator to your chart using the "Add Strategy" button ➕
Adjust the timeframe settings as per your trading strategy requirements 🎯
Monitor the interaction between the yellow (fast) and purple (slow) lines
Watch for crossovers: • Green UPWARD arrows indicate potential buying opportunities 💰 • Red DOWNWARD arrows suggest possible selling moments 📉
TIPS AND NOTES
• Higher period lengths will produce smoother signals but may lag more during volatile markets ⏳
• Lower period lengths increase responsiveness but might generate false signals in choppy environments 😐
• Ideal for both intraday scalping and medium-term swing trading strategies 📊
• Consider combining with volume indicators for enhanced accuracy ✅
LIMITATIONS
⚠️ No built-in stop-loss management
⚠️ Does not account for market depth/volume effects
⚠️ Performance may vary across different asset classes
Catatan Rilis
█ OVERVIEWTwo sine-weighted moving averages turn a crossover into a complete two-way trading system: entries, exits, ATR based stop and target levels, plus a position state machine that removes the same-bar and back-to-back noise typical of moving average crosses.
Add it to any chart, keep the defaults, and read the labels — E for long entry, S for short entry, XL and XS for exits.
█ CONCEPTS
• Sine weighting: the middle of the lookback window carries the largest weight and the ends the smallest, so the average turns earlier than a linear weighted average while staying smoother than a raw regression filter.
• Two-speed structure: the fast line reacts, the slow line confirms, and their cross is the trend trigger.
• Signal governance: a position state machine decides which trigger is actionable, so a signal only fires when it actually changes the position state.
• Volatility aware risk: stop and target distances scale with the Average True Range, which keeps them comparable across symbols and timeframes.
█ HOW TO USE
1 — Add the indicator to a chart. Defaults are Fast 8 and Slow 21 on the close.
2 — Read the labels and the background tint: green tint means long, red tint means short, no tint means flat.
3 — Watch the dashboard in the top right for position, entry price, ATR, stop, target, HTF gate and remaining cooldown.
4 — Build alerts from the eight alert conditions; every alert is evaluated on bar close.
Input Parameters
• Core: Price Source, Fast Period Length (8), Slow Period Length (21), Enable Short Signals (on).
• Signal Governance: Extreme Break Exit (on) with Exit Lookback (10 bars), Same-bar Entry/Exit Exclusivity (on), Cooldown Bars (3, set 0 to disable).
• Risk Management: Show TP / SL Levels (on), ATR Length (14), Stop Loss (2.0 xATR), Take Profit (3.0 xATR).
• Re-entry: Enable Re-entry (off), Anchor Window Around Exit Bar (1), Breakout Source (Close or High/Low).
• HTF Direction Gate: Enable (off), HTF Timeframe (240), HTF EMA Length (50).
• Visuals: Show Signal Labels, Show Position Background, Show Dashboard.
█ SIGNALS
• E — long entry, green label below the bar.
• S — short entry, red label above the bar.
• XL / XS — exit of a long or short position, orange cross.
• E+XS / S+XL — stop and reverse: the exit and the opposite entry land on the same bar.
• RL / RS — optional re-entry after an exit, teal and maroon labels.
• The fill between the lines is yellow while the fast line leads and fuchsia while it lags.
█ FEATURES
• Two-way signals with one switch for long-only markets.
• Exit on the opposite cross or on a break of the N-bar extreme.
• ATR stop and target drawn as broken lines while a trade is open.
• Eight alert conditions plus dynamic alerts that carry the price in the message.
• Optional higher timeframe direction gate and re-entry module, both off by default.
█ IMPLEMENTATION NOTES
Weights follow sin(i * pi / length) across the lookback window, so the weight sum never collapses to zero. While history is still short the value returns na instead of a misleading flat zero, which keeps the crosses honest on the first bars.
The higher timeframe gate reads confirmed HTF bars only, so it does not repaint. All signals are evaluated on the closing price of each bar, which is why alerts use the once per bar close setting.
█ LIMITATIONS
• Crossover systems lag by construction; in ranges expect whipsaw even with the cooldown active.
• The extreme break exit can hold a trade too long on very volatile symbols — shorten the lookback there.
• The HTF gate needs a timeframe larger than the chart timeframe and reports INVALID TF otherwise.
• Stop and target levels are reference values for manual management; the indicator does not place orders.
█ NOTES
• Sine weighted averaging idea credited to Patrick Lafferty; the governance, risk and alert layers are original additions in this version.
• Released under the Mozilla Public License 2.0.
═════════════════════════════════════════════════════════════════════════
For questions or feedback, use the comments section below.
Feel free to use and modify — attribution appreciated.
Don't miss out! The first and most important thing to do is to join my Discord chat now! Click here to start your adventure: discord.com/invite/ZTGpQJq My new github for more pine development soon: github.com/wxt-coder
Pernyataan Penyangkalan
Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.
Don't miss out! The first and most important thing to do is to join my Discord chat now! Click here to start your adventure: discord.com/invite/ZTGpQJq My new github for more pine development soon: github.com/wxt-coder
Pernyataan Penyangkalan
Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.