Skip to content

Commit

Permalink
refactor: some some candle initialization logic into base class
Browse files Browse the repository at this point in the history
  • Loading branch information
phmatray committed Nov 27, 2023
1 parent 07f3c7e commit 06f5846
Show file tree
Hide file tree
Showing 69 changed files with 400 additions and 778 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A modern and robust C# Technical Analysis library based on the original open-sou
[![GitHub contributors](https://img.shields.io/github/contributors/phmatray/TaLibStandard)](https://github.com/phmatray/TaLibStandard/graphs/contributors)
[![GitHub last commit](https://img.shields.io/github/last-commit/phmatray/TaLibStandard)](https://github.com/phmatray/TaLibStandard/commits/master)
[![codecov](https://codecov.io/gh/phmatray/TaLibStandard/branch/main/graph/badge.svg?token=041C4QKW6O)](https://app.codecov.io/gh/phmatray/TaLibStandard/tree/main)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/84e2475f22a04bc1bed551f081029e82)](https://www.codacy.com/gh/phmatray/TaLibStandard/dashboard?utm_source=github.com&utm_medium=referral&utm_content=phmatray/TaLibStandard&utm_campaign=Badge_Grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/84e2475f22a04bc1bed551f081029e82)](https://app.codacy.com/gh/phmatray/TaLibStandard/dashboard)

---

Expand Down
4 changes: 4 additions & 0 deletions docs/common/Atypical.TechnicalAnalysis.Common.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
- **[IsColorOpposite(int, int)](CandleIndicator_T_.IsColorOpposite(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.IsColorOpposite(int, int)')** `Method`
- **[IsColorRed(int)](CandleIndicator_T_.IsColorRed(int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.IsColorRed(int)')** `Method`
- **[IsColorSame(int, int)](CandleIndicator_T_.IsColorSame(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.IsColorSame(int, int)')** `Method`
- **[PrepareOutput(int, int)](CandleIndicator_T_.PrepareOutput(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.PrepareOutput(int, int)')** `Method` Prepares the output variables.
- **[ValidateIndices(int, int)](CandleIndicator_T_.ValidateIndices(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.ValidateIndices(int, int)')** `Method` Validates the specified indices.
- **[ValidateParameters(T)](CandleIndicator_T_.ValidateParameters(T).md 'TechnicalAnalysis.Common.CandleIndicator<T>.ValidateParameters(T)')** `Method` Validates the specified parameters.
- **[ValidatePriceArrays()](CandleIndicator_T_.ValidatePriceArrays().md 'TechnicalAnalysis.Common.CandleIndicator<T>.ValidatePriceArrays()')** `Method` Validates the price arrays.
- **[CandleIndicatorResult](CandleIndicatorResult.md 'TechnicalAnalysis.Common.CandleIndicatorResult')** `Class` Represents the result of the candlestick pattern indicator.
- **[CandleIndicatorResult(RetCode, int, int, int[])](CandleIndicatorResult.CandleIndicatorResult(RetCode,int,int,int[]).md 'TechnicalAnalysis.Common.CandleIndicatorResult.CandleIndicatorResult(TechnicalAnalysis.Common.RetCode, int, int, int[])')** `Constructor` Initializes a new instance of the CandleResult class.
- **[Integers](CandleIndicatorResult.Integers.md 'TechnicalAnalysis.Common.CandleIndicatorResult.Integers')** `Property` Gets the array of integers indicating the presence of the candle pattern. (values are -100, 0 or 100)
Expand Down
27 changes: 27 additions & 0 deletions docs/common/CandleIndicator_T_.PrepareOutput(int,int).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md 'Atypical.TechnicalAnalysis.Common')
### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md#TechnicalAnalysis.Common 'TechnicalAnalysis.Common').[CandleIndicator&lt;T&gt;](CandleIndicator_T_.md 'TechnicalAnalysis.Common.CandleIndicator<T>')

## CandleIndicator<T>.PrepareOutput(int, int) Method

Prepares the output variables.

```csharp
protected virtual (int outBegIdx,int outNBElement,int[] outInteger) PrepareOutput(int startIdx, int endIdx);
```
#### Parameters

<a name='TechnicalAnalysis.Common.CandleIndicator_T_.PrepareOutput(int,int).startIdx'></a>

`startIdx` [System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')

The start index.

<a name='TechnicalAnalysis.Common.CandleIndicator_T_.PrepareOutput(int,int).endIdx'></a>

`endIdx` [System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')

The end index.

#### Returns
[&lt;](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')[,](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')[,](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')[System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')[[]](https://docs.microsoft.com/en-us/dotnet/api/System.Array 'System.Array')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.ValueTuple 'System.ValueTuple')
A tuple containing the output variables.
23 changes: 23 additions & 0 deletions docs/common/CandleIndicator_T_.ValidateIndices(int,int).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md 'Atypical.TechnicalAnalysis.Common')
### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md#TechnicalAnalysis.Common 'TechnicalAnalysis.Common').[CandleIndicator&lt;T&gt;](CandleIndicator_T_.md 'TechnicalAnalysis.Common.CandleIndicator<T>')

## CandleIndicator<T>.ValidateIndices(int, int) Method

Validates the specified indices.

```csharp
protected virtual void ValidateIndices(ref int startIdx, ref int endIdx);
```
#### Parameters

<a name='TechnicalAnalysis.Common.CandleIndicator_T_.ValidateIndices(int,int).startIdx'></a>

`startIdx` [System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')

The start index.

<a name='TechnicalAnalysis.Common.CandleIndicator_T_.ValidateIndices(int,int).endIdx'></a>

`endIdx` [System.Int32](https://docs.microsoft.com/en-us/dotnet/api/System.Int32 'System.Int32')

The end index.
17 changes: 17 additions & 0 deletions docs/common/CandleIndicator_T_.ValidateParameters(T).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md 'Atypical.TechnicalAnalysis.Common')
### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md#TechnicalAnalysis.Common 'TechnicalAnalysis.Common').[CandleIndicator&lt;T&gt;](CandleIndicator_T_.md 'TechnicalAnalysis.Common.CandleIndicator<T>')

## CandleIndicator<T>.ValidateParameters(T) Method

Validates the specified parameters.

```csharp
protected virtual void ValidateParameters(T penetration);
```
#### Parameters

<a name='TechnicalAnalysis.Common.CandleIndicator_T_.ValidateParameters(T).penetration'></a>

`penetration` [T](CandleIndicator_T_.md#TechnicalAnalysis.Common.CandleIndicator_T_.T 'TechnicalAnalysis.Common.CandleIndicator<T>.T')

The penetration to validate.
10 changes: 10 additions & 0 deletions docs/common/CandleIndicator_T_.ValidatePriceArrays().md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md 'Atypical.TechnicalAnalysis.Common')
### [TechnicalAnalysis.Common](Atypical.TechnicalAnalysis.Common.md#TechnicalAnalysis.Common 'TechnicalAnalysis.Common').[CandleIndicator&lt;T&gt;](CandleIndicator_T_.md 'TechnicalAnalysis.Common.CandleIndicator<T>')

## CandleIndicator<T>.ValidatePriceArrays() Method

Validates the price arrays.

```csharp
protected virtual void ValidatePriceArrays();
```
4 changes: 4 additions & 0 deletions docs/common/CandleIndicator_T_.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ Inheritance [System.Object](https://docs.microsoft.com/en-us/dotnet/api/System.O
| [IsColorOpposite(int, int)](CandleIndicator_T_.IsColorOpposite(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.IsColorOpposite(int, int)') | |
| [IsColorRed(int)](CandleIndicator_T_.IsColorRed(int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.IsColorRed(int)') | |
| [IsColorSame(int, int)](CandleIndicator_T_.IsColorSame(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.IsColorSame(int, int)') | |
| [PrepareOutput(int, int)](CandleIndicator_T_.PrepareOutput(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.PrepareOutput(int, int)') | Prepares the output variables. |
| [ValidateIndices(int, int)](CandleIndicator_T_.ValidateIndices(int,int).md 'TechnicalAnalysis.Common.CandleIndicator<T>.ValidateIndices(int, int)') | Validates the specified indices. |
| [ValidateParameters(T)](CandleIndicator_T_.ValidateParameters(T).md 'TechnicalAnalysis.Common.CandleIndicator<T>.ValidateParameters(T)') | Validates the specified parameters. |
| [ValidatePriceArrays()](CandleIndicator_T_.ValidatePriceArrays().md 'TechnicalAnalysis.Common.CandleIndicator<T>.ValidatePriceArrays()') | Validates the price arrays. |
31 changes: 5 additions & 26 deletions src/TechnicalAnalysis.Candles/Candle2Crows/Candle2Crows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ public class Candle2Crows<T>(in T[] open, in T[] high, in T[] low, in T[] close)
{
private T _bodyLongPeriodTotal = T.Zero;

private void ValidateIndices(ref int startIdx, ref int endIdx)
{
// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
}

/// <summary>
/// Computes the <see cref="Candle2Crows{T}"/> indicator.
/// </summary>
Expand All @@ -42,19 +29,11 @@ private void ValidateIndices(ref int startIdx, ref int endIdx)
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ public class Candle3BlackCrows<T>(in T[] open, in T[] high, in T[] low, in T[] c
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
16 changes: 4 additions & 12 deletions src/TechnicalAnalysis.Candles/Candle3Inside/Candle3Inside.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,11 @@ public class Candle3Inside<T>(in T[] open, in T[] high, in T[] low, in T[] close
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ public class Candle3LineStrike<T>(in T[] open, in T[] high, in T[] low, in T[] c
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
16 changes: 4 additions & 12 deletions src/TechnicalAnalysis.Candles/Candle3Outside/Candle3Outside.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,11 @@ public class Candle3Outside<T>(in T[] open, in T[] high, in T[] low, in T[] clos
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ public class Candle3StarsInSouth<T>(in T[] open, in T[] high, in T[] low, in T[]
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ public class Candle3WhiteSoldiers<T>(in T[] open, in T[] high, in T[] low, in T[
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,12 @@ public CandleIndicatorResult Compute(int startIdx, int endIdx, in T optInPenetra
_penetration = optInPenetration;

// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);

// Verify parameters
ArgumentOutOfRangeException.ThrowIfNegative(optInPenetration);
// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();
ValidateParameters(optInPenetration);

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,11 @@ public class CandleAdvanceBlock<T>(in T[] open, in T[] high, in T[] low, in T[]
public CandleIndicatorResult Compute(int startIdx, int endIdx)
{
// Initialize output variables
int outBegIdx = default;
int outNBElement = default;
int[] outInteger = new int[int.Max(0, endIdx - startIdx + 1)];

// Validate the requested output range.
ArgumentOutOfRangeException.ThrowIfNegative(startIdx);
ArgumentOutOfRangeException.ThrowIfNegative(endIdx - startIdx);

// Verify required price component.
ArgumentNullException.ThrowIfNull(Open);
ArgumentNullException.ThrowIfNull(High);
ArgumentNullException.ThrowIfNull(Low);
ArgumentNullException.ThrowIfNull(Close);
(int outBegIdx, int outNBElement, int[] outInteger) = PrepareOutput(startIdx, endIdx);

// Validations
ValidateIndices(ref startIdx, ref endIdx);
ValidatePriceArrays();

// Identify the minimum number of price bar needed to calculate at least one output.
int lookbackTotal = GetLookback();
Expand Down
Loading

0 comments on commit 06f5846

Please sign in to comment.