-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoloniex_api.php
258 lines (226 loc) · 9.4 KB
/
poloniex_api.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
// FINAL TESTED CODE - Created by Compcentral
// NOTE: currency pairs are reverse of what most exchanges use...
// For instance, instead of XPM_BTC, use BTC_XPM
// [success] => 1
// [response] => 1NnPUGsWj6MuSVpTaLiUh7nsH84f5AszkX
// )
class poloniex
{
protected $api_key;
protected $api_secret;
protected $trading_url = "https://poloniex.com/tradingApi";
protected $public_url = "https://poloniex.com/public";
public function __construct($api_key, $api_secret)
{
$this->api_key = $api_key;
$this->api_secret = $api_secret;
}
private function query(array $req = array())
{
// API settings
$key = $this->api_key;
$secret = $this->api_secret;
// generate a nonce to avoid problems with 32bit systems
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
// generate the POST data string
$post_data = http_build_query($req, '', '&');
$sign = hash_hmac('sha512', $post_data, $secret);
// generate the extra headers
$headers = array(
'Key: '.$key,
'Sign: '.$sign,
);
// curl handle (initialize if required)
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch,
CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; Poloniex PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
);
}
curl_setopt($ch, CURLOPT_URL, $this->trading_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// run the query
$res = curl_exec($ch);
if ($res === false) {
throw new Exception('Curl error: '.curl_error($ch));
}
//echo $res;
$dec = json_decode($res, true);
if (!$dec) {
//throw new Exception('Invalid data: '.$res);
return false;
} else {
return $dec;
}
}
protected function retrieveJSON($URL)
{
$opts = array('http' =>
array(
'method' => 'GET',
'timeout' => 10
)
);
$context = stream_context_create($opts);
$feed = file_get_contents($URL, false, $context);
$json = json_decode($feed, true);
return $json;
}
public function get_balances()
{
return $this->query(
array(
'command' => 'returnBalances'
)
);
}
public function return_currencies()
{
return $this->retrieveJSON("$this->public_url?command=returnCurrencies");
}
public function generate_address($currency)
{
return $this->query(
array(
'command' => 'generateNewAddress',
'currency' => strtoupper($currency)
)
);
}
public function get_open_orders($pair)
{
return $this->query(
array(
'command' => 'returnOpenOrders',
'currencyPair' => strtoupper($pair)
)
);
}
public function get_my_trade_history($pair)
{
return $this->query(
array(
'command' => 'returnTradeHistory',
'currencyPair' => strtoupper($pair)
)
);
}
public function buy($pair, $rate, $amount)
{
return $this->query(
array(
'command' => 'buy',
'currencyPair' => strtoupper($pair),
'rate' => $rate,
'amount' => $amount
)
);
}
public function sell($pair, $rate, $amount)
{
return $this->query(
array(
'command' => 'sell',
'currencyPair' => strtoupper($pair),
'rate' => $rate,
'amount' => $amount
)
);
}
public function cancel_order($pair, $order_number)
{
return $this->query(
array(
'command' => 'cancelOrder',
'currencyPair' => strtoupper($pair),
'orderNumber' => $order_number
)
);
}
public function withdraw($currency, $amount, $address)
{
return $this->query(
array(
'command' => 'withdraw',
'currency' => strtoupper($currency),
'amount' => $amount,
'address' => $address
)
);
}
public function get_trade_history($pair)
{
$trades = $this->retrieveJSON($this->public_url.'?command=returnTradeHistory¤cyPair='.strtoupper($pair));
return $trades;
}
public function get_order_book($pair)
{
$orders = $this->retrieveJSON($this->public_url.'?command=returnOrderBook¤cyPair='.strtoupper($pair));
return $orders;
}
public function get_volume()
{
$volume = $this->retrieveJSON($this->public_url.'?command=return24hVolume');
return $volume;
}
public function get_ticker($pair = "ALL")
{
$pair = strtoupper($pair);
$prices = $this->retrieveJSON($this->public_url.'?command=returnTicker');
if ($pair == "ALL") {
return $prices;
} else {
$pair = strtoupper($pair);
if (isset($prices[$pair])) {
return $prices[$pair];
} else {
return array();
}
}
}
public function get_trading_pairs()
{
$tickers = $this->retrieveJSON($this->public_url.'?command=returnTicker');
return array_keys($tickers);
}
public function get_total_btc_balance()
{
$balances = $this->get_balances();
$prices = $this->get_ticker();
$tot_btc = 0;
foreach ($balances as $coin => $amount) {
$pair = "BTC_".strtoupper($coin);
// convert coin balances to btc value
if ($amount > 0) {
if ($coin != "BTC") {
$tot_btc += $amount * $prices[$pair];
} else {
$tot_btc += $amount;
}
}
// process open orders as well
if ($coin != "BTC") {
$open_orders = $this->get_open_orders($pair);
foreach ($open_orders as $order) {
if ($order['type'] == 'buy') {
$tot_btc += $order['total'];
} elseif ($order['type'] == 'sell') {
$tot_btc += $order['amount'] * $prices[$pair];
}
}
}
}
return $tot_btc;
}
}
$api_sec = "LALALALALA";
$api_key = "LALALALALAY";
$poloniex_api = new poloniex($api_key, $api_sec);