Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add ADR custom indicator (example) #1286

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/examples/CustomIndicatorsLibrary/Adr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.ObjectModel;

using Skender.Stock.Indicators;

namespace Custom.Stock.Indicators;

// Custom results classes
// This inherits many of the extension methods, like .RemoveWarmupPeriods()
public sealed class DrResult : ResultBase, IReusableResult
{
// date property is inherited here,
// so you only need to add custom items
public decimal Dr { get; set; }

// to enable further chaining
double? IReusableResult.Value => (double)Dr;
}

public sealed class AdrResult : ResultBase, IReusableResult
{
public decimal Dr { get; set; }
public double? Adr { get; set; }
public double? Adrp { get; set; }

double? IReusableResult.Value => Adrp;
}

public static partial class CustomIndicators
{
// ADR calculation
public static IEnumerable<AdrResult> GetAdr<TQuote>(
this IEnumerable<TQuote> quotes,
int lookbackPeriods)
where TQuote : IQuote
{
// TODO: this a bit of a hack, only to demonstrate the concept
// of a custom indicator. This may be added to the library in the future.

// sort quotes and convert to collection or list
Collection<TQuote> quotesList = quotes
.ToSortedCollection();

// initialize
int length = quotes.Count();

// daily range
List<DrResult> drResult = quotes
.Select(x => new DrResult
{
Date = x.Date,
Dr = x.High - x.Low
}).ToList();

// average daily range
List<AdrResult> results = drResult
.GetSma(lookbackPeriods)
.Select(x => new AdrResult
{
Date = x.Date,
Adr = x.Sma
}).ToList();

// combine, add average daily range percentage
for (int i = 0; i < length; i++)
{
results[i].Dr = drResult[i].Dr;
results[i].Adrp = results[i].Adr / (double)quotesList[i].Close;
}

return results;
}
}
2 changes: 1 addition & 1 deletion docs/examples/CustomIndicatorsLibrary/AtrWma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class AtrWmaResult : ResultBase, IReusableResult
double? IReusableResult.Value => AtrWma;
}

public static class CustomIndicators
public static partial class CustomIndicators
{
// Custom ATR WMA calculation
public static IEnumerable<AtrWmaResult> GetAtrWma<TQuote>(
Expand Down
Loading