Skip to content

Commit

Permalink
add binance future
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Oct 30, 2024
1 parent fb2d6a6 commit 5af05d5
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/Exchanges/Binance.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function buy(array $data){
function cancel(array $data){
$data=$this->redata($data);

switch ($this->checkType()){
switch ($this->checkType($data)){
case 'future':{
$this->platform_future=$this->getPlatform('future');
return $this->platform_future->trade()->deleteOrder($data);
Expand Down
77 changes: 43 additions & 34 deletions src/Map/RequestTraderMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ function buy(array $data){
print_r($data);
var_dump($data['_entry'] ?? '');die;*/

switch ($this->checkType($data['_entry'] ?? '')){
switch ($this->checkType(isset($data['_entry']) ?? '')){
case 'swap':
case 'delivery':
case 'future':{
//LONG多 SHORT空
//开多 buy long 平多 sell long
//开空 buy short 开空 sell short
//平空 buy short 开空 sell short
if($data['_entry']) $map['side']='BUY';
else $map['side']='SELL';

Expand Down Expand Up @@ -258,37 +258,30 @@ function buy(array $data){
$map['quantity']=$data['_number'] ?? ($data['quantity'] ?? '');
}

switch ($this->checkType($data['_entry'] ?? '')){
switch ($this->checkType(isset($data['_entry']) ?? '')){
case 'swap':
case 'delivery':
case 'future':{
//LONG多 SHORT空
//开多 buy long 平多 sell long
//开空 buy short 开空 sell short
//平空 buy short 开空 sell short
if($data['_entry']) $map['side']='BUY';
else $map['side']='SELL';

$map['positionSide']='LONG';
break;
}
case 'spot':{
$map['newOrderRespType']=$data['newOrderRespType'] ?? 'ACK';
break;
}
default:{
$map['quantity']=$data['_number'] ?? ($data['quantity'] ?? '');
}
}
$map['type']='MARKET';
}

switch ($this->checkType()){
case 'spot':{
$map['newOrderRespType']=$data['newOrderRespType'] ?? 'ACK';
break;
}
default:{
$map['positionSide']='LONG';
$map['side'] = $data['_entry'] ? 'BUY' : 'SELL';
}
}

//支持原生参数
$data['side']=$map['side'];

Expand Down Expand Up @@ -541,16 +534,43 @@ function sell(array $data){
$map['quantity']=$data['_number'] ?? ($data['quantity'] ?? '');
$map['price']=$data['_price'] ?? ($data['quoteOrderQty'] ?? '');
$map['type']='LIMIT';

switch ($this->checkType(isset($data['_entry']) ?? '')){
case 'swap':
case 'delivery':
case 'future':{
//LONG多 SHORT空
//开多 buy long 平多 sell long
//平空 buy short 开空 sell short
if($data['_entry']) $map['side']='SELL';
else $map['side']='BUY';

$map['positionSide']='SHORT';
break;
}
default:{
}
}
}else{
switch ($this->checkType()){
case 'spot':{
if(isset($data['_price'])){
$map['quoteOrderQty']=$data['_price'] ?? ($data['quoteOrderQty'] ?? '');
}
if(isset($data['_price'])){
$map['quoteOrderQty']=$data['_price'] ?? ($data['quoteOrderQty'] ?? '');
}

if(isset($data['_number'])){
$map['quantity']=$data['_number'] ?? ($data['quantity'] ?? '');
}
if(isset($data['_number'])){
$map['quantity']=$data['_number'] ?? ($data['quantity'] ?? '');
}

switch ($this->checkType(isset($data['_entry']) ?? '')){
case 'swap':
case 'delivery':
case 'future':{
if($data['_entry']) $map['side']='SELL';
else $map['side']='BUY';

$map['positionSide']='SHORT';
break;
}
case 'spot':{
$map['newOrderRespType']=$data['newOrderRespType'] ?? 'ACK';
break;
}
Expand All @@ -561,17 +581,6 @@ function sell(array $data){
$map['type']='MARKET';
}

switch ($this->checkType()){
case 'spot':{
$map['newOrderRespType']=$data['newOrderRespType'] ?? 'ACK';
break;
}
default:{
$map['positionSide']='SHORT';
$map['side'] = $data['_entry'] ? 'BUY' : 'SELL';
}
}

//支持原生参数
$data['side']=$map['side'];

Expand Down
187 changes: 183 additions & 4 deletions tests/binance.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,34 +388,213 @@

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
$exchanges->setPlatform('future')->setVersion('v1');

$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);
die;
sleep(5);
$result=$exchanges->trader()->cancel([
'_symbol'=>'ETHUSDT',
'_symbol'=>'BTCUSD_241227',
//'_order_id'=>$result['orderId'],
'_client_id'=>$_client_id,
//'_client_id'=>'cd0986e64b3faca04724b82a3ca279f2'
//'_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);
5 changes: 5 additions & 0 deletions tests/bitget.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
//Set the request timeout to 60 seconds by default
'timeout'=>10,

'curl'=>[
CURLOPT_PROXY => 'proxy.local',
CURLOPT_PROXYPORT => '10808',
CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5,
]
]);

$action=intval($_GET['action'] ?? 0);//http pattern
Expand Down
11 changes: 8 additions & 3 deletions tests/gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
$key=$keysecret['gate']['key'];
$secret=$keysecret['gate']['secret'];

$exchanges=new Exchanges('gate',$key,$secret);
$exchanges=new Exchanges('gate',$key,$secret,'','https://api.gateio.ws/a/');


//Support for more request Settings
$exchanges->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

'curl'=>[
CURLOPT_PROXY => 'proxy.local',
CURLOPT_PROXYPORT => '10808',
CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5,
]
]);

$action=intval($_GET['action'] ?? 0);//http pattern
Expand Down Expand Up @@ -269,7 +274,7 @@

case 1001:{
try {
$result=$exchanges->getPlatform('spot')->account()->get();
$result=$exchanges->getPlatform()->account()->get();
print_r($result);
}catch (\Exception $e){
print_r(json_decode($e->getMessage(),true));
Expand Down
Loading

0 comments on commit 5af05d5

Please sign in to comment.