Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_sirius-master/services/instruments_info_cache.py
5932 views
1
from api_calls.get_info import get_shares
2
from utils.util import is_file_exists, read_dict_from_file, write_dict_to_file, pretty_dict
3
4
instruments_cache = {}
5
instruments_cache_filename = 'data/cache_instruments'
6
7
8
# Кэш инструментов. Зачитывается с сервера один раз и записывается в файл. Кэш можно сбросить запуском --reset-robot
9
10
def get_instruments_cache():
11
return instruments_cache
12
13
14
def make_instrument_key(exchange, ticker):
15
return exchange + '_' + ticker
16
17
18
def get_instrument_info(exchange, ticker):
19
global instruments_cache
20
return get_instrument_info_by_key(make_instrument_key(exchange, ticker))
21
22
23
def get_instrument_info_by_key(key):
24
global instruments_cache
25
return instruments_cache[key]
26
27
28
def init_instruments_cache():
29
global instruments_cache
30
31
if is_file_exists(instruments_cache_filename):
32
instruments_cache = read_dict_from_file(instruments_cache_filename)
33
return
34
35
shares = get_shares()['instruments']
36
37
instruments_cache = {}
38
39
for instrument in shares:
40
ticker = instrument['ticker']
41
exchange = instrument['exchange']
42
instruments_cache[make_instrument_key(exchange, ticker)] = {
43
'ticker': instrument['ticker'],
44
'exchange': instrument['exchange'],
45
'figi': instrument['figi'],
46
'min_lot': instrument['lot'],
47
'currency': instrument['currency'].upper(),
48
'class_code': instrument['classCode']
49
}
50
51
write_dict_to_file(instruments_cache_filename, instruments_cache)
52
53
54