-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_historical_futures.py
More file actions
113 lines (71 loc) · 2.87 KB
/
task_historical_futures.py
File metadata and controls
113 lines (71 loc) · 2.87 KB
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
from binance_service import binance_client
import time
import app
import model_service
from sqlalchemy import create_engine
import keys
from datetime import datetime as dt
import traceback
import utils
import model
engine = model.get_engine()
def download_info_while(symbol, startTime, interval='1m', limit=1500):
startTime = int(dt.strptime(startTime, '%Y-%m-%d %H:%M:%S').timestamp() * 1000)
last_previous_date = False
# endpoint = 'https://api.binance.com/api/v3/klines'
md_acumulated = []
while True:
try:
if not md_acumulated == []:
startTime = md_acumulated[-1][0] # busco ultima fecha
md_acumulated = md_acumulated[0:-1] # borro ultimo valor
endTime = startTime + 82800000
r = binance_client.futures_coin_klines(symbol=symbol, interval= interval, limit= limit,
startTime = startTime, endTime = endTime)
if r == {'code': -1121, 'msg': 'Invalid symbol.'}:
print(f'Invalid symbol {symbol}. No pudo bajar la md')
break
md_acumulated += r
if r == []:
print(f'fechas no validas ticker {symbol}')
break
if startTime == last_previous_date:
break
last_previous_date = startTime
except:
traceback.print_exc()
return md_acumulated
def getHistorical(symbol, startTime, endTime = None, quote_currency='USDT', interval='1m', limit=1500):
"""
Interval: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
Limit: 500, max: 1000
startTime: %Y-%m-%d
endTime: %Y-%m-%d OR "hoy"
"""
r = download_info_while(symbol, startTime, interval=interval, limit=limit)
return r
def task_historical_futures(start_time = '2021-04-01 00:00:00'):
engine.dispose()
tickers = utils.currencies()
tickers = tickers[0]
if not start_time:
start_time = dt.fromisoformat('2021-01-01')
while app.running:
for ticker in tickers:
# print(ticker, end=', ')
try:
last_date = model_service.last_date(ticker, engine, tabla= 'future_historical')
if last_date:
model_service.del_row(last_date, engine, tabla= 'future_historical')
start_time = last_date[1]
historical_data = getHistorical(ticker, startTime= start_time)
if historical_data != []:
model_service.save_historical_data_futures(engine, ticker, historical_data)
except:
traceback.print_exc()
pass
time.sleep(5)
# print('Futures terminado, esperando 30 segundos')
time.sleep(30)
if __name__ == '__main__':
task_historical_futures()