Skip to content

Commit

Permalink
Optimize Bollinger Bands indicator with Numba JIT compilation
Browse files Browse the repository at this point in the history
- Implement Numba-accelerated Bollinger Bands calculation function
- Replace moving standard deviation with efficient Numba-optimized implementation
- Simplify function parameters and improve computational efficiency
- Maintain consistent function interface and return types
  • Loading branch information
saleh-mir committed Feb 13, 2025
1 parent 6d44f2d commit 1a67d0c
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions jesse/indicators/bollinger_bands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import namedtuple

import numpy as np
from numba import njit

from jesse.helpers import get_candle_source, slice_candles
from jesse.indicators.ma import ma
Expand All @@ -10,16 +11,35 @@
BollingerBands = namedtuple('BollingerBands', ['upperband', 'middleband', 'lowerband'])


def moving_std(source, period):
# Compute moving standard deviation for each window of 'period' elements
result = np.empty_like(source, dtype=float)
result[:period-1] = float('nan')
for i in range(period - 1, len(source)):
window = source[i - period + 1: i + 1]
result[i] = np.std(window)
@njit
def _moving_std_numba(source, period):
n = len(source)
result = np.empty(n, dtype=np.float64)
# Fill the first period-1 entries with NaN
for i in range(period - 1):
result[i] = np.nan
# Calculate standard deviation for each window of 'period' elements
for i in range(period - 1, n):
sum_val = 0.0
sum_sq = 0.0
for j in range(i - period + 1, i + 1):
x = source[j]
sum_val += x
sum_sq += x * x
mean = sum_val / period
variance = sum_sq / period - mean * mean
# Guard against possible negative variance from precision issues
if variance < 0.0:
variance = 0.0
result[i] = variance ** 0.5
return result


def moving_std(source, period):
# Use the Numba-accelerated function instead of the Python loop with np.std
return _moving_std_numba(source, period)


def bollinger_bands(
candles: np.ndarray,
period: int = 20,
Expand Down

0 comments on commit 1a67d0c

Please sign in to comment.