feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
// Relative Volatility Index (RVI) Indicator — Revised (1995) version
|
|
|
|
|
|
// Averages original RVI computed on High and Low series separately
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// RVI: Relative Volatility Index (Revised)
|
|
|
|
|
|
/// Computes original RVI on the High series and on the Low series, then averages.
|
|
|
|
|
|
/// Each channel classifies stddev direction based on its own price change.
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// <b>Calculation steps (per channel — High and Low independently):</b>
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// <list type="number">
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// <item>Calculate population standard deviation over stdevLength</item>
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// <item>Classify by price change: if up, upStd = stddev; if down, downStd = stddev</item>
|
|
|
|
|
|
/// <item>Smooth upStd and downStd with RMA (Wilder's smoothing with bias correction)</item>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// <item>channelRVI = 100 × avgUpStd / (avgUpStd + avgDownStd)</item>
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// </list>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// <b>Final:</b> RVI = (RVI_high + RVI_low) / 2
|
2026-02-02 13:42:47 -08:00
|
|
|
|
///
|
|
|
|
|
|
/// <b>Sources:</b>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// Donald Dorsey (1993, original; 1995, revised). Technical Analysis of Stocks & Commodities.
|
|
|
|
|
|
/// FM Labs: https://www.fmlabs.com/reference/RVI.htm
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// </remarks>
|
|
|
|
|
|
[SkipLocalsInit]
|
|
|
|
|
|
public sealed class Rvi : AbstractBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private const double Epsilon = 1e-10;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly int _stdevLength;
|
|
|
|
|
|
private readonly int _rmaLength;
|
|
|
|
|
|
private readonly double _alpha;
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
private readonly RingBuffer _hiBuf;
|
|
|
|
|
|
private readonly RingBuffer _loBuf;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Auto)]
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
private record struct ChState(
|
2026-02-02 13:42:47 -08:00
|
|
|
|
double PrevPrice,
|
|
|
|
|
|
double Sum,
|
|
|
|
|
|
double SumSq,
|
|
|
|
|
|
double RawRmaUp,
|
|
|
|
|
|
double EUp,
|
|
|
|
|
|
double RawRmaDown,
|
|
|
|
|
|
double EDown,
|
|
|
|
|
|
int FillCount
|
|
|
|
|
|
);
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
private ChState _hi, _phi;
|
|
|
|
|
|
private ChState _lo, _plo;
|
|
|
|
|
|
private double _lastValue, _pLastValue;
|
|
|
|
|
|
|
2026-02-02 13:42:47 -08:00
|
|
|
|
public Rvi(int stdevLength = 10, int rmaLength = 14)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (stdevLength < 2)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("Standard deviation length must be at least 2", nameof(stdevLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
if (rmaLength < 1)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("RMA length must be at least 1", nameof(rmaLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
2026-02-02 13:42:47 -08:00
|
|
|
|
_stdevLength = stdevLength;
|
|
|
|
|
|
_rmaLength = rmaLength;
|
|
|
|
|
|
_alpha = 1.0 / rmaLength;
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
_hiBuf = new RingBuffer(stdevLength);
|
|
|
|
|
|
_loBuf = new RingBuffer(stdevLength);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
WarmupPeriod = stdevLength + rmaLength;
|
|
|
|
|
|
Name = $"Rvi({stdevLength},{rmaLength})";
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
|
|
|
|
|
var init = new ChState(double.NaN, 0, 0, 0, 1.0, 0, 1.0, 0);
|
|
|
|
|
|
_hi = _phi = init;
|
|
|
|
|
|
_lo = _plo = init;
|
|
|
|
|
|
_lastValue = _pLastValue = 50.0;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Rvi(ITValuePublisher source, int stdevLength = 10, int rmaLength = 14)
|
|
|
|
|
|
: this(stdevLength, rmaLength)
|
|
|
|
|
|
{
|
|
|
|
|
|
source.Pub += Handle;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Handle(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
public override bool IsHot => _hi.FillCount >= _stdevLength;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
public int StdevLength => _stdevLength;
|
|
|
|
|
|
public int RmaLength => _rmaLength;
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public override TValue Update(TValue input, bool isNew = true)
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
return UpdateCore(input.Time, input.Value, input.Value, isNew);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public TValue Update(TBar bar, bool isNew = true)
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
return UpdateCore(bar.Time, bar.High, bar.Low, isNew);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TSeries Update(TBarSeries source)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (source.Count == 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
return [];
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
Span<double> highs = len <= 128 ? stackalloc double[len] : new double[len];
|
|
|
|
|
|
Span<double> lows = len <= 128 ? stackalloc double[len] : new double[len];
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
highs[i] = source[i].High;
|
|
|
|
|
|
lows[i] = source[i].Low;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
tSpan[i] = source[i].Time;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
BatchDual(highs, lows, vSpan, _stdevLength, _rmaLength);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
// Sync internal state
|
2026-02-02 13:42:47 -08:00
|
|
|
|
for (int i = 0; i < len; i++)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
Update(source[i], isNew: true);
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
2026-02-02 13:42:47 -08:00
|
|
|
|
public override TSeries Update(TSeries source)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (source.Count == 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
return [];
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
// Single-price series: same value to both channels
|
2026-02-02 13:42:47 -08:00
|
|
|
|
Batch(source.Values, vSpan, _stdevLength, _rmaLength);
|
|
|
|
|
|
source.Times.CopyTo(tSpan);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
Update(new TValue(source.Times[i], source.Values[i]), isNew: true);
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
private TValue UpdateCore(long timeTicks, double hiPrice, double loPrice, bool isNew)
|
2026-02-02 13:42:47 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
_phi = _hi;
|
|
|
|
|
|
_plo = _lo;
|
|
|
|
|
|
_pLastValue = _lastValue;
|
|
|
|
|
|
_hiBuf.Snapshot();
|
|
|
|
|
|
_loBuf.Snapshot();
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
_hi = _phi;
|
|
|
|
|
|
_lo = _plo;
|
|
|
|
|
|
_lastValue = _pLastValue;
|
|
|
|
|
|
_hiBuf.Restore();
|
|
|
|
|
|
_loBuf.Restore();
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
// Handle non-finite
|
|
|
|
|
|
if (!double.IsFinite(hiPrice) || !double.IsFinite(loPrice))
|
2026-02-02 13:42:47 -08:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
Last = new TValue(timeTicks, _lastValue);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
PubEvent(Last, isNew);
|
|
|
|
|
|
return Last;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
double rviHi = UpdateChannel(ref _hi, _hiBuf, hiPrice);
|
|
|
|
|
|
double rviLo = UpdateChannel(ref _lo, _loBuf, loPrice);
|
|
|
|
|
|
double rviValue = (rviHi + rviLo) * 0.5;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
if (!double.IsFinite(rviValue))
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
rviValue = _lastValue;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
else
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
_lastValue = rviValue;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
|
|
|
|
|
Last = new TValue(timeTicks, rviValue);
|
|
|
|
|
|
PubEvent(Last, isNew);
|
|
|
|
|
|
return Last;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
private double UpdateChannel(ref ChState s, RingBuffer buf, double price)
|
|
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
if (double.IsNaN(s.PrevPrice))
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
buf.Add(price);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
s = s with
|
|
|
|
|
|
{
|
|
|
|
|
|
PrevPrice = price,
|
|
|
|
|
|
Sum = price,
|
|
|
|
|
|
SumSq = price * price,
|
|
|
|
|
|
FillCount = 1
|
|
|
|
|
|
};
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
return 50.0;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
|
|
|
|
|
double priceChange = price - s.PrevPrice;
|
|
|
|
|
|
|
|
|
|
|
|
double oldSum = s.Sum;
|
|
|
|
|
|
double oldSumSq = s.SumSq;
|
|
|
|
|
|
int oldCount = s.FillCount;
|
|
|
|
|
|
|
|
|
|
|
|
if (buf.Count == _stdevLength)
|
2026-02-02 13:42:47 -08:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
double oldest = buf[0];
|
|
|
|
|
|
oldSum -= oldest;
|
|
|
|
|
|
oldSumSq -= oldest * oldest;
|
|
|
|
|
|
oldCount--;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
buf.Add(price);
|
|
|
|
|
|
double newSum = oldSum + price;
|
|
|
|
|
|
double newSumSq = oldSumSq + (price * price);
|
|
|
|
|
|
int newCount = oldCount + 1;
|
|
|
|
|
|
|
|
|
|
|
|
double currentStdDev = 0.0;
|
|
|
|
|
|
if (newCount > 1)
|
2026-02-02 13:42:47 -08:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
double mean = newSum / newCount;
|
|
|
|
|
|
double variance = (newSumSq / newCount) - (mean * mean);
|
|
|
|
|
|
variance = Math.Max(0.0, variance);
|
|
|
|
|
|
currentStdDev = Math.Sqrt(variance);
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
double upStdVal = 0.0;
|
|
|
|
|
|
double downStdVal = 0.0;
|
|
|
|
|
|
if (priceChange > 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
upStdVal = currentStdDev;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
else if (priceChange < 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
downStdVal = currentStdDev;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
double rawRmaUp = Math.FusedMultiplyAdd(s.RawRmaUp, _rmaLength - 1, upStdVal) / _rmaLength;
|
|
|
|
|
|
double eUp = (1 - _alpha) * s.EUp;
|
|
|
|
|
|
double avgUpStd = eUp > Epsilon ? rawRmaUp / (1.0 - eUp) : rawRmaUp;
|
|
|
|
|
|
|
|
|
|
|
|
double rawRmaDown = Math.FusedMultiplyAdd(s.RawRmaDown, _rmaLength - 1, downStdVal) / _rmaLength;
|
|
|
|
|
|
double eDown = (1 - _alpha) * s.EDown;
|
|
|
|
|
|
double avgDownStd = eDown > Epsilon ? rawRmaDown / (1.0 - eDown) : rawRmaDown;
|
|
|
|
|
|
|
|
|
|
|
|
double sumAvgStd = avgUpStd + avgDownStd;
|
|
|
|
|
|
double rvi = sumAvgStd > Epsilon ? (100.0 * avgUpStd / sumAvgStd) : 50.0;
|
|
|
|
|
|
|
|
|
|
|
|
s = new ChState(price, newSum, newSumSq, rawRmaUp, eUp, rawRmaDown, eDown, newCount);
|
|
|
|
|
|
return rvi;
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
2026-02-02 13:42:47 -08:00
|
|
|
|
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < source.Length; i++)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
Update(new TValue(DateTime.UtcNow, source[i]), isNew: true);
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
2026-02-02 13:42:47 -08:00
|
|
|
|
public override void Reset()
|
|
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
var init = new ChState(double.NaN, 0, 0, 0, 1.0, 0, 1.0, 0);
|
|
|
|
|
|
_hi = _phi = init;
|
|
|
|
|
|
_lo = _plo = init;
|
|
|
|
|
|
_lastValue = _pLastValue = 50.0;
|
|
|
|
|
|
_hiBuf.Clear();
|
|
|
|
|
|
_loBuf.Clear();
|
2026-02-02 13:42:47 -08:00
|
|
|
|
Last = default;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
// --- Static Batch methods ---
|
|
|
|
|
|
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// <summary>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// Batch RVI for a single-price series (same value to both channels → original behavior).
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// </summary>
|
2026-02-10 21:33:16 -08:00
|
|
|
|
public static TSeries Batch(TSeries source, int stdevLength = 10, int rmaLength = 14)
|
2026-02-02 13:42:47 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (stdevLength < 2)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("Standard deviation length must be at least 2", nameof(stdevLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
if (rmaLength < 1)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("RMA length must be at least 1", nameof(rmaLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
int len = source.Count;
|
|
|
|
|
|
var t = new List<long>(len);
|
|
|
|
|
|
var v = new List<double>(len);
|
|
|
|
|
|
CollectionsMarshal.SetCount(t, len);
|
|
|
|
|
|
CollectionsMarshal.SetCount(v, len);
|
|
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
Batch(source.Values, CollectionsMarshal.AsSpan(v), stdevLength, rmaLength);
|
|
|
|
|
|
source.Times.CopyTo(CollectionsMarshal.AsSpan(t));
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// Batch RVI for a bar series (revised: high+low average).
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// </summary>
|
2026-02-10 21:33:16 -08:00
|
|
|
|
public static TSeries Batch(TBarSeries source, int stdevLength = 10, int rmaLength = 14)
|
2026-02-02 13:42:47 -08:00
|
|
|
|
{
|
|
|
|
|
|
var rvi = new Rvi(stdevLength, rmaLength);
|
|
|
|
|
|
return rvi.Update(source);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
/// Span-based batch for single-price series. Same price to both channels → original behavior.
|
2026-02-02 13:42:47 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void Batch(
|
|
|
|
|
|
ReadOnlySpan<double> prices,
|
|
|
|
|
|
Span<double> output,
|
|
|
|
|
|
int stdevLength = 10,
|
|
|
|
|
|
int rmaLength = 14)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (stdevLength < 2)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("Standard deviation length must be at least 2", nameof(stdevLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
if (rmaLength < 1)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("RMA length must be at least 1", nameof(rmaLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
if (output.Length < prices.Length)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
throw new ArgumentException("Output span must be at least as long as prices span", nameof(output));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
// Single-price: feed same data to both channels, average = original
|
|
|
|
|
|
BatchDual(prices, prices, output, stdevLength, rmaLength);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Span-based batch for dual-channel (high + low) revised RVI.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void BatchDual(
|
|
|
|
|
|
ReadOnlySpan<double> highs,
|
|
|
|
|
|
ReadOnlySpan<double> lows,
|
|
|
|
|
|
Span<double> output,
|
|
|
|
|
|
int stdevLength = 10,
|
|
|
|
|
|
int rmaLength = 14)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (stdevLength < 2)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
throw new ArgumentException("Standard deviation length must be at least 2", nameof(stdevLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
if (rmaLength < 1)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
throw new ArgumentException("RMA length must be at least 1", nameof(rmaLength));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
|
|
|
|
|
int len = highs.Length;
|
|
|
|
|
|
if (len == 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
return;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
if (output.Length < len)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
throw new ArgumentException("Output span must be at least as long as input span", nameof(output));
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
|
|
|
|
|
|
// Allocate temp buffers for each channel's RVI output
|
|
|
|
|
|
Span<double> rviHi = len <= 256 ? stackalloc double[len] : new double[len];
|
|
|
|
|
|
Span<double> rviLo = len <= 256 ? stackalloc double[len] : new double[len];
|
|
|
|
|
|
|
|
|
|
|
|
BatchSingleChannel(highs, rviHi, stdevLength, rmaLength);
|
|
|
|
|
|
BatchSingleChannel(lows, rviLo, stdevLength, rmaLength);
|
|
|
|
|
|
|
|
|
|
|
|
// Average
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
output[i] = (rviHi[i] + rviLo[i]) * 0.5;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
feat: add new indicators (Decay, Edecay, MinusDi, MinusDm, PlusDi, PlusDm, Maxindex, Minindex, Sarext) and update pine scripts, core libs, validation tests, and python bindings
2026-03-09 13:45:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Computes original (single-channel) RVI for one price series.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static void BatchSingleChannel(
|
|
|
|
|
|
ReadOnlySpan<double> prices,
|
|
|
|
|
|
Span<double> output,
|
|
|
|
|
|
int stdevLength,
|
|
|
|
|
|
int rmaLength)
|
|
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
int len = prices.Length;
|
|
|
|
|
|
if (len == 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
return;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
double alpha = 1.0 / rmaLength;
|
|
|
|
|
|
|
|
|
|
|
|
Span<double> priceBuffer = stdevLength <= 256 ? stackalloc double[stdevLength] : new double[stdevLength];
|
|
|
|
|
|
int head = 0;
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
double sum = 0;
|
|
|
|
|
|
double sumSq = 0;
|
|
|
|
|
|
double prevPrice = double.NaN;
|
|
|
|
|
|
double lastValue = 50.0;
|
|
|
|
|
|
|
|
|
|
|
|
double rawRmaUp = 0;
|
|
|
|
|
|
double eUp = 1.0;
|
|
|
|
|
|
double rawRmaDown = 0;
|
|
|
|
|
|
double eDown = 1.0;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double price = prices[i];
|
|
|
|
|
|
|
|
|
|
|
|
if (double.IsNaN(prevPrice))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!double.IsFinite(price))
|
|
|
|
|
|
{
|
|
|
|
|
|
output[i] = lastValue;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (count < stdevLength)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
count++;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
double oldest = priceBuffer[head];
|
|
|
|
|
|
sum -= oldest;
|
|
|
|
|
|
sumSq -= oldest * oldest;
|
|
|
|
|
|
}
|
|
|
|
|
|
priceBuffer[head] = price;
|
|
|
|
|
|
head = (head + 1) % stdevLength;
|
|
|
|
|
|
sum += price;
|
|
|
|
|
|
sumSq += price * price;
|
|
|
|
|
|
|
|
|
|
|
|
prevPrice = price;
|
|
|
|
|
|
output[i] = 50.0;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!double.IsFinite(price))
|
|
|
|
|
|
{
|
|
|
|
|
|
output[i] = lastValue;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double priceChange = price - prevPrice;
|
|
|
|
|
|
prevPrice = price;
|
|
|
|
|
|
|
|
|
|
|
|
if (count < stdevLength)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
count++;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
double oldest = priceBuffer[head];
|
|
|
|
|
|
sum -= oldest;
|
|
|
|
|
|
sumSq -= oldest * oldest;
|
|
|
|
|
|
}
|
|
|
|
|
|
priceBuffer[head] = price;
|
|
|
|
|
|
head = (head + 1) % stdevLength;
|
|
|
|
|
|
sum += price;
|
|
|
|
|
|
sumSq += price * price;
|
|
|
|
|
|
|
|
|
|
|
|
double currentStdDev = 0.0;
|
|
|
|
|
|
if (count > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
double mean = sum / count;
|
|
|
|
|
|
double variance = (sumSq / count) - (mean * mean);
|
|
|
|
|
|
variance = Math.Max(0.0, variance);
|
|
|
|
|
|
currentStdDev = Math.Sqrt(variance);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double upStdVal = 0.0;
|
|
|
|
|
|
double downStdVal = 0.0;
|
|
|
|
|
|
if (priceChange > 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
upStdVal = currentStdDev;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
else if (priceChange < 0)
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
downStdVal = currentStdDev;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
rawRmaUp = Math.FusedMultiplyAdd(rawRmaUp, rmaLength - 1, upStdVal) / rmaLength;
|
|
|
|
|
|
eUp = (1 - alpha) * eUp;
|
|
|
|
|
|
double avgUpStd = eUp > Epsilon ? rawRmaUp / (1.0 - eUp) : rawRmaUp;
|
|
|
|
|
|
|
|
|
|
|
|
rawRmaDown = Math.FusedMultiplyAdd(rawRmaDown, rmaLength - 1, downStdVal) / rmaLength;
|
|
|
|
|
|
eDown = (1 - alpha) * eDown;
|
|
|
|
|
|
double avgDownStd = eDown > Epsilon ? rawRmaDown / (1.0 - eDown) : rawRmaDown;
|
|
|
|
|
|
|
|
|
|
|
|
double sumAvgStd = avgUpStd + avgDownStd;
|
|
|
|
|
|
double rviValue = sumAvgStd > Epsilon ? (100.0 * avgUpStd / sumAvgStd) : 50.0;
|
|
|
|
|
|
|
|
|
|
|
|
if (!double.IsFinite(rviValue))
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
rviValue = lastValue;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
else
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
{
|
2026-02-02 13:42:47 -08:00
|
|
|
|
lastValue = rviValue;
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|
2026-02-02 13:42:47 -08:00
|
|
|
|
|
|
|
|
|
|
output[i] = rviValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-10 21:33:16 -08:00
|
|
|
|
|
|
|
|
|
|
public static (TSeries Results, Rvi Indicator) Calculate(TSeries source, int stdevLength = 10, int rmaLength = 14)
|
|
|
|
|
|
{
|
|
|
|
|
|
var indicator = new Rvi(stdevLength, rmaLength);
|
|
|
|
|
|
TSeries results = indicator.Update(source);
|
|
|
|
|
|
return (results, indicator);
|
|
|
|
|
|
}
|
fix(docs): correct .md documentation across errors, dynamics, filters, forecasts, momentum, numerics, oscillators, reversals, statistics, trends, volatility, volume
2026-03-10 18:38:23 -07:00
|
|
|
|
}
|