-
Notifications
You must be signed in to change notification settings - Fork 22
/
place_order.py
78 lines (61 loc) · 2.07 KB
/
place_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf8 -*-
"""
clikraken.api.private.place_order
This module queries the AddOrder method of Kraken's API
and outputs the results in a tabular format.
Licensed under the Apache License, Version 2.0. See the LICENSE file.
"""
import clikraken.global_vars as gv
from clikraken.api.api_utils import query_api
from clikraken.clikraken_utils import check_trading_agreement
from clikraken.log_utils import logger
def place_order(args):
"""Place an order."""
# Parameters to pass to the API
api_params = {
'pair': args.pair,
'type': args.type,
'ordertype': args.ordertype,
'volume': args.volume,
'starttm': args.starttm,
'expiretm': args.expiretm,
'leverage': args.leverage,
}
if gv.TRADING_AGREEMENT == 'agree':
api_params['trading_agreement'] = 'agree'
if args.ordertype == 'limit':
if args.price is None:
logger.error('For limit orders, the price must be given!')
return
else:
api_params['price'] = args.price
elif args.ordertype == 'market':
if args.price is not None:
logger.warn('price is ignored for market orders!')
check_trading_agreement()
if args.userref:
api_params['userref'] = args.userref
oflags = [] # order flags
if args.ordertype == 'limit':
if not args.nopost:
# for limit orders, by default set post-only order flag
oflags.append('post')
if args.viqc:
oflags.append('viqc')
if oflags:
api_params['oflags'] = ','.join(oflags)
if args.validate:
api_params['validate'] = 'true'
res = query_api('private', 'AddOrder', api_params, args)
descr = res.get('descr')
odesc = descr.get('order', 'No description available!')
print(odesc)
txid = res.get('txid')
if not txid:
if args.validate:
logger.info('Validating inputs only. Order not submitted!')
else:
logger.warn('Order was NOT successfully added!')
else:
for tx in txid:
print(tx)