forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
212 lines (169 loc) · 6.15 KB
/
test.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
# -*- coding: utf-8 -*-
import ccxt
import time
import json
import argparse
class Argv (object):
pass
argv = Argv()
parser = argparse.ArgumentParser()
parser.add_argument('--nonce', type=int, help='integer')
parser.add_argument('exchange', type=str, help='exchange id in lowercase', nargs='?')
parser.add_argument('symbol', type=str, help='symbol in uppercase', nargs='?')
parser.parse_args(namespace=argv)
exchanges = {}
# ------------------------------------------------------------------------------
# string coloring functions
def style(s, style): return str(s) # style + str (s) + '\033[0m'
def green(s): return style(s, '\033[92m')
def blue(s): return style(s, '\033[94m')
def yellow(s): return style(s, '\033[93m')
def red(s): return style(s, '\033[91m')
def pink(s): return style(s, '\033[95m')
def bold(s): return style(s, '\033[1m')
def underline(s): return style(s, '\033[4m')
# print a colored string
def dump(*args):
print(' '.join([str(arg) for arg in args]))
# ------------------------------------------------------------------------------
def test_exchange_symbol_orderbook(exchange, symbol):
delay = int(exchange.rateLimit / 1000)
time.sleep(delay)
dump(green(exchange.id), green(symbol), 'fetching order book...')
orderbook = exchange.fetch_order_book(symbol)
dump(
green(exchange.id),
green(symbol),
'order book',
orderbook['datetime'],
'bid: ' + str(orderbook['bids'][0][0] if len(orderbook['bids']) else 'N/A'),
'bidVolume: ' + str(orderbook['bids'][0][1] if len(orderbook['bids']) else 'N/A'),
'ask: ' + str(orderbook['asks'][0][0] if len(orderbook['asks']) else 'N/A'),
'askVolume: ' + str(orderbook['asks'][0][1] if len(orderbook['asks']) else 'N/A'))
def test_exchange_symbol_ticker(exchange, symbol):
delay = int(exchange.rateLimit / 1000)
time.sleep(delay)
dump(green(exchange.id), green(symbol), 'fetching ticker...')
ticker = exchange.fetch_ticker(symbol)
dump(
green(exchange.id),
green(symbol),
'ticker',
ticker['datetime'],
'high: ' + str(ticker['high']),
'low: ' + str(ticker['low']),
'bid: ' + str(ticker['bid']),
'ask: ' + str(ticker['ask']),
'volume: ' + str(ticker['quoteVolume']))
def test_exchange_symbol(exchange, symbol):
dump(green('SYMBOL: ' + symbol))
test_exchange_symbol_ticker(exchange, symbol)
if exchange.id == 'coinmarketcap':
dump(green(exchange.fetchGlobal()))
else:
test_exchange_symbol_orderbook(exchange, symbol)
def load_exchange(exchange):
exchange.load_markets()
def test_exchange(exchange):
dump(green('EXCHANGE: ' + exchange.id))
# delay = 2
keys = list(exchange.markets.keys())
# ..........................................................................
# public API
symbol = keys[0]
symbols = [
'BTC/USD',
'BTC/CNY',
'BTC/EUR',
'BTC/ETH',
'ETH/BTC',
'BTC/JPY',
'LTC/BTC',
'USD/SLL',
]
for s in symbols:
if s in keys:
symbol = s
break
if symbol.find('.d') < 0:
test_exchange_symbol(exchange, symbol)
# ..........................................................................
# private API
if (not hasattr(exchange, 'apiKey') or (len(exchange.apiKey) < 1)):
return
balance = exchange.fetch_balance()
dump(green(exchange.id), 'balance', balance)
# time.sleep(delay)
# amount = 1
# price = 0.0161
# marketBuy = exchange.create_market_buy_order(symbol, amount)
# print(marketBuy)
# time.sleep(delay)
# marketSell = exchange.create_market_sell_order(symbol, amount)
# print(marketSell)
# time.sleep(delay)
# limitBuy = exchange.create_limit_buy_order(symbol, amount, price)
# print(limitBuy)
# time.sleep(delay)
# limitSell = exchange.create_limit_sell_order(symbol, amount, price)
# print(limitSell)
# time.sleep(delay)
# ------------------------------------------------------------------------------
def try_all_proxies(exchange, proxies):
current_proxy = 0
max_retries = len(proxies)
# a special case for ccex
if exchange.id == 'ccex':
current_proxy = 1
for num_retries in range(0, max_retries):
try:
exchange.proxy = proxies[current_proxy]
current_proxy = (current_proxy + 1) % len(proxies)
load_exchange(exchange)
test_exchange(exchange)
break
except ccxt.ExchangeError as e:
dump(yellow(type(e).__name__), e.args)
except ccxt.AuthenticationError as e:
dump(yellow(type(e).__name__), str(e))
except ccxt.DDoSProtection as e:
dump(yellow(type(e).__name__), e.args)
except ccxt.RequestTimeout as e:
dump(yellow(type(e).__name__), str(e))
except ccxt.ExchangeNotAvailable as e:
dump(yellow(type(e).__name__), e.args)
# ------------------------------------------------------------------------------
proxies = [
'',
'https://cors-anywhere.herokuapp.com/',
'https://crossorigin.me/',
# 'http://cors-proxy.htmldriven.com/?url=', # we don't want this for now
]
# load the api keys from config
with open('./keys.json') as file:
config = json.load(file)
# instantiate all exchanges
for id in ccxt.exchanges:
exchange = getattr(ccxt, id)
exchanges[id] = exchange({'verbose': False})
# set up api keys appropriately
tuples = list(ccxt.Exchange.keysort(config).items())
for (id, params) in tuples:
options = list(params.items())
for key in params:
setattr(exchanges[id], key, params[key])
# move gdax to sandbox
exchanges['gdax'].urls['api'] = 'https://api-public.sandbox.gdax.com'
if argv.exchange:
exchange = exchanges[argv.exchange]
symbol = argv.symbol
if symbol:
load_exchange(exchange)
test_exchange_symbol(exchange, symbol)
else:
try_all_proxies(exchange, proxies)
else:
tuples = list(ccxt.Exchange.keysort(exchanges).items())
for (id, params) in tuples:
exchange = exchanges[id]
try_all_proxies(exchange, proxies)