Skip to content

Commit

Permalink
use IReadOnly result inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveSkender committed Oct 14, 2024
1 parent e5c4a4c commit 5df1ffd
Show file tree
Hide file tree
Showing 207 changed files with 406 additions and 517 deletions.
4 changes: 2 additions & 2 deletions src/_common/Generics/Pruning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static partial class Utility
{
// REMOVE SPECIFIC PERIODS
public static IReadOnlyList<T> RemoveWarmupPeriods<T>(
this IEnumerable<T> series,
this IReadOnlyList<T> series,
int removePeriods)
=> removePeriods < 0
? throw new ArgumentOutOfRangeException(nameof(removePeriods), removePeriods,
Expand All @@ -15,7 +15,7 @@ public static IReadOnlyList<T> RemoveWarmupPeriods<T>(

// REMOVE PERIODS
internal static List<T> Remove<T>(
this IEnumerable<T> series,
this IReadOnlyList<T> series,
int removePeriods)
{
List<T> seriesList = series.ToList();
Expand Down
5 changes: 1 addition & 4 deletions src/_common/Generics/Transforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ public static partial class Utility
// TO COLLECTION
internal static Collection<T> ToCollection<T>(this IEnumerable<T> source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
ArgumentNullException.ThrowIfNull(source);

Collection<T> collection = [.. source];

Expand Down
22 changes: 4 additions & 18 deletions src/_common/Math/Numerical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ public static class Numerical
// STANDARD DEVIATION
public static double StdDev(this double[] values)
{
// validate parameters
if (values is null)
{
throw new ArgumentNullException(
nameof(values),
"StdDev values cannot be null.");
}
ArgumentNullException.ThrowIfNull(
values, "StdDev values cannot be null.");

int n = values.Length;

Expand Down Expand Up @@ -43,17 +38,8 @@ public static double StdDev(this double[] values)
public static double Slope(double[] x, double[] y)
{
// validate parameters
if (x is null)
{
throw new ArgumentNullException(
nameof(x), "Slope X values cannot be null.");
}

if (y is null)
{
throw new ArgumentNullException(
nameof(y), "Slope Y values cannot be null.");
}
ArgumentNullException.ThrowIfNull(x, "Slope X values cannot be null.");
ArgumentNullException.ThrowIfNull(y, "Slope Y values cannot be null.");

if (x.Length != y.Length)
{
Expand Down
2 changes: 1 addition & 1 deletion src/_common/ObsoleteV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IEnumerable<AlligatorResult> GetAlligator(
public static IEnumerable<EmaResult> GetEma<TQuote>(
this IEnumerable<TQuote> quotes, int lookbackPeriods)
where TQuote : IQuote
=> Api.GetEma(quotes, lookbackPeriods);
=> quotes.ToSortedList().ToEma(lookbackPeriods);

// REMOVAL OF INTEGRATED SMAs (evaluates to ERRORs)

Expand Down
2 changes: 0 additions & 2 deletions src/_common/Quotes/Quote.Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace Skender.Stock.Indicators;

public static partial class Utility
{
private static readonly CultureInfo NativeCulture = Thread.CurrentThread.CurrentUICulture;

/* LISTS */

// convert TQuote type list to built-in Quote type list
Expand Down
57 changes: 14 additions & 43 deletions src/_common/Quotes/Quote.Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,10 @@

namespace Skender.Stock.Indicators;

// QUOTE UTILITIES
// QUOTE UTILITIES: VALIDATION

public static partial class Utility
{
// VALIDATION
/// <include file='./info.xml' path='info/type[@name="Validate"]/*' />
///
public static IReadOnlyList<TQuote> Validate<TQuote>(
this IEnumerable<TQuote> quotes)
where TQuote : IQuote
{
// we cannot rely on date consistency when looking back, so we force sort

List<TQuote> quotesList = quotes.ToSortedList();

// check for duplicates
DateTime lastDate = DateTime.MinValue;
foreach (TQuote q in quotesList)
{
// check for duplicates
if (lastDate == q.Timestamp)
{
throw new InvalidQuotesException(
string.Format(NativeCulture, "Duplicate date found on {0}.", q.Timestamp));
}

lastDate = q.Timestamp;
}

return quotesList;
}

/// <summary>
/// Check that quotes are valid and in ascending order.
/// </summary>
Expand All @@ -50,34 +22,33 @@ public static IReadOnlyList<TQuote> Validate<TQuote>(
this IReadOnlyList<TQuote> quotes)
where TQuote : IQuote
{
// assumes already sorted
if (quotes is null)
ArgumentNullException.ThrowIfNull(quotes);

if (quotes.Count == 0)
{
throw new ArgumentNullException(nameof(quotes));
return quotes;
}

// check for duplicates/sequence
DateTime lastDate = DateTime.MinValue;
foreach (TQuote q in quotes)
DateTime lastDate = quotes[0].Timestamp;
for (int i = 1; i < quotes.Count; i++)
{
// check for duplicates
if (lastDate == q.Timestamp)
DateTime currentDate = quotes[i].Timestamp;

if (lastDate == currentDate)
{
throw new InvalidQuotesException(
string.Format(CultureInfo.InvariantCulture, "Duplicate date found on {0}.", q.Timestamp));
string.Format(CultureInfo.InvariantCulture, "Duplicate date found on {0}.", currentDate));
}

// check for sequence
if (lastDate > q.Timestamp)
if (lastDate > currentDate)
{
throw new InvalidQuotesException(
string.Format(NativeCulture, "Quotes are out of sequence on {0}.", q.Timestamp));
string.Format(CultureInfo.InvariantCulture, "Quotes are out of sequence on {0}.", currentDate));
}

lastDate = q.Timestamp;
lastDate = currentDate;
}

return quotes;
}

}
17 changes: 1 addition & 16 deletions src/_common/Quotes/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@

<info>

<type name="Validate">
<summary>
Validate historical quotes.
<para>
See
<see href="https://dotnet.StockIndicators.dev/utilities/#validate-quote-history?utm_source=library&amp;utm_medium=inline-help&amp;utm_campaign=embedded">documentation</see>
for more information.
</para>
</summary>
<typeparam name="TQuote">Configurable Quote type. See Guide for more information.</typeparam>
<param name="quotes">Historical price quotes.</param>
<returns>Time series of historical quote values.</returns>
<exception cref="InvalidQuotesException">Validation check failed.</exception>
</type>

<type name="Aggregate">
<summary>
Converts historical quotes into larger bar sizes.
Expand Down Expand Up @@ -49,4 +34,4 @@
<exception cref="ArgumentOutOfRangeException">Invalid parameter value provided.</exception>
</type>

</info>
</info>
7 changes: 2 additions & 5 deletions src/_common/Reusable/Reusable.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IReadOnlyList<IReusable> ToReusableList<TQuote>(
/// <param name="results">Indicator results to evaluate.</param>
/// <returns>Time series of indicator results, condensed.</returns>
public static IReadOnlyList<T> Condense<T>(
this IEnumerable<T> results)
this IReadOnlyList<T> results)
where T : IReusable
{
List<T> resultsList = results
Expand All @@ -37,9 +37,6 @@ public static IReadOnlyList<T> Condense<T>(
x => double.IsNaN(x.Value));

return resultsList;

// TODO: remove specific indicator 'Condense()' methods
// that are now redundant to this generic method (not all are)
}

/// <summary>
Expand All @@ -54,7 +51,7 @@ public static IReadOnlyList<T> Condense<T>(
/// <param name="results">Indicator results to evaluate.</param>
/// <returns>Time series of results, pruned.</returns>
internal static IReadOnlyList<T> RemoveWarmupPeriods<T>(
this IEnumerable<T> results)
this IReadOnlyList<T> results)
where T : IReusable
{
// this is the default implementation, it will
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Adx/Adx.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<AdxResult> RemoveWarmupPeriods(
this IEnumerable<AdxResult> results)
this IReadOnlyList<AdxResult> results)
{
int n = results
.ToList()
Expand Down
2 changes: 1 addition & 1 deletion src/a-d/Alligator/Alligator.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IReadOnlyList<AlligatorResult> Condense(

// remove recommended periods
public static IReadOnlyList<AlligatorResult> RemoveWarmupPeriods(
this IEnumerable<AlligatorResult> results)
this IReadOnlyList<AlligatorResult> results)
{
int removePeriods = results
.ToList()
Expand Down
2 changes: 1 addition & 1 deletion src/a-d/Alma/Alma.Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static partial class Indicator
{
// SERIES, from CHAIN
public static IReadOnlyList<AlmaResult> GetAlma<T>(
this IEnumerable<T> results,
this IReadOnlyList<T> results,
int lookbackPeriods = 9,
double offset = 0.85,
double sigma = 6)
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Alma/Alma.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<AlmaResult> RemoveWarmupPeriods(
this IEnumerable<AlmaResult> results)
this IReadOnlyList<AlmaResult> results)
{
int removePeriods = results
.ToList()
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Aroon/Aroon.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<AroonResult> RemoveWarmupPeriods(
this IEnumerable<AroonResult> results)
this IReadOnlyList<AroonResult> results)
{
int removePeriods = results
.ToList()
Expand Down
2 changes: 1 addition & 1 deletion src/a-d/Atr/Atr.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static AtrResult Increment<TQuote>(

// remove recommended periods
public static IReadOnlyList<AtrResult> RemoveWarmupPeriods(
this IEnumerable<AtrResult> results)
this IReadOnlyList<AtrResult> results)
{
int removePeriods = results
.ToList()
Expand Down
8 changes: 4 additions & 4 deletions src/a-d/AtrStop/AtrStop.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Skender.Stock.Indicators;
public static partial class AtrStop
{
// CONDENSE (REMOVE null results)
/// <inheritdoc cref="Utility.Condense{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.Condense{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<AtrStopResult> Condense(
this IEnumerable<AtrStopResult> results)
this IReadOnlyList<AtrStopResult> results)
{
List<AtrStopResult> resultsList = results
.ToList();
Expand All @@ -18,9 +18,9 @@ public static IReadOnlyList<AtrStopResult> Condense(
}

// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<AtrStopResult> RemoveWarmupPeriods(
this IEnumerable<AtrStopResult> results)
this IReadOnlyList<AtrStopResult> results)
{
int removePeriods = results
.ToList()
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Awesome/Awesome.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<AwesomeResult> RemoveWarmupPeriods(
this IEnumerable<AwesomeResult> results)
this IReadOnlyList<AwesomeResult> results)
{
int removePeriods = results
.ToList()
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Beta/Beta.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<BetaResult> RemoveWarmupPeriods(
this IEnumerable<BetaResult> results)
this IReadOnlyList<BetaResult> results)
{
int removePeriods = results
.ToList()
Expand Down
2 changes: 1 addition & 1 deletion src/a-d/BollingerBands/BollingerBands.Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static partial class Indicator
{
// SERIES, from CHAIN
public static IReadOnlyList<BollingerBandsResult> GetBollingerBands<T>(
this IEnumerable<T> results,
this IReadOnlyList<T> results,
int lookbackPeriods = 20,
double standardDeviations = 2)
where T : IReusable
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/BollingerBands/BollingerBands.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<BollingerBandsResult> RemoveWarmupPeriods(
this IEnumerable<BollingerBandsResult> results)
this IReadOnlyList<BollingerBandsResult> results)
{
int removePeriods = results
.ToList()
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Bop/Bop.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<BopResult> RemoveWarmupPeriods(
this IEnumerable<BopResult> results)
this IReadOnlyList<BopResult> results)
{
int removePeriods = results
.ToList()
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/Cci/Cci.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Skender.Stock.Indicators;
public static partial class Indicator
{
// remove recommended periods
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IEnumerable{T})"/>
/// <inheritdoc cref="Utility.RemoveWarmupPeriods{T}(IReadOnlyList{T})"/>
public static IReadOnlyList<CciResult> RemoveWarmupPeriods(
this IEnumerable<CciResult> results)
this IReadOnlyList<CciResult> results)
{
int removePeriods = results
.ToList()
Expand Down
4 changes: 2 additions & 2 deletions src/a-d/ChaikinOsc/ChaikinOsc.StaticSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ private static List<ChaikinOscResult> CalcChaikinOsc<TQuote>(
List<AdlResult> adlResults = source.CalcAdl();

// fast/slow EMA of ADL
List<EmaResult> adlEmaSlow = adlResults.CalcEma(slowPeriods);
List<EmaResult> adlEmaFast = adlResults.CalcEma(fastPeriods);
IReadOnlyList<EmaResult> adlEmaSlow = adlResults.ToEma(slowPeriods);
IReadOnlyList<EmaResult> adlEmaFast = adlResults.ToEma(fastPeriods);

// roll through source values
for (int i = 0; i < length; i++)
Expand Down
Loading

0 comments on commit 5df1ffd

Please sign in to comment.