-
Notifications
You must be signed in to change notification settings - Fork 2
/
KrakenTB2.py
151 lines (129 loc) · 4.69 KB
/
KrakenTB2.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# Copyright Ali Raheem 2019
# GPLv3
import sqlite3, krakenex, datetime, sys
class DB():
def __init__(self, path='orders.sqlite'):
self.conn = sqlite3.connect(path)
self.c = self.conn.cursor()
self.initialise()
def initialise(self):
query = '''CREATE TABLE IF NOT EXISTS `orders`
(txid TEXT,
pair TEXT,
price REAL,
volume REAL,
offset REAL,
ticker TEXT,
status INTEGER,
note TEXT)'''
self.c.execute(query)
def commit(self):
self.conn.commit()
def getActiveTxid(self):
return getTxid(self.getActive())
def getNewTxid(self):
return getTxid(self.getNew())
def getOrders(self, status):
query = '''SELECT * FROM `orders` WHERE `status` = ?'''
self.c.execute(query, (status,) )
return self.c.fetchall()
def getActive(self):
return self.getOrders(1)
def getNew(self):
return self.getOrders(2)
def setNewActive(self):
query = '''UPDATE `orders` SET `status` = 1 WHERE `status` = 2'''
self.c.execute(query)
def addOrder(self, txid, pair, price, volume, offset, ticker, status, note):
query = '''INSERT INTO `orders` VALUES (?, ?, ?, ?, ?, ?, ?, ?)'''
self.c.execute(query, (txid, pair, price, volume, offset, ticker, status, note))
def cancelOrder(self, txid):
self.setOrderStatus(txid, 0)
def setOrderActive(self, txid):
self.setOrderStatus(txid, 1)
def setOrderStatus(self, txid, status):
query = '''UPDATE `orders` SET `status` = ? WHERE `txid` = ?'''
self.c.execute(query, (status, txid))
def getTickers(orders):
res = []
for _, _, _, _, _, ticker, _, _ in orders:
res.append(ticker)
return res
def getTxid(orders):
res = []
for txid, _, _, _, _, _, _, _ in orders:
res.append(txid)
return res
class API():
def __init__(self, keyfile="kraken.key"):
self.k = krakenex.API()
self.k.load_key(keyfile)
def getTicker(self, tickers):
res = self.k.query_public('Ticker', {'pair': ','.join(tickers)})
if len(res['error']) == 0:
return res['result']
raise Exception(res['error'][0])
def getOpenOrders(self):
return list(self.k.query_private('OpenOrders', {'trades': False})['result']['open'].keys())
def cancelOrder(self, txid):
if txid == None:
return None
return self.k.query_private('CancelOrder', {'txid': txid})
def addOrder(self, pair, price, volume):
response = self.k.query_private('AddOrder', {
'pair': pair,
'type': 'sell',
'ordertype': 'stop-loss',
'price': price,
'volume': volume})
if len(response['error']) == 0:
return response['result']['txid'][0]
else:
raise Exception(response['error'][0])
def close(self):
self.k.close()
if __name__ == "__main__":
db = DB()
api = API()
print(datetime.datetime.now().strftime("%Y-%l-%d %H:%M:%S"))
# TODO: This should be handled by command line arguments.
# Maybe we can also pass active orders to start with,
# of different inital offset and maintainence offset
# db.addOrder("", "XRPEUR", 0, 50, 0.008, "XXRPZEUR", 2, "")
active_orders_api = set(api.getOpenOrders())
active_orders_db = set(db.getActiveTxid())
stale_orders = active_orders_db - active_orders_api
for order in stale_orders:
print("Cancelling stale order", order)
db.cancelOrder(order)
db.commit() # Commit inactivating stale orders.
db.setNewActive()
active_tickers = set(getTickers(db.getActive()))
if len(active_tickers) == 0:
print("No orders to process.\nQuitting.")
db.commit()
api.close()
sys.exit()
prices = getTicker(active_tickers)
print("Active Orders\n=============================")
for txid, pair, price, volume, offset, ticker, status, note in db.getActive():
close_price = round(float(prices[ticker]['c'][0]), 5)
print(txid, "pair:", pair, "Stop price:", price, "volume:", volume)
current_price = round(close_price - offset, 5)
print("Close price:", close_price, "New stop:", current_price)
if current_price > price: # TODO: Move this out to an evaluation function to allow buy and sell and more complex evaluation.
print("Replaceing order", txid)
api.cancelOrder(txid) # TODO: Handle errors here with an error status and note.
# TODO: I think the order shouldn't be cancelled but held in another status,
# which will automtically be readded next run.
db.cancelOrder(txid) # Should we commit this?
txid = api.addOrder(pair, current_price, volume)
db.addOrder(txid, pair, current_price, volume, offset, ticker, 1, note)
db.commit() # Commit each successfully added order.
else:
print("Keeping", txid)
print("----------------------------------------------")
print()
db.commit()
api.close()