-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin.py
76 lines (60 loc) · 1.86 KB
/
bitcoin.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
import websocket, json
import pandas as pd
import dateutil.parser
import datetime
from datetime import date, datetime, timedelta
minutes_processed = {}
minute_candlesticks = []
current_tick = None
previous_tick = None
socket = 'wss://ws-feed.pro.coinbase.com'
def on_open(ws):
print("Connection is opened")
subscribe_msg = {
"type": "subscribe",
"channels": [
{
"name": "ticker",
"product_ids": [
"BTC-USD"
]
}
]
}
ws.send(json.dumps(subscribe_msg))
def on_message(ws, message):
global current_tick, previous_tick
previous_tick = current_tick
current_tick = json.loads(message)
# print(current_tick)
print("=== Received Tick ===")
print(f"{current_tick['price']} @ {current_tick['time']}")
tick_datetime_object = dateutil.parser.parse(current_tick['time'])
timenow = tick_datetime_object + timedelta(hours=8)
tick_dt = timenow.strftime("%m/%d/%Y %H:%M")
print(tick_datetime_object.minute)
print(tick_dt)
if not tick_dt in minutes_processed:
print("This is a new candlestick")
minutes_processed[tick_dt] = True
if len(minute_candlesticks) > 0:
minute_candlesticks[-1]['close'] = previous_tick['price']
minute_candlesticks.append({
'minute': tick_dt,
'open': current_tick['price'],
'high': current_tick['price'],
'low': current_tick['price']
})
df = pd.DataFrame(minute_candlesticks[:-1])
df.to_csv("bitcoin_data_tut.csv")
if len(minute_candlesticks) > 0:
current_candlestick = minute_candlesticks[-1]
if current_tick['price'] > current_candlestick['high']:
current_candlestick['high'] = current_tick['price']
if current_tick['price'] < current_candlestick['low']:
current_candlestick['low'] = current_tick['price']
print("== Candlesticks ==")
for candlestick in minute_candlesticks:
print(candlestick)
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)
ws.run_forever()