2024-10-27 09:38:53 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
2024-09-22 17:31:24 -07:00
|
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// <summary>
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// ZSCORE: Standardized Distance Measure
|
|
|
|
|
|
/// A statistical measure that indicates how many standard deviations an observation
|
|
|
|
|
|
/// is from the mean. Z-scores normalize data to a standard scale, making it useful
|
|
|
|
|
|
/// for comparing values across different distributions.
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// The Zscore calculation process:
|
|
|
|
|
|
/// 1. Calculates mean of the period
|
|
|
|
|
|
/// 2. Computes standard deviation
|
|
|
|
|
|
/// 3. Measures distance from mean
|
|
|
|
|
|
/// 4. Normalizes by standard deviation
|
2024-10-11 18:02:09 -07:00
|
|
|
|
///
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// Key characteristics:
|
|
|
|
|
|
/// - Scale-independent measure
|
|
|
|
|
|
/// - Symmetric around zero
|
|
|
|
|
|
/// - Normal distribution context
|
|
|
|
|
|
/// - Outlier identification
|
|
|
|
|
|
/// - Comparative analysis tool
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Formula:
|
|
|
|
|
|
/// Z = (x - μ) / σ
|
|
|
|
|
|
/// where:
|
|
|
|
|
|
/// x = current value
|
|
|
|
|
|
/// μ = mean
|
|
|
|
|
|
/// σ = standard deviation
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Market Applications:
|
|
|
|
|
|
/// - Mean reversion strategies
|
|
|
|
|
|
/// - Overbought/oversold signals
|
|
|
|
|
|
/// - Volatility breakouts
|
|
|
|
|
|
/// - Cross-asset comparison
|
|
|
|
|
|
/// - Statistical arbitrage
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Sources:
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Standard_score
|
|
|
|
|
|
/// "Statistical Analysis in Trading" - Technical Analysis
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Note: Assumes approximately normal distribution
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// </remarks>
|
2024-10-27 09:38:53 -07:00
|
|
|
|
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public class Zscore : AbstractBase
|
|
|
|
|
|
{
|
2024-09-30 15:53:48 -07:00
|
|
|
|
private readonly int Period;
|
2024-09-23 22:08:40 -07:00
|
|
|
|
private readonly CircularBuffer _buffer;
|
2024-09-22 17:31:24 -07:00
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// <param name="period">The number of points to consider for Z-score calculation.</param>
|
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
|
2024-10-06 14:44:43 -07:00
|
|
|
|
public Zscore(int period)
|
2024-10-06 06:59:26 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (period < 2)
|
|
|
|
|
|
{
|
2024-10-27 09:38:53 -07:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(period),
|
|
|
|
|
|
"Period must be greater than or equal to 2 for Z-score calculation.");
|
2024-09-22 17:31:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
Period = period;
|
|
|
|
|
|
WarmupPeriod = 2;
|
|
|
|
|
|
_buffer = new CircularBuffer(period);
|
|
|
|
|
|
Name = $"ZScore(period={period})";
|
|
|
|
|
|
Init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// <param name="source">The data source object that publishes updates.</param>
|
|
|
|
|
|
/// <param name="period">The number of points to consider for Z-score calculation.</param>
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public Zscore(object source, int period) : this(period)
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
var pubEvent = source.GetType().GetEvent("Pub");
|
|
|
|
|
|
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public override void Init()
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
base.Init();
|
|
|
|
|
|
_buffer.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-06 06:59:26 +00:00
|
|
|
|
protected override void ManageState(bool isNew)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
_lastValidValue = Input.Value;
|
|
|
|
|
|
_index++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-06 06:59:26 +00:00
|
|
|
|
protected override double Calculation()
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
ManageState(Input.IsNew);
|
|
|
|
|
|
_buffer.Add(Input.Value, Input.IsNew);
|
|
|
|
|
|
|
|
|
|
|
|
double zScore = 0;
|
2024-10-27 09:38:53 -07:00
|
|
|
|
if (_buffer.Count >= 2) // Need at least 2 points for standard deviation
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
var values = _buffer.GetSpan().ToArray();
|
|
|
|
|
|
double mean = values.Average();
|
|
|
|
|
|
double n = values.Length;
|
|
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
// Calculate sample standard deviation
|
2024-09-22 17:31:24 -07:00
|
|
|
|
double sumSquaredDeviations = values.Sum(x => Math.Pow(x - mean, 2));
|
2024-10-27 09:38:53 -07:00
|
|
|
|
double standardDeviation = Math.Sqrt(sumSquaredDeviations / (n - 1));
|
2024-09-22 17:31:24 -07:00
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
if (standardDeviation != 0) // Avoid division by zero
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
zScore = (Input.Value - mean) / standardDeviation;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IsHot = _buffer.Count >= Period;
|
|
|
|
|
|
return zScore;
|
|
|
|
|
|
}
|
2024-10-11 18:02:09 -07:00
|
|
|
|
}
|