-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
209 lines (158 loc) · 6.11 KB
/
test.php
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
<?php
date_default_timezone_set ('UTC');
include 'build/ccxt.php';
date_default_timezone_set ('UTC');
function style ($s, $style) { return $style . $s . "\033[0m"; }
function green ($s) { return style ($s, "\033[92m"); }
function blue ($s) { return style ($s, "\033[94m"); }
function yellow ($s) { return style ($s, "\033[93m"); }
function red ($s) { return style ($s, "\033[91m"); }
function pink ($s) { return style ($s, "\033[95m"); }
function bold ($s) { return style ($s, "\033[1m"); }
function underline ($s) { return style ($s, "\033[4m"); }
function dump ($s) { echo implode (' ', func_get_args ()) . "\n"; }
$exchanges = null;
// $shortopts = '';
// $longopts = array (
// "nonce::", // '::' means optional, ':' means required
// );
// $options = getopt ($shortopts, $longopts);
// var_dump ($options);
// exit ();
//-----------------------------------------------------------------------------
foreach (\ccxt\Exchange::$exchanges as $id) {
$exchange = '\\ccxt\\' . $id;
$exchanges[$id] = new $exchange (array ('verbose' => false));
}
$config = json_decode (file_get_contents ('./keys.json'), true);
foreach ($config as $id => $params)
foreach ($params as $key => $value)
if (array_key_exists ($id, $exchanges))
$exchanges[$id]->$key = $value;
$exchanges['gdax']->urls['api'] = 'https://api-public.sandbox.gdax.com';
$exchanges['anxpro']->proxy = 'https://cors-anywhere.herokuapp.com/';
function test_exchange_symbol_ticker ($exchange, $symbol) {
dump (green ($exchange->id), green ($symbol), 'fetching ticker...');
$ticker = $exchange->fetch_ticker ($symbol);
dump (green ($exchange->id), green ($symbol), 'ticker:', implode (' ', array (
$ticker['datetime'],
'high: ' . $ticker['high'],
'low: ' . $ticker['low'],
'bid: ' . $ticker['bid'],
'ask: ' . $ticker['ask'],
'volume: ' . $ticker['quoteVolume'])));
}
function test_exchange_symbol_orderbook ($exchange, $symbol) {
dump (green ($exchange->id), green ($symbol), 'fetching order book...');
$orderbook = $exchange->fetch_order_book ($symbol);
dump (green ($exchange->id), green ($symbol), 'order book:', implode (' ', array (
$orderbook['datetime'],
'bid: ' . @$orderbook['bids'][0][0],
'bidVolume: ' . @$orderbook['bids'][0][1],
'ask: ' . @$orderbook['asks'][0][0],
'askVolume: ' . @$orderbook['asks'][0][1])));
}
function test_exchange_symbol ($exchange, $symbol) {
$delay = $exchange->rateLimit * 1000;
usleep ($delay);
test_exchange_symbol_ticker ($exchange, $symbol);
usleep ($delay);
if ($exchange->id == 'coinmarketcap')
dump (var_export ($exchange->fetchGlobal ()));
else
test_exchange_symbol_orderbook ($exchange, $symbol);
}
function load_exchange ($exchange) {
$markets = $exchange->load_markets ();
$symbols = array_keys ($markets);
dump (green ($exchange->id), green (count ($symbols)), 'symbols:', implode (', ', $symbols));
}
function try_all_proxies ($exchange, $proxies) {
$current_proxy = 0;
$max_retries = count ($proxies);
// a special case for ccex
if ($exchange->id == 'ccex')
$currentProxy = 1;
for ($i = 0; $i < $max_retries; $i++) {
try {
$exchange->proxy = $proxies[$current_proxy];
$current_proxy = (++$current_proxy) % count ($proxies);
load_exchange ($exchange);
test_exchange ($exchange);
break;
} catch (\ccxt\RequestTimeout $e) {
dump (yellow ('[Timeout Error] ' . $e->getMessage () . ' (ignoring)'));
} catch (\ccxt\DDoSProtection $e) {
dump (yellow ('[DDoS Protection Error] ' . $e->getMessage () . ' (ignoring)'));
} catch (\ccxt\AuthenticationError $e) {
dump (yellow ('[Authentication Error] ' . $e->getMessage () . ' (ignoring)'));
} catch (\ccxt\ExchangeNotAvailable $e) {
dump (yellow ('[Exchange Not Available] ' . $e->getMessage () . ' (ignoring)'));
} catch (\ccxt\NotSupported $e) {
dump (yellow ('[Not Supported] ' . $e->getMessage () . ' (ignoring)'));
} catch (\ccxt\NetworkError $e) {
dump (yellow ('[Network Error] ' . $e->getMessage () . ' (ignoring)'));
} catch (\ccxt\ExchangeError $e) {
dump (yellow ('[Exchange Error] ' . $e->getMessage () . ' (ignoring)'));
} catch (Exception $e) {
dump (red ('[Error] ' . $e->getMessage ()));
}
}
}
function test_exchange ($exchange) {
$delay = $exchange->rateLimit * 1000;
$symbol = $exchange->symbols[0];
$symbols = array (
'BTC/USD',
'BTC/CNY',
'BTC/EUR',
'BTC/ETH',
'ETH/BTC',
'BTC/JPY',
'LTC/BTC',
);
foreach ($symbols as $s) {
if (in_array ($s, $exchange->symbols)) {
$symbol = $s;
break;
}
}
if (strpos ($symbol, '.d') === false) {
dump (green ('SYMBOL:'), green ($symbol));
test_exchange_symbol ($exchange, $symbol);
}
// usleep ($delay);
// $trades = $exchange->fetch_trades (array_keys ($markets)[0]);
// var_dump ($trades);
if ((!$exchange->apiKey) or (strlen ($exchange->apiKey) < 1))
return;
usleep ($delay);
$balance = $exchange->fetch_balance ();
print_r ($balance);
}
$proxies = array (
'',
'https://cors-anywhere.herokuapp.com/',
'https://crossorigin.me/',
// 'http://cors-proxy.htmldriven.com/?url=', // we don't want this for now
);
if (count ($argv) > 1) {
if ($exchanges[$argv[1]]) {
$id = $argv[1];
$exchange = $exchanges[$id];
dump (green ('EXCHANGE:'), green ($exchange->id));
if (count ($argv) > 2) {
load_exchange ($exchange);
test_exchange_symbol ($exchange, $argv[2]);
} else {
try_all_proxies ($exchange, $proxies);
}
} else {
echo $argv[1] + " not found.\n";
}
} else {
foreach ($exchanges as $id => $exchange) {
try_all_proxies ($exchange, $proxies);
}
}
?>