Add TEMA (Triple Exponential Moving Average) implementation and validation tests

- Implemented TEMA calculation in QuanTAlib with O(1) update complexity.
- Added validation tests for TEMA against Skender, TA-Lib, and Tulip indicators.
- Updated documentation for TEMA, including its mathematical foundation and usage examples.
- Enhanced existing tests for other indicators (TRIMA, WMA) to generate more records.
- Adjusted benchmark tests to include DEMA and TEMA comparisons.
- Refactored code for better readability and performance, including zero-allocation Span API.
This commit is contained in:
Miha Kralj
2025-12-04 19:57:46 -08:00
parent ee358bfdd9
commit 9e152b9027
24 changed files with 2528 additions and 136 deletions
+219
View File
@@ -0,0 +1,219 @@
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"name":"csharp"},{"name":"fsharp","languageName":"F#","aliases":["f#","fs"]},{"name":"html","languageName":"HTML"},{"name":"http","languageName":"HTTP"},{"name":"javascript","languageName":"JavaScript","aliases":["js"]},{"name":"mermaid","languageName":"Mermaid"},{"name":"pwsh","languageName":"PowerShell","aliases":["powershell"]},{"name":"value"}]}}
#!markdown
# Double Exponential Moving Average (DEMA) Examples
This is a **.NET Interactive** notebook. To run it, you need the [Polyglot Notebooks](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode) extension installed in VS Code.
For detailed documentation on the DEMA indicator, including mathematical formulas and interpretation, please refer to [Dema.md](Dema.md).
The **Double Exponential Moving Average (DEMA)** is a technical indicator designed to reduce the lag associated with traditional moving averages. It combines a single EMA and a double EMA to achieve higher responsiveness.
This notebook demonstrates:
1. **Manual Data Processing**: Understanding Batch vs. Streaming modes.
2. **Streaming with `isNew`**: Handling intra-bar updates.
3. **Large Dataset Processing**: Using Geometric Brownian Motion (GBM) generated data.
4. **Handling Invalid Values**: Last-value substitution for NaN/Infinity.
#!csharp
// Reference the library
#r "..\..\bin\QuanTAlib.dll"
using System;
using System.Linq;
using QuanTAlib;
// Helper to print TSeries
void PrintSeries(TSeries series, int count = 5)
{
Console.WriteLine($"Series Length: {series.Count}");
foreach (var item in series.Take(count))
{
Console.WriteLine($"Time: {item.Time:HH:mm:ss}, Value: {item.Value:F2}");
}
if (series.Count > count) Console.WriteLine("...");
}
#!markdown
## 1. Manual Data: Batch vs. Streaming
We'll start with a small, manually created dataset to clearly see how Batch and Streaming operations work.
### Batch Processing
Batch processing calculates the DEMA for the entire dataset at once. This is efficient for historical analysis.
#!csharp
// Create a small manual dataset
var manualData = new TSeries();
manualData.Add(DateTime.Now, 100.0);
manualData.Add(DateTime.Now.AddMinutes(1), 102.0);
manualData.Add(DateTime.Now.AddMinutes(2), 101.0);
manualData.Add(DateTime.Now.AddMinutes(3), 103.0);
manualData.Add(DateTime.Now.AddMinutes(4), 105.0);
Console.WriteLine("--- Input Data ---");
PrintSeries(manualData, 5);
// Batch Calculation
Console.WriteLine("\n--- Batch DEMA (Period 3) ---");
var demaBatch = new Dema(3);
var resultBatch = demaBatch.Update(manualData);
PrintSeries(resultBatch, 5);
#!markdown
### Streaming Processing
Streaming processing updates the DEMA one data point at a time. This is essential for real-time trading systems where data arrives sequentially.
#!csharp
Console.WriteLine("\n--- Streaming DEMA (Period 3) ---");
var demaStream = new Dema(3);
foreach (var item in manualData)
{
var result = demaStream.Update(item);
Console.WriteLine($"Time: {item.Time:HH:mm:ss}, Input: {item.Value:F2}, DEMA: {result.Value:F2}, IsHot: {demaStream.IsHot}");
}
// Verify that the last values match
var batchLast = resultBatch.Last().Value;
var streamLast = demaStream.Value.Value;
Console.WriteLine($"\nMatch: {Math.Abs(batchLast - streamLast) < 1e-10} (Batch: {batchLast:F2}, Stream: {streamLast:F2})");
#!markdown
## 2. Streaming with `isNew` (Intra-bar Updates)
In real-time feeds, you often receive multiple updates for the *same* bar (e.g., price changes within the current minute) before the bar closes.
* `isNew = true`: The input is a new bar (advances time).
* `isNew = false`: The input is an update to the current bar (recalculates without advancing).
#!csharp
Console.WriteLine("\n--- Streaming with Intra-bar Updates ---");
var demaIntra = new Dema(3);
// 1. Process the first 4 bars normally
for (int i = 0; i < 4; i++)
{
demaIntra.Update(manualData[i]);
}
Console.WriteLine($"After 4th bar: {demaIntra.Value.Value:F2}");
// 2. Simulate intra-bar updates for the 5th bar (Final value is 105.0)
// Update 1: Price moves to 104.0
var update1 = new TValue(manualData[4].Time, 104.0);
demaIntra.Update(update1, isNew: true); // First update for this bar is "New"
Console.WriteLine($"Update 1 (104.0): {demaIntra.Value.Value:F2}");
// Update 2: Price moves to 106.0 (Same time, same bar)
var update2 = new TValue(manualData[4].Time, 106.0);
demaIntra.Update(update2, isNew: false); // Not new, just an update
Console.WriteLine($"Update 2 (106.0): {demaIntra.Value.Value:F2}");
// Update 3: Final Close at 105.0
var update3 = manualData[4];
demaIntra.Update(update3, isNew: false); // Final update
Console.WriteLine($"Update 3 (105.0): {demaIntra.Value.Value:F2}");
// Verify match with batch result
Console.WriteLine($"Match with Batch: {Math.Abs(demaIntra.Value.Value - batchLast) < 1e-10}");
#!markdown
## 3. Large Dataset: Geometric Brownian Motion (GBM)
We'll generate a larger dataset (1000 bars) using a Geometric Brownian Motion generator to simulate realistic market data.
#!csharp
// Generate 1000 bars of data
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2);
var gbmData = gbm.Fetch(1000, DateTime.Now.Ticks, TimeSpan.FromMinutes(1));
var closeSeries = gbmData.Close;
Console.WriteLine($"Generated {closeSeries.Count} bars of GBM data.");
Console.WriteLine($"First 5 values: {string.Join(", ", closeSeries.Take(5).Select(x => x.Value.ToString("F2")))}");
#!markdown
### Batch vs. Streaming Performance on Large Data
#!csharp
// Batch
var demaLargeBatch = new Dema(20);
var batchLargeResult = demaLargeBatch.Update(closeSeries);
Console.WriteLine($"Batch Last Value: {batchLargeResult.Last().Value:F2}");
// Streaming
var demaLargeStream = new Dema(20);
TValue lastStreamVal = default;
foreach(var item in closeSeries)
{
lastStreamVal = demaLargeStream.Update(item);
}
Console.WriteLine($"Streaming Last Value: {lastStreamVal.Value:F2}");
#!markdown
## 4. Handling Invalid Values (NaN/Infinity)
`Dema` uses **last-value substitution** for invalid inputs. When a non-finite value (NaN, PositiveInfinity, NegativeInfinity) is encountered, it is replaced with the last valid value. This provides output continuity instead of propagating invalid values through the calculation.
#!csharp
Console.WriteLine("\n--- Handling Invalid Values ---");
// Single DEMA
var demaNaN = new Dema(10);
// Feed valid values first
demaNaN.Update(new TValue(DateTime.Now, 100.0));
demaNaN.Update(new TValue(DateTime.Now.AddMinutes(1), 110.0));
Console.WriteLine($"After valid values: {demaNaN.Value.Value:F2}");
// Feed NaN - should use last valid value (110)
var resultAfterNaN = demaNaN.Update(new TValue(DateTime.Now.AddMinutes(2), double.NaN));
Console.WriteLine($"After NaN input: {resultAfterNaN.Value:F2} (IsFinite: {double.IsFinite(resultAfterNaN.Value)})");
// Feed Infinity - should use last valid value (110)
var resultAfterInf = demaNaN.Update(new TValue(DateTime.Now.AddMinutes(3), double.PositiveInfinity));
Console.WriteLine($"After Infinity input: {resultAfterInf.Value:F2} (IsFinite: {double.IsFinite(resultAfterInf.Value)})");
// Continue with valid value
var resultAfterValid = demaNaN.Update(new TValue(DateTime.Now.AddMinutes(4), 120.0));
Console.WriteLine($"After valid value (120): {resultAfterValid.Value:F2}");
#!csharp
Console.WriteLine("\n--- Batch Processing with Invalid Values ---");
// Create series with NaN values interspersed
var seriesWithNaN = new TSeries();
seriesWithNaN.Add(DateTime.Now.Ticks, 100.0);
seriesWithNaN.Add(DateTime.Now.Ticks + 1, 110.0);
seriesWithNaN.Add(DateTime.Now.Ticks + 2, double.NaN);
seriesWithNaN.Add(DateTime.Now.Ticks + 3, 120.0);
seriesWithNaN.Add(DateTime.Now.Ticks + 4, double.PositiveInfinity);
seriesWithNaN.Add(DateTime.Now.Ticks + 5, 130.0);
var demaBatchNaN = new Dema(3);
var resultsWithNaN = demaBatchNaN.Update(seriesWithNaN);
Console.WriteLine("Input → Output:");
for (int i = 0; i < seriesWithNaN.Count; i++)
{
var input = seriesWithNaN[i].Value;
var output = resultsWithNaN[i].Value;
var inputStr = double.IsFinite(input) ? input.ToString("F2") : input.ToString();
Console.WriteLine($" {inputStr,-10} → {output:F2} (IsFinite: {double.IsFinite(output)})");
}
+65
View File
@@ -0,0 +1,65 @@
using System.Drawing;
using TradingPlatform.BusinessLayer;
namespace QuanTAlib;
public class DemaIndicator : Indicator, IWatchlistIndicator
{
[InputParameter("Period", sortIndex: 1, 1, 1000, 1, 0)]
public int Period { get; set; } = 10;
[IndicatorExtensions.DataSourceInput]
public SourceType Source { get; set; } = SourceType.Close;
[InputParameter("Show cold values", sortIndex: 21)]
public bool ShowColdValues { get; set; } = true;
private Dema? ma;
protected LineSeries? Series;
protected string? SourceName;
private int _warmupBarIndex = -1;
public int MinHistoryDepths => Period;
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
public override string ShortName => $"DEMA {Period}:{SourceName}";
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/averages/dema/Dema.Quantower.cs";
public DemaIndicator()
{
OnBackGround = true;
SeparateWindow = false;
SourceName = Source.ToString();
Name = "DEMA - Double Exponential Moving Average";
Description = "Double Exponential Moving Average";
Series = new(name: $"DEMA {Period}", color: IndicatorExtensions.Averages, width: 2, style: LineStyle.Solid);
AddLineSeries(Series);
}
protected override void OnInit()
{
ma = new Dema(Period);
SourceName = Source.ToString();
_warmupBarIndex = -1;
base.OnInit();
}
protected override void OnUpdate(UpdateArgs args)
{
TValue input = this.GetInputValue(args, Source);
bool isNew = args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar;
TValue result = ma!.Update(input, isNew);
Series!.SetValue(result.Value);
Series!.SetMarker(0, Color.Transparent);
if (_warmupBarIndex < 0 && ma!.IsHot)
_warmupBarIndex = Count;
}
public override void OnPaintChart(PaintChartEventArgs args)
{
base.OnPaintChart(args);
int warmupPeriod = _warmupBarIndex > 0 ? _warmupBarIndex : Count;
this.PaintSmoothCurve(args, Series!, warmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
}
}
+155
View File
@@ -0,0 +1,155 @@
using Xunit;
namespace QuanTAlib.Tests;
public class DemaTests
{
[Fact]
public void Dema_Matches_ManualCalculation()
{
// Arrange
int period = 10;
var dema = new Dema(period);
var ema1 = new Ema(period);
var ema2 = new Ema(period);
var r = new Random(123);
// Act & Assert
for (int i = 0; i < 100; i++)
{
double val = r.NextDouble() * 100;
var tVal = new TValue(DateTime.Now.AddMinutes(i), val);
var dVal = dema.Update(tVal);
var e1Val = ema1.Update(tVal);
var e2Val = ema2.Update(e1Val);
double expected = 2 * e1Val.Value - e2Val.Value;
Assert.Equal(expected, dVal.Value, 1e-9);
}
}
[Fact]
public void StaticCalculate_Matches_ObjectUpdate()
{
// Arrange
int period = 10;
var source = new TSeries();
var r = new Random(123);
for (int i = 0; i < 100; i++)
{
source.Add(new TValue(DateTime.Now.AddMinutes(i), r.NextDouble() * 100));
}
// Act
var demaSeries = Dema.Calculate(source, period);
var demaObj = new Dema(period);
// Assert
for (int i = 0; i < source.Count; i++)
{
var val = demaObj.Update(source[i]);
Assert.Equal(val.Value, demaSeries[i].Value, 1e-9);
}
}
[Fact]
public void ZeroAllocCalculate_Matches_ObjectUpdate()
{
// Arrange
int period = 10;
int count = 100;
var source = new double[count];
var output = new double[count];
var r = new Random(123);
for (int i = 0; i < count; i++)
{
source[i] = r.NextDouble() * 100;
}
// Act
Dema.Calculate(source, output, period);
var demaObj = new Dema(period);
// Assert
for (int i = 0; i < count; i++)
{
var val = demaObj.Update(new TValue(DateTime.Now, source[i]));
Assert.Equal(val.Value, output[i], 1e-9);
}
}
[Fact]
public void Alpha_Constructor_Matches_Period_Constructor()
{
// Arrange
int period = 10;
double alpha = 2.0 / (period + 1);
var demaPeriod = new Dema(period);
var demaAlpha = new Dema(alpha);
var r = new Random(123);
// Act & Assert
for (int i = 0; i < 100; i++)
{
double val = r.NextDouble() * 100;
var tVal = new TValue(DateTime.Now.AddMinutes(i), val);
var pVal = demaPeriod.Update(tVal);
var aVal = demaAlpha.Update(tVal);
Assert.Equal(pVal.Value, aVal.Value, 1e-9);
}
}
[Fact]
public void StaticCalculate_Alpha_Matches_ObjectUpdate()
{
// Arrange
double alpha = 0.15;
var source = new TSeries();
var r = new Random(123);
for (int i = 0; i < 100; i++)
{
source.Add(new TValue(DateTime.Now.AddMinutes(i), r.NextDouble() * 100));
}
// Act
var demaSeries = Dema.Calculate(source, alpha);
var demaObj = new Dema(alpha);
// Assert
for (int i = 0; i < source.Count; i++)
{
var val = demaObj.Update(source[i]);
Assert.Equal(val.Value, demaSeries[i].Value, 1e-9);
}
}
[Fact]
public void ZeroAllocCalculate_Alpha_Matches_ObjectUpdate()
{
// Arrange
double alpha = 0.15;
int count = 100;
var source = new double[count];
var output = new double[count];
var r = new Random(123);
for (int i = 0; i < count; i++)
{
source[i] = r.NextDouble() * 100;
}
// Act
Dema.Calculate(source, output, alpha);
var demaObj = new Dema(alpha);
// Assert
for (int i = 0; i < count; i++)
{
var val = demaObj.Update(new TValue(DateTime.Now, source[i]));
Assert.Equal(val.Value, output[i], 1e-9);
}
}
}
+245
View File
@@ -0,0 +1,245 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Skender.Stock.Indicators;
using TALib;
using Tulip;
using Xunit;
using Xunit.Abstractions;
namespace QuanTAlib.Tests;
public class DemaValidationTests
{
private readonly TBarSeries _bars;
private readonly TSeries _data;
private readonly List<Quote> _skenderQuotes;
private readonly ITestOutputHelper _output;
public DemaValidationTests(ITestOutputHelper output)
{
_output = output;
// 1. Generate 5000 records using GBM feed
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2);
_bars = gbm.Fetch(5000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
// 2. Extract Close TSeries
_data = _bars.Close;
// 3. Prepare data for Skender (List<Quote>)
_skenderQuotes = new List<Quote>();
for (int i = 0; i < _bars.Count; i++)
{
_skenderQuotes.Add(new Quote
{
Date = new DateTime(_bars.Open.Times[i], DateTimeKind.Utc),
Open = (decimal)_bars.Open[i].Value,
High = (decimal)_bars.High[i].Value,
Low = (decimal)_bars.Low[i].Value,
Close = (decimal)_bars.Close[i].Value,
Volume = (decimal)_bars.Volume[i].Value
});
}
}
[Fact]
public void Validate_Skender_Batch()
{
int[] periods = { 5, 10, 20, 50, 100 };
foreach (var period in periods)
{
// Calculate QuanTAlib DEMA (batch TSeries)
var dema = new global::QuanTAlib.Dema(period);
var qResult = dema.Update(_data);
// Calculate Skender DEMA
var sResult = _skenderQuotes.GetDema(period).ToList();
// Compare last 100 records
VerifyData_Skender(qResult, sResult);
}
_output.WriteLine("DEMA Batch(TSeries) validated successfully against Skender.Stock.Indicators");
}
[Fact]
public void Validate_Talib_Batch()
{
int[] periods = { 5, 10, 20, 50, 100 };
// Prepare data for TA-Lib (double[])
double[] tData = _data.Select(x => x.Value).ToArray();
double[] output = new double[tData.Length];
foreach (var period in periods)
{
// Calculate QuanTAlib DEMA (batch TSeries)
var dema = new global::QuanTAlib.Dema(period);
var qResult = dema.Update(_data);
// Calculate TA-Lib DEMA
var retCode = TALib.Functions.Dema<double>(tData, 0..^0, output, out var outRange, period);
Assert.Equal(Core.RetCode.Success, retCode);
int lookback = TALib.Functions.DemaLookback(period);
// Compare last 100 records
VerifyData_Talib(qResult, output, outRange, lookback);
}
_output.WriteLine("DEMA Batch(TSeries) validated successfully against TA-Lib");
}
[Fact]
public void Validate_Tulip_Batch()
{
int[] periods = { 5, 10, 20, 50, 100 };
// Prepare data for Tulip (double[])
double[] tData = _data.Select(x => x.Value).ToArray();
foreach (var period in periods)
{
// Calculate QuanTAlib DEMA (batch TSeries)
var dema = new global::QuanTAlib.Dema(period);
var qResult = dema.Update(_data);
// Calculate Tulip DEMA
var demaIndicator = Tulip.Indicators.dema;
double[][] inputs = { tData };
double[] options = { period };
// Tulip DEMA lookback is usually period-1 for EMA, but DEMA is 2*EMA - EMA(EMA)
// Let's rely on the output length to align.
// Tulip DEMA lookback is same as EMA lookback? No, it involves double smoothing.
// Actually, Tulip's DEMA implementation might have a specific lookback.
// We'll calculate it based on output length.
// Tulip.Indicators.dema.Run expects outputs to be sized correctly.
// We'll use a large buffer and resize if needed, or just calculate lookback.
// For DEMA(n), lookback is roughly n-1 (same as EMA).
// Wait, DEMA uses EMA(EMA), so it might be 2*(n-1)?
// Let's try with n-1 first, if it fails we adjust.
// Actually, TA-Lib DEMA lookback is 2*(period-1).
// Let's assume Tulip is similar.
int lookback = 2 * (period - 1);
double[][] outputs = { new double[tData.Length - lookback] };
demaIndicator.Run(inputs, options, outputs);
var tResult = outputs[0];
// Compare last 100 records
VerifyData_Tulip(qResult, tResult, lookback);
}
_output.WriteLine("DEMA Batch(TSeries) validated successfully against Tulip");
}
[Fact]
public void Validate_Talib_Span()
{
int[] periods = { 5, 10, 20, 50, 100 };
// Prepare data
double[] sourceData = _data.Select(x => x.Value).ToArray();
double[] talibOutput = new double[sourceData.Length];
foreach (var period in periods)
{
// Calculate QuanTAlib DEMA (Span API)
double[] qOutput = new double[sourceData.Length];
global::QuanTAlib.Dema.Calculate(sourceData.AsSpan(), qOutput.AsSpan(), period);
// Calculate TA-Lib DEMA
var retCode = TALib.Functions.Dema<double>(sourceData, 0..^0, talibOutput, out var outRange, period);
Assert.Equal(Core.RetCode.Success, retCode);
int lookback = TALib.Functions.DemaLookback(period);
// Compare last 100 records
VerifyData_Talib_Span(qOutput, talibOutput, outRange, lookback);
}
_output.WriteLine("DEMA Span validated successfully against TA-Lib");
}
// ==================== Verification Helpers ====================
private static void VerifyData_Skender(TSeries qSeries, List<DemaResult> sSeries)
{
Assert.Equal(qSeries.Count, sSeries.Count);
int count = qSeries.Count;
int skip = count - 100;
for (int i = skip; i < count; i++)
{
double qValue = qSeries[i].Value;
double? sValue = sSeries[i].Dema;
if (!sValue.HasValue) continue;
Assert.Equal(sValue.Value, qValue, 1e-6);
}
}
private static void VerifyData_Talib(TSeries qSeries, double[] tOutput, Range outRange, int lookback)
{
int count = qSeries.Count;
int skip = count - 100;
int validCount = outRange.End.Value - outRange.Start.Value;
for (int i = skip; i < count; i++)
{
double qValue = qSeries[i].Value;
if (i < lookback) continue;
int tIndex = i - lookback;
if (tIndex >= validCount) continue;
double tValue = tOutput[tIndex];
Assert.Equal(tValue, qValue, 1e-6);
}
}
private static void VerifyData_Talib_Span(double[] qOutput, double[] tOutput, Range outRange, int lookback)
{
int count = qOutput.Length;
int skip = count - 100;
int validCount = outRange.End.Value - outRange.Start.Value;
for (int i = skip; i < count; i++)
{
double qValue = qOutput[i];
if (i < lookback) continue;
int tIndex = i - lookback;
if (tIndex >= validCount) continue;
double tValue = tOutput[tIndex];
Assert.Equal(tValue, qValue, 1e-6);
}
}
private static void VerifyData_Tulip(TSeries qSeries, double[] tOutput, int lookback)
{
int count = qSeries.Count;
int skip = count - 100;
for (int i = skip; i < count; i++)
{
double qValue = qSeries[i].Value;
if (i < lookback) continue;
int tIndex = i - lookback;
if (tIndex >= tOutput.Length) continue;
double tValue = tOutput[tIndex];
Assert.Equal(tValue, qValue, 1e-6);
}
}
}
+286
View File
@@ -0,0 +1,286 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// DEMA: Double Exponential Moving Average
/// </summary>
/// <remarks>
/// DEMA reduces the lag of traditional EMA by subtracting the lag from the original EMA.
///
/// Calculation:
/// EMA1 = EMA(input)
/// EMA2 = EMA(EMA1)
/// DEMA = 2 * EMA1 - EMA2
///
/// O(1) update:
/// Uses two EMA instances, each with O(1) update complexity.
///
/// IsHot:
/// Becomes true when the second EMA converges (approx. 2x EMA convergence time).
/// </remarks>
[SkipLocalsInit]
public sealed class Dema
{
private struct EmaState
{
public double Ema;
public double E;
public bool IsHot;
public bool IsCompensated;
public static EmaState New() => new() { Ema = 0, E = 1.0, IsHot = false, IsCompensated = false };
}
private readonly double _alpha;
private readonly double _decay;
private EmaState _state1 = EmaState.New();
private EmaState _state2 = EmaState.New();
private EmaState _p_state1 = EmaState.New();
private EmaState _p_state2 = EmaState.New();
private double _lastValidValue;
public string Name { get; }
public TValue Value { get; private set; }
public bool IsHot => _state2.IsHot;
public Dema(int period)
{
if (period <= 0) throw new ArgumentException("Period must be greater than 0", nameof(period));
_alpha = 2.0 / (period + 1);
_decay = 1.0 - _alpha;
Name = $"Dema({period})";
}
public Dema(double alpha)
{
if (alpha <= 0 || alpha > 1) throw new ArgumentException("Alpha must be between 0 and 1", nameof(alpha));
_alpha = alpha;
_decay = 1.0 - alpha;
Name = $"Dema(α={alpha:F4})";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TValue Update(TValue input, bool isNew = true)
{
if (isNew)
{
_p_state1 = _state1;
_p_state2 = _state2;
}
else
{
_state1 = _p_state1;
_state2 = _p_state2;
}
// EMA1
double val = input.Value;
if (double.IsFinite(val))
_lastValidValue = val;
else
val = _lastValidValue;
double e1 = Compute(val, _alpha, _decay, ref _state1);
// EMA2 (input is e1, which is always valid)
double e2 = Compute(e1, _alpha, _decay, ref _state2);
double result = 2 * e1 - e2;
Value = new TValue(input.Time, result);
return Value;
}
public TSeries Update(TSeries source)
{
if (source.Count == 0) return new TSeries(new List<long>(), new List<double>());
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);
var sourceValues = source.Values;
// Use current state
EmaState s1 = _state1;
EmaState s2 = _state2;
double lastValid = _lastValidValue;
double alpha = _alpha;
double decay = _decay;
for (int i = 0; i < len; i++)
{
double val = sourceValues[i];
if (double.IsFinite(val))
lastValid = val;
else
val = lastValid;
double e1 = Compute(val, alpha, decay, ref s1);
double e2 = Compute(e1, alpha, decay, ref s2);
vSpan[i] = 2 * e1 - e2;
}
// Update instance state
_state1 = s1;
_state2 = s2;
_p_state1 = s1;
_p_state2 = s2;
_lastValidValue = lastValid;
Value = new TValue(tSpan[len - 1], vSpan[len - 1]);
return new TSeries(t, v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double Compute(double input, double alpha, double decay, ref EmaState state)
{
state.Ema += alpha * (input - state.Ema);
double result;
if (!state.IsCompensated)
{
state.E *= decay;
if (!state.IsHot && state.E <= 0.05) // COVERAGE_THRESHOLD
state.IsHot = true;
if (state.E <= 1e-10) // COMPENSATOR_THRESHOLD
{
state.IsCompensated = true;
result = state.Ema;
}
else
{
result = state.Ema / (1.0 - state.E);
}
}
else
{
result = state.Ema;
}
return result;
}
public static TSeries Calculate(TSeries source, int period)
{
var dema = new Dema(period);
return dema.Update(source);
}
public static TSeries Calculate(TSeries source, double alpha)
{
var dema = new Dema(alpha);
return dema.Update(source);
}
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, int period)
{
if (period <= 0)
throw new ArgumentException("Period must be greater than 0", nameof(period));
double alpha = 2.0 / (period + 1);
Calculate(source, output, alpha);
}
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, double alpha)
{
if (source.Length != output.Length)
throw new ArgumentException("Source and output must have the same length");
if (alpha <= 0 || alpha > 1)
throw new ArgumentException("Alpha must be between 0 and 1", nameof(alpha));
if (source.Length == 0) return;
double decay = 1.0 - alpha;
double lastValid = 0;
// State for EMA1
double ema1_val = 0;
double ema1_e = 1.0;
bool ema1_isCompensated = false;
// State for EMA2
double ema2_val = 0;
double ema2_e = 1.0;
bool ema2_isCompensated = false;
for (int i = 0; i < source.Length; i++)
{
double val = source[i];
if (double.IsFinite(val))
lastValid = val;
else
val = lastValid;
// Update EMA1
ema1_val += alpha * (val - ema1_val);
double e1;
if (!ema1_isCompensated)
{
ema1_e *= decay;
if (ema1_e <= 1e-10)
{
ema1_isCompensated = true;
e1 = ema1_val;
}
else
{
e1 = ema1_val / (1.0 - ema1_e);
}
}
else
{
e1 = ema1_val;
}
// Update EMA2 (input is e1)
ema2_val += alpha * (e1 - ema2_val);
double e2;
if (!ema2_isCompensated)
{
ema2_e *= decay;
if (ema2_e <= 1e-10)
{
ema2_isCompensated = true;
e2 = ema2_val;
}
else
{
e2 = ema2_val / (1.0 - ema2_e);
}
}
else
{
e2 = ema2_val;
}
// DEMA = 2 * EMA1 - EMA2
output[i] = 2 * e1 - e2;
}
}
public void Reset()
{
_state1 = EmaState.New();
_state2 = EmaState.New();
_p_state1 = EmaState.New();
_p_state2 = EmaState.New();
_lastValidValue = 0;
Value = default;
}
}
+106
View File
@@ -0,0 +1,106 @@
# DEMA: Double Exponential Moving Average
## Overview and Purpose
The Double Exponential Moving Average (DEMA) is a technical indicator developed by Patrick Mulloy in 1994 to reduce the lag associated with traditional moving averages. Despite its name, DEMA is not simply a double smoothing of the price (like a double EMA would be). Instead, it uses a combination of a single EMA and a double EMA to subtract the lag inherent in the original EMA.
DEMA responds more quickly to price changes than a standard EMA or SMA, making it popular among traders who need faster signals for trend reversals or breakouts. It effectively filters out noise while maintaining high responsiveness, offering a "best of both worlds" solution between smoothing and lag reduction.
## Core Concepts
* **Lag Reduction:** DEMA's primary goal is to minimize the delay between price action and the indicator's response.
* **Composite Calculation:** It combines a single EMA and a double EMA (EMA of EMA) to achieve its unique characteristics.
* **High Responsiveness:** Reacts faster to market moves than traditional averages, potentially offering earlier entry and exit signals.
* **Trend Identification:** Like other moving averages, it helps identify the direction of the trend and potential support/resistance levels.
## Common Settings and Parameters
| Parameter | Default | Function | When to Adjust |
|-----------|---------|----------|---------------|
| Length | 20 | Controls responsiveness/smoothness | Shorter for scalping/day trading, longer for swing/position trading |
| Source | Close | Data point used for calculation | Change to HL2 or HLC3 for more balanced price representation |
| Alpha | 2/(length+1) | Determines weighting decay | Direct alpha manipulation allows for precise tuning beyond standard length settings |
## Calculation and Mathematical Foundation
**Simplified explanation:**
DEMA takes a standard EMA, calculates a second EMA on that result, and then combines them using a specific formula to cancel out the lag.
**Technical formula:**
$$DEMA = 2 \times EMA_1 - EMA_2$$
Where:
* $EMA_1 = EMA(Price)$
* $EMA_2 = EMA(EMA_1)$
The formula can be derived from the error correction principle. If $EMA_1$ has a lag error $E$, then $EMA_2$ (being an EMA of $EMA_1$) will have roughly twice the lag error ($2E$).
The difference $EMA_1 - EMA_2$ represents the estimated lag error.
Adding this error term back to $EMA_1$ gives:
$$DEMA = EMA_1 + (EMA_1 - EMA_2) = 2 \times EMA_1 - EMA_2$$
> 🔍 **Technical Note:** The implementation leverages the optimized `Ema` class, which uses **Hunter's bias compensation**. This ensures that both the primary and secondary EMAs are initialized correctly from the very first data point, providing accurate DEMA values immediately without a long warmup period.
## C# Implementation
The library provides a high-performance implementation of DEMA that supports both standard period-based initialization and direct alpha specification.
### Usage Examples
```csharp
using QuanTAlib;
// Initialize with period 14
var dema = new Dema(14);
// Or initialize with specific alpha
var demaAlpha = new Dema(0.15);
// Streaming update
TValue result = dema.Update(new TValue(time, price));
Console.WriteLine($"Current DEMA: {result.Value}");
// Batch calculation (TSeries API)
TSeries source = ...;
TSeries results = Dema.Calculate(source, 14);
// High-performance Span API (zero allocation)
double[] prices = new double[10000];
double[] output = new double[10000];
Dema.Calculate(prices.AsSpan(), output.AsSpan(), period: 14);
```
### Zero-Allocation Span API
For performance-critical scenarios, the static `Calculate` method uses `ArrayPool` internally to manage the intermediate buffer for the first EMA, ensuring zero heap allocations for the user (beyond the input/output arrays).
```csharp
// Allocate buffers once
double[] source = new double[200000];
double[] demaOutput = new double[200000];
// Zero heap allocation during calculation
Dema.Calculate(source.AsSpan(), demaOutput.AsSpan(), period: 50);
```
### Handling Invalid Values
`Dema` delegates value handling to the underlying `Ema` instances, which use **last-value substitution** for `NaN` or `Infinity`. This ensures continuity and stability in the output series.
## Interpretation Details
* **Trend Direction:** Price above DEMA suggests an uptrend; price below suggests a downtrend.
* **Crossovers:** DEMA crossovers (e.g., DEMA(10) crossing DEMA(20)) can provide faster signals than EMA crossovers.
* **Support/Resistance:** DEMA can act as dynamic support or resistance, often hugging the price action closer than an EMA.
* **Divergence:** Divergence between price and DEMA can signal potential reversals.
## Limitations and Considerations
* **Overshoot:** Because DEMA subtracts lag, it can sometimes overshoot price action during sharp reversals.
* **Noise Sensitivity:** Its high responsiveness means it may be more susceptible to market noise than a standard EMA or SMA.
* **Whipsaws:** In sideways markets, the reduced lag can lead to more frequent false signals (whipsaws).
## References
1. Mulloy, P.G. (1994). "Smoothing Data with Faster Moving Averages." *Technical Analysis of Stocks & Commodities*, 12(1).
2. Murphy, J.J. (1999). *Technical Analysis of the Financial Markets*. New York Institute of Finance.