-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathbinance-api-single.php
More file actions
188 lines (186 loc) · 6.86 KB
/
binance-api-single.php
File metadata and controls
188 lines (186 loc) · 6.86 KB
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
<?php // https://github.com/jaggedsoft/php-binance-api
// Old version intended for use without composer. Supports PHP 5.6
// This project is no longer maintained by me. I am only maintaining the future branch which requires composer, and the node binance api.
// Credit for updates goes to David Jones: https://github.com/dxjones
class Binance {
public $btc_value = 0.00;
protected $base = "https://api.binance.com/api/", $api_key, $api_secret;
public function __construct($api_key, $api_secret) {
$this->api_key = $api_key;
$this->api_secret = $api_secret;
}
public function ping() {
return $this->request("v1/ping");
}
public function time() {
return $this->request("v1/time");
}
public function exchangeInfo() {
return $this->request("v1/exchangeInfo");
}
public function buy_test($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order_test("BUY", $symbol, $quantity, $price, $type, $flags);
}
public function sell_test($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order_test("SELL", $symbol, $quantity, $price, $type, $flags);
}
public function buy($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order("BUY", $symbol, $quantity, $price, $type, $flags);
}
public function sell($symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
return $this->order("SELL", $symbol, $quantity, $price, $type, $flags);
}
public function cancel($symbol, $orderid) {
return $this->signedRequest("v3/order",["symbol"=>$symbol, "orderId"=>$orderid], "DELETE");
}
public function orderStatus($symbol, $orderid) {
return $this->signedRequest("v3/order",["symbol"=>$symbol, "orderId"=>$orderid]);
}
public function openOrders($symbol) {
return $this->signedRequest("v3/openOrders",["symbol"=>$symbol]);
}
public function orders($symbol, $limit = 500) {
return $this->signedRequest("v3/allOrders",["symbol"=>$symbol, "limit"=>$limit]);
}
public function trades($symbol) {
return $this->signedRequest("v3/myTrades",["symbol"=>$symbol]);
}
public function prices() {
return $this->priceData($this->request("v1/ticker/allPrices"));
}
public function bookPrices() {
return $this->bookPriceData($this->request("v1/ticker/allBookTickers"));
}
public function account() {
return $this->signedRequest("v3/account");
}
public function depth($symbol) {
return $this->request("v1/depth",["symbol"=>$symbol]);
}
public function balances($priceData = false) {
return $this->balanceData($this->signedRequest("v3/account"),$priceData);
}
public function prevDay($symbol) {
return $this->request("v1/ticker/24hr", ["symbol"=>$symbol]);
}
private function request($url, $params = [], $method = "GET") {
$opt = [
"http" => [
"method" => $method,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\n"
]
];
$context = stream_context_create($opt);
$query = http_build_query($params, '', '&');
return json_decode(file_get_contents($this->base.$url.'?'.$query, false, $context), true);
}
private function signedRequest($url, $params = [], $method = "GET") {
$params['timestamp'] = number_format(microtime(true)*1000,0,'.','');
$query = http_build_query($params, '', '&');
$signature = hash_hmac('sha256', $query, $this->api_secret);
$opt = [
"http" => [
"method" => $method,
"ignore_errors" => true,
"header" => "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)\r\nX-MBX-APIKEY: {$this->api_key}\r\nContent-type: application/x-www-form-urlencoded\r\n"
]
];
if ( $method == 'GET' ) {
// parameters encoded as query string in URL
$endpoint = "{$this->base}{$url}?{$query}&signature={$signature}";
} else {
// parameters encoded as POST data (in $context)
$endpoint = "{$this->base}{$url}";
$postdata = "{$query}&signature={$signature}";
$opt['http']['content'] = $postdata;
}
$context = stream_context_create($opt);
return json_decode(file_get_contents($endpoint, false, $context), true);
}
private function order_test($side, $symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
$opt = [
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"quantity" => $quantity,
"recvWindow" => 60000
];
if ( $type == "LIMIT" ) {
$opt["price"] = $price;
$opt["timeInForce"] = "GTC";
}
// allow additional options passed through $flags
if ( isset($flags['recvWindow']) ) $opt['recvWindow'] = $flags['recvWindow'];
if ( isset($flags['timeInForce']) ) $opt['timeInForce'] = $flags['timeInForce'];
if ( isset($flags['stopPrice']) ) $opt['stopPrice'] = $flags['stopPrice'];
if ( isset($flags['icebergQty']) ) $opt['icebergQty'] = $flags['icebergQty'];
return $this->signedRequest("v3/order/test", $opt, "POST");
}
private function order($side, $symbol, $quantity, $price, $type = "LIMIT", $flags = []) {
$opt = [
"symbol" => $symbol,
"side" => $side,
"type" => $type,
"quantity" => $quantity,
"recvWindow" => 60000
];
if ( $type == "LIMIT" ) {
$opt["price"] = $price;
$opt["timeInForce"] = "GTC";
}
// allow additional options passed through $flags
if ( isset($flags['recvWindow']) ) $opt["recvWindow"] = $flags['recvWindow'];
if ( isset($flags['timeInForce']) ) $opt["timeInForce"] = $flags['timeInForce'];
if ( isset($flags['stopPrice']) ) $opt['stopPrice'] = $flags['stopPrice'];
if ( isset($flags['icebergQty']) ) $opt['icebergQty'] = $flags['icebergQty'];
return $this->signedRequest("v3/order", $opt, "POST");
}
//1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
public function candlesticks($symbol, $interval = "5m") {
return $this->request("v1/klines",["symbol"=>$symbol, "interval"=>$interval]);
}
private function balanceData($array, $priceData = false) {
if ( $priceData ) $btc_value = 0.00;
$balances = [];
foreach ( $array['balances'] as $obj ) {
$asset = $obj['asset'];
$balances[$asset] = ["available"=>$obj['free'], "onOrder"=>$obj['locked'], "btcValue"=>0.00000000];
if ( $priceData ) {
if ( $obj['free'] < 0.00000001 ) continue;
if ( $asset == 'BTC' ) {
$balances[$asset]['btcValue'] = $obj['free'];
$btc_value+= $obj['free'];
continue;
}
$btcValue = number_format($obj['free'] * $priceData[$asset.'BTC'],8,'.','');
$balances[$asset]['btcValue'] = $btcValue;
$btc_value+= $btcValue;
}
}
if ( $priceData ) {
uasort($balances, function($a, $b) { return $a['btcValue'] < $b['btcValue']; });
$this->btc_value = $btc_value;
}
return $balances;
}
private function bookPriceData($array) {
$bookprices = [];
foreach ( $array as $obj ) {
$bookprices[$obj['symbol']] = [
"bid"=>$obj['bidPrice'],
"bids"=>$obj['bidQty'],
"ask"=>$obj['askPrice'],
"asks"=>$obj['askQty']
];
}
return $bookprices;
}
private function priceData($array) {
$prices = [];
foreach ( $array as $obj ) {
$prices[$obj['symbol']] = $obj['price'];
}
return $prices;
}
}
// https://www.binance.com/restapipub.html