mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 02:58:05 +00:00
Class optimization
This commit is contained in:
+40
-17
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -33,11 +32,13 @@ public class Maaf : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _priceBuffer;
|
||||
private readonly CircularBuffer _smoothBuffer;
|
||||
private double _prevFilter, _prevValue2;
|
||||
private readonly double _threshold;
|
||||
private double _p_prevFilter, _p_prevValue2;
|
||||
|
||||
private readonly int _period;
|
||||
private readonly double _invSix = 1.0 / 6.0;
|
||||
private readonly double[] _sortBuffer; // Pre-allocated buffer for sorting
|
||||
|
||||
private double _prevFilter, _prevValue2;
|
||||
private double _p_prevFilter, _p_prevValue2;
|
||||
|
||||
/// <param name="period">The initial period for the filter (default 39).</param>
|
||||
/// <param name="threshold">The threshold for adaptive adjustment (default 0.002).</param>
|
||||
@@ -47,6 +48,7 @@ public class Maaf : AbstractBase
|
||||
_threshold = threshold;
|
||||
_priceBuffer = new CircularBuffer(4);
|
||||
_smoothBuffer = new CircularBuffer(period);
|
||||
_sortBuffer = new double[period]; // Pre-allocate sorting buffer
|
||||
Name = "MAAF";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
@@ -61,15 +63,17 @@ public class Maaf : AbstractBase
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_priceBuffer.Clear();
|
||||
_smoothBuffer.Clear();
|
||||
_prevFilter = 0;
|
||||
_prevValue2 = 0;
|
||||
base.Init();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -86,6 +90,30 @@ public class Maaf : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double CalculateSmooth()
|
||||
{
|
||||
return (_priceBuffer[^1] + 2.0 * (_priceBuffer[^2] + _priceBuffer[^3]) + _priceBuffer[^4]) * _invSix;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double GetMedian(int length)
|
||||
{
|
||||
// Copy values to pre-allocated buffer
|
||||
var span = _smoothBuffer.GetSpan().Slice(_smoothBuffer.Count - length, length);
|
||||
span.CopyTo(_sortBuffer.AsSpan(0, length));
|
||||
|
||||
// Sort the required portion
|
||||
System.Array.Sort(_sortBuffer, 0, length);
|
||||
return _sortBuffer[length / 2];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static double CalculateAlpha(int length)
|
||||
{
|
||||
return 2.0 / (length + 1);
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(IsNew);
|
||||
@@ -97,7 +125,7 @@ public class Maaf : AbstractBase
|
||||
return Input.Value;
|
||||
}
|
||||
|
||||
double smooth = (_priceBuffer[^1] + (2 * _priceBuffer[^2]) + (2 * _priceBuffer[^3]) + _priceBuffer[^4]) / 6;
|
||||
double smooth = CalculateSmooth();
|
||||
_smoothBuffer.Add(smooth, Input.IsNew);
|
||||
|
||||
if (_smoothBuffer.Count < _period)
|
||||
@@ -111,28 +139,23 @@ public class Maaf : AbstractBase
|
||||
|
||||
while (value3 > _threshold && length > 0)
|
||||
{
|
||||
double alpha = 2.0 / (length + 1);
|
||||
|
||||
var sortedValues = _smoothBuffer.TakeLast(length).OrderBy(x => x).ToList();
|
||||
double value1 = sortedValues[length / 2];
|
||||
double alpha = CalculateAlpha(length);
|
||||
double value1 = GetMedian(length);
|
||||
value2 = alpha * (smooth - _prevValue2) + _prevValue2;
|
||||
|
||||
if (value1 != 0)
|
||||
{
|
||||
value3 = Math.Abs(value1 - value2) / value1;
|
||||
value3 = System.Math.Abs(value1 - value2) / value1;
|
||||
}
|
||||
|
||||
length -= 2;
|
||||
}
|
||||
|
||||
if (length < 3) length = 3;
|
||||
|
||||
double finalAlpha = 2.0 / (length + 1);
|
||||
length = System.Math.Max(length, 3);
|
||||
double finalAlpha = CalculateAlpha(length);
|
||||
double filter = finalAlpha * (smooth - _prevFilter) + _prevFilter;
|
||||
|
||||
_p_prevFilter = _prevFilter;
|
||||
_prevFilter = filter;
|
||||
_p_prevValue2 = _prevValue2;
|
||||
_prevValue2 = value2;
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
Reference in New Issue
Block a user