Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_sirius-master/strategy/buy_sell_utils.py
5933 views
1
import logging
2
import time
3
4
from api_calls.prod_buy import prod_place_order, prod_get_order_state, prod_cancel_order
5
from api_calls.sandbox_buy import sandbox_place_order, sandbox_get_order_state, sandbox_cancel_order
6
from utils.datetime_utils import current_date
7
from utils.settings import settings
8
from utils.util import price_to_float
9
10
order_poll_interval = 1
11
seconds_to_wait_for_order_finished = 300
12
13
14
def make_deal_result(price, deal_price, commission, datetime):
15
return {'price': price, 'deal_price': deal_price, 'commission': commission, 'datetime': datetime}
16
17
18
def buy(account_info, instrument_info, candles):
19
try:
20
if settings()['MAIN']['mode'] == 'history_test':
21
return execute_history_test_order(instrument_info, candles)
22
if settings()['MAIN']['mode'] == 'sandbox':
23
return place_order_and_wait_for_finish(account_info, instrument_info, 'buy', True)
24
if settings()['MAIN']['mode'] == 'prod':
25
return place_order_and_wait_for_finish(account_info, instrument_info, 'buy', False)
26
except Exception as ex:
27
logging.error("Buy failed, account_info = {}, instrument_info = {}, exception = {}",
28
account_info, instrument_info, ex)
29
return None
30
31
32
def sell(account_info, instrument_info, candles):
33
try:
34
if settings()['MAIN']['mode'] == 'history_test':
35
return execute_history_test_order(instrument_info, candles)
36
if settings()['MAIN']['mode'] == 'sandbox':
37
return place_order_and_wait_for_finish(account_info, instrument_info, 'sell', True)
38
if settings()['MAIN']['mode'] == 'prod':
39
return place_order_and_wait_for_finish(account_info, instrument_info, 'sell', False)
40
except Exception as ex:
41
logging.error("Sell failed, account_info = {}, instrument_info = {}, exception = {}",
42
account_info, instrument_info, ex)
43
return None
44
45
46
def execute_history_test_order(instrument_info, candles):
47
last_candle = candles[-1]
48
test_price = last_candle['price']
49
50
min_lot = instrument_info['min_lot']
51
52
deal_price = test_price * min_lot
53
commission = 0.003 * deal_price
54
return make_deal_result(test_price, deal_price, commission, last_candle['time'])
55
56
57
def place_order_and_wait_for_finish(account_info, instrument_info, deal_type, is_sandbox):
58
account_id = account_info['account_id']
59
60
if is_sandbox:
61
order = sandbox_place_order(account_id, instrument_info['figi'], 1, deal_type)
62
else:
63
order = prod_place_order(account_id, instrument_info['figi'], 1, deal_type)
64
65
order_id = order['orderId']
66
order_state = wait_for_order_statuses(account_id, order_id,
67
['EXECUTION_REPORT_STATUS_FILL', 'EXECUTION_REPORT_STATUS_REJECTED'], is_sandbox)
68
logging.info("Trying to make '{}' for instrument = {}".format(deal_type, instrument_info))
69
70
if order_state['executionReportStatus'] == 'EXECUTION_REPORT_STATUS_FILL':
71
price = price_to_float(order_state['executedOrderPrice']['units'], order_state['executedOrderPrice']['nano'])
72
deal_price = price_to_float(order_state['executedOrderPrice']['units'],
73
order_state['executedOrderPrice']['nano'])
74
commission = price_to_float(order_state['executedCommission']['units'],
75
order_state['executedCommission']['nano'])
76
exec_datetime = current_date()
77
78
logging.info(
79
"Deal '{}' successfully finished, instrument = {}, price = {}, deal_price = {}, commission = {} datetime = {}"
80
.format(deal_type, instrument_info, price, deal_price, commission, exec_datetime))
81
82
return make_deal_result(price, deal_price, commission, exec_datetime)
83
else:
84
if is_sandbox:
85
sandbox_cancel_order(account_id, order_id)
86
else:
87
prod_cancel_order(account_id, order_id)
88
logging.error("Deal '{}' failed, instrument = {}, last order state = {}. Cancelling order"
89
.format(deal_type, instrument_info, order_state))
90
return None
91
92
93
def wait_for_order_statuses(account_id, order_id, order_statuses, is_sandbox):
94
start = time.time()
95
order_state = None
96
97
while True:
98
time.sleep(order_poll_interval)
99
if is_sandbox:
100
order_state = sandbox_get_order_state(account_id, order_id)
101
else:
102
order_state = prod_get_order_state(account_id, order_id)
103
104
logging.debug("Last order state = {}".format(order_state))
105
order_status = order_state['executionReportStatus']
106
if order_status in order_statuses:
107
break
108
seconds_elapsed = time.time() - start
109
if seconds_elapsed > seconds_to_wait_for_order_finished:
110
break
111
112
return order_state
113
114