ADL with tests

This commit is contained in:
Miha Kralj
2022-11-07 09:43:33 -08:00
parent 7456b4a401
commit af33331d8c
3 changed files with 64 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
namespace QuanTAlib;
using System;
/* <summary>
ADL: Chaikin Accumulation/Distribution Line
ADL is a volume-based indicator that measures the cumulative Money Flow Volume:
1. Money Flow Multiplier = [(Close - Low) - (High - Close)] /(High - Low)
2. Money Flow Volume = Money Flow Multiplier x Volume for the Period
3. ADL = Previous ADL + Current Period's Money Flow Volume
Sources:
https://school.stockcharts.com/doku.php?id=technical_indicators:accumulation_distribution_line
</summary> */
public class ADL_Series : Single_TBars_Indicator
{
private double _lastadl, _lastlastadl;
public ADL_Series(TBars source, bool useNaN = false) : base(source, 0, useNaN)
{
this._lastadl = this._lastlastadl = 0;
if (_bars.Count > 0)
{ base.Add(_bars); }
}
public override void Add((DateTime t, double o, double h, double l, double c, double v) TBar, bool update)
{
if (update)
{ this._lastadl = this._lastlastadl; }
double _mfm = ((TBar.c - TBar.l) - (TBar.h - TBar.c)) / (TBar.h - TBar.l);
double _mfv = _mfm * TBar.v;
double _adl = this._lastadl + _mfv;
this._lastlastadl = this._lastadl;
this._lastadl = _adl;
var ret = (TBar.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _adl);
base.Add(ret, update);
}
}
+9
View File
@@ -99,6 +99,15 @@ public class Skender_Stock
Assert.Equal(Math.Round((double)SK.Last().Atr!, 8), Math.Round(QL.Last().v, 8)); Assert.Equal(Math.Round((double)SK.Last().Atr!, 8), Math.Round(QL.Last().v, 8));
} }
[Fact]
public void ADL()
{
ADL_Series QL = new(this.bars, false);
var SK = this.quotes.GetAdl();
Assert.Equal(Math.Round((double)SK.Last().Adl!, 6), Math.Round(QL.Last().v, 6));
}
[Fact] [Fact]
public void CCI() public void CCI()
{ {
+12
View File
@@ -13,6 +13,7 @@ public class TA_LIB
private readonly double[] inhigh; private readonly double[] inhigh;
private readonly double[] inlow; private readonly double[] inlow;
private readonly double[] inclose; private readonly double[] inclose;
private readonly double[] involume;
public TA_LIB() public TA_LIB()
{ {
@@ -22,6 +23,7 @@ public class TA_LIB
this.inhigh = this.bars.High.v.ToArray(); this.inhigh = this.bars.High.v.ToArray();
this.inlow = this.bars.Low.v.ToArray(); this.inlow = this.bars.Low.v.ToArray();
this.inclose = this.bars.Close.v.ToArray(); this.inclose = this.bars.Close.v.ToArray();
this.involume = this.bars.Volume.v.ToArray();
} }
///////////////////////////////////////// /////////////////////////////////////////
@@ -98,6 +100,16 @@ public class TA_LIB
Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8)); Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8));
} }
[Fact]
public void ADL()
{
ADL_Series QL = new(this.bars, false);
Core.Ad(this.inhigh, this.inlow, this.inclose, this.involume, 0, this.bars.Count - 1, this.TALIB, out int outBegIdx, out _);
Assert.Equal(Math.Round(this.TALIB[this.TALIB.Length - outBegIdx - 1], 8), Math.Round(QL.Last().v, 8));
}
[Fact] [Fact]
public void ATR() public void ATR()
{ {