-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkraken.py
More file actions
121 lines (103 loc) · 3.74 KB
/
Copy pathkraken.py
File metadata and controls
121 lines (103 loc) · 3.74 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import logging
import krakenex
class Kraken:
def __init__(self):
self.k = krakenex.API()
self.k.load_key('kraken.key')
def create_new_order(self, pair, type, ordertype, volume, leverage, price=0):
order_data = {'pair': pair,
'type': type,
'ordertype': ordertype,
'volume': volume,
'leverage': leverage}
if price > 0:
order_data["price"] = price
result = self.k.query_private('AddOrder', order_data)
dict(result)
if result['error']:
for e in result['error']:
logging.error("Creating new order failed: " + e)
return {}
else:
logging.info("Created order: " + result['result']['descr']['order'] + " [" + result['result']['txid'][0] + "]")
return result['result']
def cancel_order(self, txid):
req_data = {'txid': txid}
result = self.k.query_private('CancelOrder', req_data)
dict(result)
if result['error']:
for e in result['error']:
logging.error("Canceling order [" + txid + "] failed: " + e)
return {}
else:
return result['result']
def get_open_positions(self, pair=None, ordertxid=None):
req_data = {'docalcs': 'true'}
if pair:
req_data['pair'] = pair
if ordertxid:
req_data['ordertxid'] = ordertxid
result = self.k.query_private('OpenPositions', req_data)
dict(result)
if result['error']:
for e in result['error']:
logging.error("Fetching open positions failed: " + e)
return {}
else:
found = []
if pair:
# we are searching for a specific pair
for id in result['result']:
if result['result'][id]['pair'] == pair:
found.append(result['result'][id])
# we didn't find that pair
if not found:
return {}
else:
return found
if ordertxid:
for id in result['result']:
if result['result'][id]['ordertxid'] == ordertxid:
found.append(result['result'][id])
# we didn't find that ordertx
if not found:
return {}
else:
return found
return {}
def get_open_orders(self):
req_data = {'docalcs': 'true'}
result = self.k.query_private('OpenOrders', req_data)
dict(result)
if result['error']:
for e in result['error']:
logging.error("Fetching open orders failed: " + e)
return {}
else:
return result['result']['open']
def get_closed_orders(self):
req_data = {'docalcs': 'true'}
result = self.k.query_private('ClosedOrders', req_data)
dict(result)
if result['error']:
for e in result['error']:
logging.error("Fetching closed orders failed: " + e)
return {}
else:
return result['result']
def get_balance(self):
r = self.k.query_private('Balance')
if r["error"]:
logging.error("Fetching balance failed: " + r["error"])
return {}
else:
return r["result"]
def get_ticker(self, pair):
req_data = {'pair': pair}
r = self.k.query_public('Ticker', req_data)
if r["error"]:
for e in r['error']:
logging.error("Fetching ticker failed: " + e)
return {}
else:
return r["result"]