-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_start_timestamp.py
104 lines (98 loc) · 4.79 KB
/
find_start_timestamp.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
import requests
import time
import datetime
import pytz
import psycopg2
cur_time = datetime.datetime.now(pytz.timezone("Europe/Vilnius")).replace(microsecond=0)
cur_unix_time = int(time.time())
four_week_interval = 60 * 60 * 24 * 7 * 4
one_week_interval = 60 * 60 * 24 * 7
one_day_interval = 60 * 60 * 24
one_hour_interval = 60 * 60
one_minute_interval = 60
class FindStartTimestamp:
def get_candles(self, market_symbol, start, limit=1):
ohlc_url = f"https://www.bitstamp.net/api/v2/ohlc/{market_symbol}/"
params = {"step": 60, "limit": limit, "start": start}
resp = requests.get(ohlc_url, params=params)
results = resp.json()
return results["data"]["ohlc"]
def find_starting_timestamp(self, market_symbol, new_pair):
if new_pair:
start = cur_unix_time - one_week_interval
results = self.get_candles(market_symbol, start)
if not results:
while not results:
start = start + one_day_interval
results = self.get_candles(market_symbol, start)
if results:
while results:
start = start - one_hour_interval
results = self.get_candles(market_symbol, start)
if not results:
while not results:
start = start + one_minute_interval
results = self.get_candles(market_symbol, start)
if results:
start_timestamp = results[0]["timestamp"]
return start_timestamp
elif not new_pair:
start = cur_unix_time - four_week_interval
results = self.get_candles(market_symbol, start)
while results: # * empty python list is considered falsy
start = start - four_week_interval
results = self.get_candles(market_symbol, start)
if not results:
while not results:
start = start + one_week_interval
results = self.get_candles(market_symbol, start)
if results:
while results:
start = start - one_day_interval
results = self.get_candles(market_symbol, start)
if not results:
while not results:
start = start + one_hour_interval
results = self.get_candles(market_symbol, start)
if results:
while results:
start = start - one_minute_interval
results = self.get_candles(
market_symbol, start
)
if not results:
while not results:
start = (
start + one_minute_interval
)
results = self.get_candles(
market_symbol, start
)
if results:
start_timestamp = results[
0
]["timestamp"]
return start_timestamp
def update_start_timestamp_in_main_table(
self, timestamp, unix_timestamp, connection, pair
):
update_start_timestamp_query = """--sql
UPDATE bitstamp_pairs
SET start_timestamp = %(timestamp)s,
unix_timestamp = %(unix_timestamp)s
WHERE
pair_url = %(pair_url)s
"""
with psycopg2.connect(connection) as conn:
cur = conn.cursor()
cur.execute(
update_start_timestamp_query,
{
"timestamp": timestamp,
"unix_timestamp": unix_timestamp,
"pair_url": pair,
},
)
conn.commit()
print(f"Start timestamp updated for: {pair}")
cur.close()