-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
207 lines (166 loc) · 6.18 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
# coding=utf-8
import ccxt
import time
import sys
import json
import argparse
class Argv (object):
pass
argv = Argv ()
parser = argparse.ArgumentParser ()
parser.add_argument ('--nonce', type = int, help = 'integer')
parser.add_argument ('market', type = str, help = 'market id in lowercase', nargs = '?')
parser.add_argument ('symbol', type = str, help = 'symbol in uppercase', nargs = '?')
parser.parse_args (namespace = argv)
markets = {}
#------------------------------------------------------------------------------
# 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_market_symbol_orderbook (market, symbol):
delay = int (market.rateLimit / 1000)
time.sleep (delay)
dump (green (market.id), green (symbol), 'fetching order book...')
orderbook = market.fetch_order_book (symbol)
dump (green (market.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_market_symbol_ticker (market, symbol):
delay = int (market.rateLimit / 1000)
time.sleep (delay)
dump (green (market.id), green (symbol), 'fetching ticker...')
ticker = market.fetch_ticker (symbol)
dump (green (market.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_market_symbol (market, symbol):
dump (green ('SYMBOL: ' + 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: ' + market.id))
delay = 2
keys = list (market.products.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_market_symbol (market, symbol)
#..........................................................................
# private API
if (not hasattr (market, 'apiKey') or (len (market.apiKey) < 1)):
return
balance = market.fetch_balance ()
dump (green (market.id), 'balance', 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)
#------------------------------------------------------------------------------
def try_all_proxies (market, proxies):
current_proxy = 0
max_retries = len (proxies)
# a special case for ccex
if market.id == 'ccex':
currentProxy = 1
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.MarketError as e:
dump (yellow (type (e).__name__), e.args)
except ccxt.AuthenticationError as e:
dump (yellow (type (e).__name__), str (e))
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)
#------------------------------------------------------------------------------
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 markets
for id in ccxt.markets:
market = getattr (ccxt, id)
markets[id] = market ({ 'verbose': False })
# 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'
if argv.market:
market = markets[argv.market]
symbol = argv.symbol
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)