def test_get_ups_and_downs(data, parameter, expected): """ Test get ups and down functionality. """ assert get_ups_and_downs( data, parameter) == expected, f"Expected ups and downs to be: {expected}."
def get_rsi(self, prices: int = 14, parameter: str = 'close', shift: int = 0, round_value: bool = True, update: bool = True) -> float: """ Returns relative strength index. :param update: Boolean for whether function should call API and get latest data or not. :param prices: Amount of prices to iterate through. :param parameter: Parameter to use for iterations. By default, it's close. :param shift: Amount of prices to shift prices by. :param round_value: Boolean that determines whether final value is rounded or not. :return: Final relative strength index. """ if not self.is_valid_average_input(shift, prices): raise ValueError('Invalid input specified.') if shift > 0: updateDict = False data = self.data shift -= 1 else: updateDict = True data = self.data + [self.get_current_data()] if update else self.get_total_non_updated_data() start = len(data) - 500 - prices - shift if len(data) > 500 + prices + shift else 0 ups, downs = get_ups_and_downs(data=data[start:len(data) - shift], parameter=parameter) averageUp, averageDown = self.helper_get_ema(ups, downs, prices) rs = averageUp / averageDown rsi = 100 - 100 / (1 + rs) if shift == 0 and updateDict: self.rsi_data[prices] = rsi if round_value: return round(rsi, self.precision) return rsi
def get_rsi(self, data: list, prices: int, parameter: str = 'close', shift: int = 0, round_value: bool = False) -> float: """ Returns relative strength index. :param data: Data values. :param prices: Amount of prices to iterate through. :param parameter: Parameter to use for iterations. By default, it's close. :param shift: Amount of prices to shift prices by. :param round_value: Boolean that determines whether final value is rounded or not. :return: Final relative strength index. """ if shift > 0 and prices in self.rsi_dictionary: rsi = self.rsi_dictionary[prices]['close'][-shift][0] elif prices in self.rsi_dictionary: alpha = 1 / prices difference = data[-1][parameter] - data[-2][parameter] if difference > 0: up = difference * alpha + self.rsi_dictionary[prices]['close'][-1][1] * (1 - alpha) down = self.rsi_dictionary[prices]['close'][-1][2] * (1 - alpha) else: up = self.rsi_dictionary[prices]['close'][-1][1] * (1 - alpha) down = -difference * alpha + self.rsi_dictionary[prices]['close'][-1][2] * (1 - alpha) rsi = 100 if down == 0 else 100 - 100 / (1 + up / down) self.rsi_dictionary[prices]['close'].append((rsi, up, down)) else: if shift > 0: data = data[:-shift] ups, downs = get_ups_and_downs(data=data, parameter=parameter) rsi = self.helper_get_ema(ups, downs, prices) return round(rsi, self.precision) if round_value else rsi
def test_get_ups_and_downs(data, parameter, expected): assert get_ups_and_downs( data, parameter) == expected, f"Expected ups and downs to be: {expected}."