mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-23 13:08:04 +00:00
clean code fixes
This commit is contained in:
+20
-10
@@ -9,7 +9,8 @@ namespace QuanTAlib;
|
||||
/// the most recent value in a given period. It uses a circular buffer to
|
||||
/// efficiently manage the data points within the specified period.
|
||||
/// </remarks>
|
||||
public class Zscore : AbstractBase {
|
||||
public class Zscore : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
|
||||
@@ -20,8 +21,10 @@ public class Zscore : AbstractBase {
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 2.
|
||||
/// </exception>
|
||||
public Zscore(int period) : base() {
|
||||
if (period < 2) {
|
||||
public Zscore(int period)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2 for Z-score calculation.");
|
||||
}
|
||||
Period = period;
|
||||
@@ -36,7 +39,8 @@ public class Zscore : AbstractBase {
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the Z-score.</param>
|
||||
public Zscore(object source, int period) : this(period) {
|
||||
public Zscore(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
@@ -44,7 +48,8 @@ public class Zscore : AbstractBase {
|
||||
/// <summary>
|
||||
/// Initializes the Zscore instance by clearing the buffer.
|
||||
/// </summary>
|
||||
public override void Init() {
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
@@ -53,8 +58,10 @@ public class Zscore : AbstractBase {
|
||||
/// Manages the state of the Zscore instance based on whether a new value is being processed.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
||||
protected override void ManageState(bool isNew) {
|
||||
if (isNew) {
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
{
|
||||
_lastValidValue = Input.Value;
|
||||
_index++;
|
||||
}
|
||||
@@ -72,13 +79,15 @@ public class Zscore : AbstractBase {
|
||||
/// where x is the input value, μ is the mean of the period, and σ is the sample standard deviation.
|
||||
/// If there are fewer than 2 data points or if the standard deviation is 0, the method returns 0.
|
||||
/// </remarks>
|
||||
protected override double Calculation() {
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double zScore = 0;
|
||||
if (_buffer.Count >= 2) { // We need at least 2 data points for Z-score
|
||||
if (_buffer.Count >= 2)
|
||||
{ // We need at least 2 data points for Z-score
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
double mean = values.Average();
|
||||
double n = values.Length;
|
||||
@@ -86,7 +95,8 @@ public class Zscore : AbstractBase {
|
||||
double sumSquaredDeviations = values.Sum(x => Math.Pow(x - mean, 2));
|
||||
double standardDeviation = Math.Sqrt(sumSquaredDeviations / (n - 1)); // Sample standard deviation
|
||||
|
||||
if (standardDeviation != 0) { // Avoid division by zero
|
||||
if (standardDeviation != 0)
|
||||
{ // Avoid division by zero
|
||||
zScore = (Input.Value - mean) / standardDeviation;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user