2026-01-18 19:02:03 -08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
|
|
|
|
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// AD: Accumulation/Distribution Line
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2026-01-31 11:21:09 -08:00
|
|
|
|
/// Cumulative indicator using volume and price to assess accumulation or distribution.
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// Rising AD confirms accumulation; falling confirms distribution.
|
2026-01-18 19:02:03 -08:00
|
|
|
|
///
|
2026-01-31 11:21:09 -08:00
|
|
|
|
/// Calculation: <c>MFM = [(Close - Low) - (High - Close)] / (High - Low)</c>,
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// <c>MFV = MFM × Volume</c>, <c>AD = prev_AD + MFV</c>. If High equals Low, MFM is 0.
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// </remarks>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// <seealso href="Ad.md">Detailed documentation</seealso>
|
|
|
|
|
|
/// <seealso href="ad.pine">Reference Pine Script implementation</seealso>
|
2026-01-18 19:02:03 -08:00
|
|
|
|
[SkipLocalsInit]
|
2026-03-16 12:45:13 -07:00
|
|
|
|
public sealed class Ad : ITValuePublisher
|
2026-01-18 19:02:03 -08:00
|
|
|
|
{
|
2026-03-16 12:45:13 -07:00
|
|
|
|
private double _ad;
|
|
|
|
|
|
private double _p_ad;
|
2026-01-18 19:02:03 -08:00
|
|
|
|
private bool _isInitialized;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Display name for the indicator.
|
|
|
|
|
|
/// </summary>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
public static string Name => "AD";
|
2026-01-18 19:02:03 -08:00
|
|
|
|
|
|
|
|
|
|
public event TValuePublishedHandler? Pub;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// Current AD value.
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TValue Last { get; private set; }
|
|
|
|
|
|
|
2026-02-11 20:38:38 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Minimum number of data points required before the indicator becomes valid.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int WarmupPeriod { get; } = 1;
|
|
|
|
|
|
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// True if the indicator has processed at least one bar.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool IsHot => _isInitialized;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// Creates a new AD indicator.
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// </summary>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
public Ad()
|
2026-01-18 19:02:03 -08:00
|
|
|
|
{
|
|
|
|
|
|
_isInitialized = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Resets the indicator state.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
|
{
|
2026-03-16 12:45:13 -07:00
|
|
|
|
_ad = 0;
|
|
|
|
|
|
_p_ad = 0;
|
2026-01-18 19:02:03 -08:00
|
|
|
|
_isInitialized = false;
|
|
|
|
|
|
Last = default;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
|
public TValue Update(TBar input, bool isNew = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
2026-03-16 12:45:13 -07:00
|
|
|
|
_p_ad = _ad;
|
2026-01-18 19:02:03 -08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-03-16 12:45:13 -07:00
|
|
|
|
_ad = _p_ad;
|
2026-01-18 19:02:03 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double highLowRange = input.High - input.Low;
|
|
|
|
|
|
double mfm = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (highLowRange > double.Epsilon)
|
|
|
|
|
|
{
|
|
|
|
|
|
mfm = (input.Close - input.Low - (input.High - input.Close)) / highLowRange;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double mfv = mfm * input.Volume;
|
2026-03-16 12:45:13 -07:00
|
|
|
|
_ad += mfv;
|
2026-01-18 19:02:03 -08:00
|
|
|
|
|
|
|
|
|
|
_isInitialized = true;
|
2026-03-16 12:45:13 -07:00
|
|
|
|
Last = new TValue(input.Time, _ad);
|
2026-01-18 19:02:03 -08:00
|
|
|
|
Pub?.Invoke(this, new TValueEventArgs { Value = Last, IsNew = isNew });
|
|
|
|
|
|
return Last;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// Updates AD with a TValue input.
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <exception cref="NotSupportedException">
|
2026-03-16 12:45:13 -07:00
|
|
|
|
/// AD requires OHLCV bar data to calculate the Money Flow Multiplier and Volume.
|
2026-01-18 19:02:03 -08:00
|
|
|
|
/// Use Update(TBar) instead.
|
|
|
|
|
|
/// </exception>
|
|
|
|
|
|
#pragma warning disable S2325 // Method signature must match ITValuePublisher contract
|
|
|
|
|
|
public TValue Update(TValue input, bool isNew = true)
|
|
|
|
|
|
#pragma warning restore S2325
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException(
|
2026-03-16 12:45:13 -07:00
|
|
|
|
"AD requires OHLCV bar data to calculate the Money Flow Multiplier and Volume. " +
|
2026-01-18 19:02:03 -08:00
|
|
|
|
"Use Update(TBar) instead.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TSeries Update(TBarSeries source)
|
|
|
|
|
|
{
|
|
|
|
|
|
var t = new List<long>(source.Count);
|
|
|
|
|
|
var v = new List<double>(source.Count);
|
|
|
|
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < source.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var val = Update(source[i], isNew: true);
|
|
|
|
|
|
t.Add(val.Time);
|
|
|
|
|
|
v.Add(val.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-11 20:38:38 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes the indicator state using the provided bar series history.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="source">Historical bar data.</param>
|
|
|
|
|
|
public void Prime(TBarSeries source)
|
|
|
|
|
|
{
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
if (source.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < source.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Update(source[i], isNew: true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 21:33:16 -08:00
|
|
|
|
public static TSeries Batch(TBarSeries source)
|
2026-01-18 19:02:03 -08:00
|
|
|
|
{
|
2026-01-25 16:01:45 -08:00
|
|
|
|
if (source.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
2026-01-18 19:02:03 -08:00
|
|
|
|
|
|
|
|
|
|
var t = source.Open.Times.ToArray(); // Times are same for all series
|
|
|
|
|
|
var v = new double[source.Count];
|
|
|
|
|
|
|
2026-02-10 21:33:16 -08:00
|
|
|
|
Batch(source.High.Values, source.Low.Values, source.Close.Values, source.Volume.Values, v);
|
2026-01-18 19:02:03 -08:00
|
|
|
|
|
|
|
|
|
|
return new TSeries(t, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2026-02-10 21:33:16 -08:00
|
|
|
|
public static void Batch(ReadOnlySpan<double> high, ReadOnlySpan<double> low, ReadOnlySpan<double> close, ReadOnlySpan<double> volume, Span<double> output)
|
2026-01-18 19:02:03 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (high.Length != low.Length || high.Length != close.Length || high.Length != volume.Length || high.Length != output.Length)
|
2026-01-25 16:01:45 -08:00
|
|
|
|
{
|
2026-01-18 19:02:03 -08:00
|
|
|
|
throw new ArgumentException("All spans must be of the same length", nameof(output));
|
2026-01-25 16:01:45 -08:00
|
|
|
|
}
|
2026-01-18 19:02:03 -08:00
|
|
|
|
|
|
|
|
|
|
int len = high.Length;
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (Vector.IsHardwareAccelerated && len >= Vector<double>.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
int vectorSize = Vector<double>.Count;
|
|
|
|
|
|
var epsilon = new Vector<double>(double.Epsilon);
|
|
|
|
|
|
|
|
|
|
|
|
for (; i <= len - vectorSize; i += vectorSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
var h = new Vector<double>(high.Slice(i, vectorSize));
|
|
|
|
|
|
var l = new Vector<double>(low.Slice(i, vectorSize));
|
|
|
|
|
|
var c = new Vector<double>(close.Slice(i, vectorSize));
|
|
|
|
|
|
var vol = new Vector<double>(volume.Slice(i, vectorSize));
|
|
|
|
|
|
|
|
|
|
|
|
var hl = h - l;
|
|
|
|
|
|
var num = c - l - (h - c);
|
|
|
|
|
|
|
|
|
|
|
|
var mask = Vector.GreaterThan(hl, epsilon);
|
|
|
|
|
|
var safeHl = Vector.ConditionalSelect(mask, hl, Vector<double>.One);
|
|
|
|
|
|
var mfm = num / safeHl;
|
|
|
|
|
|
mfm = Vector.ConditionalSelect(mask, mfm, Vector<double>.Zero);
|
|
|
|
|
|
|
|
|
|
|
|
var mfv = mfm * vol;
|
|
|
|
|
|
mfv.CopyTo(output.Slice(i, vectorSize));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double h = high[i];
|
|
|
|
|
|
double l = low[i];
|
|
|
|
|
|
double c = close[i];
|
|
|
|
|
|
double vol = volume[i];
|
|
|
|
|
|
|
|
|
|
|
|
double hl = h - l;
|
|
|
|
|
|
double mfm = 0;
|
|
|
|
|
|
if (hl > double.Epsilon)
|
|
|
|
|
|
{
|
|
|
|
|
|
mfm = (c - l - (h - c)) / hl;
|
|
|
|
|
|
}
|
|
|
|
|
|
output[i] = mfm * vol;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double sum = 0;
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
sum += output[i];
|
|
|
|
|
|
output[i] = sum;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-10 21:33:16 -08:00
|
|
|
|
|
2026-03-16 12:45:13 -07:00
|
|
|
|
public static (TSeries Results, Ad Indicator) Calculate(TBarSeries source)
|
2026-02-10 21:33:16 -08:00
|
|
|
|
{
|
2026-03-16 12:45:13 -07:00
|
|
|
|
var indicator = new Ad();
|
2026-02-10 21:33:16 -08:00
|
|
|
|
TSeries results = indicator.Update(source);
|
|
|
|
|
|
return (results, indicator);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|