Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_sirius-master/services/history_test_data.py
5933 views
1
import logging
2
import time
3
from datetime import timedelta
4
5
from api_calls.get_info import get_candles_by_figi
6
from services.instruments_info_cache import get_instrument_info
7
from utils.datetime_utils import date_minus_days, current_date, string_to_date
8
from utils.settings import settings
9
from utils.util import read_dict_from_file, write_dict_to_file, pretty_dict
10
11
# методы используются в режиме history_test
12
13
# тестовые даты старта и остановки. Определяются как самая ранняя/поздняя дата свечей при инициализации
14
# данных в init_history_test_data()
15
test_current_date = None
16
test_stop_date = None
17
poll_interval_minutes = int(settings()['TRADE']['poll_interval_minutes'])
18
19
20
def init_history_test_data():
21
global test_current_date
22
global test_stop_date
23
res = read_dict_from_file('data/test_history')
24
if res is None:
25
return res
26
27
for info in res:
28
start_date = string_to_date(info['candles'][0]['time'])
29
stop_date = string_to_date(info['candles'][-1]['time'])
30
if test_current_date is None or start_date < test_current_date:
31
test_current_date = start_date
32
if test_stop_date is None or stop_date > test_stop_date:
33
test_stop_date = stop_date
34
35
return res
36
37
38
test_info = init_history_test_data()
39
40
41
def get_test_current_date():
42
return test_current_date
43
44
45
# сдвинуть тестовую дату на poll_interval
46
def get_next_test_current_date():
47
global test_current_date
48
test_current_date = test_current_date + timedelta(minutes=poll_interval_minutes)
49
return test_current_date
50
51
52
def get_test_data(exchange, ticker):
53
for instrument in test_info:
54
if instrument['exchange'] == exchange and instrument['ticker'] == ticker:
55
return instrument
56
return None
57
58
59
# TODO speed up search
60
def test_get_candles_by_date(exchange, ticker, from_date_time_timestamp, to_date_time_timestamp):
61
ticker_test_info = get_test_data(exchange, ticker)
62
if ticker_test_info is None:
63
return []
64
65
ticker_candles = ticker_test_info['candles']
66
res = []
67
for candle in ticker_candles:
68
candle_date = candle['datetime']
69
if candle_date < from_date_time_timestamp:
70
continue
71
elif candle_date > to_date_time_timestamp:
72
break
73
res.append(candle)
74
return res
75
76
77
# условие остановки при тестовом режиме
78
def test_get_stop():
79
global test_stop_date
80
return get_test_current_date() > test_stop_date
81
82
83
def prepare_history_file(days):
84
instruments = read_dict_from_file('instruments_history_test')
85
86
logging.info("Preparing history test for {} days for following instruments:\n{}"
87
.format(days, pretty_dict(instruments)))
88
89
for instrument in instruments:
90
instrument_info = get_instrument_info(instrument['exchange'], instrument['ticker'])
91
figi = instrument_info['figi']
92
candles = []
93
94
for i in range(0, days):
95
candles.extend(get_candles_by_figi(figi, date_minus_days(current_date(), days - i),
96
date_minus_days(current_date(), days - i - 1),
97
'CANDLE_INTERVAL_1_MIN')['candles'])
98
for candle in candles:
99
candle['datetime'] = string_to_date(candle['time']).timestamp()
100
instrument['candles'] = candles
101
logging.info("Candles for one day for instrument = {} {} has been prepared"
102
.format(instrument['exchange'], instrument['ticker']))
103
104
time.sleep(2) # to avoid rate limit break
105
106
write_dict_to_file('data/test_history', instruments)
107
108
logging.info("History file successfully prepared")
109
110