-
Notifications
You must be signed in to change notification settings - Fork 66
/
binance.php
602 lines (492 loc) · 14.9 KB
/
binance.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
/**
* @author lin <[email protected]>
*
* Most of them are unfinished and need your help
* https://github.com/zhouaini528/binance-php.git
* */
use Lin\Exchange\Exchanges;
require __DIR__ .'../../vendor/autoload.php';
include 'key_secret.php';
$key=$keysecret['binance']['key'];
$secret=$keysecret['binance']['secret'];
$spot_host='https://api.binance.com';
$future_host='https://fapi.binance.com';
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
//Support for more request Settings
$exchanges->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,
//If you are developing locally and need an agent, you can set this
//'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
//'verify'=>false,
]);
$action=intval($_GET['action'] ?? 0);//http pattern
if(empty($action)) $action=intval($argv[1]);//cli pattern
switch ($action){
//***********system error
//exception testing
case 1:{
$result=$exchanges->trader()->buy([
'_symbol'=>'exception testing',
'_number'=>'0.01',
]);
/*
return Array
(
[_error] => Array
(
[code] => -1100
[msg] => Illegal characters found in parameter 'symbol'; legal range is '^[A-Z0-9_]{1,20}$'.
[_method] => POST
[_url] => https://api.binance.com/api/v3/order
[_httpcode] => 400
)
)
*/
break;
}
case 2:{
//If you are developing locally and need an agent, you can set this
$exchanges->setOptions([
'proxy'=>true,
]);
//More flexible Settings
$exchanges->setOptions([
'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
],
]);
//Get current account information.
$result=$exchanges->account()->get();
break;
}
//******************************Spot
//***********Spot Market
case 100:{
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'10',
]);
break;
}
case 101:{
//The original parameters
$result=$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'type'=>'MARKET',
'quantity'=>'0.01',
]);
break;
}
case 102:{
$result=$exchanges->trader()->sell([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
]);
break;
}
case 103:{
//The original parameters
$result=$exchanges->trader()->sell([
'symbol'=>'BTCUSDT',
'type'=>'MARKET',
'quantity'=>'0.01',
]);
break;
}
//***********Spot Limit
case 150:{
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_price'=>'2000',
]);
break;
}
case 151:{
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binance.vision');
//The original parameters
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'newClientOrderId'=>$_client_id,
'symbol'=>'BTCUSDT',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'50000',
'timeInForce'=>'GTC',
]);
break;
}
case 152:{
$result=$exchanges->trader()->sell([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_price'=>'9000',
]);
break;
}
case 153:{
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binance.vision');
//The original parameters
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->sell([
'newClientOrderId'=>$_client_id,
'symbol'=>'BTCUSDT',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'90000',
'timeInForce'=>'GTC',
]);
break;
}
case 300:{
$result=$exchanges->trader()->show([
'_symbol'=>'ETHUSDT',
//'_order_id'=>'324314658',
//'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
'_order_id'=>'4135060190',
'_client_id'=>'b383abd88086f623e762c35727455b9a',
]);
break;
}
case 301:{
$result=$exchanges->trader()->show([
'symbol'=>'BTCUSDT',
'orderId'=>'324317395',
//'origClientOrderId'=>'',
]);
break;
}
case 302:{
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSDT',
'_order_id'=>'324314658',
]);
break;
}
case 303:{
$result=$exchanges->trader()->cancel([
'symbol'=>'BTCUSDT',
'orderId'=>'324317395',
//'origClientOrderId'=>'',
]);
break;
}
case 304:{
//Get current account information.
$result=$exchanges->account()->get();
break;
}
//******************************Complete spot flow
case 400:{
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'_symbol'=>'ETHUSDT',
'_number'=>'0.01',
'_price'=>'1000',
'_client_id'=>$_client_id,
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'ETHUSDT',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'cd0986e64b3faca04724b82a3ca279f2'
]);
break;
}
//******************************Complete future flow
case 450:{
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.001',
'_price'=>'6500',
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSDT',
'_order_id'=>$result['orderId'],
//'_client_id'=>$_client_id,
]);
break;
}
case 461:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->account()->get();
break;
}
case 462:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.001',
]);
break;
}
case 463:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'quantity'=>'0.001',
'type'=>'LIMIT',
'price'=>'6500',
'timeInForce'=>'GTC',
]);
break;
}
case 463:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'quantity'=>'0.001',
'type'=>'LIMIT',
'price'=>'6500',
'timeInForce'=>'GTC',
]);
break;
}
case 464:{
$exchanges=new Exchanges('binance',$key,$secret,$future_host);
$result=$exchanges->trader()->show([
'_symbol'=>'BTCUSDT',
'_order_id'=>'487693783',
]);
break;
}
case 1001:{
//Public API
$exchanges=new Exchanges('binance');
$exchanges->setOptions([
'timeout'=>10,
'proxy'=>true,
'verify'=>false,
]);
$result=$exchanges->getPlatform()->system()->getDepth([
'symbol'=>'BTCUSDT',
]);
break;
}
case 4000:{
$_client_id=md5(rand(1,999999999));//custom ID
$exchanges->setPlatform('spot')->setVersion('v1');
$result=$exchanges->trader()->buy([
'_symbol'=>'ETHUSDT',
'_number'=>'0.01',
'_price'=>'1000',
'_client_id'=>$_client_id,
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'ETHUSDT',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'cd0986e64b3faca04724b82a3ca279f2'
]);
break;
}
case 4001:{
//Send in a new order.
try {
$result=$exchanges->getPlatform('spot')->trade()->postOrder([
'symbol'=>'ETHUSDT',
'side'=>'BUY',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'1000',
'timeInForce'=>'GTC',
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
//Check an order's status.
try {
$result=$exchanges->getPlatform('spot')->user()->getOrder([
'symbol'=>'ETHUSDT',
'orderId'=>$result['orderId'],
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
//Cancel an active order.
try {
$result=$exchanges->getPlatform('spot')->trade()->deleteOrder([
'symbol'=>'ETHUSDT',
'orderId'=>$result['orderId'],
//'origClientOrderId'=>'1111111',
]);
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
}
break;
}
case 4002:{
//币本位 永续测试
//BTCUSD_PERP 币本位 永续
//现价交易
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('swap');
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSD_PERP',
'_number'=>'1',
'_price'=>'60000',
'_entry'=>true,
'_client_id'=>$_client_id,
]);
print_r($result);
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSD_PERP',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'dab73aca1b7d2550b8cc0d60e9f3dca5'
]);
break;
}
case 4003:{
//币本位 永续测试
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('swap');
$result=$exchanges->trader()->show([
'_symbol'=>'BTCUSD_PERP',
//'_order_id'=>$result['orderId'],
//'_client_id'=>$_client_id,
'_client_id'=>'c059bd2a219c9e482e4987073621b476'
]);
break;
}
case 4004:{
//币本位 永续测试
//BTCUSD_PERP 币本位 永续
//市价交易
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('swap');
$_client_id=md5(rand(1,999999999));//custom ID
//市价 开多
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSD_PERP',
'_number'=>'1',
'_entry'=>true,
'_client_id'=>$_client_id,
]);
print_r($result);
sleep(5);
//市价 平多
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSD_PERP',
'_number'=>'1',
'_entry'=>false,
'_client_id'=>$_client_id,
]);
break;
}
case 5002:{
//BTCUSD_241227 币本位 交割
//现价交易
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('future');
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSD_241227',
'_number'=>'1',
'_price'=>'50000',
'_entry'=>true,
'_client_id'=>$_client_id,
]);
print_r($result);
sleep(5);
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSD_241227',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'dab73aca1b7d2550b8cc0d60e9f3dca5'
]);
break;
}
case 5003:{
//BTCUSD_241227 币本位 交割
//市价交易
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('future');
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSD_241227',
'_number'=>'1',
'_entry'=>true,
'_client_id'=>$_client_id,
]);
print_r($result);
sleep(5);
//市价 平多
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSD_241227',
'_number'=>'1',
'_entry'=>false,
'_client_id'=>$_client_id,
]);
break;
}
case 6002:{
//BTCUSDT u本位 永续
//现价交易
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('swap');
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->sell([
'_symbol'=>'BTCUSDT',
'_number'=>'1',
'_price'=>'90000',
'_entry'=>true,
'_client_id'=>$_client_id,
]);
print_r($result);
sleep(5);
$result=$exchanges->trader()->cancel([
'_symbol'=>'BTCUSDT',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'dab73aca1b7d2550b8cc0d60e9f3dca5'
]);
break;
}
case 6003:{
//BTCUSDT u本位 永续
//市价交易
$exchanges=new Exchanges('binance',$key,$secret,'https://testnet.binancefuture.com');
$exchanges->setPlatform('swap');
$_client_id=md5(rand(1,999999999));//custom ID
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_entry'=>true,
'_client_id'=>$_client_id,
]);
print_r($result);
sleep(5);
//市价 平多
$result=$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_entry'=>false,
'_client_id'=>$_client_id,
]);
break;
}
default:{
echo 'nothing';
exit;
}
}
print_r($result);