Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_sirius-master/services/account_info.py
5932 views
1
import logging
2
3
from api_calls.prod_account import get_prod_accounts, get_prod_positions
4
from api_calls.sandbox_account import get_sandbox_accounts, get_sandbox_portfolio, get_sandbox_positions
5
from utils.settings import settings
6
from utils.util import price_to_float
7
8
9
# управление счётом. если не удалось загрузить - это fatal, завершаем программу
10
# если у пользователя несколько счетов, выбирается первый
11
# TODO - добавить в settings выбор счёта
12
13
def create_account_info(account_id):
14
return {'account_id': account_id}
15
16
17
def prepare_account_info():
18
account_info = None
19
try:
20
if settings()['MAIN']['mode'] == 'history_test':
21
account_info = create_account_info(0)
22
elif settings()['MAIN']['mode'] == 'sandbox':
23
account_info = get_account_info(True)
24
elif settings()['MAIN']['mode'] == 'prod':
25
account_info = get_account_info(False)
26
except:
27
pass
28
finally:
29
if account_info is None:
30
logging.fatal("Failed to load account info. May be you should check tokens.")
31
quit(-1)
32
else:
33
logging.info("Account info loaded = {}".format(account_info))
34
return account_info
35
36
37
def get_account_info(is_sandbox):
38
if is_sandbox:
39
accounts = get_sandbox_accounts()['accounts']
40
else:
41
accounts = get_prod_accounts()['accounts']
42
43
if len(accounts) == 0:
44
logging.warning("No accounts found!")
45
return None
46
elif len(accounts) != 1:
47
logging.warning("More than 1 account. Accounts = {}. Robot will trade on the first one = {}"
48
.format(accounts, accounts[0]))
49
50
account_id = accounts[0]['id']
51
res = create_account_info(account_id)
52
return res
53
54
55
def has_enough_money(account_info):
56
positions = None
57
if settings()['MAIN']['mode'] == 'history_test':
58
return True
59
elif settings()['MAIN']['mode'] == 'sandbox':
60
positions = get_sandbox_positions(account_info['account_id'])
61
elif settings()['MAIN']['mode'] == 'prod':
62
positions = get_prod_positions(account_info['account_id'])
63
64
min_usd = float(settings()['TRADE']['min_usd'])
65
min_rub = float(settings()['TRADE']['min_rub'])
66
67
current_usd = 0
68
current_rub = 0
69
70
for currency_positions in positions['money']:
71
if currency_positions['currency'] == 'rub':
72
current_rub = price_to_float(currency_positions['units'], currency_positions['nano'])
73
elif currency_positions['currency'] == 'usd':
74
current_usd = price_to_float(currency_positions['units'], currency_positions['nano'])
75
76
if current_usd >= min_usd and current_rub >= min_rub:
77
logging.debug("Current money usd = {}, rub = {}".format(current_usd, current_rub))
78
return True
79
else:
80
logging.warning("Robot has not enough money, usd = {}, rub = {}. "
81
"Minimum settings: usd = {}, rub = {}"
82
.format(current_usd, current_rub, min_usd, min_rub))
83
return False
84
85