|
1 | 1 | import numpy as np |
2 | 2 | from unittest import TestCase |
3 | 3 | from pyindicators import is_divergence, bearish_divergence_multi_dataframe, \ |
4 | | - PyIndicatorException |
| 4 | + PyIndicatorException, bullish_divergence_multi_dataframe |
5 | 5 |
|
6 | 6 | import pandas as pd |
7 | 7 |
|
@@ -261,3 +261,162 @@ def test_different_timeframes_align_correctly(self): |
261 | 261 | ) |
262 | 262 | self.assertIn("bearish_divergence", result.columns) |
263 | 263 | self.assertTrue(any(result["bearish_divergence"])) |
| 264 | + |
| 265 | + |
| 266 | +class TestBullishDivergenceMultiDataFrame(TestCase): |
| 267 | + |
| 268 | + def test_bullish_divergence_detected(self): |
| 269 | + # Setup indicator (e.g., RSI) and price (e.g., Close) with divergence |
| 270 | + indicator = pd.DataFrame({ |
| 271 | + "RSI": [50, 60, 70, 65, 60, 58, 55], |
| 272 | + }, index=pd.date_range("2022-01-01", periods=7)) |
| 273 | + price = pd.DataFrame({ |
| 274 | + "Close": [100, 102, 105, 108, 110, 112, 115], # Higher highs |
| 275 | + }, index=pd.date_range("2022-01-01", periods=7)) |
| 276 | + |
| 277 | + result = pd.DataFrame(index=indicator.index) |
| 278 | + |
| 279 | + # Force peaks manually for deterministic test |
| 280 | + indicator["RSI_lows"] = [0, 0, 0, 0, -1, 0, 0] # Two indicator highs |
| 281 | + price["Close_lows"] = [0, 0, 0, 0, 1, 0, 0] # Two price highs |
| 282 | + |
| 283 | + out = bullish_divergence_multi_dataframe( |
| 284 | + first_df=indicator, |
| 285 | + second_df=price, |
| 286 | + result_df=result, |
| 287 | + first_column="RSI", |
| 288 | + second_column="Close", |
| 289 | + window_size=2, |
| 290 | + result_column="bullish_divergence" |
| 291 | + ) |
| 292 | + |
| 293 | + self.assertIn("bullish_divergence", out.columns) |
| 294 | + self.assertTrue(any(out["bullish_divergence"])) |
| 295 | + |
| 296 | + indicator = pd.DataFrame({ |
| 297 | + "RSI": [50, 60, 70, 65, 60, 58, 55], |
| 298 | + }, index=pd.date_range("2022-01-01", periods=7)) |
| 299 | + price = pd.DataFrame({ |
| 300 | + "Close": [100, 102, 105, 108, 110, 112, 115], # Higher highs |
| 301 | + }, index=pd.date_range("2022-01-01", periods=7)) |
| 302 | + |
| 303 | + result = pd.DataFrame(index=indicator.index) |
| 304 | + |
| 305 | + # Force peaks manually for deterministic test |
| 306 | + indicator["RSI_lows"] = [0, 0, 0, 0, 1, 0, 0] # Two indicator highs |
| 307 | + price["Close_lows"] = [0, 0, 0, 0, 1, 0, 0] # Two price highs |
| 308 | + |
| 309 | + out = bullish_divergence_multi_dataframe( |
| 310 | + first_df=indicator, |
| 311 | + second_df=price, |
| 312 | + result_df=result, |
| 313 | + first_column="RSI", |
| 314 | + second_column="Close", |
| 315 | + window_size=2, |
| 316 | + result_column="bullish_divergence" |
| 317 | + ) |
| 318 | + |
| 319 | + self.assertIn("bullish_divergence", out.columns) |
| 320 | + self.assertFalse(any(out["bullish_divergence"])) |
| 321 | + |
| 322 | + indicator = pd.DataFrame({ |
| 323 | + "RSI": [50, 60, 70, 65, 60, 58, 55], |
| 324 | + }, index=pd.date_range("2022-01-01", periods=7)) |
| 325 | + price = pd.DataFrame({ |
| 326 | + "Close": [100, 102, 105, 108, 110, 112, 115], # Higher highs |
| 327 | + }, index=pd.date_range("2022-01-01", periods=7)) |
| 328 | + |
| 329 | + result = pd.DataFrame(index=indicator.index) |
| 330 | + |
| 331 | + # Force peaks manually for deterministic test |
| 332 | + indicator["RSI_lows"] = [0, 0, 0, -1, 0, 0, 0] # Two indicator highs |
| 333 | + price["Close_lows"] = [0, 0, 0, 0, 0, 1, 0] # Two price highs |
| 334 | + |
| 335 | + out = bullish_divergence_multi_dataframe( |
| 336 | + first_df=indicator, |
| 337 | + second_df=price, |
| 338 | + result_df=result, |
| 339 | + first_column="RSI", |
| 340 | + second_column="Close", |
| 341 | + window_size=2, |
| 342 | + result_column="bullish_divergence" |
| 343 | + ) |
| 344 | + |
| 345 | + self.assertIn("bullish_divergence", out.columns) |
| 346 | + self.assertFalse(any(out["bullish_divergence"])) |
| 347 | + |
| 348 | + out = bullish_divergence_multi_dataframe( |
| 349 | + first_df=indicator, |
| 350 | + second_df=price, |
| 351 | + result_df=result, |
| 352 | + first_column="RSI", |
| 353 | + second_column="Close", |
| 354 | + window_size=3, |
| 355 | + result_column="bullish_divergence" |
| 356 | + ) |
| 357 | + |
| 358 | + self.assertIn("bullish_divergence", out.columns) |
| 359 | + self.assertTrue(any(out["bullish_divergence"])) |
| 360 | + |
| 361 | + def test_missing_column_exception(self): |
| 362 | + df1 = pd.DataFrame({"RSI": [50, 60]}, index=pd.date_range("2022-01-01", periods=2)) |
| 363 | + df2 = pd.DataFrame({"Close": [100, 110]}, index=pd.date_range("2022-01-01", periods=2)) |
| 364 | + result = pd.DataFrame(index=df1.index) |
| 365 | + |
| 366 | + with self.assertRaises(PyIndicatorException): |
| 367 | + bullish_divergence_multi_dataframe( |
| 368 | + first_df=df1.drop("RSI", axis=1), |
| 369 | + second_df=df2, |
| 370 | + result_df=result, |
| 371 | + first_column="RSI", |
| 372 | + second_column="Close" |
| 373 | + ) |
| 374 | + |
| 375 | + def test_not_enough_data_exception(self): |
| 376 | + df1 = pd.DataFrame({"RSI": [50]}, index=pd.date_range("2022-01-01", periods=1)) |
| 377 | + df2 = pd.DataFrame({"Close": [100]}, index=pd.date_range("2022-01-01", periods=1)) |
| 378 | + result = pd.DataFrame(index=df1.index) |
| 379 | + |
| 380 | + # Assume detect_peaks adds _highs column |
| 381 | + df1["RSI_lows"] = [1] |
| 382 | + df2["Close_lows"] = [1] |
| 383 | + |
| 384 | + with self.assertRaises(PyIndicatorException): |
| 385 | + bullish_divergence_multi_dataframe( |
| 386 | + first_df=df1, |
| 387 | + second_df=df2, |
| 388 | + result_df=result, |
| 389 | + first_column="RSI", |
| 390 | + second_column="Close", |
| 391 | + window_size=3 |
| 392 | + ) |
| 393 | + |
| 394 | + def test_different_timeframes_align_correctly(self): |
| 395 | + daily_index = pd.date_range("2022-01-01", periods=2, freq="D") |
| 396 | + indicator_df = pd.DataFrame({ |
| 397 | + "RSI": [65, 60], |
| 398 | + }, index=daily_index) |
| 399 | + |
| 400 | + # 2-hour close prices — only some times will match the daily timestamps |
| 401 | + two_hour_index = pd.date_range("2022-01-01", periods=12, freq="2h") |
| 402 | + price_df = pd.DataFrame({ |
| 403 | + "Close": [100, 102, 105, 108, 110, 112, 115, 117, 120, 122, 125, 130] |
| 404 | + }, index=two_hour_index) |
| 405 | + |
| 406 | + result_df = pd.DataFrame(index=price_df.index) |
| 407 | + |
| 408 | + # Inject fake peaks |
| 409 | + indicator_df["RSI_lows"] = [-1, 0] |
| 410 | + price_df["Close_lows"] = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 411 | + |
| 412 | + result = bullish_divergence_multi_dataframe( |
| 413 | + first_df=indicator_df, |
| 414 | + second_df=price_df, |
| 415 | + result_df=result_df, |
| 416 | + first_column="RSI", |
| 417 | + second_column="Close", |
| 418 | + window_size=2, |
| 419 | + result_column="bullish_divergence" |
| 420 | + ) |
| 421 | + self.assertIn("bullish_divergence", result.columns) |
| 422 | + self.assertTrue(any(result["bullish_divergence"])) |
0 commit comments