-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_data_clickhouse.py
87 lines (65 loc) · 2.47 KB
/
get_data_clickhouse.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
# Ryuryu's Bybit OHLC Data Saver
# Clickhouse Edition (Production Mode #6973)
# -------------------------------------------
# (C) 2023 Ryan Hayabusa
# Github: https://github.com/ryu878
# Discord: ryuryu#4087
# Mail: [email protected]
# Web: https://aadresearch.xyz
# Discord: ryuryu#4087
# -------------------------------------------
# sudo apt-get install apt-transport-https ca-certificates dirmngr
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E0C56BD4
# echo "deb https://repo.clickhouse.tech/deb/stable/ main/" | sudo tee \
# /etc/apt/sources.list.d/clickhouse.list
# sudo apt-get update
# sudo apt-get install -y clickhouse-server clickhouse-client
# sudo service clickhouse-server start
# clickhouse-client
# python3 -m venv .pb-ch && source /.pb-ch/bin/activate
# pip install clickhouse-driver
# pip install pandas
# pip install pybit
import json
from config import *
import pandas as pd
from pybit import inverse_perpetual
from clickhouse_driver import Client
session_unauth_inverse = inverse_perpetual.HTTP(endpoint=endpoint)
client = Client(host=host,password=password)
show_databases = client.execute('SHOW DATABASES')
print(show_databases)
client.execute(f'DROP DATABASE IF EXISTS {database}')
# Get Data from Bybit REST API
data = session_unauth_inverse.query_kline(symbol=symbol,interval=interval,limit=limit,from_time=1654104498)
result = data['result']
print(result)
df = pd.DataFrame(result)
df = df.drop(columns = ['turnover','volume'])
df = df.assign(oth = df['open_time'])
df['oth'] = pd.to_datetime(df['oth'], unit='s', origin='unix')
print(df)
# Create DataBase
client.execute(f'DROP DATABASE IF EXISTS {database}')
client.execute(f'CREATE DATABASE {database}')
show_databases = client.execute('SHOW DATABASES')
print(show_databases)
client = Client(host=host, user=user, password=password, port=port, database=database, settings={'columnar': True})
client.execute(f'''CREATE TABLE {table} (
symbol String,
interval String,
open_time Int32,
open String,
high String,
low String,
close String)
ENGINE = Memory'''
)
show_tables = client.execute(f'SHOW TABLES FROM {database}')
print(show_tables)
columns = client.execute(f"SELECT * FROM {table}", with_column_types="True")
print(columns)
client.execute(f"INSERT INTO {table} VALUES", df.to_dict('records'), types_check=True)
clickhouse_data = client.execute(f"SELECT * FROM {table}")
clickhouse_data = json.dumps(clickhouse_data)
print(clickhouse_data)