Skip to content

Commit

Permalink
Add SafeDivision() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Nov 7, 2024
1 parent d5b2e4d commit ee886c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Indicators/McGinleyDynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ protected override decimal ComputeNextValue(IReadOnlyWindow<IndicatorDataPoint>
{
return Current.Value;
}
return Current.Value + (input.Value - Current.Value) / (_period * (decimal)Math.Pow((double)(input.Value / Current.Value), 4.0));
var ratioValue = (double)Extensions.SafeDivision(input.Value, Current.Value, 0);
return Current.Value + Extensions.SafeDivision((input.Value - Current.Value), (_period * (decimal)Math.Pow(ratioValue, 4.0)), 0);
}
}

Expand Down
38 changes: 38 additions & 0 deletions Tests/Indicators/McGinleyDynamicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using NUnit.Framework;
using QuantConnect.Indicators;
using System.Collections.Generic;

namespace QuantConnect.Tests.Indicators
{
Expand Down Expand Up @@ -62,5 +63,42 @@ public override void ResetsProperly()
indicator.Update(new DateTime(2024, 7, 9, 0, 3, 0), 2.0m);
Assert.AreEqual(indicator.Current.Value, 2.0m);
}

[Test]
public override void WorksWithLowValues()
{
var indicator = new McGinleyDynamic("test", 10);

var startDate = new DateTime(2020, 6, 4);
var dataPoints = new List<IndicatorDataPoint>()
{
new IndicatorDataPoint(startDate, 0m),
new IndicatorDataPoint(startDate.AddDays(1), 0m),
new IndicatorDataPoint(startDate.AddDays(2), 0m),
new IndicatorDataPoint(startDate.AddDays(3), 0m),
new IndicatorDataPoint(startDate.AddDays(4), 3.27743794800m),
new IndicatorDataPoint(startDate.AddDays(5), 7.46527532600m),
new IndicatorDataPoint(startDate.AddDays(6), 2.54419732600m),
new IndicatorDataPoint(startDate.AddDays(7), 0m),
new IndicatorDataPoint(startDate.AddDays(8), 0m),
new IndicatorDataPoint(startDate.AddDays(9), 0.71847738800m),
new IndicatorDataPoint(startDate.AddDays(10), 0m),
new IndicatorDataPoint(startDate.AddDays(11), 1.86016748400m),
new IndicatorDataPoint(startDate.AddDays(12), 0.45273917600m),
new IndicatorDataPoint(startDate.AddDays(13), 0m),
new IndicatorDataPoint(startDate.AddDays(14), 1.80111454800m),
new IndicatorDataPoint(startDate.AddDays(15), 0m),
new IndicatorDataPoint(startDate.AddDays(16), 2.74596152400m),
new IndicatorDataPoint(startDate.AddDays(17), 0m),
new IndicatorDataPoint(startDate.AddDays(18), 0m),
new IndicatorDataPoint(startDate.AddDays(19), 0m),
new IndicatorDataPoint(startDate.AddDays(20), 0.84642541600m),
};

for (int i=0; i < 21; i++)
{
indicator.Update(dataPoints[i]);
}
}
}
}

0 comments on commit ee886c9

Please sign in to comment.