gpt4 book ai didi

Connors RSI Calculation Python Not Calculating Correctly(Connors RSI计算Python计算不正确)

转载 作者:bug小助手 更新时间:2023-10-25 19:38:15 26 4
gpt4 key购买 nike



I am trying to calculate the connors rsi in python using pandas and numpy. I want to calculate it with the default values of ConnorsRSI(3,2,100).

我正在试着用熊猫和NumPy来计算蟒蛇中的Connors RSI。我想用ConnorsRSI的默认值(3,2,100)来计算它。


The formula for the connors RSI is : [ RSI(Close,3) + RSI(Streak,2) + PercentRank(100) ] / 3

Connors RSI的公式为:[RSI(Close,3)+RSI(Streak,2)+PercentRank(100)]/3


I start with the rsi(3):

我从RSI(3)开始:


# Returns RSI values
def rsi(close, periods):

close_delta = close.diff()

# Make two series: one for lower closes and one for higher closes
up = close_delta.clip(lower=0)
down = -1 * close_delta.clip(upper=0)

ma_up = up.ewm(com = periods - 1, adjust=True, min_periods = periods).mean()
ma_down = down.ewm(com = periods - 1, adjust=True, min_periods = periods).mean()

rsi = ma_up / ma_down
rsi = 100 - (100/(1 + rsi))
return rsi

df['rsi(3)'] = rsi(df['close'],3)
df.tail()

Then I calculate the streak

然后我计算出连胜线


def get_streaks(closing_prices):
# logic tables
series = pd.DataFrame(closing_prices)
geq = series >= series.shift(1) # True if rising
eq = series == series.shift(1) # True if equal
logic_table = pd.concat([geq, eq], axis=1)

streaks = [0] # holds the streak duration, starts with 0

for row in logic_table.iloc[1:].itertuples(): # iterate through logic table
if row[2]: # same value as before
streaks.append(0)
continue
last_value = streaks[-1]
if row[1]: # higher value than before
streaks.append(last_value + 1 if last_value >= 0 else 1) # increase or reset to +1
else: # lower value than before
streaks.append(last_value - 1 if last_value < 0 else -1) # decrease or reset to -1

return np.array(streaks, dtype=float)
df['streaks_numpy'] = get_streaks(df['close'] )
df.tail()

then i calculate a 2 period RSI with the streak

然后我用条纹计算出2个周期的RSI


df['streak_rsi'] = rsi(df['streaks_numpy'],2)

I then calculate the Percent Rank

然后我计算百分比排名


# Define the lookback period
lookback = 100

# Calculate the one-day return
one_day_return = df['close'].pct_change()

# Calculate the number of values in the lookback period that are less than the current return
less_than_current = one_day_return.rolling(window=lookback).apply(
lambda x: np.sum(x < x[-1]), raw=True)

# Calculate the total number of values in the lookback period
total_values = one_day_return.rolling(window=lookback).apply(
lambda x: np.size(x), raw=True)

# Calculate the Percent Rank value
percent_rank = less_than_current / total_values
df['percent_rank'] = percent_rank

and to get thr connors rsi i add them and divide by 3

为了得到康纳斯的RSI,我把它们相加,然后除以3


# calculate the CRSI
df['CRSI'] = (df['rsi(3)'] + df['streak_rsi'] + df['percent_rank']) / 3

So the code runs but the results are wrong, the rsi calculates perfectly i manually checked the streaks output , which looked like it suppose to, leaving me with the percent rank calculation that might be wrong, so, im looking for some extra eyes to help me see where i go wrong in my logic. Thanks

所以代码运行,但结果是错误的,RSI计算完美我手动检查条纹输出,这看起来像它假设,让我与百分比排名计算可能是错误的,所以,我寻找一些额外的眼睛,以帮助我看到我在我的逻辑错误。谢谢


更多回答
优秀答案推荐

maybe something like this will work

也许这样的事情会奏效


a = data['Close'].to_numpy()
n = 10
percent_rank = np.array([sum(a[i + 1 - n:i + 1] < a[i]) / n for i in range(len(a))])
percent_rank[:n - 1] = np.nan

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com