如何正确计算?(其他编程语言发帖也可以)
好吧,让我从QuantFX
模块中添加一个
一个人可能会遇到许多公式,有些带有示例,有些带有验证数据集,所以让我选择一个这样的公式,使用经过numba.jit
修饰的 python 代码,并带有一些numpy
矢量化技巧:
def RSI_func( priceVEC, RSI_period = 14 ):
"""
__doc__
USE:
RSI_func( priceVEC, RSI_period = 14 )
Developed by J. Welles Wilder and introduced in his 1978 book,
New Concepts in Technical Trading Systems, the Relative Strength Index
(RSI) is an extremely useful and popular momentum oscillator.
The RSI compares the magnitude of a stock's recent gains
to the magnitude of its recent losses and turns that information
into a number that ranges from 0 to 100.
It takes a single parameter, the number of time periods to use
in the calculation. In his book, Wilder recommends using 14 periods.
The RSI's full name is actually rather unfortunate as it is easily
confused with other forms of Relative Strength analysis such as
John Murphy's "Relative Strength" charts and IBD's "Relative Strength"
rankings.
Most other kinds of "Relative Strength" stuff involve using
more than one stock in the calculation. Like most true indicators,
the RSI only needs one stock to be computed.
In order to avoid confusion,
many people avoid using the RSI's full name and just call it "the RSI."
( Written by Nicholas Fisher)
PARAMS:
pricesVEC - a vector of price-DOMAIN-alike data in natural order
RSI_period - a positive integer for an RSI averaging period
RETURNS:
a vector of RSI values
EXAMPLE:
>>> RSI_func( np.asarray( [ 46.1250, 47.1250, 46.4375, 46.9375, 44.9375,
44.2500, 44.6250, 45.7500, 47.8125, 47.5625,
47.0000, 44.5625, 46.3125, 47.6875, 46.6875,
45.6875, 43.0625, 43.5625, 44.8750, 43.6875
]
),
RSI_period = 14
)
array( [ 51.77865613, 51.77865613, 51.77865613, 51.77865613, 51.77865613, 51.77865613, 51.77865613,
51.77865613, 51.77865613, 51.77865613, 51.77865613, 51.77865613, 51.77865613,
51.77865613, 48.47708511, 41.07344947, 42.86342911, 47.38184958, 43.99211059
]
)
OUGHT BE:
51.779, 48.477, 41.073, 42.863, 47.382, 43.992
[PASSED]
Ref.s:
>>> http://cns.bu.edu/~gsc/CN710/fincast/Technical%20_indicators/Relative%20Strength%20Index%20(RSI).htm
"""
deltas = np.diff( priceVEC )
seed = deltas[:RSI_period]
up = seed[seed >= 0].sum() / RSI_period
down = -seed[seed < 0].sum() / RSI_period
rs = up / down
rsi = 50. * np.ones_like( priceVEC ) # NEUTRAL VALUE PRE-FILL
rsi[:RSI_period] = 100. - ( 100. / ( 1. + rs ) )
for i in np.arange( RSI_period, len( priceVEC )-1 ):
delta = deltas[i]
if delta > 0:
upval = delta
downval = 0.
else:
upval = 0.
downval = -delta
up = ( up * ( RSI_period - 1 ) + upval ) / RSI_period
down = ( down * ( RSI_period - 1 ) + downval ) / RSI_period
rs = up / down
rsi[i] = 100. - ( 100. / ( 1. + rs ) )
return rsi[:-1]
鉴于您希望“准确地反映”他人的图表,有一种最安全的模式可以交叉检查他们实际用于 RSI 计算的公式。看到差异是很常见的,因此“精确”匹配需要对他们实际用于生成数据的内容进行调查(如果使用 D1-time-frame ,还要注意各自的行政 UTC 偏移差异)。