pine files

This commit is contained in:
Miha Kralj
2026-01-31 14:05:53 -08:00
parent 51e885a4a6
commit 5ed4b6c0fc
102 changed files with 2883 additions and 593 deletions
+4 -6
View File
@@ -8,14 +8,12 @@ namespace QuanTAlib;
/// ALMA: Arnaud Legoux Moving Average
/// </summary>
/// <remarks>
/// ALMA uses a Gaussian distribution to determine weights for the moving average.
/// Definition:
/// m = offset * (period - 1)
/// s = period / sigma
/// W_i = exp( - (i - m)^2 / (2 * s^2) )
/// Gaussian-weighted MA with adjustable offset and sigma for responsiveness control.
/// Higher offset (0-1) = more responsive; higher sigma = sharper weights.
///
/// The final ALMA is the weighted sum of the price window divided by the sum of weights.
/// Calculation: <c>W_i = exp(-(i - m)² / (2s²))</c> where <c>m = offset × (period-1)</c>.
/// </remarks>
/// <seealso href="Alma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Alma : AbstractBase
{
+7 -1
View File
@@ -4,8 +4,14 @@ namespace QuanTAlib;
/// <summary>
/// BLMA: Blackman Moving Average
/// A weighted moving average using the Blackman window function for smoother transitions.
/// </summary>
/// <remarks>
/// Window-based MA using Blackman coefficients (a0=0.42, a1=0.5, a2=0.08).
/// Minimizes spectral leakage with smooth taper to zero at edges.
///
/// Calculation: <c>W_i = 0.42 - 0.5×cos(2πi/(n-1)) + 0.08×cos(4πi/(n-1))</c>.
/// </remarks>
/// <seealso href="Blma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Blma : AbstractBase
{
+5 -5
View File
@@ -8,12 +8,12 @@ namespace QuanTAlib;
/// BWMA: Bessel-Weighted Moving Average
/// </summary>
/// <remarks>
/// BWMA applies a Bessel window over the last N samples (FIR).
/// <para>Window coefficient definition:</para>
/// <para>x(i) = 2*i/(p-1) - 1 (maps i to [-1, 1]), arg = 1 - x(i)^2</para>
/// <para>w(i) = arg^(order/2 + 0.5) (with PineScript special-cases for order 0 and 1)</para>
/// <para>Output = sum(window[i] * w(i)) / sum(w(i))</para>
/// FIR MA using Bessel window coefficients with adjustable order.
/// Higher order produces sharper window; order 0 = parabolic.
///
/// Calculation: <c>W_i = (1 - x²)^(order/2 + 0.5)</c> where <c>x = 2i/(n-1) - 1</c>.
/// </remarks>
/// <seealso href="Bwma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Bwma : AbstractBase
{
+5 -12
View File
@@ -4,22 +4,15 @@ using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// Convolution Indicator
/// CONV: Convolution Filter
/// </summary>
/// <remarks>
/// Applies a custom kernel (weights) to the data window.
/// The kernel is applied such that kernel[0] multiplies the oldest data point in the window,
/// and kernel[n-1] multiplies the newest data point.
/// FIR filter applying custom kernel weights via dot product.
/// Foundation for all window-based moving averages.
///
/// Calculation:
/// Result = Sum(kernel[i] * data[i]) for i = 0 to n-1
///
/// Complexity:
/// Update: O(K) where K is kernel length.
///
/// IMPORTANT: This class implements IDisposable. When using the constructor with ITValuePublisher,
/// you MUST dispose the instance to unsubscribe from the source event and prevent memory leaks.
/// Calculation: <c>Result = Σ(kernel[i] × data[i])</c> where kernel[0] weights oldest sample.
/// </remarks>
/// <seealso href="Conv.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Conv : AbstractBase
{
+4 -4
View File
@@ -8,12 +8,12 @@ namespace QuanTAlib;
/// DWMA: Double Weighted Moving Average
/// </summary>
/// <remarks>
/// DWMA applies a Weighted Moving Average (WMA) twice.
/// It provides a smoother curve than a standard WMA but with slightly more lag.
/// Double-pass WMA for enhanced smoothing with slight additional lag.
/// Triangular-like weighting via cascaded linear filters.
///
/// Formula:
/// DWMA = WMA(WMA(source, period), period)
/// Calculation: <c>DWMA = WMA(WMA(source, n), n)</c>.
/// </remarks>
/// <seealso href="Dwma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Dwma : AbstractBase
{
+4 -7
View File
@@ -8,15 +8,12 @@ namespace QuanTAlib;
/// GWMA: Gaussian-Weighted Moving Average
/// </summary>
/// <remarks>
/// GWMA uses a centered Gaussian window to weight price data.
/// Definition:
/// center = (period - 1) / 2
/// W_i = exp(-0.5 * ((i - center) / (sigma * period))^2)
/// Centered Gaussian window weighting with sigma-controlled bell curve width.
/// Symmetric smoothing emphasizing center of window.
///
/// The final GWMA is the weighted sum of the price window divided by the sum of weights.
/// Unlike ALMA (which has an offset parameter), GWMA centers the Gaussian peak at the
/// middle of the window and uses sigma to control the bell curve width.
/// Calculation: <c>W_i = exp(-0.5×((i - center)/(σ×n))²)</c> centered at <c>(n-1)/2</c>.
/// </remarks>
/// <seealso href="Gwma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Gwma : AbstractBase
{
+4 -21
View File
@@ -1,6 +1,3 @@
// Hamma.cs - Hamming Moving Average
// Finite Impulse Response (FIR) filter using Hamming window weighting.
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -9,28 +6,14 @@ namespace QuanTAlib;
/// <summary>
/// HAMMA: Hamming Moving Average
/// A weighted moving average using Hamming window coefficients, providing good
/// spectral characteristics with reduced side lobes compared to simple windowing.
/// </summary>
/// <remarks>
/// <b>Key characteristics</b>
/// <list type="bullet">
/// <item><description>Hamming window: w[i] = 0.54 - 0.46 × cos(2πi/(period-1))</description></item>
/// <item><description>Raised-cosine window with specific coefficients for optimal side-lobe suppression</description></item>
/// <item><description>First side lobe is approximately -43 dB down from main lobe</description></item>
/// <item><description>Widely used in digital signal processing and spectral analysis</description></item>
/// </list>
/// Window-based MA using Hamming raised-cosine coefficients (0.54/0.46).
/// -43 dB first side lobe for superior spectral characteristics.
///
/// <b>Calculation</b>
/// <code>
/// w[i] = 0.54 - 0.46 × cos(2π × i / (period - 1))
/// HAMMA = Σ(price[i] × w[i]) / Σ(w[i])
/// </code>
///
/// <b>Sources</b>
/// Richard W. Hamming - "Digital Filters" (1977)
/// Oppenheim, Schafer - "Discrete-Time Signal Processing"
/// Calculation: <c>W_i = 0.54 - 0.46×cos(2πi/(n-1))</c>.
/// </remarks>
/// <seealso href="Hamma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Hamma : AbstractBase
{
+4 -21
View File
@@ -1,6 +1,3 @@
// Hanma.cs - Hanning Moving Average
// Finite Impulse Response (FIR) filter using Hanning window weighting.
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -9,28 +6,14 @@ namespace QuanTAlib;
/// <summary>
/// HANMA: Hanning Moving Average
/// A weighted moving average using Hanning (Hann) window coefficients, providing
/// excellent spectral characteristics with smooth transitions at window edges.
/// </summary>
/// <remarks>
/// <b>Key characteristics</b>
/// <list type="bullet">
/// <item><description>Hanning window: w[i] = 0.5 × (1 - cos(2πi/(period-1)))</description></item>
/// <item><description>Raised-cosine window that reaches zero at both endpoints</description></item>
/// <item><description>First side lobe is approximately -32 dB down from main lobe</description></item>
/// <item><description>Also known as Hann window (after Julius von Hann)</description></item>
/// </list>
/// Window-based MA using Hanning (Hann) raised-cosine coefficients.
/// Zero at endpoints for smooth spectral transition; -32 dB first side lobe.
///
/// <b>Calculation</b>
/// <code>
/// w[i] = 0.5 × (1 - cos(2π × i / (period - 1)))
/// HANMA = Σ(price[i] × w[i]) / Σ(w[i])
/// </code>
///
/// <b>Sources</b>
/// Julius von Hann - Austrian meteorologist
/// Blackman, Tukey - "The Measurement of Power Spectra" (1958)
/// Calculation: <c>W_i = 0.5×(1 - cos(2πi/(n-1)))</c>.
/// </remarks>
/// <seealso href="Hanma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Hanma : AbstractBase
{
+4 -6
View File
@@ -10,14 +10,12 @@ namespace QuanTAlib;
/// HMA: Hull Moving Average
/// </summary>
/// <remarks>
/// HMA reduces lag by using a combination of weighted moving averages.
/// Lag-reduced MA combining weighted MAs with square root smoothing.
/// SIMD-accelerated intermediate calculation (AVX-512/AVX2/NEON).
///
/// Calculation:
/// HMA = WMA(sqrt(n), 2 * WMA(n/2, price) - WMA(n, price))
///
/// Sources:
/// https://alan.hull.com.au/hma.html
/// Calculation: <c>HMA = WMA(√n, 2×WMA(n/2) - WMA(n))</c>.
/// </remarks>
/// <seealso href="Hma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Hma : AbstractBase
{
+4 -23
View File
@@ -1,6 +1,3 @@
// Hwma.cs - Holt-Winters Moving Average
// Triple exponential smoothing with level, velocity, and acceleration components.
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -9,30 +6,14 @@ namespace QuanTAlib;
/// <summary>
/// HWMA: Holt-Winters Moving Average
/// A triple exponential smoothing filter that tracks level (F), velocity (V), and
/// acceleration (A) components for adaptive trend following.
/// </summary>
/// <remarks>
/// <b>Key characteristics</b>
/// <list type="bullet">
/// <item><description>Triple exponential smoothing with level, velocity, and acceleration</description></item>
/// <item><description>Adapts quickly to trend changes via higher-order derivatives</description></item>
/// <item><description>When period specified: α = 2/(period+1), β = γ = 1/period</description></item>
/// <item><description>O(1) complexity per bar - no windowing required</description></item>
/// </list>
/// Triple exponential smoothing tracking level (F), velocity (V), and acceleration (A).
/// O(1) adaptive trend follower responding quickly via higher-order derivatives.
///
/// <b>Calculation</b>
/// <code>
/// F = α × source + (1-α) × (prevF + prevV + 0.5 × prevA)
/// V = β × (F - prevF) + (1-β) × (prevV + prevA)
/// A = γ × (V - prevV) + (1-γ) × prevA
/// output = F + V + 0.5 × A
/// </code>
///
/// <b>Sources</b>
/// Holt, C.E. (1957) - "Forecasting Seasonals and Trends by Exponentially Weighted Moving Averages"
/// Winters, P.R. (1960) - "Forecasting Sales by Exponentially Weighted Moving Averages"
/// Calculation: <c>Output = F + V + 0.5×A</c> with recursive updates.
/// </remarks>
/// <seealso href="Hwma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Hwma : AbstractBase
{
+5 -20
View File
@@ -4,30 +4,15 @@ using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// LSMA: Least Squares Moving Average
/// LSMA: Least Squares Moving Average (Linear Regression)
/// </summary>
/// <remarks>
/// LSMA calculates the linear regression line for the last n values and returns the value at the current position (or offset).
/// Uses a RingBuffer for storage and O(1) updates for regression sums.
/// Linear regression endpoint with O(1) updates using running sums.
/// Projects trend line value at current bar (or offset position).
///
/// Calculation:
/// Uses linear regression y = mx + b where x=0 is the current bar and x increases into the past.
/// m = (n * sum_xy - sum_x * sum_y) / denominator
/// b = (sum_y - m * sum_x) / n
/// LSMA = b - m * offset
///
/// O(1) update:
/// sum_y_new = sum_y_old - oldest + newest
/// sum_xy_new = sum_xy_old + sum_y_prev - n * oldest
///
/// IsHot:
/// Becomes true when the buffer is full (period samples processed).
///
/// Disposal:
/// When constructed with an ITValuePublisher source, Lsma subscribes to the source's Pub event.
/// Call Dispose() to unsubscribe and prevent memory leaks, especially in long-running applications
/// or when creating many short-lived indicator instances.
/// Calculation: <c>LSMA = b - m × offset</c> where <c>m = (n×Σxy - Σx×Σy) / denom</c>.
/// </remarks>
/// <seealso href="Lsma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Lsma : AbstractBase
{
+3 -16
View File
@@ -7,24 +7,11 @@ namespace QuanTAlib;
/// PWMA: Parabolic Weighted Moving Average
/// </summary>
/// <remarks>
/// PWMA applies parabolic weighting to data points, giving significantly more weight to recent values.
/// Uses triple running sums for O(1) complexity per update.
/// Quadratic weighting (w[i]=i²) emphasizing recent values via O(1) triple running sums.
///
/// Weights: w(i) = i^2
///
/// Calculation:
/// PWMA = Sum(i^2 * P_i) / Sum(i^2)
///
/// O(1) update logic:
/// S1_new = S1_old - oldest + newest
/// S2_new = S2_old - S1_old + n * newest
/// S3_new = S3_old - 2*S2_old + S1_old + n^2 * newest
///
/// Where:
/// S1 is simple sum
/// S2 is linear weighted sum
/// S3 is parabolic weighted sum
/// Calculation: <c>PWMA = Σ(i²×P_i) / Σ(i²)</c> with efficient incremental updates.
/// </remarks>
/// <seealso href="Pwma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Pwma : AbstractBase
{
+4 -22
View File
@@ -1,6 +1,3 @@
// Sgma.cs - Savitzky-Golay Moving Average
// FIR filter using polynomial fitting for smoothing with shape preservation.
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -9,29 +6,14 @@ namespace QuanTAlib;
/// <summary>
/// SGMA: Savitzky-Golay Moving Average
/// A FIR filter that uses polynomial fitting to smooth data while preserving
/// higher moments (peaks, valleys, and inflection points) better than simple averaging.
/// </summary>
/// <remarks>
/// <b>Key characteristics</b>
/// <list type="bullet">
/// <item><description>Uses polynomial fitting for smoothing</description></item>
/// <item><description>Preserves peak shapes better than standard MAs</description></item>
/// <item><description>Period must be odd (even periods are adjusted to next odd)</description></item>
/// <item><description>Polynomial degree (0-4) controls smoothing vs shape preservation</description></item>
/// <item><description>O(N) complexity per bar due to window convolution</description></item>
/// </list>
/// Polynomial-fitting FIR filter preserving peaks and inflection points.
/// Superior shape preservation vs standard MAs; odd period required.
///
/// <b>Weight calculation</b>
/// For polynomial degree d, weights are based on:
/// <code>
/// w_i = 1 - |norm_x|^d where norm_x = (i - half_window) / half_window
/// </code>
///
/// <b>Sources</b>
/// Savitzky, A., Golay, M.J.E. (1964) - "Smoothing and Differentiation of Data by Simplified Least Squares Procedures"
/// Analytical Chemistry 36(8): 1627-1639
/// Calculation: <c>W_i = 1 - |norm_x|^d</c> with degree 0-4 controlling smoothing.
/// </remarks>
/// <seealso href="Sgma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Sgma : AbstractBase
{
+4 -13
View File
@@ -8,21 +8,12 @@ namespace QuanTAlib;
/// SINEMA: Sine-Weighted Moving Average
/// </summary>
/// <remarks>
/// <para>SINEMA applies sine-wave weighting to data points within the lookback window.
/// Weights are calculated as sin(π * (i+1) / period) for each position i, creating a
/// smooth bell-shaped weighting that emphasizes middle values while gracefully
/// tapering at the edges.</para>
/// <para>Calculation:
/// w[i] = sin(π * (i+1) / period)
/// SINEMA = Σ(P[i] * w[i]) / Σ(w[i])</para>
/// Sine-wave weighting creating smooth bell-shaped emphasis on middle values.
/// Better noise reduction than SMA while preserving mid-frequency trends.
///
/// Unlike SMA's uniform weighting or WMA's linear ramp, sine weighting provides
/// a smooth transition that can reduce high-frequency noise while preserving
/// mid-frequency trends.
///
/// IsHot:
/// Becomes true when the buffer is full (period samples processed).
/// Calculation: <c>W_i = sin(π×(i+1)/n)</c>; <c>SINEMA = Σ(P_i×W_i) / Σ(W_i)</c>.
/// </remarks>
/// <seealso href="Sinema.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Sinema : AbstractBase
{
+4 -10
View File
@@ -12,18 +12,12 @@ namespace QuanTAlib;
/// SMA: Simple Moving Average
/// </summary>
/// <remarks>
/// <para>SMA calculates the arithmetic mean of the last n values.
/// Uses a RingBuffer for storage and manual running sum for O(1) complexity per update.</para>
/// <para>Calculation:
/// SMA = (P_n + P_(n-1) + ... + P_1) / n</para>
/// Arithmetic mean of the last n values using running sum for O(1) updates.
/// SIMD-accelerated batch processing (AVX-512/AVX2/NEON).
///
/// O(1) update:
/// S_new = S_old - oldest + newest
/// SMA = S_new / n
///
/// IsHot:
/// Becomes true when the buffer is full (period samples processed).
/// Calculation: <c>SMA = Σ(values) / n</c>.
/// </remarks>
/// <seealso href="Sma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Sma : AbstractBase
{
+3 -12
View File
@@ -8,20 +8,11 @@ namespace QuanTAlib;
/// TRIMA: Triangular Moving Average
/// </summary>
/// <remarks>
/// TRIMA applies triangular weighting to data points, emphasizing the middle of the window.
/// Equivalent to a double SMA: SMA(SMA(period1), period2).
/// Triangular weighting emphasizing the middle via double SMA. O(1) updates.
///
/// Calculation:
/// p1 = (period + 1) / 2
/// p2 = period / 2 + 1
/// TRIMA = SMA(SMA(input, p1), p2)
///
/// O(1) update:
/// Uses two SMA instances, each with O(1) update complexity.
///
/// IsHot:
/// Becomes true when both internal SMAs are hot.
/// Calculation: <c>TRIMA = SMA(SMA(p1), p2)</c> where <c>p1 = (n+1)/2</c>, <c>p2 = n/2+1</c>.
/// </remarks>
/// <seealso href="Trima.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Trima : AbstractBase
{
+4 -10
View File
@@ -10,18 +10,12 @@ namespace QuanTAlib;
/// WMA: Weighted Moving Average
/// </summary>
/// <remarks>
/// <para>WMA applies linear weighting to data points, giving more weight to recent values.
/// Uses dual running sums for O(1) complexity per update.</para>
/// <para>Calculation:
/// WMA = (n*P_n + (n-1)*P_(n-1) + ... + 1*P_1) / (n*(n+1)/2)</para>
/// Linear weighting giving more weight to recent values. O(1) via dual running sums.
/// SIMD-accelerated batch processing (AVX-512/AVX2/NEON).
///
/// O(1) update:
/// S_new = S - oldest + newest
/// W_new = W - S_old + n*newest
///
/// IsHot:
/// Becomes true when the buffer is full (period samples processed).
/// Calculation: <c>WMA = Σ(w_i × P_i) / Σ(w_i)</c> where <c>w_i = i</c>.
/// </remarks>
/// <seealso href="Wma.md">Detailed documentation</seealso>
[SkipLocalsInit]
public sealed class Wma : AbstractBase
{