Skip to content

Commit

Permalink
Minor formatting cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
luisbarrancos committed Nov 24, 2023
1 parent cf7de4d commit c222dba
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions pandas_ta/momentum/crsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@
def calculate_streak_conv(prices: Array1d) -> Array1d:
"""Calculate the streak of consecutive price increases or decreases.
This function computes the streak of consecutive daily price increases or decreases.
A positive streak indicates consecutive days of price increases, while a negative streak
indicates consecutive days of price decreases. The streak is reset to zero when the
direction of the price change reverses.
This function computes the streak of consecutive daily price increases
or decreases. A positive streak indicates consecutive days of price
increases, while a negative streak indicates consecutive days of price
decreases. The streak is reset to zero when the direction of the price
change reverses.
Parameters:
prices (np.array): An array of prices.
Returns:
np.array: An array representing the streak of price changes.
The function first calculates the difference between consecutive prices. It then
assigns a +1 for each positive change, -1 for each negative change, and 0 for no change.
The result is an array where each element represents the streak value for that day.
The function first calculates the difference between consecutive prices.
It then assigns a +1 for each positive change, -1 for each negative change,
and 0 for no change. The result is an array where each element represents
the streak value for that day.
Example:
>>> prices = np.array([100, 101, 102, 100, 100, 101, 102, 103])
Expand All @@ -47,7 +49,10 @@ def calculate_streak_conv(prices: Array1d) -> Array1d:
def calculate_percent_rank(close: Series, lookback: Int) -> Series:
"""Calculate the Percent Rank of daily returns over given period.
The Percent Rank is computed by comparing the daily returns of a financial instrument to its previous returns within a given lookback window. It measures the percentage of values in the lookback period that are less than the current value.
The Percent Rank is computed by comparing the daily returns of a financial
instrument to its previous returns within a given lookback window. It
measures the percentage of values in the lookback period that are less than
the current value.
Args:
close (pd.Series): Series of 'close's
Expand All @@ -56,8 +61,13 @@ def calculate_percent_rank(close: Series, lookback: Int) -> Series:
Returns:
pd.Series: A Pandas Series containing the Percent Rank values.
The function first calculates the daily returns of the 'close' prices. It then creates a rolling window of these returns and compares each value in the window to the current value (the last value in each window). The Percent Rank is calculated as the percentage of values in each window that are less than the current value.
The result is a Series where the initial part (up to 'lookback - 1') is padded with NaNs, and the rest contains the Percent Rank values.
The function first calculates the daily returns of the 'close' prices. It
then creates a rolling window of these returns and compares each value in
the window to the current value (the last value in each window). The
Percent Rank is calculated as the percentage of values in each window that
are less than the current value.
The result is a Series where the initial part (up to 'lookback - 1') is
padded with NaNs, and the rest contains the Percent Rank values.
Example:
>>> close = pd.Series([100, 80, 75, 123, 140, 80, 70, 40, 100, 120])
Expand All @@ -78,9 +88,9 @@ def calculate_percent_rank(close: Series, lookback: Int) -> Series:

daily_returns_np = (close.pct_change()).to_numpy()

# Iterate over daily returns with fixed window size for given loopback period, then
# compare the previous periods against the last (current) period in each
# window row
# Iterate over daily returns with fixed window size for given loopback
# period, then compare the previous periods against the last (current)
# period in each window row
rolling_windows = np.lib.stride_tricks.sliding_window_view(
daily_returns_np, window_shape=lookback
)
Expand All @@ -107,11 +117,15 @@ def crsi(
) -> Series:
"""Connors Relative Strength Index (RSI)
Connors RSI (CRSI) integrates Relative Strength Index (RSI), UpDown Length, and Rate of Change (ROC) of RSI components to evaluate overbought and oversold conditions in financial markets, providing insights into price momentum and potential reversals.
Connors RSI (CRSI) integrates Relative Strength Index (RSI), UpDown Length,
and Rate of Change (ROC) of RSI components to evaluate overbought and
oversold conditions in financial markets, providing insights into price
momentum and potential reversals.
Sources:
Connors, L., Alvarez, C., & Radtke, M. (2012). An Introduction to ConnorsRSI.
Connors Research Trading Strategy Series. ISBN 978-0-9853072-9-5.
Connors, L., Alvarez, C., & Radtke, M. (2012). An Introduction to
ConnorsRSI. Connors Research Trading Strategy Series.
ISBN 978-0-9853072-9-5.
Retrieved from https://alvarezquanttrading.com/blog/connorsrsi-analysis/
https://www.tradingview.com/support/solutions/43000502017-connors-rsi-crsi/
Expand All @@ -121,8 +135,7 @@ def crsi(
length_streak (int): It's period. Default: 2
length_rank (int): It's period. Default: 100
scalar (float): How much to magnify. Default: 100
talib (bool): If TA Lib is installed and talib is True, Returns
the TA Lib version. Default: True
talib (bool): Use TAlib for RSI if available. Default: True
drift (int): The difference period. Default: 1
offset (int): How many periods to offset the result. Default: 0
Expand Down Expand Up @@ -162,7 +175,7 @@ def crsi(
close_rsi = RSI(close, length_rsi)
streak_rsi = RSI(streak, length_streak)

# Both TA-lib and Pandas-TA use the Wilder's RSI with its smoothing function.
# Both TA-lib and Pandas-TA use the Wilder's RSI and its smoothing function.
else:
close_rsi = rsi(
close,
Expand Down

0 comments on commit c222dba

Please sign in to comment.