-
Notifications
You must be signed in to change notification settings - Fork 0
/
upbit_producer.py
289 lines (241 loc) · 9.99 KB
/
upbit_producer.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
import requests
from kafka import KafkaProducer
from json import dumps
import time
from dotenv import load_dotenv
import os
import pandas as pd
from datetime import datetime
load_dotenv()
class UpbitAPI:
def __init__(self, access_key, secret_key):
self.access_key = access_key
self.secret_key = secret_key
def get_account(self):
payload = {
"access_key": self.access_key,
"nonce": str(uuid.uuid4()),
}
jwt_token = jwt.encode(payload, self.secret_key)
authorization_token = f"Bearer {jwt_token}"
headers = {
"Authorization": authorization_token,
}
url = "https://api.upbit.com/v1/accounts"
response = requests.get(url, headers=headers)
return response.json()
def get_candle_data(self, market, count):
url = (
f"https://api.upbit.com/v1/candles/minutes/1?market={market}&count={count}"
)
headers = {"accept": "application/json"}
try:
response = requests.get(url, headers=headers)
response = response.json()[0]
output = {
"timestamp": response["timestamp"],
"open": response["opening_price"],
"high": response["high_price"],
"low": response["low_price"],
"trade": response["trade_price"],
"candle_acc_trade_price": response["candle_acc_trade_price"],
"candle_acc_trade_volume": response["candle_acc_trade_volume"],
}
return output
except Exception as e:
print("Candle Error:", e)
return None
def get_trade_data(self, market, count):
url = f"https://api.upbit.com/v1/trades/ticks?market={market}&count={count}"
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
try:
response = response.json()[0]
output = {
"timestamp": response["timestamp"],
"trade_price": response["trade_price"],
"trade_volume": response["trade_volume"],
"prev_closing_price": response["prev_closing_price"],
"change_price": response["change_price"],
"trade_time_utc": response["trade_time_utc"],
}
return output
except Exception as e:
print("Trade Error:", e)
return None
def get_ticker_data(self, market, count):
url = f"https://api.upbit.com/v1/ticker?markets={market}&count={count}"
headers = {"accept": "application/json"}
try:
response = requests.get(url, headers=headers)
response = response.json()[0]
output = {
"timestamp": response["timestamp"],
'trade_date': response['trade_date'],
"trade_time": response["trade_time"],
"opening_price": response["opening_price"],
"high_price": response["high_price"],
"low_price": response["low_price"],
"trade_price": response["trade_price"],
"prev_closing_price": response["prev_closing_price"],
"change": response["change"],
"change_price": response["change_price"],
"change_rate": response["change_rate"],
"signed_change_price": response["signed_change_price"],
"signed_change_rate": response["signed_change_rate"],
"trade_volume": response["trade_volume"],
"acc_trade_price": response["acc_trade_price"],
"acc_trade_volume": response["acc_trade_volume"],
}
return output
except Exception as e:
print("Ticker Error:", e)
return None
def get_orderbook_data(self):
url = "https://api.upbit.com/v1/orderbook?markets=KRW-BTC&level=0"
headers = {"accept": "application/json"}
try:
response = requests.get(url, headers=headers)
response = response.json()[0]
output = {
"timestamp": response["timestamp"],
"total_ask_size": response["total_ask_size"],
"total_bid_size": response["total_bid_size"],
"max_ask_price": response["orderbook_units"][0]["ask_price"],
"max_bid_price": response["orderbook_units"][0]["bid_price"],
"min_ask_price": response["orderbook_units"][-1]["ask_price"],
"min_bid_price": response["orderbook_units"][-1]["bid_price"],
"median_ask_price": response["orderbook_units"][len(response["orderbook_units"]) // 2]["ask_price"],
"median_bid_price": response["orderbook_units"][len(response["orderbook_units"]) // 2]["bid_price"],
}
except Exception as e:
print("Orderbook Error:", e)
output = None
return output
def get_candle_data_and_send_to_kafka(api_instance, market, count, interval=10):
producer = KafkaProducer(
acks=0,
compression_type="gzip",
bootstrap_servers=["localhost:9092"],
value_serializer=lambda x: dumps(x).encode("utf-8"),
)
try:
while True:
data_candle = api_instance.get_candle_data(market, count)
if data_candle is not None:
send_to_kafka("upbit_candle_topic", data_candle, producer)
time.sleep(interval)
except Exception as e:
print("Candle Error:", e)
finally:
producer.close()
def get_trade_data_and_send_to_kafka(api_instance, market, count, interval=10):
producer = KafkaProducer(
acks=0,
compression_type="gzip",
bootstrap_servers=["localhost:9092"],
value_serializer=lambda x: dumps(x).encode("utf-8"),
)
try:
while True:
data_trade = api_instance.get_trade_data(market, count)
if data_trade is not None:
send_to_kafka("upbit_trade_topic", data_trade, producer)
time.sleep(interval)
except Exception as e:
print("Trade Error:", e)
finally:
producer.close()
def get_ticker_data_and_send_to_kafka(api_instance, market, count, interval=10):
producer = KafkaProducer(
acks=0,
compression_type="gzip",
bootstrap_servers=["localhost:9092"],
value_serializer=lambda x: dumps(x).encode("utf-8"),
)
try:
while True:
data_ticker = api_instance.get_ticker_data(market, count)
if data_ticker is not None:
send_to_kafka("upbit_ticker_topic", data_ticker, producer)
else:
print("Ticker data is None")
time.sleep(interval)
except Exception as e:
print("Ticker Error:", e)
finally:
producer.close()
def get_orderbook_data_and_send_to_kafka(api_instance, interval=10):
producer = KafkaProducer(
acks=0,
compression_type="gzip",
bootstrap_servers=["localhost:9092"],
value_serializer=lambda x: dumps(x).encode("utf-8"),
)
try:
while True:
data_orderbook = api_instance.get_orderbook_data()
if data_orderbook is not None:
send_to_kafka("upbit_orderbook_topic", data_orderbook, producer)
time.sleep(interval)
except Exception as e:
print("Orderbook Error:", e)
finally:
producer.close()
def send_to_kafka(topic, data, producer):
producer.send(topic, value=data)
producer.flush()
print(f"Data sent to Kafka topic {topic}")
def get_data_send_kafka(api_instance, market, count, interval):
producer = KafkaProducer(
acks=0,
compression_type="gzip",
bootstrap_servers=["localhost:9092"],
value_serializer=lambda x: dumps(x).encode("utf-8"),
)
while True:
try:
data = api_instance.get_candle_data(market, count)
if data is not None:
send_to_kafka("upbit_candle_topic", data, producer)
data = api_instance.get_trade_data(market, count)
if data is not None:
send_to_kafka("upbit_trade_topic", data, producer)
data = api_instance.get_ticker_data(market, count)
if data is not None:
send_to_kafka("upbit_ticker_topic", data, producer)
data = api_instance.get_orderbook_data()
if data is not None:
send_to_kafka("upbit_orderbook_topic", data, producer)
except Exception as e:
print("Sending data to Kafak Error:", e)
time.sleep(interval)
def main():
access_key = os.getenv("ACCESS_KEY")
secret_key = os.getenv("SECRET_KEY")
upbit_api = UpbitAPI(access_key, secret_key)
# get_candle_data_and_send_to_kafka(upbit_api, "KRW-BTC", 1)
# get_trade_data_and_send_to_kafka(upbit_api, "KRW-BTC", 1)
# get_ticker_data_and_send_to_kafka(upbit_api, "KRW-BTC", 1)
# get_orderbook_data_and_send_to_kafka(upbit_api)
get_data_send_kafka(upbit_api, "KRW-BTC", 1, 5)
# while True:
# try:
# # data_candle = upbit_api.get_candle_data("KRW-BTC", 1)
# # data_trade = upbit_api.get_trade_data("KRW-BTC", 1)
# data_ticker = upbit_api.get_ticker_data("KRW-BTC", 1)
# # data_orderbook = upbit_api.get_orderbook_data()
# print(data_ticker)
# # send_to_kafka("upbit_candle_topic", data_candle)
# # send_to_kafka("upbit_trade_topic", data_trade)
# # print(data_ticker)
# # get_candle_data_and_send_to_kafka.remote(upbit_api, "KRW-BTC", 1, producer)
# # get_trade_data_and_send_to_kafka.remote(upbit_api, "KRW-BTC", 1, producer)
# # get_ticker_data_and_send_to_kafka.remote(upbit_api, "KRW-BTC", 1, producer)
# # get_orderbook_data_and_send_to_kafka.remote(upbit_api, producer)
# except Exception as e:
# print(e)
# time.sleep(10)
if __name__ == "__main__":
main()
# print(response.text)