Skip to content

Fix RuntimeWarning: divide by zero in retracement calculation#99

Merged
joshyattridge merged 1 commit intojoshyattridge:masterfrom
wavebyrd:fix-divide-by-zero
Apr 3, 2026
Merged

Fix RuntimeWarning: divide by zero in retracement calculation#99
joshyattridge merged 1 commit intojoshyattridge:masterfrom
wavebyrd:fix-divide-by-zero

Conversation

@wavebyrd
Copy link
Copy Markdown
Contributor

Summary

Fix the divide by zero warning in fib_retracements() when top == bottom.

Problem

RuntimeWarning: divide by zero encountered in scalar divide
100 - ((ohlc["high"][i] - top) / (bottom - top)) * 100

Solution

Add a guard to check if the divisor is zero before performing division:

  • For bullish direction: check top - bottom != 0
  • For bearish direction: check bottom - top != 0

When the divisor is zero, return 0 as the retracement value.

Changes

# Before
100 - ((ohlc["high"].iloc[i] - top) / (bottom - top)) * 100

# After  
divisor = bottom - top
100 - ((ohlc["high"].iloc[i] - top) / divisor) * 100 if divisor != 0 else 0

Fixes #33

🤖 Generated with Claude Code

Add zero-division guard when calculating current_retracement for both
bullish (direction == 1) and bearish (direction == -1) cases. When
top == bottom, the retracement is set to 0 instead of causing a
RuntimeWarning.

Fixes joshyattridge#33

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@wavebyrd
Copy link
Copy Markdown
Contributor Author

Hi! Just checking in on this fix for the divide-by-zero warning. Happy to make any adjustments if needed.

@joshyattridge joshyattridge merged commit 9a7cac4 into joshyattridge:master Apr 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RuntimeWarning: divide by zero encountered in scalar divide

2 participants