Skip to content

Commit 2808299

Browse files
committed
Add multi df dataframe divergence tests
1 parent ce24d31 commit 2808299

File tree

2 files changed

+443
-29
lines changed

2 files changed

+443
-29
lines changed

pyindicators/indicators/divergence.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,19 @@ def bullish_divergence(
278278
is_polars = isinstance(data, pl.DataFrame)
279279
df = data.to_pandas() if is_polars else data.copy()
280280

281-
# Check if the two columns are in the data
282-
if first_column not in data.columns or second_column not in data.columns:
283-
raise PyIndicatorException(
284-
f"{first_column} and {second_column} columns "
285-
"are required in the data"
286-
)
281+
# Check if the highs and lows columns are present
282+
first_column_lows = f"{first_column}_lows"
283+
second_column_lows = f"{second_column}_lows"
284+
285+
if first_column_lows not in data.columns \
286+
or second_column_lows not in data.columns:
287+
288+
# Check if the two columns are in the data
289+
if first_column not in data.columns or second_column not in data.columns:
290+
raise PyIndicatorException(
291+
f"{first_column} and {second_column} columns "
292+
"are required in the data"
293+
)
287294

288295
if window_size < 1:
289296
raise PyIndicatorException("Window size must be greater than 0")
@@ -294,10 +301,6 @@ def bullish_divergence(
294301
f"It currently has {len(data)} data points"
295302
)
296303

297-
# Check if the highs and lows columns are present
298-
first_column_lows = f"{first_column}_lows"
299-
second_column_lows = f"{second_column}_lows"
300-
301304
if first_column_lows not in data.columns:
302305
data = detect_peaks(
303306
data,
@@ -380,15 +383,6 @@ def bearish_divergence(
380383
the function will consider the current and previous data point,
381384
e.g. this will be true [1, 0] and [0, -1]
382385
and false [0, 0] and [0, -1].
383-
number_of_data_points (int): The number of data points
384-
to consider when using a sliding windows size when checking for
385-
divergence. For example, if the number_of_data_points
386-
is 1, the function will consider only the current two data points.
387-
If the number_of_data_points is 4 and the window size is 2,
388-
the function will consider the current and previous 3 data
389-
points when checking for divergence. Then the function will
390-
slide the window by 1 and check the next 2 data points until
391-
the end of the data.
392386
result_column (str): The name of the column to store
393387
the bearish divergence results. Defaults to "bearish_divergence".
394388
number_of_neighbors_to_compare (int): The number of neighboring
@@ -410,12 +404,19 @@ def bearish_divergence(
410404
is_polars = isinstance(data, pl.DataFrame)
411405
df = data.to_pandas() if is_polars else data.copy()
412406

413-
# Check if the two columns are in the data
414-
if first_column not in data.columns or second_column not in data.columns:
415-
raise PyIndicatorException(
416-
f"{first_column} and {second_column} columns "
417-
"are required in the data"
418-
)
407+
# Check if the highs and lows columns are present
408+
first_column_highs = f"{first_column}_highs"
409+
second_column_highs = f"{second_column}_highs"
410+
411+
if first_column_highs not in data.columns \
412+
or second_column_highs not in data.columns:
413+
414+
# Check if the two columns are in the data
415+
if first_column not in data.columns or second_column not in data.columns:
416+
raise PyIndicatorException(
417+
f"{first_column} and {second_column} columns "
418+
"are required in the data"
419+
)
419420

420421
if window_size < 1:
421422
raise PyIndicatorException("Window size must be greater than 0")
@@ -463,7 +464,6 @@ def bearish_divergence(
463464
df[result_column] = result
464465
return pl.DataFrame(df) if is_polars else df
465466

466-
467467
def bearish_divergence_multi_dataframe(
468468
first_df: Union[pd.DataFrame, pl.DataFrame],
469469
second_df: Union[pd.DataFrame, pl.DataFrame],
@@ -511,7 +511,9 @@ def bearish_divergence_multi_dataframe(
511511
(first_df, first_column, "first_df"),
512512
(second_df, second_column, "second_df")
513513
]:
514-
if col not in df.columns:
514+
high_column = f"{col}_highs"
515+
516+
if high_column not in df.columns and col not in df.columns:
515517
raise PyIndicatorException(f"{col} column is missing in {label}")
516518

517519
# Determine which df has more granular datetime index
@@ -635,7 +637,9 @@ def bullish_divergence_multi_dataframe(
635637
(first_df, first_column, "first_df"),
636638
(second_df, second_column, "second_df")
637639
]:
638-
if col not in df.columns:
640+
lows_column = f"{col}_lows"
641+
642+
if lows_column not in df.columns and col not in df.columns:
639643
raise PyIndicatorException(f"{col} column is missing in {label}")
640644

641645
# Determine which df has more granular datetime index

0 commit comments

Comments
 (0)