-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcryptosignals.py
123 lines (102 loc) · 3.88 KB
/
cryptosignals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
import time
## I am not your financial adviser, nor is this tool. Use this program as an educational tool, and nothing more.
## None of the contributors to this project are liable for any losses you may incur. Be wise and always do your own research.
def coinpaprika_api():
api = "https://api.coinpaprika.com/v1/tickers/"
try:
data = requests.get(api,timeout=5).json()
return data
except requests.exceptions.RequestException:
return False
except (ValueError):
return False
def historical_price(id,start,end):
api = "https://api.coinpaprika.com/v1/coins/" + id + "/ohlcv/historical?start=" + str(start) + "&end="+ str(end)
try:
data = requests.get(api,timeout=5).json()
return data
except requests.exceptions.RequestException:
return False
except (ValueError):
return False
def rank_filter(data):
filtered_data = [x for x in data if x['rank'] < 50]
return filtered_data
def bullish_filter(data):
filtered_data = [x for x in data if ((x['quotes']['USD']['volume_24h_change_24h'] > 1 and x['quotes']['USD']['percent_change_24h'] > 1) or (x['quotes']['USD']['volume_24h_change_24h'] < -1 and x['quotes']['USD']['percent_change_24h'] < -1))]
return filtered_data
def bearish_filter(data):
filtered_data = [x for x in data if ((x['quotes']['USD']['volume_24h_change_24h'] > 1 and x['quotes']['USD']['percent_change_24h'] < -1) or (x['quotes']['USD']['volume_24h_change_24h'] < -1 and x['quotes']['USD']['percent_change_24h'] > 1))]
return filtered_data
def relativeStrengthIndex(prices,n):
RSI = []
m = len(prices)
avgGain = 0
avgLoss = 0
i = 0
while i < 14:
change = prices[i+1] - prices[i]
if change >= 0:
avgGain = change + avgGain
else:
avgLoss = avgLoss - change
i+=1
avgGain = avgGain/n
avgLoss = avgLoss/n
t = 14
while t < m:
smoothedrs = avgGain/avgLoss
RSI.append(( 100 - 100/ (1+smoothedrs)))
if t < m-1:
change = prices[t+1] - prices[t]
if change >= 0:
avgGain = (avgGain * 13 + change) / n
avgLoss = (avgLoss * 13) / n
else:
avgGain = (avgGain * 13) / n
avgLoss = (avgLoss * 13 - change) / n
t+=1
return RSI
def exponentialMovingAverage(prices,n):
m = len(prices)
a = 2/(n+1)
EMA = []
EMA.append(prices[0])
i = 1
while i < m:
EMA.append((a * prices[i]) + ((1 - a) * EMA[i - 1]))
i+=1
return EMA
days = 5219689;
data = coinpaprika_api()
rankdata = rank_filter(data)
bullishdata = bullish_filter(rankdata)
bearishdata = bearish_filter(rankdata)
all_data = bullishdata + bearishdata
for x in all_data:
prices = []
name = x['name']
change = x['quotes']['USD']['percent_change_24h']
volume = x['quotes']['USD']['volume_24h_change_24h']
id = x['id']
endunix = time.time()
startunix = endunix - days
historical = historical_price(id,int(startunix),int(endunix))
for x in historical:
prices.append( x['close'])
trend = "N/A"
ema8 = exponentialMovingAverage(prices,8)
ema21 = exponentialMovingAverage(prices,21)
current_8 = ema8.pop()
current_21 = ema21.pop()
previous_8 = ema8.pop()
previous_21 = ema21.pop()
if current_8 > current_21 and previous_8 < previous_21:
trend = "Upward Trend"
if current_8 < current_21 and previous_8 > previous_21:
trend = "Downward Trend"
if trend != "N/A":
rsi = relativeStrengthIndex(prices,14)
current_rsi = rsi.pop()
print(name + "\nChange(24h): " + str(change) + "\nVolume(24h): " + str(volume)+ "\nRSI(14): " + str(current_rsi) + "\nTrend: " + trend +"\n----------------------")