-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrypto_screener.py
executable file
·300 lines (213 loc) · 8.8 KB
/
crypto_screener.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/home/coil/anaconda3/bin/python3
import pandas as pd
import matplotlib.pyplot as plt
import requests
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
# pretty printing of pandas dataframe
#pd.set_option('expand_frame_repr', False)
# ############################################ #
# __crypto+specific_part__ #
# ############################################ #
# GET CURRENT PRICE DATA
def get_current_data(from_sym='BTC', to_sym='USD', exchange=''):
url = 'https://min-api.cryptocompare.com/data/price'
parameters = {'fsym': from_sym,
'tsyms': to_sym }
if exchange:
print('exchange: ', exchange)
parameters['e'] = exchange
# response comes as json
response = requests.get(url, params=parameters)
data = response.json()
print(data)
return data
# this is just testing function to see if we get the data
def get_hist_data(from_sym='BTC', to_sym='USD', timeframe = 'day', limit=2000, aggregation=1, exchange=''):
baseurl = 'https://min-api.cryptocompare.com/data/v2/histo'
baseurl += timeframe
parameters = {'fsym': from_sym,
'tsym': to_sym,
'limit': limit,
'aggregate': aggregation}
if exchange:
print('exchange: ', exchange)
parameters['e'] = exchange
print('baseurl: ', baseurl)
print('timeframe: ', timeframe)
print('parameters: ', parameters)
# response comes as json
response = requests.get(baseurl, params=parameters)
data = response.json()['Data']['Data']
#print(data)
return data
def data_to_dataframe(data):
#data from json is in array of dictionaries
df = pd.DataFrame.from_dict(data)
# time is stored as an epoch, we need normal dates
df['time'] = pd.to_datetime(df['time'], unit='s')
df.set_index('time', inplace=True)
print(df.tail())
return df
def plot_data(df, cryptocurrency, target_currency):
# got his warning because combining matplotlib
# and time in pandas converted from epoch to normal date
# To register the converters:
# >>> from pandas.plotting import register_matplotlib_converters
# >>> register_matplotlib_converters()
# warnings.warn(msg, FutureWarning)
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
plt.figure(figsize=(15,5))
plt.title('{} / {} price data'.format(cryptocurrency, target_currency))
plt.plot(df.index, df.close)
plt.legend()
plt.show()
return None
# ############################################ #
# __general_part__ #
# ############################################ #
username = 'coin.market.cap.000@gmail.com'
password = ''
# compute RSI values
def computeRSI (data, time_window):
diff = data.diff(1).dropna() # diff in one field(one day)
#this preservers dimensions off diff values
up_chg = 0 * diff
down_chg = 0 * diff
# up change is equal to the positive difference, otherwise equal to zero
up_chg[diff > 0] = diff[ diff>0 ]
# down change is equal to negative deifference, otherwise equal to zero
down_chg[diff < 0] = diff[ diff < 0 ]
# check pandas documentation for ewm
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html
# values are related to exponential decay
# we set com=time_window-1 so we get decay alpha=1/time_window
up_chg_avg = up_chg.ewm(com=time_window-1 , min_periods=time_window).mean()
down_chg_avg = down_chg.ewm(com=time_window-1 , min_periods=time_window).mean()
rs = abs(up_chg_avg/down_chg_avg)
rsi = 100 - 100/(1+rs)
return rsi
def computeSMA(data, window):
# simple moving average
sma = data.rolling(window=window).mean()
return sma
def computeEMA(data, span):
# simple moving average
ema = data.ewm(span=span, adjust=False).mean()
return ema
def construct_df(data):
#get data from yahoo API
df = data_to_dataframe(data)
# compute both types of moving averages
for i in range(50, 250, 50):
#print(i)
df['SMA_{}'.format(i)] = computeSMA(df['close'], i)
for i in range(50, 250, 50):
#print(i)
df['EMA_{}'.format(i)] = computeEMA(df['close'], i)
return df
def send_email(data_rsi, data_200_ema, data_50_ema, data_200_ema_vicinity, username, password):
smtp_ssl_host = 'smtp.gmail.com'
smtp_ssl_port = 465
sender = 'coin.market.cap.000@gmail.com'
receiver = 'michal.vasulka@yahoo.com'
# implicitly joined string
msg_body_rsi = ("stock ticker RSI around 30 \n"
"possible long entry \n"
"ticker/s: \n"
+ data_rsi + "\n\n")
msg_body_200_ema = ("went above 200 EMA recently \n"
"possible long entry \n"
"ticker/s: \n"
+ data_200_ema + "\n\n")
msg_body_50_ema = ("in vicinity of 50 EMA \n"
"alerting \n"
"ticker/s: \n"
+ data_50_ema + "\n\n")
msg_body_200_ema_vicinity = ("in vicinity of 200 EMA \n"
"strong alert - support/resistance \n"
"ticker/s: \n"
+ data_200_ema_vicinity + "\n\n")
msg_body = msg_body_rsi + msg_body_200_ema + msg_body_50_ema + msg_body_200_ema_vicinity
message = MIMEText(msg_body, "plain")
# treat message as dictionary
message['subject'] = 'crypto event'
message['from'] = sender
message['to'] = receiver
# contact gmail server and send mail via my gmail dummy account
try:
server = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
server.login(username, password)
server.sendmail(sender, receiver, message.as_string())
server.quit()
print("Successfully sent email")
except:
print("Error: unable to send email")
# ############################################ #
# __execution_part__ #
# ############################################ #
tickers = ['BTC', 'ETH', 'LTC', 'NEO', 'XLM', 'XRP', 'OMG', 'LINK', \
'ADA', 'NANO', 'MIOTA', 'DOT', 'EOS', 'ICX', 'ARK']
target_currency = 'USD'
# implement lists as dictionaries for clarity
signal = {}
signal['RSI'] = []
signal['EMA_200'] = []
signal['EMA_50'] = []
signal['EMA_200_vicinity'] = []
for ticker in tickers:
try:
# quickly test data aquisition
#get_current_data(ticker, target_currency, 'coinbase')
#data = get_hist_data(ticker, target_currency, 'day', 1000, exchange='bitfinex')
data = get_hist_data(ticker, target_currency, 'day', 1000)
#plotting to be removed when running on server
#plot_data(df, ticker, target_currency)
# df = get_data(ticker) #just gets data
df = construct_df(data) #gets data and adds MAs to the df (implement RSI later)
#adds RSI column to dataframe
df['RSI'] = computeRSI(df['close'], 14)
# RSI <= 30 is long signal
# if last day RSI data (today) is oversold, send mail
print('ticker:', ticker)
print('rsi today', df['RSI'].iloc[-1])
## RSI day before <= threshold and RSI today above - long signal
##if (df['RSI'].iloc[-2] < 30 and df['RSI'].iloc[-1] >= 30):
## long_list.append(ticker)
#s __signal_conditions__
if (df['RSI'].iloc[-1] <= 30):
signal['RSI'].append(ticker)
# was below 200 EMA few days ago but today is above 200 EMA
# possible long
if (
(df['EMA_200'].iloc[-5] > df['close'].iloc[-5]) and
(df['EMA_200'].iloc[-1] < df['close'].iloc[-1])
):
signal['EMA_200'].append(ticker)
# price in vicinity 50 EMA
# possible long or at least alert
if (
((df['EMA_50'].iloc[-1] / df['close'].iloc[-1]) >= 0.97) and
((df['EMA_50'].iloc[-1] / df['close'].iloc[-1]) <= 1.03)
):
signal['EMA_50'].append(ticker)
# price in vicinity 200 EMA
# possible long or at least alert
if (
((df['EMA_200'].iloc[-1] / df['close'].iloc[-1]) >= 0.97) and
((df['EMA_200'].iloc[-1] / df['close'].iloc[-1]) <= 1.03)
):
signal['EMA_200_vicinity'].append(ticker)
except Exception as e:
print("type error: " + str(e))
if ( len(signal['RSI']) > 0 ) or \
( len(signal['EMA_200']) > 0 ) or \
( len(signal['EMA_50']) > 0 ) or \
( len(signal['EMA_200_vicinity']) > 0 ) :
rsi_str = ' '.join(map(str, signal['RSI']))
ema_200_str = ' '.join(map(str, signal['EMA_200']))
ema_50_str = ' '.join(map(str, signal['EMA_50']))
ema_200_vicinity_str = ' '.join(map(str, signal['EMA_200_vicinity']))
send_email(rsi_str, ema_200_str, ema_50_str, ema_200_vicinity_str, username, password)