Skip to content

Commit

Permalink
Added custom Counter indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinR committed Apr 8, 2024
1 parent 926012a commit 82a3b3e
Show file tree
Hide file tree
Showing 7 changed files with 1,093 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

The project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.0 - TBD
- Added Counter Indicator

## 1.0.1 - 2024-04-08
- Fixed #12 Inaccurate verify_indicators method in Hexital

Expand Down
2 changes: 2 additions & 0 deletions hexital/indicators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .adx import ADX
from .amorph import Amorph
from .atr import ATR
from .counter import Counter
from .ema import EMA
from .hla import HighLowAverage
from .kc import KC
Expand All @@ -19,6 +20,7 @@

INDICATOR_MAP = {
"Amorph": Amorph,
"Counter": Counter,
"ADX": ADX,
"ATR": ATR,
"EMA": EMA,
Expand Down
41 changes: 41 additions & 0 deletions hexital/indicators/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from dataclasses import dataclass, field

from hexital.core.indicator import Indicator


@dataclass(kw_only=True)
class Counter(Indicator):
"""Counter
Simple Indictor which will count the current streak of a given value,
specifically designed for bool values, but useable on any other re-occuring values.
E.G Count the streak for current input value == count_value
Args:
Input value (str)
Count Value(bool | int): Default True
- What to check value matches.
"""

_name: str = field(init=False, default="COUNT")
input_value: str
count_value: bool | int = True

def _generate_name(self) -> str:
return f"{self._name}_{self.input_value.split('.')[0]}"

def _calculate_reading(self, index: int) -> float | dict | None:
reading = self.reading(self.input_value)
count = self.prev_reading()

if not count:
count = 0

if reading is None:
return count

if self.count_value == reading:
count += 1
else:
count = 0

return count
13 changes: 13 additions & 0 deletions tests/data/fixtures_indicator_source_of_truth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

SOURCE_OF_TRUTH_PATH = "tests/data/source_of_truth/indicator"
SOURCE_OF_TRUTH_HEX_PATH = "tests/data/source_of_truth/hex_indicators"


@pytest.fixture(name="expected_adx")
Expand Down Expand Up @@ -159,3 +160,15 @@ def fixture_expected_wma():
def fixture_expected_vwma():
csv_files = open(f"{SOURCE_OF_TRUTH_PATH}/VWMA.json")
return json.load(csv_files)


@pytest.fixture(name="expected_counter_bear")
def fixture_expected_counter_bear():
csv_files = open(f"{SOURCE_OF_TRUTH_HEX_PATH}/COUNTER_supertrend_bear.json")
return json.load(csv_files)


@pytest.fixture(name="expected_counter_bull")
def fixture_expected_counter_bull():
csv_files = open(f"{SOURCE_OF_TRUTH_HEX_PATH}/COUNTER_supertrend_bull.json")
return json.load(csv_files)
Loading

0 comments on commit 82a3b3e

Please sign in to comment.