mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-27 17:27:43 +00:00
67ad6f0cba
Comprehensive refactor across all indicators replacing the periodic ResyncInterval-based drift correction (every 1000 ticks recalculate from scratch) with Kahan compensated summation for running sums. Key changes: - Remove ResyncInterval constants and TickCount fields from all State records - Add Kahan compensation fields (SumComp, SumSqComp, etc.) to State records - Replace naive sum += val - removed with Kahan delta pattern - Remove Resync()/RecalculateSum() methods that did O(N) recalculation - Update batch/SIMD paths to use Kahan compensation instead of resync loops - IIR filters (EMA, REMA, RGMA) simplified: inherently self-correcting - Version bump to 0.8.7 - Build system: README version stamping via Directory.Build.props - Minor doc/test tolerance adjustments for new numerical characteristics Affected modules: channels, core, cycles, dynamics, errors, momentum, oscillators, statistics, trends_FIR, trends_IIR, volatility, volume
378 lines
11 KiB
C#
378 lines
11 KiB
C#
using System.Buffers;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace QuanTAlib;
|
|
|
|
/// <summary>
|
|
/// Bias (also known as Disparity Index): Measures the percentage deviation of a price from its moving average.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Bias (BIAS) calculates how far the current price deviates from its Simple Moving Average (SMA),
|
|
/// expressed as a percentage. It's commonly used to identify overbought/oversold conditions.
|
|
///
|
|
/// Formula:
|
|
/// BIAS = (Price - SMA) / SMA = Price/SMA - 1
|
|
///
|
|
/// Key Features:
|
|
/// - O(1) time complexity per update using running sum
|
|
/// - Zero allocation in hot path
|
|
/// - Handles division by zero (returns 0 when SMA is 0)
|
|
/// - NaN/Infinity safe with last-valid-value substitution
|
|
///
|
|
/// IsHot:
|
|
/// Becomes true when the buffer is full (period samples processed).
|
|
/// </remarks>
|
|
[SkipLocalsInit]
|
|
public sealed class Bias : AbstractBase
|
|
{
|
|
private readonly int _period;
|
|
private readonly RingBuffer _buffer;
|
|
private readonly TValuePublishedHandler _handler;
|
|
|
|
[StructLayout(LayoutKind.Auto)]
|
|
private record struct State
|
|
{
|
|
public double Sum;
|
|
public double SumComp;
|
|
public double LastInput;
|
|
public double LastValidValue;
|
|
}
|
|
|
|
private State _state;
|
|
private State _p_state;
|
|
private const double Epsilon = 1e-10;
|
|
|
|
/// <summary>
|
|
/// Creates Bias with specified period.
|
|
/// </summary>
|
|
/// <param name="period">Number of values for SMA calculation (must be > 0)</param>
|
|
public Bias(int period)
|
|
{
|
|
if (period <= 0)
|
|
{
|
|
throw new ArgumentException("Period must be greater than 0", nameof(period));
|
|
}
|
|
|
|
_period = period;
|
|
_buffer = new RingBuffer(period);
|
|
Name = $"Bias({period})";
|
|
WarmupPeriod = period;
|
|
_handler = Handle;
|
|
}
|
|
|
|
public Bias(ITValuePublisher source, int period) : this(period)
|
|
{
|
|
source.Pub += _handler;
|
|
}
|
|
|
|
public Bias(TSeries source, int period) : this(period)
|
|
{
|
|
source.Pub += _handler;
|
|
Prime(source.Values);
|
|
if (source.Count > 0)
|
|
{
|
|
Last = new TValue(source.LastTime, Last.Value);
|
|
}
|
|
_p_state = _state;
|
|
}
|
|
|
|
private void Handle(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
|
|
|
|
/// <summary>
|
|
/// True if Bias has enough data to produce valid results.
|
|
/// Bias is "hot" when the buffer is full (has received at least 'period' values).
|
|
/// </summary>
|
|
public override bool IsHot => _buffer.IsFull;
|
|
|
|
/// <summary>
|
|
/// Initializes the indicator state using the provided history.
|
|
/// </summary>
|
|
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
|
|
{
|
|
if (source.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Reset state
|
|
_buffer.Clear();
|
|
_state = default;
|
|
_p_state = default;
|
|
|
|
int warmupLength = Math.Min(source.Length, WarmupPeriod);
|
|
int startIndex = source.Length - warmupLength;
|
|
|
|
// Seed LastValidValue
|
|
_state.LastValidValue = double.NaN;
|
|
for (int i = startIndex - 1; i >= 0; i--)
|
|
{
|
|
if (double.IsFinite(source[i]))
|
|
{
|
|
_state.LastValidValue = source[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (double.IsNaN(_state.LastValidValue))
|
|
{
|
|
for (int i = startIndex; i < source.Length; i++)
|
|
{
|
|
if (double.IsFinite(source[i]))
|
|
{
|
|
_state.LastValidValue = source[i];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Feed the buffer and calculate sum
|
|
for (int i = startIndex; i < source.Length; i++)
|
|
{
|
|
double val = GetValidValue(source[i]);
|
|
_buffer.Add(val);
|
|
_state.Sum += val;
|
|
_state.LastInput = val;
|
|
}
|
|
|
|
// Calculate final Bias
|
|
double sma = _state.Sum / _buffer.Count;
|
|
double bias = Math.Abs(sma) > Epsilon ? (_state.LastInput - sma) / sma : 0;
|
|
Last = new TValue(DateTime.MinValue, bias);
|
|
_p_state = _state;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private double GetValidValue(double input)
|
|
{
|
|
if (double.IsFinite(input))
|
|
{
|
|
_state.LastValidValue = input;
|
|
return input;
|
|
}
|
|
return _state.LastValidValue;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private void UpdateState(double val)
|
|
{
|
|
double removed = _buffer.Count == _buffer.Capacity ? _buffer.Oldest : 0.0;
|
|
|
|
// Kahan compensated summation
|
|
double delta = val - removed - _state.SumComp;
|
|
double newSum = _state.Sum + delta;
|
|
_state.SumComp = (newSum - _state.Sum) - delta;
|
|
_state.Sum = newSum;
|
|
|
|
_buffer.Add(val);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override TValue Update(TValue input, bool isNew = true)
|
|
{
|
|
if (isNew)
|
|
{
|
|
_p_state = _state;
|
|
_buffer.Snapshot();
|
|
|
|
double val = GetValidValue(input.Value);
|
|
UpdateState(val);
|
|
_state.LastInput = val;
|
|
}
|
|
else
|
|
{
|
|
// Restore both scalar state and buffer state
|
|
_state = _p_state;
|
|
_buffer.Restore();
|
|
|
|
// Use restored LastValidValue for NaN handling without updating it
|
|
double val = double.IsFinite(input.Value) ? input.Value : _state.LastValidValue;
|
|
|
|
// Replicate the same operation as isNew=true: UpdateState
|
|
// This properly removes oldest and adds newest, maintaining sliding window
|
|
UpdateState(val);
|
|
_state.LastInput = val;
|
|
}
|
|
|
|
// Calculate Bias: (Price - SMA) / SMA
|
|
double sma = _state.Sum / _buffer.Count;
|
|
double bias = Math.Abs(sma) > Epsilon ? (_state.LastInput - sma) / sma : 0;
|
|
|
|
Last = new TValue(input.Time, bias);
|
|
PubEvent(Last, isNew);
|
|
return Last;
|
|
}
|
|
|
|
public override TSeries Update(TSeries source)
|
|
{
|
|
if (source.Count == 0)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
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);
|
|
|
|
Batch(source.Values, vSpan, _period);
|
|
source.Times.CopyTo(tSpan);
|
|
|
|
Prime(source.Values);
|
|
|
|
Last = new TValue(tSpan[len - 1], vSpan[len - 1]);
|
|
return new TSeries(t, v);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates Bias for the entire series using a new instance.
|
|
/// </summary>
|
|
public static TSeries Batch(TSeries source, int period)
|
|
{
|
|
var bias = new Bias(period);
|
|
return bias.Update(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculates Bias in-place using O(1) running sum.
|
|
/// Zero-allocation method for maximum performance.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period)
|
|
{
|
|
if (source.Length != output.Length)
|
|
{
|
|
throw new ArgumentException("Source and output must have the same length", nameof(output));
|
|
}
|
|
|
|
if (period <= 0)
|
|
{
|
|
throw new ArgumentException("Period must be greater than 0", nameof(period));
|
|
}
|
|
|
|
int len = source.Length;
|
|
if (len == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
CalculateScalarCore(source, output, period);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a batch calculation and returns a "Hot" Bias instance.
|
|
/// </summary>
|
|
public static (TSeries Results, Bias Indicator) Calculate(TSeries source, int period)
|
|
{
|
|
var bias = new Bias(period);
|
|
TSeries results = bias.Update(source);
|
|
return (results, bias);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static void CalculateScalarCore(ReadOnlySpan<double> source, Span<double> output, int period)
|
|
{
|
|
int len = source.Length;
|
|
|
|
const int StackAllocThreshold = 256;
|
|
double[]? bufferArray = period > StackAllocThreshold ? ArrayPool<double>.Shared.Rent(period) : null;
|
|
Span<double> buffer = period <= StackAllocThreshold
|
|
? stackalloc double[period]
|
|
: bufferArray!.AsSpan(0, period);
|
|
|
|
double sum = 0;
|
|
double sumComp = 0;
|
|
double lastValid = double.NaN;
|
|
|
|
// Find first valid value
|
|
for (int k = 0; k < len; k++)
|
|
{
|
|
if (double.IsFinite(source[k]))
|
|
{
|
|
lastValid = source[k];
|
|
break;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
int bufferIndex = 0;
|
|
|
|
// Warmup phase
|
|
int warmupEnd = Math.Min(period, len);
|
|
for (int i = 0; i < warmupEnd; i++)
|
|
{
|
|
double val = source[i];
|
|
if (double.IsFinite(val))
|
|
{
|
|
lastValid = val;
|
|
}
|
|
else
|
|
{
|
|
val = lastValid;
|
|
}
|
|
|
|
sum += val;
|
|
buffer[i] = val;
|
|
|
|
double n = i + 1;
|
|
double sma = sum / n;
|
|
output[i] = Math.Abs(sma) > Epsilon ? (val - sma) / sma : 0;
|
|
}
|
|
|
|
// Main phase with sliding window
|
|
for (int i = period; i < len; i++)
|
|
{
|
|
double val = source[i];
|
|
if (double.IsFinite(val))
|
|
{
|
|
lastValid = val;
|
|
}
|
|
else
|
|
{
|
|
val = lastValid;
|
|
}
|
|
|
|
// Kahan-compensated delta update for sum
|
|
double oldVal = buffer[bufferIndex];
|
|
double delta = val - oldVal;
|
|
double y = delta - sumComp;
|
|
double t = sum + y;
|
|
sumComp = (t - sum) - y;
|
|
sum = t;
|
|
|
|
buffer[bufferIndex] = val;
|
|
bufferIndex++;
|
|
if (bufferIndex >= period)
|
|
{
|
|
bufferIndex = 0;
|
|
}
|
|
|
|
double sma = sum / period;
|
|
output[i] = Math.Abs(sma) > Epsilon ? (val - sma) / sma : 0;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (bufferArray != null)
|
|
{
|
|
ArrayPool<double>.Shared.Return(bufferArray);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the Bias state.
|
|
/// </summary>
|
|
public override void Reset()
|
|
{
|
|
_buffer.Clear();
|
|
_state = default;
|
|
_p_state = default;
|
|
Last = default;
|
|
}
|
|
} |