├── README.md
├── config.php
├── example.php
└── finex.php
/README.md:
--------------------------------------------------------------------------------
1 | # Bitfinex PHP Library
2 |
3 | This repo is the outcome of me ignoring the 2016 superbowl. Instead I streamed this for an hour and half on youtube (https://www.youtube.com/watch?v=rky1EnDqmhI).
4 | In the end we had a mini PHP library for the bitfinex API and a very simple trading bot. The bot uses bitcoinaverage.com API to determine if it
5 | is a "good" time to buy or sell.
6 |
7 |
8 | =======
9 | BTC: 1NPrfWgJfkANmd1jt88A141PjhiarT8d9U
10 |
11 |
12 | # Usage
13 | 0. Requirements
14 |
15 | * Bitfinex account with funds in appropriate finex wallets (exchange, trading)
16 | * Bitfinex API Keys and secret key
17 | * A server that is able to run PHP files.
18 | * (Optional) Access to Cronjobs (this way you can automate trading)
19 |
20 | 1. Download or clone the main project and extract files.
21 |
22 | `example.php` will make trades if you run it. DELETE EVERYTHING AFTER `require_once(config.php);` if you don't want the bot to execute a trade. If you want the bot to run automatically, you will need to set up the `example.php` file to run as a cron job every N minutes/hours/days etc.
23 |
24 | 2. Update the config.php file with your api keys
25 |
26 | $api_key = "your_api_key";
27 | $api_secret = "your_api_secret";
28 |
29 |
30 | 3. How to do a market buy order on the exchange market
31 |
32 | $trade = new bitfinex($api_key, $api_secret);
33 | $buy = $trade->new_order("BTCUSD", "0.01", "1", "buy", "exchange market");
34 |
35 | 4. How to do a limit sell order on the margin market
36 |
37 | $trade = new bitfinex($api_key, $api_secret);
38 | $buy = $trade->new_order("BTCUSD", "0.01", "380.29", "sell", "limit");
39 |
40 |
41 | ## Additional Info
42 |
43 | The basic schematic is:
44 |
45 | $variable = new bitfinex($api_key, $api_secret);
46 | $variable2 = $variable->new_order("SYMBOL", "AMOUNT", "PRICE", "SIDE", "TYPE");
47 |
48 | When making a limit order the price is the price you are looking to get met. When making a market order the price can be any random number.
49 | The `SIDE` refers to buy or sell. The `TYPE` is the type of order. If you want to do margin trades, enter the type without `exchange` in the front.
50 |
51 | Example (exchange buy):
52 | ("BTCUSD", "0.01", "1", "buy", "exchange market");
53 |
54 | Example (margin buy):
55 | ("BTCUSD", "0.01", "1", "buy", "market");
56 |
57 | ## Cancelling Orders
58 |
59 | $order_id = "943715";
60 | $execute = $trade->cancel_order($order_id);
61 |
62 | ## Cancel All Orders
63 |
64 | $execute = $trade->cancel_all();
65 |
66 |
67 | ## Account Info
68 |
69 | $execute = $trade->account_info();
70 | print_r($execute);
71 | /*
72 | example response
73 | [{
74 | "maker_fees":"0.1",
75 | "taker_fees":"0.2",
76 | "fees":[{
77 | "pairs":"BTC",
78 | "maker_fees":"0.1",
79 | "taker_fees":"0.2"
80 | },{
81 | "pairs":"LTC",
82 | "maker_fees":"0.1",
83 | "taker_fees":"0.2"
84 | },
85 | {
86 | "pairs":"DRK",
87 | "maker_fees":"0.1",
88 | "taker_fees":"0.2"
89 | }]
90 | }]
91 | */
92 |
93 | ## Deposit
94 | $method = "bitcoin";
95 | $wallet = "trading";
96 | $renew = 1;
97 | $execute = $trade->deposit($method, $wallet, $renew);
98 |
99 | Deposit generates a BTC address to deposit funds into bitfinex
100 | $renew will generate a new fresh deposit address if set to 1, default is 0.
101 |
102 | /*
103 | example response
104 | {
105 | "result":"success",
106 | "method":"bitcoin",
107 | "currency":"BTC",
108 | "address":"1A2wyHKJ4KWEoahDHVxwQy3kdd6g1qiSYV"
109 | }
110 | */
111 |
112 |
113 | ## Positions
114 | Shows current positions.
115 |
116 | $execute = $trade->positions();
117 |
118 | /*
119 | Example response:
120 | [{
121 | "id":943715,
122 | "symbol":"btcusd",
123 | "status":"ACTIVE",
124 | "base":"246.94",
125 | "amount":"1.0",
126 | "timestamp":"1444141857.0",
127 | "swap":"0.0",
128 | "pl":"-2.22042"
129 | }]
130 | */
131 |
132 |
133 | ## Close Position
134 |
135 | $position_id = 841235;
136 | $execute = $trade->close_position($position_id);
137 |
138 |
139 | ## Claim Position
140 |
141 | $execute = $trade->claim_position($position_id);
142 |
143 |
144 | ## Get Balance of Account
145 |
146 | $execute = $trade->fetch_balance();
147 |
148 |
149 | ## Get Margin Info of Account
150 |
151 | $execute = $trade->margin_infos();
152 |
153 |
154 | ## Transfer funds to different wallet on your account
155 |
156 | $amount = 2.014;
157 | $currency = "btc";
158 | $from = "exchange";
159 | $to = "trading";
160 | $execute = $trade->transfer($amount, $currency, $from, $to);
161 |
162 |
163 | =======
164 |
165 |
166 |
--------------------------------------------------------------------------------
/config.php:
--------------------------------------------------------------------------------
1 |
7 |
--------------------------------------------------------------------------------
/example.php:
--------------------------------------------------------------------------------
1 | 2){
19 | //good time to buy
20 | echo "24h Avg: ".$avg."
";
21 | echo "Current Ask: ".$ask."
";
22 | $trade = new bitfinex($api_key, $api_secret);
23 | $buy = $trade->new_order("BTCUSD", "0.01", "1", "buy", "exchange market");
24 | print_r($buy);
25 | }
26 |
27 | if($diff2 > 2){
28 | //good time to sell
29 | echo "24h Avg: ".$avg."
";
30 | echo "Current Bid: ".$bid."
";
31 | $trade = new bitfinex($api_key, $api_secret);
32 | $sell = $trade->new_order("BTCUSD", "0.01", "1", "sell", "exchange market");
33 | print_r($sell);
34 | }
35 |
36 | ?>
37 |
--------------------------------------------------------------------------------
/finex.php:
--------------------------------------------------------------------------------
1 | apikey = $apikey;
11 | $this->secret = $secret;
12 | }
13 |
14 | public function new_order($symbol, $amount, $price, $side, $type)
15 | {
16 | $request = "/v1/order/new";
17 | $data = array(
18 | "request" => $request,
19 | "symbol" => $symbol,
20 | "amount" => $amount,
21 | "price" => $price,
22 | "exchange" => "bitfinex",
23 | "side" => $side,
24 | "type" => $type
25 | );
26 | return $this->hash_request($data);
27 | }
28 | /*
29 | response
30 | {
31 | "id":448364249,
32 | "symbol":"btcusd",
33 | "exchange":"bitfinex",
34 | "price":"0.01",
35 | "avg_execution_price":"0.0",
36 | "side":"buy",
37 | "type":"exchange limit",
38 | "timestamp":"1444272165.252370982",
39 | "is_live":true,
40 | "is_cancelled":false,
41 | "is_hidden":false,
42 | "was_forced":false,
43 | "original_amount":"0.01",
44 | "remaining_amount":"0.01",
45 | "executed_amount":"0.0",
46 | "order_id":448364249
47 | }
48 | */
49 |
50 | public function cancel_order($order_id)
51 | {
52 | $request = "/v1/order/cancel";
53 | $data = array(
54 | "request" => $request,
55 | "order_id" => (int)$order_id
56 | );
57 | return $this->hash_request($data);
58 | }
59 |
60 | public function cancel_all()
61 | {
62 | $request = "/v1/order/cancel/all";
63 | $data = array(
64 | "request" => $request
65 | );
66 | return $this->hash_request($data);
67 | }
68 |
69 | public function account_info()
70 | {
71 | $request = "/v1/account_infos";
72 | $data = array(
73 | "request" => $request
74 | );
75 | return $this->hash_request($data);
76 | }
77 | /*
78 | response
79 | [{
80 | "maker_fees":"0.1",
81 | "taker_fees":"0.2",
82 | "fees":[{
83 | "pairs":"BTC",
84 | "maker_fees":"0.1",
85 | "taker_fees":"0.2"
86 | },{
87 | "pairs":"LTC",
88 | "maker_fees":"0.1",
89 | "taker_fees":"0.2"
90 | },
91 | {
92 | "pairs":"DRK",
93 | "maker_fees":"0.1",
94 | "taker_fees":"0.2"
95 | }]
96 | }]
97 | */
98 |
99 | public function deposit($method, $wallet, $renew)
100 | {
101 | $request = "/v1/deposit/new";
102 | $data = array(
103 | "request" => $request,
104 | "method" => $method,
105 | "wallet_name" => $wallet,
106 | "renew" => $renew
107 | );
108 | return $this->hash_request($data);
109 | }
110 | /*
111 | depost generates a BTC address to deposit funds into bitfinex
112 | example: deposit("bitcoin", "trading", $renew);
113 | $renew will generate a new fresh deposit address if set to 1, default is 0
114 | //response
115 | {
116 | "result":"success",
117 | "method":"bitcoin",
118 | "currency":"BTC",
119 | "address":"3FdY9coNq47MLiKhG2FLtKzdaXS3hZpSo4"
120 | }
121 | */
122 |
123 | public function positions()
124 | {
125 | $request = "/v1/positions";
126 | $data = array(
127 | "request" => $request
128 | );
129 | return $this->hash_request($data);
130 | }
131 | /*
132 | response
133 | [{
134 | "id":943715,
135 | "symbol":"btcusd",
136 | "status":"ACTIVE",
137 | "base":"246.94",
138 | "amount":"1.0",
139 | "timestamp":"1444141857.0",
140 | "swap":"0.0",
141 | "pl":"-2.22042"
142 | }]
143 | */
144 |
145 | public function close_position($position_id)
146 | {
147 | $request = "/v1/position/close";
148 | $data = array(
149 | "request" => $request,
150 | "position_id" => (int)$position_id
151 | );
152 | return $this->hash_request($data);
153 | }
154 |
155 | public function claim_position($position_id, $amount)
156 | {
157 | $request = "/v1/position/claim";
158 | $data = array(
159 | "request" => $request,
160 | "position_id" => (int)$position_id,
161 | "amount" => $amount
162 | );
163 | return $this->hash_request($data);
164 | }
165 | /*
166 | response
167 | {
168 | "id":943715,
169 | "symbol":"btcusd",
170 | "status":"ACTIVE",
171 | "base":"246.94",
172 | "amount":"1.0",
173 | "timestamp":"1444141857.0",
174 | "swap":"0.0",
175 | "pl":"-2.2304"
176 | }
177 | */
178 |
179 | public function fetch_balance()
180 | {
181 | $request = "/v1/balances";
182 | $data = array(
183 | "request" => $request
184 | );
185 | return $this->hash_request($data);
186 | }
187 | /*
188 | response
189 | [{
190 | "type":"deposit",
191 | "currency":"btc",
192 | "amount":"0.0",
193 | "available":"0.0"
194 | },{
195 | "type":"deposit",
196 | "currency":"usd",
197 | "amount":"1.0",
198 | "available":"1.0"
199 | },{
200 | "type":"exchange",
201 | "currency":"btc",
202 | "amount":"1",
203 | "available":"1"
204 | },{
205 | "type":"exchange",
206 | "currency":"usd",
207 | "amount":"1",
208 | "available":"1"
209 | },{
210 | "type":"trading",
211 | "currency":"btc",
212 | "amount":"1",
213 | "available":"1"
214 | },{
215 | "type":"trading",
216 | "currency":"usd",
217 | "amount":"1",
218 | "available":"1"
219 | }]
220 | */
221 |
222 | public function margin_infos()
223 | {
224 | $request = "/v1/margin_infos";
225 | $data = array(
226 | "request" => $request
227 | );
228 | return $this->hash_request($data);
229 | }
230 | /*
231 | [{
232 | "margin_balance":"14.80039951",
233 | "tradable_balance":"-12.50620089",
234 | "unrealized_pl":"-0.18392",
235 | "unrealized_swap":"-0.00038653",
236 | "net_value":"14.61609298",
237 | "required_margin":"7.3569",
238 | "leverage":"2.5",
239 | "margin_requirement":"13.0",
240 | "margin_limits":[{
241 | "on_pair":"BTCUSD",
242 | "initial_margin":"30.0",
243 | "margin_requirement":"15.0",
244 | "tradable_balance":"-0.329243259666666667"
245 | }]
246 | */
247 |
248 | public function transfer($amount, $currency, $from, $to)
249 | {
250 | $request = "/v1/transfer";
251 | $data = array(
252 | "request" => $request,
253 | "amount" => $amount,
254 | "currency" => $currency,
255 | "walletfrom" => $from,
256 | "walletto" => $to
257 | );
258 | return $this->hash_request($data);
259 | }
260 | /*
261 | response
262 | [{
263 | "status":"success",
264 | "message":"1.0 USD transfered from Exchange to Deposit"
265 | }]
266 | */
267 |
268 | private function headers($data)
269 | {
270 | $data["nonce"] = strval(round(microtime(true) * 10,0));
271 | $payload = base64_encode(json_encode($data));
272 | $signature = hash_hmac("sha384", $payload, $this->secret);
273 | return array(
274 | "X-BFX-APIKEY: " . $this->apikey,
275 | "X-BFX-PAYLOAD: " .$payload,
276 | "X-BFX-SIGNATURE: " . $signature
277 | );
278 | }
279 |
280 | private function hash_request($data)
281 | {
282 | $ch = curl_init();
283 | $bfurl = $this->url . $data["request"];
284 | $headers = $this->headers($data);
285 | curl_setopt_array($ch, array(
286 | CURLOPT_URL => $bfurl,
287 | CURLOPT_POST => true,
288 | CURLOPT_RETURNTRANSFER => true,
289 | CURLOPT_HTTPHEADER => $headers,
290 | CURLOPT_SSL_VERIFYPEER => false,
291 | CURLOPT_POSTFIELDS => ""
292 | ));
293 | $ccc = curl_exec($ch);
294 | return json_decode($ccc, true);
295 | }
296 |
297 | }
298 |
299 | ?>
300 |
301 |
--------------------------------------------------------------------------------