Files
QuanTAlib/lib/trends/dwma/Dwma.cs
T

145 lines
4.1 KiB
C#
Raw Normal View History

2025-12-10 18:22:10 -05:00
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// 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.
///
/// Formula:
/// DWMA = WMA(WMA(source, period), period)
/// </remarks>
[SkipLocalsInit]
2025-12-16 21:16:50 -08:00
public sealed class Dwma : AbstractBase
2025-12-10 18:22:10 -05:00
{
private readonly int _period;
private readonly Wma _wma1;
private readonly Wma _wma2;
private readonly TValuePublishedHandler _handler;
2025-12-10 18:22:10 -05:00
2025-12-16 21:16:50 -08:00
public override bool IsHot => _wma1.IsHot && _wma2.IsHot;
2025-12-10 18:22:10 -05:00
/// <summary>
/// Creates DWMA with specified period.
/// </summary>
/// <param name="period">Window size (must be > 0)</param>
public Dwma(int period)
{
if (period <= 0)
throw new ArgumentException("Period must be greater than 0", nameof(period));
_period = period;
_wma1 = new Wma(period);
_wma2 = new Wma(period);
_handler = Handle;
2025-12-10 18:22:10 -05:00
Name = $"Dwma({period})";
2025-12-16 21:16:50 -08:00
WarmupPeriod = period * 2;
2025-12-10 18:22:10 -05:00
}
public Dwma(ITValuePublisher source, int period) : this(period)
{
source.Pub += _handler;
2025-12-10 18:22:10 -05:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-12-16 21:16:50 -08:00
public override TValue Update(TValue input, bool isNew = true)
2025-12-10 18:22:10 -05:00
{
TValue wma1Result = _wma1.Update(input, isNew);
Last = _wma2.Update(wma1Result, isNew);
PubEvent(Last, isNew);
2025-12-10 18:22:10 -05:00
return Last;
}
2025-12-16 21:16:50 -08:00
public override TSeries Update(TSeries source)
2025-12-10 18:22:10 -05:00
{
if (source.Count == 0) return [];
2025-12-10 18:22:10 -05:00
int len = source.Count;
var t = new List<long>(len);
var v = new List<double>(len);
CollectionsMarshal.SetCount(t, len);
CollectionsMarshal.SetCount(v, len);
var tSpan = CollectionsMarshal.AsSpan(t);
var vSpan = CollectionsMarshal.AsSpan(v);
source.Times.CopyTo(tSpan);
Calculate(source.Values, vSpan, _period);
// Restore state
// We need to replay the last part to restore the internal WMAs state
// Since DWMA is WMA(WMA), the effective lookback is roughly 2*Period
// But to be safe and simple, we can just reset and replay the last 2*Period bars.
2025-12-16 21:16:50 -08:00
2025-12-10 18:22:10 -05:00
_wma1.Reset();
_wma2.Reset();
2025-12-16 21:16:50 -08:00
2025-12-10 18:22:10 -05:00
int warmup = _period * 2; // Approximate warmup needed
int startIndex = Math.Max(0, len - warmup);
2025-12-16 21:16:50 -08:00
2025-12-10 18:22:10 -05:00
for (int i = startIndex; i < len; i++)
{
Update(new TValue(source.Times[i], source.Values[i]));
}
return new TSeries(t, v);
}
private void Handle(object? sender, TValueEventArgs args)
{
Update(args.Value, args.IsNew);
}
2025-12-16 21:16:50 -08:00
public override void Prime(ReadOnlySpan<double> source)
{
Reset();
foreach (var value in source)
{
Update(new TValue(DateTime.MinValue, value));
}
}
public static TSeries Batch(TSeries source, int period)
2025-12-10 18:22:10 -05:00
{
var dwma = new Dwma(period);
return dwma.Update(source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period)
{
if (period <= 0)
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than zero");
2025-12-10 18:22:10 -05:00
if (source.Length != output.Length)
throw new ArgumentException("Source and output must have the same length", nameof(output));
2025-12-10 18:22:10 -05:00
// We need a temporary buffer for the first WMA pass
// Use stackalloc for small sizes, heap for large
if (source.Length <= 1024)
{
Span<double> temp = stackalloc double[source.Length];
2025-12-16 21:16:50 -08:00
Wma.Batch(source, temp, period);
Wma.Batch(temp, output, period);
2025-12-10 18:22:10 -05:00
}
else
{
double[] temp = new double[source.Length];
2025-12-16 21:16:50 -08:00
Wma.Batch(source, temp, period);
Wma.Batch(temp, output, period);
2025-12-10 18:22:10 -05:00
}
}
2025-12-16 21:16:50 -08:00
public override void Reset()
2025-12-10 18:22:10 -05:00
{
_wma1.Reset();
_wma2.Reset();
Last = default;
}
}