def get_rate_suggestion(self, cur, rates=None, method='percentile'): """ Return the suggested rate from analysed data. This is the main method for retrieving data from this module. Currently this only supports returning of a single value, the suggested rate. However this will be expanded to suggest a lower and higher rate for spreads. :param cur: The currency (database) to remove data from :param rates: This is used for unit testing only. It allows you to populate the data used for the suggestion. :param method: The method by which you want to calculate the suggestion. :return: A float with the suggested rate for the currency. """ error_msg = "WARN: Exception found when analysing markets, if this happens for more than a couple minutes " +\ "please create a Github issue so we can fix it. Otherwise, you can ignore it. Error" try: rates = self.get_rate_list( cur, self.get_analysis_seconds(method)) if rates is None else rates if not isinstance(rates, pd.DataFrame): raise ValueError("Rates must be a Pandas DataFrame") if len(rates) == 0: print("Rate list not populated") if self.ma_debug_log: print( "DEBUG:get_analysis_seconds: cur: {0} method:{1} rates:{2}" .format(cur, method, rates)) return 0 if self.ma_debug_log: print( "Cur:{0}, MACD:{1:.6f}, Perc:{2:.6f}, Best:{3:.6f}".format( cur, truncate(self.get_MACD_rate(cur, rates), 6), self.get_percentile(rates.rate0.values.tolist(), self.lending_style), rates.rate0.iloc[-1])) if method == 'percentile': return self.get_percentile(rates.rate0.values.tolist(), self.lending_style) if method == 'MACD': return truncate(self.get_MACD_rate(cur, rates), 6) except MarketDataException: if method != 'percentile': print( "Caught exception during {0} analysis, using percentile for now" .format(method)) return self.get_percentile(rates.rate0.values.tolist(), self.lending_style) else: raise except Exception as ex: self.print_exception_error(ex, error_msg, debug=self.ma_debug_log) return 0
def get_percentile(self, rates, lending_style, use_numpy=use_numpy): if use_numpy: result = numpy.percentile(rates, int(lending_style)) else: result = self.percentile(sorted(rates), lending_style / 100.0) result = truncate(result, 6) return result
def get_percentile(self, rates, lending_style, use_numpy=use_numpy): """ Take a list of rates no matter what method is being used, simple list, no pandas / numpy array """ if use_numpy: result = numpy.percentile(rates, int(lending_style)) else: result = self.percentile(sorted(rates), lending_style / 100.0) result = truncate(result, 6) return result
def get_percentile(self, rates, lending_style, use_numpy=use_numpy): """ Take a list of rates no matter what method is being used, simple list, no pandas / numpy array """ if use_numpy: result = numpy.percentile(rates, int(lending_style)) else: result = self.percentile(sorted(rates), lending_style / 100.0) result = truncate(result, 6) return result
def get_rate_suggestion(self, cur, rates=None, method='percentile'): """ Return the suggested rate from analysed data. This is the main method for retrieving data from this module. Currently this only supports returning of a single value, the suggested rate. However this will be expanded to suggest a lower and higher rate for spreads. :param cur: The currency (database) to remove data from :param rates: This is used for unit testing only. It allows you to populate the data used for the suggestion. :param method: The method by which you want to calculate the suggestion. :return: A float with the suggested rate for the currency. """ error_msg = "WARN: Exception found when analysing markets, if this happens for more than a couple minutes " +\ "please create a Github issue so we can fix it. Otherwise, you can ignore it. Error" try: rates = self.get_rate_list(cur, self.get_analysis_seconds(method)) if rates is None else rates if not isinstance(rates, pd.DataFrame): raise ValueError("Rates must be a Pandas DataFrame") if len(rates) == 0: print("Rate list not populated") if self.ma_debug_log: print("DEBUG:get_analysis_seconds: cur: {0} method:{1} rates:{2}".format(cur, method, rates)) return 0 if method == 'percentile': return self.get_percentile(rates.rate0.values.tolist(), self.lending_style) if method == 'MACD': macd_rate = truncate(self.get_MACD_rate(cur, rates), 6) if self.ma_debug_log: print("Cur:{0}, MACD:{1:.6f}, Perc:{2:.6f}, Best:{3:.6f}" .format(cur, macd_rate, self.get_percentile(rates.rate0.values.tolist(), self.lending_style), rates.rate0.iloc[-1])) return macd_rate except MarketDataException: if method != 'percentile': print("Caught exception during {0} analysis, using percentile for now".format(method)) return self.get_percentile(rates.rate0.values.tolist(), self.lending_style) else: raise except Exception as ex: self.print_exception_error(ex, error_msg, debug=self.ma_debug_log) return 0