Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveSkender committed Jun 29, 2024
1 parent 5f8e531 commit c27147c
Show file tree
Hide file tree
Showing 255 changed files with 2,702 additions and 2,772 deletions.
2 changes: 2 additions & 0 deletions Stock.Indicators.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=uote/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
16 changes: 1 addition & 15 deletions docs/pages/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ var results = quotes

{% include candlepart-options.md %}

### Using tuple quotes

`quotes.ToTupleCollection()` is a method for converting any `TQuote` collection to a simple [tuple](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/value-tuples) `(DateTime, double)` formatted `Collection`. Most indicators in our library will accept this tuple format. With that said, there are many indicators that also require the full OHLCV quote format, so it cannot be used universally.

### Sort quotes

`quotes.ToSortedCollection()` sorts any collection of `TQuote` or tuple `(DateTime, double)` and returns it as a `Collection` sorted by ascending `Timestamp`. You do not need to sort quotes before using library indicators; however, if you are creating [custom indicators]({{site.baseurl}}/custom-indicators/#content) it's important to analyze `quotes` in a proper sequence.
`quotes.ToSortedCollection()` sorts any collection of `TQuote` or `ISeries` and returns it as a `Collection` sorted by ascending `Timestamp`. You do not need to sort quotes before using library indicators; however, if you are creating [custom indicators]({{site.baseurl}}/custom-indicators/#content) it's important to analyze `quotes` in a proper sequence.

### Resize quote history

Expand Down Expand Up @@ -150,16 +146,6 @@ See [individual indicator pages]({{site.baseurl}}/indicators/#content) for infor
>
> &#128681; **Warning**: without a specified `removePeriods` value, this utility will reverse-engineer the pruning amount. When there are unusual results or when chaining multiple indicators, there will be an erroneous increase in the amount of pruning. If you want more certainty, use a specific number for `removePeriods`. Using this method on chained indicators without `removePeriods` is strongly discouraged.
### Using tuple results

`results.ToTupleCollection()` converts results to a simpler `(DateTime Timestamp, double? Value)` [tuple](https://docs.microsoft.com/dotnet/csharp/language-reference/builtin-types/value-tuples) `Collection`.

`results.ToTupleNaN()` converts results to simpler `(DateTime Timestamp, double Value)` tuple `Collection` with `null` values converted to `double.NaN`.

`results.ToTupleChainable()` is a specialty converter used to prepare [custom indicators]({{site.baseurl}}/custom-indicators/#content) for chaining by removing `null` warmup periods and converting all remaining `null` values to `double.NaN`.

> &#128681; **Warning**: warmup periods are pruned when using `.ToTupleChainable()`, resulting in fewer records.
### Sort results

`results.ToSortedCollection()` sorts any collection of indicator results and returns it as a `Collection` sorted by ascending `Timestamp`. Results from the library indicators are already sorted, so you'd only potentially need this if you're creating [custom indicators]({{site.baseurl}}/custom-indicators/#content).
Expand Down
10 changes: 5 additions & 5 deletions src/_common/Candles/Candles.Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Skender.Stock.Indicators;

// CANDLESTICK MODELS

public readonly record struct CandleProperties : IQuote, IReusable
public readonly record struct CandleProperties : IQuote
{
// base quote properties
public DateTime Timestamp { get; init; }
Expand All @@ -16,14 +16,14 @@ namespace Skender.Stock.Indicators;

// raw sizes
public decimal? Size => High - Low;
public decimal? Body => (Open > Close) ? (Open - Close) : (Close - Open);
public decimal? Body => Open > Close ? Open - Close : Close - Open;
public decimal? UpperWick => High - (Open > Close ? Open : Close);
public decimal? LowerWick => (Open > Close ? Close : Open) - Low;

// percent sizes
public double? BodyPct => (Size != 0) ? (double?)(Body / Size) : 1;
public double? UpperWickPct => (Size != 0) ? (double?)(UpperWick / Size) : 1;
public double? LowerWickPct => (Size != 0) ? (double?)(LowerWick / Size) : 1;
public double? BodyPct => Size != 0 ? (double?)(Body / Size) : 1;
public double? UpperWickPct => Size != 0 ? (double?)(UpperWick / Size) : 1;
public double? LowerWickPct => Size != 0 ? (double?)(LowerWick / Size) : 1;

// directional info
public bool IsBullish => Close > Open;
Expand Down
2 changes: 1 addition & 1 deletion src/_common/Candles/Candles.Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IEnumerable<CandleProperties> ToCandles<TQuote>(
internal static List<CandleResult> ToCandleResults<TQuote>(
this IEnumerable<TQuote> quotes)
where TQuote : IQuote => quotes
.Select(q => new CandleResult() {
.Select(q => new CandleResult {
Timestamp = q.Timestamp,
Match = Match.None,
Candle = q.ToCandle()
Expand Down
44 changes: 22 additions & 22 deletions src/_common/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Skender.Stock.Indicators;
/// </summary>
public enum Act
{
AddNew,
AddNew = 0, // default
AddOld,
Update,
Delete,
Expand All @@ -20,24 +20,24 @@ public enum Act
/// </summary>
public enum CandlePart
{
Close = 0, // default
Open,
High,
Low,
Close,
Volume,
HL2,
HLC3,
OC2,
OHL3,
OHLC4
Hl2,
Hlc3,
Oc2,
Ohl3,
Ohlc4
}

/// <summary>
/// Candle close or high/low wick values
/// </summary>
public enum EndType
{
Close = 0,
Close = 0, // default
HighLow = 1
}

Expand All @@ -50,7 +50,7 @@ public enum Match
BullSignal = 100,
BullBasis = 10,
Neutral = 1,
None = 0,
None = 0, // default
BearBasis = -10,
BearSignal = -100,
BearConfirmed = -200
Expand All @@ -61,17 +61,17 @@ public enum Match
/// </summary>
public enum MaType
{
ALMA,
DEMA,
EPMA,
EMA,
HMA,
KAMA,
MAMA,
SMA,
SMMA,
TEMA,
WMA
Sma = 0, // default
Alma,
Dema,
Epma,
Ema,
Hma,
Kama,
Mama,
Smma,
Tema,
Wma
}

/// <summary>
Expand All @@ -80,9 +80,9 @@ public enum MaType
/// </summary>
public enum PeriodSize
{
Month,
Day = 0, // default
Week,
Day,
Month,
FourHours,
TwoHours,
OneHour,
Expand Down
18 changes: 9 additions & 9 deletions src/_common/Generics/Pruning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ internal static List<T> Remove<T>(
{
return [];
}
else
{
if (removePeriods > 0)
{
for (int i = 0; i < removePeriods; i++)
{
seriesList.RemoveAt(0);
}
}

if (removePeriods <= 0)
{
return seriesList;
}

for (int i = 0; i < removePeriods; i++)
{
seriesList.RemoveAt(0);
}

return seriesList;
}
}
13 changes: 6 additions & 7 deletions src/_common/Generics/Seek.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ public static class Seeking
/// <exception cref="InvalidOperationException">
/// Sequence contains no matching element
/// </exception>
public static TSeries? Find<TSeries>(
public static TSeries Find<TSeries>(
this IEnumerable<TSeries> series,
DateTime lookupDate)
where TSeries : ISeries => series
.First(x => x.Timestamp == lookupDate);
where TSeries : ISeries
=> series.First(x => x.Timestamp == lookupDate);

// TODO: add TryFind(), like TryParse() since struct won't allow null return types.
// May just use this (above) with a try/catch and `bool` primary return type.

// FIND INDEX by DATE
public static int FindIndex<TSeries>(
this List<TSeries> series,
this List<TSeries>? series,
DateTime lookupDate)
where TSeries : ISeries => series == null
? -1
: series.FindIndex(x => x.Timestamp == lookupDate);
where TSeries : ISeries
=> series?.FindIndex(x => x.Timestamp == lookupDate) ?? -1;
}
10 changes: 5 additions & 5 deletions src/_common/Math/NullMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ namespace Skender.Stock.Indicators;
public static class NullMath
{
public static double? Abs(this double? value)
=> (value is null)
=> value is null
? null
: value < 0 ? (double)-value : (double)value;

public static decimal? Round(this decimal? value, int digits)
=> (value is null)
=> value is null
? null
: Math.Round((decimal)value, digits);

public static double? Round(this double? value, int digits)
=> (value is null)
=> value is null
? null
: Math.Round((double)value, digits);

Expand All @@ -31,12 +31,12 @@ public static double Null2NaN(this double? value)
=> value ?? double.NaN;

public static double Null2NaN(this decimal? value)
=> (value is null)
=> value is null
? double.NaN
: (double)value;

public static double? NaN2Null(this double? value)
=> (value is not null and double.NaN)
=> value is double.NaN
? null
: value;

Expand Down
36 changes: 19 additions & 17 deletions src/_common/Math/Numerix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ public static double StdDev(this double[] values)

double sd = 0;
int n = values.Length;
if (n > 1)
if (n <= 1)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += values[i];
}

double avg = sum / n;

double sumSq = 0;
for (int i = 0; i < n; i++)
{
double v = values[i];
sumSq += (v - avg) * (v - avg);
}

sd = Math.Sqrt(sumSq / n);
return sd;
}

double sum = 0;
for (int i = 0; i < n; i++)
{
sum += values[i];
}

double avg = sum / n;

double sumSq = 0;
for (int i = 0; i < n; i++)
{
double v = values[i];
sumSq += (v - avg) * (v - avg);
}

sd = Math.Sqrt(sumSq / n);

return sd;
}

Expand Down
Loading

0 comments on commit c27147c

Please sign in to comment.