-
Notifications
You must be signed in to change notification settings - Fork 193
/
retriever.py
244 lines (211 loc) · 8.6 KB
/
retriever.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
#########
# GLOBALS
#########
import os
import json
import requests
import moment
from bs4 import BeautifulSoup
from textblob import TextBlob
# Local
from engine import dataset, transformer
DIR_PATH = os.path.dirname(os.path.abspath(__file__))
#########
# HELPERS
#########
def fetch_price_data():
with open(os.path.join(DIR_PATH, "../store/ticker.json"), 'w') as price_data:
try:
response = requests.get("https://www.bitstamp.net/api/ticker/").json()
price_data.write(json.dumps({
"error": False,
"data": {
"last": round(float(response["last"]), 2),
"high": round(float(response["high"]), 2),
"low": round(float(response["low"]), 2),
"open": round(float(response["open"]), 2),
"volume": round(float(response["volume"]), 2)
}
}, indent=2))
except:
price_data.write(json.dumps({
"error": True,
"data": json.loads(price_data)["data"]
}))
with open(os.path.join(DIR_PATH, "../store/graph.json"), 'w') as graph_data:
try:
data = []
for index, row in dataset("price_data").iterrows():
data.append({
"date": moment.date(row["Date"]).format("MM/DD/YY"),
"price": row["Close"],
"volume": row["Volume (BTC)"]
})
graph_data.write(json.dumps({
"error": False,
"data": data
}, indent=2))
except:
graph_data.write(json.dumps({
"error": True,
"data": json.loads(graph_data)["data"]
}))
def fetch_tech_indicators():
with open(os.path.join(DIR_PATH, "../store/indicators.json"), 'w') as indicators_json:
try:
price_data = dataset("price_data")
indicators = transformer("calculate_indicators")(price_data)
get_signal = {
"MOM (1)": lambda v: "BUY" if v >= 0 else "SELL",
"ADX (14)": lambda v: "BUY" if v >= 25 else "SELL", # Not sure about this
"WILLR": lambda v: "SELL" if v <= -50 else "BUY",
"RSI (6)": lambda v: "SELL" if v >= 50 else "BUY",
"ATR (14)": lambda v: "N/A",
"OBV": lambda v: "N/A",
"TRIX (20)": lambda v: "N/A",
"EMA (6)": lambda v: "N/A"
}
data = []
for indicator, signal in get_signal.items():
val = round(indicators[indicator][0], 2)
data.append([indicator, str(val), signal(val)])
indicators_json.write(json.dumps({
"error": False,
"data": list(sorted(data, key=lambda i: len(i[0])))
}, indent=2))
except:
indicators_json.write(json.dumps({
"error": True,
"data": []
}, indent=2))
def fetch_blockchain_data():
with open(os.path.join(DIR_PATH, "../store/blockchain.json"), 'w') as blockchain_data_json:
try:
blockchain_data = dataset("blockchain_data")
data = [
["Confirmation Time", str(
round(blockchain_data["Conf. Time"][0], 2))],
["Block Size", str(round(blockchain_data["Block Size"][0], 2))],
["Transaction Cost", str(
round(blockchain_data["TXN Cost"][0], 2))],
["Difficulty", str(round(blockchain_data["Difficulty"][0], 2))],
["Transactions per Day", str(round(
blockchain_data["TXNs per Day"][0], 2))],
["Hash Rate (GH/s)",
str(round(blockchain_data["Hash Rate (GH/s)"][0], 2))],
["Market Capitalization", str(round(
blockchain_data["Market Cap"][0], 2))],
["Miners Revenue", str(
round(blockchain_data["Miners Revenue"][0], 2))],
["Transactions per Block", str(round(
blockchain_data["TXNs per Block"][0], 2))],
["Unique Addresses", str(round(
blockchain_data["Unique Addresses"][0], 2))],
["Total Bitcoin", str(
round(blockchain_data["Total BTC"][0], 2))],
["Transaction Fees", str(
round(blockchain_data["TXN Fees"][0], 2))]
]
blockchain_data_json.write(json.dumps({
"error": False,
"data": list(sorted(data, key=lambda i: len(i[0])))
}, indent=2))
except:
blockchain_data_json.write(json.dumps({
"error": True,
"data": []
}, indent=2))
def fetch_coindesk_stats():
with open(os.path.join(DIR_PATH, "../store/headlines.json"), 'w') as headlines_json:
try:
html = requests.get("https://www.coindesk.com/")
soup = BeautifulSoup(html.text, "html.parser")
top_container = soup.find('a', class_="top-article")
featured_containers = soup.find_all('a', class_="feature")
other_containers = soup.find_all('a', class_="stream-article")
headlines = []
for article in [top_container] + featured_containers + other_containers:
date_container = article.find("time")["datetime"]
headline_container = article.find("h3") if article.find("h3") else article.find("h1")
date_published = moment.date(date_container).format("M-D")
headline = headline_container.get_text().strip()
headlines.append((headline, date_published, article["href"]))
ordered_headlines = sorted(headlines, key=lambda h: h[1], reverse=True)
processed_headlines = []
for headline in ordered_headlines:
headline_str = headline[0].split('\n')[0]
date_published = headline[1]
sentiment = TextBlob(headline_str).sentiment.polarity
if sentiment > 0:
sentiment = "POS"
elif int(sentiment) == 0:
sentiment = "NEUT"
else:
sentiment = "NEG"
processed_headlines += [[
date_published,
headline_str,
sentiment,
headline[2]
]]
headlines_json.write(json.dumps({
"error": False,
"data": processed_headlines
}, indent=2))
except:
headlines_json.write(json.dumps({
"error": True,
"data": []
}, indent=2))
def fetch_portfolio_stats(client):
with open(os.path.join(DIR_PATH, "../store/portfolio.json"), 'w') as portfolio_json:
default_data = {
"account_balance": "$0.00",
"returns": "0.00%",
"net_profit": "$0.00",
"sharpe_ratio": "0.00",
"buy_accuracy": "0.00%",
"sell_accuracy": "0.00%",
"total_trades": "0"
}
try:
portfolio_json.write(json.dumps({
"error": False,
"data": {**default_data, **{
"account_balance": ''.join(['$', str(client.account_balance()["usd_available"])])
}}
}, indent=2))
except:
portfolio_json.write(json.dumps({
"error": True,
"data": default_data
}))
def fetch_transaction_data(client):
with open(os.path.join(DIR_PATH, "../store/transactions.json"), 'w') as transactions:
try:
transactions.write(json.dumps({
"error": False,
"data": [[txn["datetime"], txn["btc"], txn["type"]] for txn in client.user_transactions()]
}, indent=2))
except:
transactions.write(json.dumps({
"error": True,
"data": []
}, indent=2))
######
# MAIN
######
def retrieve(names, client=None):
for name in names: # TODO: Parallelize
if name == "price_data":
fetch_price_data()
elif name == "tech_indicators":
fetch_tech_indicators()
elif name == "blockchain_data":
fetch_blockchain_data()
elif name == "coindesk_headlines":
fetch_coindesk_stats()
elif name == "portfolio_stats":
fetch_portfolio_stats(client)
elif name == "transaction_log":
fetch_transaction_data(client)