forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtcx.php
169 lines (157 loc) · 6.44 KB
/
btcx.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
<?php
namespace ccxt;
class btcx extends Exchange {
public function describe () {
return array_replace_recursive (parent::describe (), array (
'id' => 'btcx',
'name' => 'BTCX',
'countries' => array ( 'IS', 'US', 'EU' ),
'rateLimit' => 1500, // support in english is very poor, unable to tell rate limits
'version' => 'v1',
'hasCORS' => false,
'urls' => array (
'logo' => 'https://user-images.githubusercontent.com/1294454/27766385-9fdcc98c-5ed6-11e7-8f14-66d5e5cd47e6.jpg',
'api' => 'https://btc-x.is/api',
'www' => 'https://btc-x.is',
'doc' => 'https://btc-x.is/custom/api-document.html',
),
'api' => array (
'public' => array (
'get' => array (
'depth/{id}/{limit}',
'ticker/{id}',
'trade/{id}/{limit}',
),
),
'private' => array (
'post' => array (
'balance',
'cancel',
'history',
'order',
'redeem',
'trade',
'withdraw',
),
),
),
'markets' => array (
'BTC/USD' => array ( 'id' => 'btc/usd', 'symbol' => 'BTC/USD', 'base' => 'BTC', 'quote' => 'USD' ),
'BTC/EUR' => array ( 'id' => 'btc/eur', 'symbol' => 'BTC/EUR', 'base' => 'BTC', 'quote' => 'EUR' ),
),
));
}
public function fetch_balance ($params = array ()) {
$balances = $this->privatePostBalance ();
$result = array ( 'info' => $balances );
$currencies = is_array ($balances) ? array_keys ($balances) : array ();
for ($c = 0; $c < count ($currencies); $c++) {
$currency = $currencies[$c];
$uppercase = strtoupper ($currency);
$account = array (
'free' => $balances[$currency],
'used' => 0.0,
'total' => $balances[$currency],
);
$result[$uppercase] = $account;
}
return $this->parse_balance($result);
}
public function fetch_order_book ($symbol, $params = array ()) {
$orderbook = $this->publicGetDepthIdLimit (array_merge (array (
'id' => $this->market_id($symbol),
'limit' => 1000,
), $params));
return $this->parse_order_book($orderbook, null, 'bids', 'asks', 'price', 'amount');
}
public function fetch_ticker ($symbol, $params = array ()) {
$ticker = $this->publicGetTickerId (array_merge (array (
'id' => $this->market_id($symbol),
), $params));
$timestamp = $ticker['time'] * 1000;
return array (
'symbol' => $symbol,
'timestamp' => $timestamp,
'datetime' => $this->iso8601 ($timestamp),
'high' => floatval ($ticker['high']),
'low' => floatval ($ticker['low']),
'bid' => floatval ($ticker['sell']),
'ask' => floatval ($ticker['buy']),
'vwap' => null,
'open' => null,
'close' => null,
'first' => null,
'last' => floatval ($ticker['last']),
'change' => null,
'percentage' => null,
'average' => null,
'baseVolume' => null,
'quoteVolume' => floatval ($ticker['volume']),
'info' => $ticker,
);
}
public function parse_trade ($trade, $market) {
$timestamp = intval ($trade['date']) * 1000;
$side = ($trade['type'] == 'ask') ? 'sell' : 'buy';
return array (
'id' => $trade['id'],
'info' => $trade,
'timestamp' => $timestamp,
'datetime' => $this->iso8601 ($timestamp),
'symbol' => $market['symbol'],
'type' => null,
'side' => $side,
'price' => $trade['price'],
'amount' => $trade['amount'],
);
}
public function fetch_trades ($symbol, $since = null, $limit = null, $params = array ()) {
$market = $this->market ($symbol);
$response = $this->publicGetTradeIdLimit (array_merge (array (
'id' => $market['id'],
'limit' => 1000,
), $params));
return $this->parse_trades($response, $market, $since, $limit);
}
public function create_order ($symbol, $type, $side, $amount, $price = null, $params = array ()) {
$response = $this->privatePostTrade (array_merge (array (
'type' => strtoupper ($side),
'market' => $this->market_id($symbol),
'amount' => $amount,
'price' => $price,
), $params));
return array (
'info' => $response,
'id' => $response['order']['id'],
);
}
public function cancel_order ($id, $symbol = null, $params = array ()) {
return $this->privatePostCancel (array ( 'order' => $id ));
}
public function sign ($path, $api = 'public', $method = 'GET', $params = array (), $headers = null, $body = null) {
$url = $this->urls['api'] . '/' . $this->version . '/';
if ($api == 'public') {
$url .= $this->implode_params($path, $params);
} else {
$this->check_required_credentials();
$nonce = $this->nonce ();
$url .= $api;
$body = $this->urlencode (array_merge (array (
'Method' => strtoupper ($path),
'Nonce' => $nonce,
), $params));
$headers = array (
'Content-Type' => 'application/x-www-form-urlencoded',
'Key' => $this->apiKey,
'Signature' => $this->hmac ($this->encode ($body), $this->encode ($this->secret), 'sha512'),
);
}
return array ( 'url' => $url, 'method' => $method, 'body' => $body, 'headers' => $headers );
}
public function request ($path, $api = 'public', $method = 'GET', $params = array (), $headers = null, $body = null) {
$response = $this->fetch2 ($path, $api, $method, $params, $headers, $body);
if (is_array ($response) && array_key_exists ('error', $response))
throw new ExchangeError ($this->id . ' ' . $this->json ($response));
return $response;
}
}