Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_trading_bot-master/trading/check_av.py
5932 views
1
from config.personal_data import get_account, get_account_type
2
from tinkoff.invest import Client
3
from config.personal_data import get_token
4
from datetime import datetime, timedelta
5
import pytz
6
7
8
def check_money(user_id, price, quantity, currency, account_id="", account_type=""):
9
if account_id == "":
10
account_id = get_account(user_id=user_id)
11
12
if account_type == "":
13
account_type = get_account_type(user_id=user_id)
14
15
with Client(get_token(user_id)) as client:
16
if account_type == "sandbox":
17
positions = client.sandbox.get_sandbox_positions(account_id=account_id)
18
else:
19
positions = client.operations.get_positions(account_id=account_id)
20
21
for i in positions.money:
22
if (i.currency == currency) and ((i.units + (i.nano * 1e-9)) > price * quantity * 1.03):
23
return True
24
25
return False
26
27
28
def check_time(user_id, figi):
29
# Получаем расписание всех площадок за неделю
30
with Client(get_token(user_id)) as client:
31
schedules = client.instruments.trading_schedules(
32
from_=datetime.utcnow(),
33
to=datetime.utcnow() + timedelta(days=6),
34
).exchanges
35
36
# Получаем инструмент
37
try:
38
security = client.instruments.get_instrument_by(id_type=1, id=figi).instrument
39
except Exception as ex:
40
print(ex)
41
error = "Такой бумаги не существует!"
42
return False, error
43
44
# Получаем время сейчас в часовом поясе UTC
45
now = datetime.utcnow().replace(tzinfo=pytz.UTC)
46
47
for i in schedules:
48
if i.exchange == security.exchange: # Если совпадают торговые площадки
49
for j in i.days:
50
if j.is_trading_day and j.start_time < now < j.end_time:
51
return True, ""
52
elif j.is_trading_day:
53
start_time = j.start_time - now
54
error = (
55
f"Бумаги <b>{security.name}</b>\n"
56
f"FIGI: {security.figi}\n"
57
f"Площадка: {security.exchange}\n"
58
f"До торгов осталось: <b>{start_time}</b>"
59
)
60
return False, error
61
62
return False, "else"
63
64