-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrma.py
More file actions
65 lines (55 loc) · 1.92 KB
/
Copy pathrma.py
File metadata and controls
65 lines (55 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import Union
import numpy as np
try:
from numba import njit, guvectorize
except ImportError:
njit = lambda a: a
from jesse.helpers import get_candle_source, slice_candles
def rma(candles: np.ndarray, length: int = 14, source_type="close", sequential=False) -> \
Union[float, np.ndarray]:
"""
:param candles: np.ndarray
:param length: int - default: 14
:param source_type: str - default: close
:param sequential: bool - default: False
:return: Union[float, np.ndarray]
"""
# github.com/ysdede
# Moving average used in RSI. It is the exponentially weighted moving average with alpha = 1 / length.
# RETURNS Exponential moving average of x with alpha = 1 / y.
# https://www.tradingview.com/pine-script-reference/#fun_rma
if length < 1:
raise ValueError('Bad parameters.')
# Accept normal array too.
if len(candles.shape) == 1:
source = candles
else:
candles = slice_candles(candles, sequential)
source = get_candle_source(candles, source_type=source_type)
res = rma_fast(source, length)
return res if sequential else res[-1]
@njit
def rma_fast(source, _length):
alpha = 1 / _length
newseries = np.copy(source)
out = np.full_like(source, np.nan)
for i in range(source.size):
if np.isnan(newseries[i - 1]):
# Sma in Numba
asum = 0.0
count = 0
for i in range(_length):
asum += source[i]
count += 1
out[i] = asum / count
for i in range(_length, len(source)):
asum += source[i] - source[i - _length]
out[i] = asum / count
newseries[i] = out[-1]
# Sma End
else:
prev = newseries[i - 1]
if np.isnan(prev):
prev = 0
newseries[i] = alpha * source[i] + (1 - alpha) * prev
return newseries