Skip to content

Commit

Permalink
fix: escaped NaN in ADX (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveSkender authored Aug 6, 2022
1 parent 5dcda9a commit 2599b77
Show file tree
Hide file tree
Showing 7 changed files with 839 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/a-d/Adx/Adx.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ internal static List<AdxResult> CalcAdx(
if (i + 1 > 2 * lookbackPeriods)
{
adx = ((prevAdx * (lookbackPeriods - 1)) + dx) / lookbackPeriods;
r.Adx = adx;
r.Adx = adx.NaN2Null();

double? priorAdx = results[i + 1 - lookbackPeriods].Adx;

r.Adxr = (adx + priorAdx) / 2;
r.Adxr = (adx + priorAdx).NaN2Null() / 2;
prevAdx = adx;
}

Expand All @@ -126,7 +126,7 @@ internal static List<AdxResult> CalcAdx(
{
sumDx += dx;
adx = sumDx / lookbackPeriods;
r.Adx = adx;
r.Adx = adx.NaN2Null();
prevAdx = adx;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/indicators/Tests.Indicators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
</ItemGroup>

<ItemGroup>
<None Update="a-d\Adx\issue859quotes.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="_common\data\bad.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -90,6 +93,9 @@
<None Update="s-z\ZigZag\zigzag.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="_common\data\zeros.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions tests/indicators/_Initialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public abstract class TestBase
internal static readonly IEnumerable<Quote> noquotes = new List<Quote>();
internal static readonly IEnumerable<Quote> onequote = TestData.GetDefault(1);
internal static readonly IEnumerable<Quote> randomQuotes = TestData.GetRandom(1000);
internal static readonly IEnumerable<Quote> zeroesQuotes = TestData.GetZeros();
internal static readonly IEnumerable<(DateTime, double)> tupleNanny = TestData.GetTupleNaN();
}
9 changes: 9 additions & 0 deletions tests/indicators/_common/Helper.Getter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ internal static IEnumerable<Quote> GetDefault(int days = 502)
internal static IEnumerable<Quote> GetRandom(int days = 502)
=> new RandomGbm(bars: days);

// ZEROS (200)
internal static IEnumerable<Quote> GetZeros(int days = 200)
=> File.ReadAllLines("_common/data/zeros.csv")
.Skip(1)
.Select(v => Importer.QuoteFromCsv(v))
.OrderByDescending(x => x.Date)
.Take(days)
.ToList();

// BAD DATA
internal static IEnumerable<Quote> GetBad(int days = 502)
=> File.ReadAllLines("_common/data/bad.csv")
Expand Down
201 changes: 201 additions & 0 deletions tests/indicators/_common/data/zeros.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
date,open,high,low,close,volume
01/03/17,0.00,0.00,0.00,0.00,0.00
01/04/17,0.00,0.00,0.00,0.00,0.00
01/05/17,0.00,0.00,0.00,0.00,0.00
01/06/17,0.00,0.00,0.00,0.00,0.00
01/09/17,0.00,0.00,0.00,0.00,0.00
01/10/17,0.00,0.00,0.00,0.00,0.00
01/11/17,0.00,0.00,0.00,0.00,0.00
01/12/17,0.00,0.00,0.00,0.00,0.00
01/13/17,0.00,0.00,0.00,0.00,0.00
01/17/17,0.00,0.00,0.00,0.00,0.00
01/18/17,0.00,0.00,0.00,0.00,0.00
01/19/17,0.00,0.00,0.00,0.00,0.00
01/20/17,0.00,0.00,0.00,0.00,0.00
01/23/17,0.00,0.00,0.00,0.00,0.00
01/24/17,0.00,0.00,0.00,0.00,0.00
01/25/17,0.00,0.00,0.00,0.00,0.00
01/26/17,0.00,0.00,0.00,0.00,0.00
01/27/17,0.00,0.00,0.00,0.00,0.00
01/30/17,0.00,0.00,0.00,0.00,0.00
01/31/17,0.00,0.00,0.00,0.00,0.00
02/01/17,0.00,0.00,0.00,0.00,0.00
02/02/17,0.00,0.00,0.00,0.00,0.00
02/03/17,0.00,0.00,0.00,0.00,0.00
02/06/17,0.00,0.00,0.00,0.00,0.00
02/07/17,0.00,0.00,0.00,0.00,0.00
02/08/17,0.00,0.00,0.00,0.00,0.00
02/09/17,0.00,0.00,0.00,0.00,0.00
02/10/17,0.00,0.00,0.00,0.00,0.00
02/13/17,0.00,0.00,0.00,0.00,0.00
02/14/17,0.00,0.00,0.00,0.00,0.00
02/15/17,0.00,0.00,0.00,0.00,0.00
02/16/17,0.00,0.00,0.00,0.00,0.00
02/17/17,0.00,0.00,0.00,0.00,0.00
02/21/17,0.00,0.00,0.00,0.00,0.00
02/22/17,0.00,0.00,0.00,0.00,0.00
02/23/17,0.00,0.00,0.00,0.00,0.00
02/24/17,0.00,0.00,0.00,0.00,0.00
02/27/17,0.00,0.00,0.00,0.00,0.00
02/28/17,0.00,0.00,0.00,0.00,0.00
03/01/17,0.00,0.00,0.00,0.00,0.00
03/02/17,0.00,0.00,0.00,0.00,0.00
03/03/17,0.00,0.00,0.00,0.00,0.00
03/06/17,0.00,0.00,0.00,0.00,0.00
03/07/17,0.00,0.00,0.00,0.00,0.00
03/08/17,0.00,0.00,0.00,0.00,0.00
03/09/17,0.00,0.00,0.00,0.00,0.00
03/10/17,0.00,0.00,0.00,0.00,0.00
03/13/17,0.00,0.00,0.00,0.00,0.00
03/14/17,0.00,0.00,0.00,0.00,0.00
03/15/17,0.00,0.00,0.00,0.00,0.00
03/16/17,0.00,0.00,0.00,0.00,0.00
03/17/17,0.00,0.00,0.00,0.00,0.00
03/20/17,0.00,0.00,0.00,0.00,0.00
03/21/17,0.00,0.00,0.00,0.00,0.00
03/22/17,0.00,0.00,0.00,0.00,0.00
03/23/17,0.00,0.00,0.00,0.00,0.00
03/24/17,0.00,0.00,0.00,0.00,0.00
03/27/17,0.00,0.00,0.00,0.00,0.00
03/28/17,0.00,0.00,0.00,0.00,0.00
03/29/17,0.00,0.00,0.00,0.00,0.00
03/30/17,0.00,0.00,0.00,0.00,0.00
03/31/17,0.00,0.00,0.00,0.00,0.00
04/03/17,0.00,0.00,0.00,0.00,0.00
04/04/17,0.00,0.00,0.00,0.00,0.00
04/05/17,0.00,0.00,0.00,0.00,0.00
04/06/17,0.00,0.00,0.00,0.00,0.00
04/07/17,0.00,0.00,0.00,0.00,0.00
04/10/17,0.00,0.00,0.00,0.00,0.00
04/11/17,0.00,0.00,0.00,0.00,0.00
04/12/17,0.00,0.00,0.00,0.00,0.00
04/13/17,0.00,0.00,0.00,0.00,0.00
04/17/17,0.00,0.00,0.00,0.00,0.00
04/18/17,0.00,0.00,0.00,0.00,0.00
04/19/17,0.00,0.00,0.00,0.00,0.00
04/20/17,0.00,0.00,0.00,0.00,0.00
04/21/17,0.00,0.00,0.00,0.00,0.00
04/24/17,0.00,0.00,0.00,0.00,0.00
04/25/17,0.00,0.00,0.00,0.00,0.00
04/26/17,0.00,0.00,0.00,0.00,0.00
04/27/17,0.00,0.00,0.00,0.00,0.00
04/28/17,0.00,0.00,0.00,0.00,0.00
05/01/17,0.00,0.00,0.00,0.00,0.00
05/02/17,0.00,0.00,0.00,0.00,0.00
05/03/17,0.00,0.00,0.00,0.00,0.00
05/04/17,0.00,0.00,0.00,0.00,0.00
05/05/17,0.00,0.00,0.00,0.00,0.00
05/08/17,0.00,0.00,0.00,0.00,0.00
05/09/17,0.00,0.00,0.00,0.00,0.00
05/10/17,0.00,0.00,0.00,0.00,0.00
05/11/17,0.00,0.00,0.00,0.00,0.00
05/12/17,0.00,0.00,0.00,0.00,0.00
05/15/17,0.00,0.00,0.00,0.00,0.00
05/16/17,0.00,0.00,0.00,0.00,0.00
05/17/17,0.00,0.00,0.00,0.00,0.00
05/18/17,0.00,0.00,0.00,0.00,0.00
05/19/17,0.00,0.00,0.00,0.00,0.00
05/22/17,0.00,0.00,0.00,0.00,0.00
05/23/17,0.00,0.00,0.00,0.00,0.00
05/24/17,0.00,0.00,0.00,0.00,0.00
05/25/17,0.00,0.00,0.00,0.00,0.00
05/26/17,0.00,0.00,0.00,0.00,0.00
05/30/17,0.00,0.00,0.00,0.00,0.00
05/31/17,0.00,0.00,0.00,0.00,0.00
06/01/17,0.00,0.00,0.00,0.00,0.00
06/02/17,0.00,0.00,0.00,0.00,0.00
06/05/17,0.00,0.00,0.00,0.00,0.00
06/06/17,0.00,0.00,0.00,0.00,0.00
06/07/17,0.00,0.00,0.00,0.00,0.00
06/08/17,0.00,0.00,0.00,0.00,0.00
06/09/17,0.00,0.00,0.00,0.00,0.00
06/12/17,0.00,0.00,0.00,0.00,0.00
06/13/17,0.00,0.00,0.00,0.00,0.00
06/14/17,0.00,0.00,0.00,0.00,0.00
06/15/17,0.00,0.00,0.00,0.00,0.00
06/16/17,0.00,0.00,0.00,0.00,0.00
06/19/17,0.00,0.00,0.00,0.00,0.00
06/20/17,0.00,0.00,0.00,0.00,0.00
06/21/17,0.00,0.00,0.00,0.00,0.00
06/22/17,0.00,0.00,0.00,0.00,0.00
06/23/17,0.00,0.00,0.00,0.00,0.00
06/26/17,0.00,0.00,0.00,0.00,0.00
06/27/17,0.00,0.00,0.00,0.00,0.00
06/28/17,0.00,0.00,0.00,0.00,0.00
06/29/17,0.00,0.00,0.00,0.00,0.00
06/30/17,0.00,0.00,0.00,0.00,0.00
07/03/17,0.00,0.00,0.00,0.00,0.00
07/05/17,0.00,0.00,0.00,0.00,0.00
07/06/17,0.00,0.00,0.00,0.00,0.00
07/07/17,0.00,0.00,0.00,0.00,0.00
07/10/17,0.00,0.00,0.00,0.00,0.00
07/11/17,0.00,0.00,0.00,0.00,0.00
07/12/17,0.00,0.00,0.00,0.00,0.00
07/13/17,0.00,0.00,0.00,0.00,0.00
07/14/17,0.00,0.00,0.00,0.00,0.00
07/17/17,0.00,0.00,0.00,0.00,0.00
07/18/17,0.00,0.00,0.00,0.00,0.00
07/19/17,0.00,0.00,0.00,0.00,0.00
07/20/17,0.00,0.00,0.00,0.00,0.00
07/21/17,0.00,0.00,0.00,0.00,0.00
07/24/17,0.00,0.00,0.00,0.00,0.00
07/25/17,0.00,0.00,0.00,0.00,0.00
07/26/17,0.00,0.00,0.00,0.00,0.00
07/27/17,0.00,0.00,0.00,0.00,0.00
07/28/17,0.00,0.00,0.00,0.00,0.00
07/31/17,0.00,0.00,0.00,0.00,0.00
08/01/17,0.00,0.00,0.00,0.00,0.00
08/02/17,0.00,0.00,0.00,0.00,0.00
08/03/17,0.00,0.00,0.00,0.00,0.00
08/04/17,0.00,0.00,0.00,0.00,0.00
08/07/17,0.00,0.00,0.00,0.00,0.00
08/08/17,0.00,0.00,0.00,0.00,0.00
08/09/17,0.00,0.00,0.00,0.00,0.00
08/10/17,0.00,0.00,0.00,0.00,0.00
08/11/17,0.00,0.00,0.00,0.00,0.00
08/14/17,0.00,0.00,0.00,0.00,0.00
08/15/17,0.00,0.00,0.00,0.00,0.00
08/16/17,0.00,0.00,0.00,0.00,0.00
08/17/17,0.00,0.00,0.00,0.00,0.00
08/18/17,0.00,0.00,0.00,0.00,0.00
08/21/17,0.00,0.00,0.00,0.00,0.00
08/22/17,0.00,0.00,0.00,0.00,0.00
08/23/17,0.00,0.00,0.00,0.00,0.00
08/24/17,0.00,0.00,0.00,0.00,0.00
08/25/17,0.00,0.00,0.00,0.00,0.00
08/28/17,0.00,0.00,0.00,0.00,0.00
08/29/17,0.00,0.00,0.00,0.00,0.00
08/30/17,0.00,0.00,0.00,0.00,0.00
08/31/17,0.00,0.00,0.00,0.00,0.00
09/01/17,0.00,0.00,0.00,0.00,0.00
09/05/17,0.00,0.00,0.00,0.00,0.00
09/06/17,0.00,0.00,0.00,0.00,0.00
09/07/17,0.00,0.00,0.00,0.00,0.00
09/08/17,0.00,0.00,0.00,0.00,0.00
09/11/17,0.00,0.00,0.00,0.00,0.00
09/12/17,0.00,0.00,0.00,0.00,0.00
09/13/17,0.00,0.00,0.00,0.00,0.00
09/14/17,0.00,0.00,0.00,0.00,0.00
09/15/17,0.00,0.00,0.00,0.00,0.00
09/18/17,0.00,0.00,0.00,0.00,0.00
09/19/17,0.00,0.00,0.00,0.00,0.00
09/20/17,0.00,0.00,0.00,0.00,0.00
09/21/17,0.00,0.00,0.00,0.00,0.00
09/22/17,0.00,0.00,0.00,0.00,0.00
09/25/17,0.00,0.00,0.00,0.00,0.00
09/26/17,0.00,0.00,0.00,0.00,0.00
09/27/17,0.00,0.00,0.00,0.00,0.00
09/28/17,0.00,0.00,0.00,0.00,0.00
09/29/17,0.00,0.00,0.00,0.00,0.00
10/02/17,0.00,0.00,0.00,0.00,0.00
10/03/17,0.00,0.00,0.00,0.00,0.00
10/04/17,0.00,0.00,0.00,0.00,0.00
10/05/17,0.00,0.00,0.00,0.00,0.00
10/06/17,0.00,0.00,0.00,0.00,0.00
10/09/17,0.00,0.00,0.00,0.00,0.00
10/10/17,0.00,0.00,0.00,0.00,0.00
10/11/17,0.00,0.00,0.00,0.00,0.00
10/12/17,0.00,0.00,0.00,0.00,0.00
10/13/17,0.00,0.00,0.00,0.00,0.00
10/16/17,0.00,0.00,0.00,0.00,0.00
10/17/17,0.00,0.00,0.00,0.00,0.00
23 changes: 23 additions & 0 deletions tests/indicators/a-d/Adx/Adx.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ public void NoQuotes()
Assert.AreEqual(1, r1.Count());
}

[TestMethod]
public void Issue859()
{
IOrderedEnumerable<Quote> test859 = File.ReadAllLines("a-d/Adx/issue859quotes.csv")
.Skip(1)
.Select(v => Importer.QuoteFromCsv(v))
.OrderByDescending(x => x.Date);

List<AdxResult> r = test859.GetAdx(14).ToList();

Assert.AreEqual(0, r.Count(x => x.Adx is double and double.NaN));
Assert.AreEqual(595, r.Count);
}

[TestMethod]
public void Zeroes()
{
List<AdxResult> r = zeroesQuotes.GetAdx(14).ToList();

Assert.AreEqual(0, r.Count(x => x.Adx is double and double.NaN));
Assert.AreEqual(200, r.Count);
}

[TestMethod]
public void Removed()
{
Expand Down
Loading

0 comments on commit 2599b77

Please sign in to comment.