def eval_algorithm(closes): """ Evaluates the SMA algorithm Args: closes: List of price closes. Returns: Float average of closes. """ return stats.avg(closes)
def eval_rs(gains, losses): """ Evaluates the RS variable in RSI algorithm Args: gains: List of price gains. losses: List of prices losses. Returns: Float of average gains over average losses. """ # Number of days that the data was collected through count = len(gains) + len(losses) avg_gains = stats.avg(gains, count=count) if gains else 1 avg_losses = stats.avg(losses,count=count) if losses else 1 if avg_losses == 0: return avg_gains else: return avg_gains / avg_losses
def eval_rs(gains, losses): """ Evaluates the RS variable in RSI algorithm Args: gains: List of price gains. losses: List of prices losses. Returns: Float of average gains over average losses. """ # Number of days that the data was collected through count = len(gains) + len(losses) avg_gains = stats.avg(gains, count=count) avg_losses = stats.avg(losses,count=count) try: return avg_gains / avg_losses except ZeroDivisionError: # No losses return avg_gains
def test_avg(self): nums = [0.24, 0.62, 0.15, 0.83, 0.12345] self.assertEqual(stats.avg(nums), 0.39269) nums = [0.24, 0.62, 0.15, 0.83, 0.12345] self.assertEqual(stats.avg(nums, count=8), 0.24543125)