Skip to content

Commit

Permalink
adding cache fot njit decorators - the result is a 3x speed improveme…
Browse files Browse the repository at this point in the history
…nt in with pytest
  • Loading branch information
kakulukia committed Apr 6, 2024
1 parent 4a957db commit 81834e1
Show file tree
Hide file tree
Showing 37 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion jesse/indicators/bandpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def bandpass(candles: np.ndarray, period: int = 20, bandwidth: float = 0.3, sou
return BandPass(bp[-1], bp_normalized[-1], signal[-1], trigger[-1])


@njit
@njit(cache=True)
def bp_fast(source, hp, alpha, beta): # Function is compiled to machine code when called the first time

bp = np.copy(hp)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/cg.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def cg(candles: np.ndarray, period: int = 10, source_type: str = "close", sequen
return same_length(candles, res) if sequential else res[-1]


@njit
@njit(cache=True)
def go_fast(source, period): # Function is compiled to machine code when called the first time
res = np.full_like(source, fill_value=np.nan)
for i in range(source.size):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/correlation_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def correlation_cycle(candles: np.ndarray, period: int = 20, threshold: int = 9,
return CC(realPart[-1], imagPart[-1], angle[-1], state[-1])


@njit
@njit(cache=True)
def go_fast(source, period): # Function is compiled to machine code when called the first time
# Correlation Cycle Function
PIx2 = 4.0 * np.arcsin(1.0)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/cwma.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def cwma(candles: np.ndarray, period: int = 14, source_type: str = "close", sequ
return res if sequential else res[-1]


@njit
@njit(cache=True)
def vpwma_fast(source, period):
newseries = np.copy(source)
for j in range(period + 1, source.shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/damiani_volatmeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def damiani_volatmeter(candles: np.ndarray, vis_atr: int = 13, vis_std: int = 20
return DamianiVolatmeter(vol[-1], t[-1])


@njit
@njit(cache=True)
def damiani_volatmeter_fast(source, sed_std, atrvis, atrsed, vis_std,
threshold): # Function is compiled to machine code when called the first time
lag_s = 0.5
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/edcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def edcf(candles: np.ndarray, period: int = 15, source_type: str = "hl2", sequen
return res if sequential else res[-1]


@njit
@njit(cache=True)
def edcf_fast(source, period):
newseries = np.full_like(source, np.nan)

Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/efi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def efi(candles: np.ndarray, period: int = 13, source_type: str = "close", seque
return res_with_nan if sequential else res_with_nan[-1]


@njit
@njit(cache=True)
def efi_fast(source, volume):
dif = np.zeros(source.size - 1)
for i in range(1, source.size):
Expand Down
4 changes: 2 additions & 2 deletions jesse/indicators/emd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def emd(candles: np.ndarray, period: int = 20, delta=0.5, fraction=0.1, sequenti
return EMD(avg_peak[-1], mean[-1], avg_valley[-1])


@njit
@njit(cache=True)
def bp_fast(price, period, delta):
# bandpass filter
beta = np.cos(2 * np.pi / period)
Expand All @@ -55,7 +55,7 @@ def bp_fast(price, period, delta):
return bp


@njit
@njit(cache=True)
def peak_valley_fast(bp, price):
peak = np.copy(bp)
valley = np.copy(bp)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/epma.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def epma(candles: np.ndarray, period: int = 11, offset: int = 4, source_type: st
return res if sequential else res[-1]


@njit
@njit(cache=True)
def epma_fast(source, period, offset):
newseries = np.copy(source)
for j in range(period + offset + 1 , source.shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/frama.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def frama(candles: np.ndarray, window: int = 10, FC: int = 1, SC: int = 300, seq
return res[-1]


@njit
@njit(cache=True)
def frame_fast(candles, n, SC, FC):
w = np.log(2.0 / (SC + 1))

Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/gauss.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def gauss(candles: np.ndarray, period: int = 14, poles: int = 4, source_type: st
return res if sequential else res[-1]


@njit
@njit(cache=True)
def gauss_fast(source, period, poles):
N = source.size
source = source[~np.isnan(source)]
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/heikin_ashi_candles.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def heikin_ashi_candles(candles: np.ndarray, sequential: bool = False) -> HA:
else:
return HA(open[-1], close[-1], high[-1], low[-1])

@njit
@njit(cache=True)
def ha_fast(source):

# index consts to facilitate reading the code
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/high_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def high_pass(candles: np.ndarray, period: int = 48, source_type: str = "close",
return None if np.isnan(hpf[-1]) else hpf[-1]


@njit
@njit(cache=True)
def high_pass_fast(source, period): # Function is compiled to machine code when called the first time
k = 1
alpha = 1 + (np.sin(2 * np.pi * k / period) - 1) / np.cos(2 * np.pi * k / period)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/high_pass_2_pole.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def high_pass_2_pole(candles: np.ndarray, period: int = 48, source_type: str = "
return None if np.isnan(hpf[-1]) else hpf[-1]


@njit
@njit(cache=True)
def high_pass_2_pole_fast(source, period, K=0.707): # Function is compiled to machine code when called the first time
alpha = 1 + (np.sin(2 * np.pi * K / period) - 1) / np.cos(2 * np.pi * K / period)
newseries = np.copy(source)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/hurst_exponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def hurst_exponent(candles: np.ndarray, min_chunksize: int = 8, max_chunksize: i
return None if np.isnan(h) else h


@njit
@njit(cache=True)
def hurst_rs(x, min_chunksize, max_chunksize, num_chunksize):
"""Estimate the Hurst exponent using R/S method.
Estimates the Hurst (H) exponent using the R/S method from the time series.
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/hwma.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def hwma(candles: np.ndarray, na: float = 0.2, nb: float = 0.1, nc: float = 0.1,
return res if sequential else res[-1]


@njit
@njit(cache=True)
def hwma_fast(source, na, nb, nc):
last_a = last_v = 0
last_f = source[0]
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/itrend.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def itrend(candles: np.ndarray, alpha: float = 0.07, source_type: str = "hl2", s
return ITREND(signal[-1], it[-1], trigger[-1])


@njit
@njit(cache=True)
def itrend_fast(source, alpha):
it = np.copy(source)
for i in range(2, 7):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/jma.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def jma(candles: np.ndarray, period:int=7, phase:float=50, power:int=2, source_t
return res if sequential else res[-1]


@njit
@njit(cache=True)
def jma_helper(src, phaseRatio, beta, alpha):
jma_val = np.copy(src)

Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/lrsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def lrsi(candles: np.ndarray, alpha: float = 0.2, sequential: bool = False) -> U
return None if np.isnan(rsi[-1]) else rsi[-1]


@njit
@njit(cache=True)
def lrsi_fast(alpha, candles):
price = (candles[:, 3] + candles[:, 4]) / 2
l0 = np.copy(price)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/maaq.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def maaq(candles: np.ndarray, period: int = 11, fast_period: int = 2, slow_perio
return res if sequential else res[-1]


@njit
@njit(cache=True)
def maaq_fast(source, temp, period):
newseries = np.copy(source)
for i in range(period, source.shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/mcginley_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def mcginley_dynamic(candles: np.ndarray, period: int = 10, k: float = 0.6, sour
return mg if sequential else mg[-1]


@njit
@njit(cache=True)
def md_fast(source, k, period):
mg = np.full_like(source, np.nan)
for i in range(source.size):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/mwdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def mwdx(candles: np.ndarray, factor: float = 0.2, source_type: str = "close", s
return res if sequential else res[-1]


@njit
@njit(cache=True)
def mwdx_fast(source, fac):
newseries = np.copy(source)
for i in range(1, source.shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/nma.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def nma(candles: np.ndarray, period: int = 40, source_type: str = "close", seque

return res if sequential else res[-1]

@njit
@njit(cache=True)
def nma_fast(source, period):
# Ensure source values are positive before taking log
source = np.clip(source, a_min=1e-10, a_max=None)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/pma.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def pma(candles: np.ndarray, source_type: str = "hl2", sequential: bool = False)
return PMA(predict[-1], trigger[-1])


@njit
@njit(cache=True)
def pma_fast(source):
predict = np.full_like(source, np.nan)
trigger = np.full_like(source, np.nan)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def reflex(candles: np.ndarray, period: int = 20, source_type: str = "close", se
return None if np.isnan(rf[-1]) else rf[-1]


@njit
@njit(cache=True)
def reflex_fast(ssf, period):
rf = np.full_like(ssf, 0)
ms = np.full_like(ssf, 0)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/rma.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def rma(candles: np.ndarray, length: int = 14, source_type="close", sequential=F
return res if sequential else res[-1]


@njit
@njit(cache=True)
def rma_fast(source, _length):
alpha = 1 / _length
newseries = np.copy(source)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/rsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def rsx(candles: np.ndarray, period: int = 14, source_type: str = "close", seque
return res if sequential else res[-1]


@njit
@njit(cache=True)
def rsx_fast(source, period):
# variables
f0 = 0
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/sqwma.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def sqwma(candles: np.ndarray, period: int = 14, source_type: str = "close", seq
return res if sequential else res[-1]


@njit
@njit(cache=True)
def sqwma_fast(source, period):
newseries = np.copy(source)
for j in range(period + 1, source.shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/srwma.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def srwma(candles: np.ndarray, period: int = 14, source_type: str = "close", seq
return res if sequential else res[-1]


@njit
@njit(cache=True)
def srwma_fast(source, period):
newseries = np.copy(source)
for j in range(period + 1, source.shape[0]):
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/supersmoother.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def supersmoother(candles: np.ndarray, period: int = 14, source_type: str = "clo
return res if sequential else res[-1]


@njit
@njit(cache=True)
def supersmoother_fast(source, period):
a = np.exp(-1.414 * np.pi / period)
b = 2 * a * np.cos(1.414 * np.pi / period)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/supersmoother_3_pole.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def supersmoother_3_pole(candles: np.ndarray, period: int = 14, source_type: str
return res if sequential else res[-1]


@njit
@njit(cache=True)
def supersmoother_fast(source, period):
a = np.exp(-np.pi / period)
b = 2 * a * np.cos(1.738 * np.pi / period)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/supertrend.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def supertrend(candles: np.ndarray, period: int = 10, factor: float = 3, sequent
return SuperTrend(super_trend[-1], changed[-1])


@njit
@njit(cache=True)
def supertrend_fast(candles, atr, factor, period):
# Calculation of SuperTrend
upper_basic = (candles[:, 3] + candles[:, 4]) / 2 + (factor * atr)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/trendflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def trendflex(candles: np.ndarray, period: int = 20, source_type: str = "close",
return None if np.isnan(tf[-1]) else tf[-1]


@njit
@njit(cache=True)
def trendflex_fast(ssf, period):
tf = np.full_like(ssf, 0)
ms = np.full_like(ssf, 0)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/vi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def vi(candles: np.ndarray, period: int = 14, sequential: bool = False) -> VI:
return VI(vpn_with_nan[-1], vmn_with_nan[-1])


@njit
@njit(cache=True)
def vi_fast(candles, period):
candles_close = candles[:, 2]
candles_high = candles[:, 3]
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/vlma.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def vlma(candles: np.ndarray, min_period: int = 5, max_period: int = 50, matype:
return res if sequential else res[-1]


@njit
@njit(cache=True)
def vlma_fast(source, a, b, c, d, min_period, max_period):
newseries = np.copy(source)
period = np.zeros_like(source)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/voss.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def voss(candles: np.ndarray, period: int = 20, predict: int = 3, bandwith: floa
return VossFilter(voss_val[-1], filt[-1])


@njit
@njit(cache=True)
def voss_fast(source, period, predict, bandwith):
voss = np.full_like(source, 0)
filt = np.full_like(source, 0)
Expand Down
2 changes: 1 addition & 1 deletion jesse/indicators/vpwma.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def vpwma(candles: np.ndarray, period: int = 14, power: float = 0.382, source_ty
return res if sequential else res[-1]


@njit
@njit(cache=True)
def vpwma_fast(source, period, power):
newseries = np.copy(source)
for j in range(period + 1, source.shape[0]):
Expand Down

0 comments on commit 81834e1

Please sign in to comment.