Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_sirius-master/sandbox_control.py
5925 views
1
import logging
2
3
from api_calls.get_info import get_share_by_ticker
4
from api_calls.sandbox_account import *
5
from services.instruments_info_cache import get_instrument_info
6
from strategy.buy_sell_utils import place_order_and_wait_for_finish
7
8
9
def sandbox_control():
10
help_str = """
11
Вспомогательный режим для управления sandbox-аккаунтом.
12
В примерах - 1eb65a36-8ecb-4bd8-b186-056547277f18 - это account_id счёта, который можно получить с помощью
13
команды info
14
Основные команды (вводить без кавычек):
15
'open_account' - открыть новый sandbox-счёт
16
'close_account 1eb65a36-8ecb-4bd8-b186-056547277f18' - закрыть sandbox-счёт
17
'info' - вывести информацию по аккаунту
18
'portfolio 1eb65a36-8ecb-4bd8-b186-056547277f18' - вывести портфолио по счёту
19
'positions 1eb65a36-8ecb-4bd8-b186-056547277f18' - вывести список позиций по счёту
20
'buy 1eb65a36-8ecb-4bd8-b186-056547277f18 MOEX ORUP' - купить акцию ORUP на бирже MOEX
21
'sell 1eb65a36-8ecb-4bd8-b186-056547277f18 SPB VEON' - продать акцию VEON на бирже SPB
22
'pay 1eb65a36-8ecb-4bd8-b186-056547277f18 USD 10000 35' - пополнить счёт на 10000 долларов и 35 центов
23
'pay 1eb65a36-8ecb-4bd8-b186-056547277f18 RUB 29999 0' - пополнить счёт на 29999 рублей
24
'q' или 'quit' - выход
25
"""
26
logging.info(help_str)
27
28
while True:
29
line = input('')
30
if len(line) == 0:
31
continue
32
if line.lower() in ['q', 'quit']:
33
break
34
else:
35
do_operation(line)
36
37
38
def do_operation(line):
39
command = line.split()[0]
40
args = line.split()[1:]
41
42
if command == 'info':
43
logging.info("Sandbox accounts = {}\n".format(pretty_dict(get_sandbox_accounts())))
44
elif command == 'portfolio':
45
logging.info("Sandbox portfolio = {}\n".format(pretty_dict(get_sandbox_portfolio(args[0]))))
46
elif command == 'positions':
47
logging.info("Sandbox positions = {}\n".format(pretty_dict(get_sandbox_positions(args[0]))))
48
elif command == 'close_account':
49
logging.info("Sandbox close account = {}\n".format(pretty_dict(close_sandbox_account(args[0]))))
50
elif command == 'open_account':
51
logging.info("Sandbox open account = {}\n".format(pretty_dict(open_sandbox_account())))
52
elif command == 'pay':
53
logging.info("Sandbox pay for account = {}\n".format(
54
pretty_dict(pay_sandbox_account(account_id=args[0], currency=args[1], units=args[2], nano=args[3]))))
55
elif command == 'buy':
56
do_operation_deal(account_id=args[0], exchange=args[1], ticker=args[2], deal_type=command)
57
elif command == 'sell':
58
do_operation_deal(account_id=args[0], exchange=args[1], ticker=args[2], deal_type=command)
59
else:
60
logging.warning("Unknown command '{}'".format(command))
61
62
63
def do_operation_deal(account_id, exchange, ticker, deal_type):
64
instrument_info = get_instrument_info(exchange, ticker)
65
place_order_and_wait_for_finish({'account_id': account_id}, instrument_info, deal_type, True)
66
67