-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbtceAPI.php
More file actions
172 lines (150 loc) · 4.76 KB
/
btceAPI.php
File metadata and controls
172 lines (150 loc) · 4.76 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
<?php
include 'exchange.php';
class Btc_e extends Exchange
{
private $p_endpoint = ''; # public endpoint
private $t_endpoint = 'https://btc-e.com/tapi'; # trade endpoint
private $apiKey = '';
private $secretKey = '';
public function __construct($key, $secret)
{
$this->apiKey = $key;
$this->secretKey = $secret;
//test request to see if keys are valid
$rights = $this->permissions;
if(!$rights) return false;
if(isset($rights->error)) return false;
if($rights->info != 1 || $rights->trade != 1) return false;
}
public function setSymbols()
{
$url = 'https://btc-e.com/api/3/info';
$json = json_decode(file_get_contents($url));
foreach($json->pairs as $pair=>$info)
{
$this->symbols[$pair] = $info;
}
return;
}
public function updatePrices()
{
$url = 'https://btc-e.com/api/3/ticker/';
foreach($this->symbols as $pair=>$info)
{
$ticker = file_get_contents($url . $pair);
$obj = json_decode($ticker);
$this->prices[$pair]['high'] = $obj->$pair->high;
$this->prices[$pair]['low'] = $obj->$pair->low;
$this->prices[$pair]['bid'] = $obj->$pair->buy;
$this->prices[$pair]['ask'] = $obj->$pair->sell;
$this->prices[$pair]['last'] = $obj->$pair->last;
$this->prices[$pair]['timestamp'] = $obj->$pair->updated;
}
print_r($this->prices);
}
public function apiCall($payload, $url)
{
$payload['nonce'] = time();
$request = http_build_query($payload, '', '&');
$signature = hash_hmac("sha512", $request, $this->secretKey);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Key: " . $this->apiKey,
'Sign: ' . $signature
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
echo curl_error($curl);
curl_close($curl);
return $response;
}
public function placeOrder($symbol, $amount, $price, $side)
{
$request = array(
'pair'=>$symbol,
'amount'=>$amount,
'rate'=>$price,
'type'=>$side,
'method'=>'Trade'
);
$url = $this->t_endpoint . '/trade';
return $this->apiCall($request, $url);
}
public function cancelOrder($id)
{
$request = array(
'method'=>'CancelOrder',
'order_id'=>$id
);
$url = $this->t_endpoint . '/CancelOrder';
return $this->apiCall($request, $url);
}
public function cancelAll()
{
$json = json_decode($this->activeOrders());
if($json->success == 0 || !empty($json->error)) return false;
$orders = $json->return;
foreach($orders as $id=>$info)
{
$res = json_decode($this->cancelOrder($id));
if($res->error) $errors[] = $res->error;
}
if(!empty($errors)) return $errors;
return true;
}
public function orderStatus($id)
{
$request = array(
'method'=>'OrderInfo',
'order_id'=>$id
);
$url = $this->t_endpoint . '/OrderInfo';
return $this->apiCall($request, $url);
}
public function activeOrders()
{
//get active orders first
$request = array(
'method'=>'ActiveOrders'
);
$url = $this->t_endpoint . '/ActiveOrders';
return $this->apiCall($request, $url);
}
public function activePositions()
{
}
public function permissions()
{
$request = array(
'method'=>'getInfo'
);
$url = $this->t_endpoint . '/getInfo';
$res = $this->apiCall($request, $url);
$json = json_decode($res);
if(!$json) return false;
if($json->success != 1) return $json;
return $json->return->rights;
}
public function buy($symbol, $amount, $price)
{
return $this->placeOrder($symbol, $amount, $price, 'buy');
}
public function sell($symbol, $amount, $price)
{
return $this->placeOrder($symbol, $amount, $price, 'sell');
}
public function getBalance()
{
$request = array(
'method'=>'getInfo'
);
$url = $this->t_endpoint . '/getInfo';
$res = $this->apiCall($request, $url);
$json = json_decode($res);
if(!$json) return false;
if($json->success != 1) return $json;
return $json->return->funds;
}
}