-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_ohlc.py
178 lines (156 loc) · 6.09 KB
/
update_ohlc.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
import os
import psycopg2
from psycopg2 import sql
import requests
from datetime import datetime
from dotenv import load_dotenv
import pandas as pd
import pytz
from find_start_timestamp import FindStartTimestamp
load_dotenv()
curr_time = datetime.now()
curr_unix_time = int(datetime.timestamp(curr_time))
to_int = lambda x: int(x)
to_float = lambda x: float(x)
to_timestamp = lambda x: pd.Timestamp(x, unit="s", tz="Europe/Vilnius")
url_dict = {
"username": os.getenv("PSQL_USER"),
"password": os.getenv("PSQL_PASSWORD"),
"host": os.getenv("DB_HOST"),
"port": os.getenv("DB_PORT"),
"database": os.getenv("DB_NAME"),
}
psycopg_conn_str = f"dbname={url_dict['database']} user={url_dict['username']} password={url_dict['password']} host={url_dict['host']} port={url_dict['port']}"
sqlalchemy_conn_str = f"postgresql+psycopg://{url_dict['username']}:{url_dict['password']}@{url_dict['host']}:{url_dict['port']}/{url_dict['database']}"
def fetch_pairs_from_db(connection):
retrieve_pairs_query = """--sql
SELECT unique_pair_id, pair_url FROM bitstamp_pairs WHERE trading_enabled = TRUE;
"""
with psycopg2.connect(connection) as conn:
cur = conn.cursor()
cur.execute(retrieve_pairs_query)
results = cur.fetchall()
cur.close()
pairs_df = pd.DataFrame(results)
pairs_df.rename(columns={0: "unique_pair_id", 1: "pair_url"}, inplace=True)
return pairs_df
def get_pair_latest_candle_timestamp(pair_url, db_connection):
table_name = f"ohlc_{pair_url}"
max_timestamp_query = """--sql
SELECT MAX(timestamp) FROM {table}
"""
with psycopg2.connect(db_connection) as conn:
cur = conn.cursor()
cur.execute(
sql.SQL(max_timestamp_query).format(table=sql.Identifier(table_name))
)
results = cur.fetchone()[0]
cur.close()
if results is None:
finder = FindStartTimestamp()
results = int(finder.find_starting_timestamp(pair_url, new_pair=True))
human_datetime = datetime.fromtimestamp(
results, tz=pytz.timezone("Europe/Vilnius")
)
finder.update_start_timestamp_in_main_table(
human_datetime, results, db_connection, pair_url
)
unix_results = results
else:
unix_results = int(datetime.timestamp(results))
return unix_results
def fetch_bitstamp_ohlc(
market_symbol,
start,
step=60,
limit=1000,
exclude_current_candle=True,
):
ohlc_url = f"https://www.bitstamp.net/api/v2/ohlc/{market_symbol}/"
params = {
"step": step,
"limit": limit,
"start": start,
"exclude_current_candle": exclude_current_candle,
}
resp = requests.get(ohlc_url, params=params)
api_data = resp.json()["data"]
ohlc_data = api_data["ohlc"]
df = pd.DataFrame(ohlc_data)
df.rename(columns={"timestamp": "unix_timestamp"}, inplace=True)
df["unix_timestamp"] = df["unix_timestamp"].astype(int)
df["timestamp"] = df["unix_timestamp"].apply(
lambda x: pd.Timestamp(x, unit="s", tz="Europe/Vilnius")
)
df = df[["timestamp", "open", "high", "low", "close", "volume"]]
return df
pairs_df = fetch_pairs_from_db(psycopg_conn_str)
pairs_df["last_timestamp"] = pairs_df["pair_url"].apply(
lambda pair_url: get_pair_latest_candle_timestamp(pair_url, psycopg_conn_str)
)
to_unix = lambda x: int(datetime.timestamp(x))
def get_new_ohlc(market_symbol, start):
ohlc_list = []
while start < curr_unix_time - 60:
try:
resp = requests.get(
f"https://www.bitstamp.net/api/v2/ohlc/{market_symbol}",
params={
"step": 60,
"limit": 1000,
"start": start,
"exclude_current_candle": True,
},
)
resp.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f"Bad status code for {market_symbol}")
if resp.status_code == 404:
print(f"Setting pair {market_symbol} trading status to DISABLED")
set_trading_status_to_disabled(market_symbol)
return
else:
pass
results = resp.json()
ohlc = results["data"]["ohlc"]
for dict in ohlc:
ohlc_list.append(dict)
start = start + 60000
return ohlc_list
def iterate_through_pairs_df_for_new_data():
pairs_df = fetch_pairs_from_db(psycopg_conn_str)
pairs_df["last_timestamp"] = pairs_df["pair_url"].apply(
lambda x: get_pair_latest_candle_timestamp(x, psycopg_conn_str)
)
for row in pairs_df.iterrows():
pair_url = row[1].values[1]
start = row[1].values[2] + 60
new_ohlc = get_new_ohlc(market_symbol=pair_url, start=start)
if new_ohlc is not None:
new_ohlc_df = pd.DataFrame(new_ohlc)
new_ohlc_df["timestamp"] = new_ohlc_df["timestamp"].apply(to_int)
new_ohlc_df["high"] = new_ohlc_df["high"].apply(to_float)
new_ohlc_df["open"] = new_ohlc_df["open"].apply(to_float)
new_ohlc_df["low"] = new_ohlc_df["low"].apply(to_float)
new_ohlc_df["close"] = new_ohlc_df["close"].apply(to_float)
new_ohlc_df["volume"] = new_ohlc_df["volume"].apply(to_float)
new_ohlc_df["timestamp"] = new_ohlc_df["timestamp"].apply(to_timestamp)
new_ohlc_df["unique_pair_id"] = row[1].values[0]
print(f"Updating database for: {row[1].values[0]} {pair_url}")
new_ohlc_df.to_sql(
f"ohlc_{pair_url}", sqlalchemy_conn_str, if_exists="append", index=False
)
else:
continue
def set_trading_status_to_disabled(market_symbol):
update_status_query = """--sql
update bitstamp_pairs
set trading_enabled = FALSE,
where pair_url = %(pair)s;
"""
with psycopg2.connect(psycopg_conn_str) as conn:
cur = conn.cursor()
cur.execute(update_status_query, {"pair": market_symbol})
conn.commit()
cur.close()
iterate_through_pairs_df_for_new_data()