diff --git a/app/analysis.py b/app/analysis.py index fd0d379e..3d850f65 100644 --- a/app/analysis.py +++ b/app/analysis.py @@ -4,7 +4,7 @@ from strategies.breakout import Breakout #from strategies.ichimoku_cloud import IchimokuCloud -#from strategies.relative_string_index import RelativeStrengthIndex +from strategies.relative_strength_index import RelativeStrengthIndex #from strategies.moving_averages import MovingAverages @@ -16,3 +16,8 @@ def analyze_breakout(self, historical_data): breakout_analyzer = Breakout() breakout_value, is_breaking_out = breakout_analyzer.find_breakout(historical_data) return breakout_value, is_breaking_out + + def analyze_rsi(self, historical_data): + rsi_analyzer = RelativeStrengthIndex() + rsi_value = rsi_analyzer.find_rsi(historical_data) + return rsi_value diff --git a/app/app.py b/app/app.py index b3587279..1b7661e2 100644 --- a/app/app.py +++ b/app/app.py @@ -20,13 +20,26 @@ #print(historical_data = BITTREX_CLIENT.get_historical_data('BTC-ETH', 30, "thirtyMin")) def get_signal(): - for coin_pair in COIN_PAIRS: + breakout_historical_data = EXCHANGE_AGGREGATOR.get_historical_data( + coin_pair=coin_pair, + period_count=5, + time_unit='fiveMin') + + rsi_historical_data = EXCHANGE_AGGREGATOR.get_historical_data( + coin_pair=coin_pair, + period_count=36, + time_unit='thirtyMin' + ) + breakout_value, is_breaking_out = STRATEGY_ANALYZER.analyze_breakout( - EXCHANGE_AGGREGATOR.get_historical_data(coin_pair)) + breakout_historical_data) if is_breaking_out: NOTIFIER.notify_all(message="{} is breaking out!".format(coin_pair)) - print("{}: \tBreakout: {}".format(coin_pair, breakout_value)) + + rsi_value = STRATEGY_ANALYZER.analyze_rsi(rsi_historical_data) + + print("{}: \tBreakout: {} \tRSI: {}".format(coin_pair, breakout_value, rsi_value)) time.sleep(300) if __name__ == "__main__": diff --git a/app/exchange.py b/app/exchange.py index 60e5e54e..03d61bdf 100644 --- a/app/exchange.py +++ b/app/exchange.py @@ -13,11 +13,14 @@ def __init__(self, config): config['exchanges']['bittrex']['required']['key'], config['exchanges']['bittrex']['required']['secret']) - def get_historical_data(self, coin_pair, period_count=5, time_unit='fiveMin'): + def get_historical_data(self, coin_pair, period_count, time_unit): """ Get history data """ return self.bittrex_client.get_historical_data(coin_pair, period_count, time_unit) - + def get_user_markets(self): + """ + Get user market balances + """ return self.bittrex_client.get_balances() diff --git a/app/strategies/relative_strength_index.py b/app/strategies/relative_strength_index.py index c8540ced..ef996ea0 100644 --- a/app/strategies/relative_strength_index.py +++ b/app/strategies/relative_strength_index.py @@ -1,48 +1,61 @@ -# Improvemnts to calculate_rsi are courtesy of community contributor "pcartwright81" -def calculate_rsi(coin_pair, period, unit): +""" +Runs the breakout strategy over the market data +""" + +from strategies.strategy_utils import Utils + +class RelativeStrengthIndex(): """ - Calculates the Relative Strength Index for a coin_pair - If the returned value is above 70, it's overbought (SELL IT!) - If the returned value is below 30, it's oversold (BUY IT!) + Runs the breakout strategy over the market data """ - closing_prices = get_closing_prices(coin_pair, period * 3, unit) - count = 0 - changes = [] - # Calculating price changes - for closing_price in closing_prices: - if count != 0: - changes.append(closing_price - closing_prices[count - 1]) - count += 1 - if count == 15: - break - # Calculating gains and losses - advances = [] - declines = [] - for change in changes: - if change > 0: - advances.append(change) - if change < 0: - declines.append(abs(change)) + # Improvemnts to calculate_rsi are courtesy of community contributor "pcartwright81" + def find_rsi(self, historical_data): + """ + Calculates the Relative Strength Index for a coin_pair + If the returned value is above 70, it's overbought (SELL IT!) + If the returned value is below 30, it's oversold (BUY IT!) + """ + utils = Utils() + closing_prices = utils.get_closing_prices(historical_data) + count = 0 + changes = [] - average_gain = (sum(advances) / 14) - average_loss = (sum(declines) / 14) - new_average_gain = average_gain - new_average_loss = average_loss - for closing_price in closing_prices: - if count > 14 and count < len(closing_prices): - close = closing_prices[count] - new_change = close - closing_prices[count - 1] - add_loss = 0 - add_gain = 0 - if new_change > 0: - add_gain = new_change - if new_change < 0: - add_loss = abs(new_change) - new_average_gain = (new_average_gain * 13 + add_gain) / 14 - new_average_loss = (new_average_loss * 13 + add_loss) / 14 + # Calculating price changes + for closing_price in closing_prices: + if count != 0: + changes.append(closing_price - closing_prices[count - 1]) count += 1 + if count == 15: + break + + # Calculating gains and losses + advances = [] + declines = [] + for change in changes: + if change > 0: + advances.append(change) + if change < 0: + declines.append(abs(change)) + + average_gain = (sum(advances) / 14) + average_loss = (sum(declines) / 14) + new_average_gain = average_gain + new_average_loss = average_loss + for closing_price in closing_prices: + if count > 14 and count < len(closing_prices): + close = closing_prices[count] + new_change = close - closing_prices[count - 1] + add_loss = 0 + add_gain = 0 + if new_change > 0: + add_gain = new_change + if new_change < 0: + add_loss = abs(new_change) + new_average_gain = (new_average_gain * 13 + add_gain) / 14 + new_average_loss = (new_average_loss * 13 + add_loss) / 14 + count += 1 - rs = new_average_gain / new_average_loss - new_rs = 100 - 100 / (1 + rs) - return new_rs + rs = new_average_gain / new_average_loss + new_rs = 100 - 100 / (1 + rs) + return new_rs diff --git a/app/strategies/strategy_utils.py b/app/strategies/strategy_utils.py new file mode 100644 index 00000000..289ffc02 --- /dev/null +++ b/app/strategies/strategy_utils.py @@ -0,0 +1,16 @@ +""" +Helper functions for the various strategies +""" + +class Utils(): + """ + Helper functions for the various strategies + """ + def get_closing_prices(self, historical_data): + """ + Returns closing prices within a specified time frame for a coin pair + """ + closing_prices = [] + for data_point in historical_data: + closing_prices.append(data_point['C']) + return closing_prices diff --git a/app/strategies/util.py b/app/strategies/util.py deleted file mode 100644 index 902bdaec..00000000 --- a/app/strategies/util.py +++ /dev/null @@ -1,14 +0,0 @@ -def get_closing_prices(coin_pair, period, unit): - """ - Returns closing prices within a specified time frame for a coin pair - :type coin_pair: str - :type period: str - :type unit: int - :return: Array of closing prices - """ - - historical_data = BITTREX_CLIENT.get_historical_data(coin_pair, period, unit) - closing_prices = [] - for data_point in historical_data: - closing_prices.append(data_point['C']) - return closing_prices