-
Notifications
You must be signed in to change notification settings - Fork 4
/
qapi.py
391 lines (312 loc) · 13.4 KB
/
qapi.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from qtrade_client.api import QtradeAPI
import random
import json
import time
import os
import sys
from log import Logger
from datetime import datetime
import requests
from dateutil import parser
from auth import QtradeAuth
DEBUG = False
DEMO = False
DEMO_MARKETS = ["NYZO", "BIS"]
log_obj = Logger("qtrader.log", "WARNING")
log = log_obj.logger
def part_percentage(part, whole):
return float(100.0) * part / whole
def load_credentials():
if os.path.exists("secret"):
with open("secret") as authfile:
return authfile.read()
else:
while True:
with open("secret", "w") as authfile:
auth = input("Enter your qTrade authentication data: ")
if ":" in auth:
authfile.write(auth)
return auth
else:
pass
class Order:
def __init__(self):
self.id = None
self.market_amount = None
self.market_amount_remaining = None
self.created_at = None
self.price = None
self.order_type = None
self.market_id = None
self.open = None
self.trades = None
class PairOrders:
def __init__(self):
self.api = api.get(
f"https://api.qtrade.io/v1/user/market/{conf.pair}"
).json()
self.base_balance = self.api["data"]["base_balance"]
self.closed_orders = self.api["data"]["closed_orders"]
self.market_balance = self.api["data"]["market_balance"]
self.open_orders = api.auth_native.orders(open=True)
class PairMarket:
def __init__(self, configuration):
self.ask = float(conf.market_api["data"]["ask"])
self.bid = float(conf.market_api["data"]["bid"])
self.spread = float(abs(self.ask - self.bid))
self.day_avg_price = float(conf.market_api["data"]["day_avg_price"])
self.day_change = float(conf.market_api["data"]["day_change"])
self.day_high = float(conf.market_api["data"]["day_high"])
self.day_low = float(conf.market_api["data"]["day_low"])
self.day_open = float(conf.market_api["data"]["day_open"])
self.day_volume_base = float(conf.market_api["data"]["day_volume_base"])
self.day_volume_market = float(conf.market_api["data"]["day_volume_market"])
self.id = int(conf.market_api["data"]["id"])
self.id_hr = conf.market_api["data"]["id_hr"]
self.last_price = float(conf.market_api["data"]["last"])
self.day_spread = float(abs(self.day_high - self.day_low))
self.spread_pct = 100 - part_percentage(self.bid, self.ask)
def pick_currency(balances_dict, currency_name):
for entry in balances_dict["data"]["balances"]:
if entry["currency"] == currency_name:
return Balance(entry)
else:
entry = {"currency": currency_name, "balance": 0}
return Balance(entry)
class Balance:
def __init__(self, balance_dict):
self.name = balance_dict["currency"]
self.balance = float(balance_dict["balance"])
class Config:
def __init__(self, **kwargs):
self.orders_placed = []
self.name = kwargs["name"]
self.pair = f"{kwargs['name']}_BTC"
if DEMO:
self.sell_amount = 0
else:
self.sell_amount = float(kwargs["sell_amount"])
self.buy_amount = float(kwargs["buy_amount"])
self.buy_longevity = int(kwargs["buy_longevity"])
self.sell_longevity = int(kwargs["sell_longevity"])
self.spread_pct_min = float(kwargs["spread_pct_min"])
self.market_api = None
self.refresh_api()
self.market_id = self.market_api["data"]["id"]
self.price_adjustment = float(kwargs["price_adjustment"])
log.warning("market_api", self.market_api)
self.max_buy_price = float(kwargs["max_buy_price"])
self.min_sell_price = float(kwargs["min_sell_price"])
self.last_refreshed = None
self.max_stash = float(kwargs["max_stash"])
self.min_stash = float(kwargs["min_stash"])
self.random_size = float(kwargs["random_size"])
self.end_pause = float(kwargs["end_pause"])
def count_orders(self):
self.orders_count = len(self.orders_placed)
return self.orders_count
def refresh_api(self):
self.last_refreshed = time.time()
self.market_api = api.get(f"https://api.qtrade.io/v1/ticker/{self.pair}").json()
def age(timestamp):
timestamp_ISO_8601 = parser.isoparse(timestamp)
epoch_ts = datetime.timestamp(timestamp_ISO_8601)
return int(time.time() - epoch_ts)
def buy(conf, pair_market):
# place a buy order
log.warning("Checking to place a buy order")
if pair_market.bid >= conf.max_buy_price:
log.warning("Market price too high to buy now")
elif conf.buy_amount <= 0:
log.warning(f"Not configured to buy (buy set to {conf.buy_amount})")
else:
btc = pick_currency(balances, "BTC")
altcoin = pick_currency(balances, conf.name)
random_value = randomize(conf.random_size)
buy_value = conf.buy_amount + random_value
final_price = pair_market.bid + conf.price_adjustment
if altcoin.balance >= conf.max_stash:
log.warning("Maximum stash reached, will not create new buy orders")
# log.warning(balance["balance"])
elif (
btc.balance >= buy_value * final_price
): # if one can afford to buy, sometimes fails because we can't refresh api on every order
# discount = percentage(trade_price_percentage, pair_market.bid)
req = {
"amount": "%.4f" % buy_value,
"market_id": conf.market_id,
"price": "%.8f" % final_price,
}
result = api.post(
"https://api.qtrade.io/v1/user/buy_limit", json=req
).json()
log.warning(result)
order_id = int(result["data"]["order"]["id"])
log.warning(f"Placed buy order {order_id}")
conf.orders_placed.append({"id": order_id, "order_type": "buy"})
else:
log.warning(
f"Insufficient balance ({'%.8f' % btc.balance}) for {conf.name} ({conf.buy_amount} units)"
)
# place a buy order
def randomize(random_size_value):
randomized = float(
"%.4f" % random.uniform((-random_size_value), (random_size_value))
)
return randomized
def sell(conf, pair_market):
log.warning("Checking to place a sell order")
# place a sell order
if pair_market.ask <= conf.min_sell_price:
log.warning("Market price too low to sell now")
elif conf.sell_amount <= 0:
log.warning(f"Not configured to sell (sell set to {conf.sell_amount})")
else:
altcoin = pick_currency(balances, conf.name)
random_value = randomize(conf.random_size)
sell_value = conf.sell_amount + random_value
final_price = pair_market.ask - conf.price_adjustment
if altcoin.balance <= conf.min_stash:
log.warning("Minimum stash reached, will not create new sell orders")
elif altcoin.balance >= sell_value:
# sell order
req = {
"amount": "%.4f" % sell_value,
"market_id": conf.market_id,
"price": "%.8f" % final_price,
}
result = api.post(
"https://api.qtrade.io/v1/user/sell_limit", json=req
).json()
log.warning(result)
order_id = result["data"]["order"]["id"]
log.warning(f"Placed sell order {order_id}")
conf.orders_placed.append({"id": order_id, "order_type": "sell"})
else:
log.warning(
f"Insufficient balance ({'%.8f' % altcoin.balance}) for {conf.name} ({conf.buy_amount} units)"
)
# place a sell order
def pick_longevity_from_type(order_type, conf):
if order_type == "buy_limit":
return conf.buy_longevity
elif order_type == "sell_limit":
return conf.sell_longevity
else:
return None
def loop_pair_orders(conf, pair_orders):
# go through orders
for order in pair_orders.open_orders:
if order["market_id"] == conf.market_id:
# log.warning(order["created_at"])
order_id = int(order["id"])
order_type = order["order_type"]
age_of_order = age(order["created_at"])
longevity = pick_longevity_from_type(order_type, conf)
if age_of_order > longevity:
log.warning(
f"Removing old {order_type} order {order_id}, ({age_of_order}/{longevity}) seconds old"
)
req = {"id": order_id}
result = api.post(
"https://api.qtrade.io/v1/user/cancel_order", json=dict(req)
)
log.warning(result)
for entry in conf.orders_placed:
if entry["id"] == order_id:
conf.orders_placed.remove(entry)
else:
log.warning(
f"{order_type} order {order_id} retained, {age_of_order}/{longevity} seconds old"
)
# go through orders
log.warning(f"Total {conf.name} orders: {conf.orders_placed}")
log.warning(f"Total number of {conf.name} orders: {conf.count_orders()}")
def market_stats(conf, pair_market):
log.warning(f"base_balance {pair_orders.base_balance}")
log.warning(f"closed_orders {pair_orders.closed_orders[:10]}")
log.warning(f"market_balance {pair_orders.market_balance}")
log.warning(f"open_orders {pair_orders.open_orders}")
log.warning(
f"api last refresh: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(conf.last_refreshed))}"
)
log.warning(f"spread: {'%.8f' % pair_market.spread}")
log.warning(f"ask: {'%.8f' % pair_market.ask}")
log.warning(f"bid: {'%.8f' % pair_market.bid}")
log.warning(f"day_avg_price: {'%.8f' % pair_market.day_avg_price}")
log.warning(f"day_change: {'%.8f' % pair_market.day_change}")
log.warning(f"day_high: {'%.8f' % pair_market.day_high}")
log.warning(f"day_low: {'%.8f' % pair_market.day_low}")
log.warning(f"day_open: {'%.8f' % pair_market.day_open}")
log.warning(f"day_volume_base: {'%.8f' % pair_market.day_volume_base}")
log.warning(f"day_volume_market: {'%.8f' % pair_market.day_volume_market}")
log.warning(f"id: {pair_market.id}")
log.warning(f"id_hr: {pair_market.id_hr}")
log.warning(f"last_price: {'%.8f' % pair_market.last_price}")
log.warning(f"day_spread: {'%.8f' % pair_market.day_spread}")
log.warning(f"spread_pct: {'%.8f' % pair_market.spread_pct}")
if __name__ == "__main__":
# Create a session object to make repeated API calls easy!
api = requests.Session()
# Create an authenticator with your API key
api.auth_native = QtradeAPI(
"https://api.qtrade.io", key=load_credentials()
) # use in the future
api.auth = QtradeAuth(load_credentials())
# load currencies
active_currencies = []
with open("config.json") as confile:
confile_contents = json.loads(confile.read())
for currency in confile_contents:
log.warning(f"Loaded {currency}")
active_currencies.append(
Config(
name=currency["name"],
sell_amount=currency["sell_amount"],
buy_amount=currency["buy_amount"],
buy_longevity=currency["buy_longevity"],
sell_longevity=currency["sell_longevity"],
spread_pct_min=currency["spread_pct_min"],
price_adjustment=float(currency["price_adjustment"]),
max_buy_price=currency["max_buy_price"],
min_sell_price=currency["min_sell_price"],
max_stash=currency["max_stash"],
min_stash=currency["min_stash"],
random_size=currency["random_size"],
end_pause=currency["end_pause"],
)
)
# load currencies
while True:
try:
me = api.get("https://api.qtrade.io/v1/user/me").json()
log.warning(me)
for conf in active_currencies:
if DEMO and conf.name not in DEMO_MARKETS:
log.warning("Demo mode active, skipping market")
break
log.warning(f"Working on {conf.pair}")
conf.refresh_api()
# Make a call to API
# move data to object
pair_market = PairMarket(conf)
pair_orders = PairOrders()
market_stats(conf, pair_market)
loop_pair_orders(conf, pair_orders) # pair conf is different
if pair_market.spread_pct < conf.spread_pct_min:
log.warning(
f"No new orders, spread {pair_market.spread_pct} too small"
)
else:
balances = api.get("https://api.qtrade.io/v1/user/balances").json()
log.warning(balances)
sell(conf, pair_market)
buy(conf, pair_market)
log.warning(f"Taking a break for {conf.end_pause} seconds")
time.sleep(conf.end_pause)
except Exception as e:
log.warning(f"Exception {e}")
if DEBUG:
raise
log.warning("Loop ended, waiting for 60 seconds")
time.sleep(60)