-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
226 lines (185 loc) · 6.82 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# coding=utf-8
import ccxt
import time
import sys
import json
markets = {}
#------------------------------------------------------------------------------
# string coloring functions
def style (s, style): return 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_market_symbol_orderbook (market, symbol):
delay = int (market.rateLimit / 1000)
time.sleep (delay)
orderbook = market.fetch_order_book (symbol)
dump (market.id, 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_market_symbol_ticker (market, symbol):
delay = int (market.rateLimit / 1000)
time.sleep (delay)
ticker = market.fetch_ticker (symbol)
dump (market.id, 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_market_symbol (market, symbol):
test_market_symbol_ticker (market, symbol)
if market.id == 'coinmarketcap':
dump (green (market.fetchGlobal ()))
else:
test_market_symbol_orderbook (market, symbol)
def load_market (market):
products = market.load_products ()
def test_market (market):
dump (green (market.id))
delay = 2
keys = list (market.products.keys ())
#..........................................................................
# public API
symbol = keys[0]
symbols = [
'BTC/USD',
'BTC/CNY',
'BTC/ETH',
'ETH/BTC',
'BTC/JPY',
'LTC/BTC',
]
for s in symbols:
if s in keys:
symbol = s
break
if symbol.find ('.d') < 0:
test_market_symbol (market, symbol)
#..........................................................................
# private API
if (not hasattr (market, 'apiKey') or (len (market.apiKey) < 1)):
return
balance = market.fetch_balance ()
dump (balance)
# time.sleep (delay)
amount = 1
price = 0.0161
# marketBuy = market.create_market_buy_order (symbol, amount)
# print (marketBuy)
# time.sleep (delay)
# marketSell = market.create_market_sell_order (symbol, amount)
# print (marketSell)
# time.sleep (delay)
# limitBuy = market.create_limit_buy_order (symbol, amount, price)
# print (limitBuy)
# time.sleep (delay)
# limitSell = market.create_limit_sell_order (symbol, amount, price)
# print (limitSell)
# time.sleep (delay)
# let tryAllProxies = async function (market, proxies) {
# let currentProxy = 0
# let maxRetries = proxies.length
# for (let numRetries = 0; numRetries < maxRetries; numRetries++) {
# try {
# market.proxy = proxies[currentProxy]
# if ([ 'coinspot' ].indexOf (market.id) < 0) {
# await loadMarket (market)
# await testMarket (market)
# break;
# }
# } catch (e) {
# currentProxy = ++currentProxy % proxies.length
# if (e instanceof ccxt.DDoSProtectionError) {
# log.bright.yellow (market.id, '[DDoS Protection Error] ' + e.message)
# } else if (e instanceof ccxt.TimeoutError) {
# log.bright.yellow (market.id, '[Timeout Error] ' + e.message)
# } else if (e instanceof ccxt.AuthenticationError) {
# log.bright.yellow (market.id, '[Authentication Error] ' + e.message)
# } else if (e instanceof ccxt.MarketNotAvailableError) {
# log.bright.yellow (market.id, '[Market Not Available Error] ' + e.message)
# } else if (e instanceof ccxt.EndpointNotAvailableError) {
# log.bright.yellow (market.id, '[Endpoint Not Available Error] ' + e.message)
# } else {
# throw e;
# }
# }
# }
# }
#------------------------------------------------------------------------------
def try_all_proxies (market, proxies):
current_proxy = 0
max_retries = len (proxies)
for num_retries in range (0, max_retries):
try:
market.proxy = proxies[current_proxy]
current_proxy = (current_proxy + 1) % len (proxies)
load_market (market)
test_market (market)
break
except ccxt.DDoSProtectionError as e:
dump (yellow (type (e).__name__), e.args)
except ccxt.TimeoutError as e:
dump (yellow (type (e).__name__), str (e))
except ccxt.MarketNotAvailableError as e:
dump (yellow (type (e).__name__), e.args)
except ccxt.AuthenticationError as e:
dump (yellow (type (e).__name__), str (e))
#------------------------------------------------------------------------------
proxies = [
'',
'https://cors-anywhere.herokuapp.com/',
'https://crossorigin.me/',
# 'http://cors-proxy.htmldriven.com/?url=', # we don't want this for now
]
# instantiate all markets
for id in ccxt.markets:
market = getattr (ccxt, id)
markets[id] = market ({ 'verbose': True })
# load the api keys from config
with open ('./keys.json') as file:
config = json.load (file)
# set up api keys appropriately
tuples = list (ccxt.Market.keysort (config).items ())
for (id, params) in tuples:
options = list (params.items ())
for key in params:
setattr (markets[id], key, params[key])
# move gdax to sandbox
markets['gdax'].urls['api'] = 'https://api-public.sandbox.gdax.com'
id = None
try:
id = sys.argv[1]
except:
id = None
if id:
market = markets[id]
symbol = None
try:
symbol = sys.argv[2]
except:
symbol = None
if symbol:
load_market (market)
test_market_symbol (market, symbol)
else:
try_all_proxies (market, proxies)
else:
tuples = list (ccxt.Market.keysort (markets).items ())
for (id, params) in tuples:
market = markets[id]
try_all_proxies (market, proxies)