Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

Commit

Permalink
Re-added RSI strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
shads2 committed Dec 29, 2017
1 parent eab9214 commit 18188e5
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 62 deletions.
7 changes: 6 additions & 1 deletion app/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
19 changes: 16 additions & 3 deletions app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 5 additions & 2 deletions app/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
97 changes: 55 additions & 42 deletions app/strategies/relative_strength_index.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions app/strategies/strategy_utils.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions app/strategies/util.py

This file was deleted.

0 comments on commit 18188e5

Please sign in to comment.