Skip to content

Commit

Permalink
add Bybit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Dec 20, 2023
1 parent 9c3653f commit 1cbcb03
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 121 deletions.
145 changes: 34 additions & 111 deletions src/Exchanges/Bybit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

namespace Lin\Exchange\Exchanges;


use Lin\Bybit\BybitInverse;
use Lin\Bybit\BybitLinear;
use Lin\Bybit\BybitV5;
use Lin\Exchange\Interfaces\AccountInterface;
use Lin\Exchange\Interfaces\MarketInterface;
use Lin\Exchange\Interfaces\TraderInterface;
Expand All @@ -16,20 +14,14 @@ class BaseBybit
{
protected $platform_linear;
protected $platform_inverse;
protected $host;

function __construct(BybitLinear $platform_linear=null,BybitInverse $platform_inverse=null,$host){
$this->platform_linear=$platform_linear;
$this->platform_inverse=$platform_inverse;
$this->host=$host;
}
protected $platform_v5;

protected function checkType(){
if(stripos($this->host,'bybitlinear')!==false){
return 'bybitlinear';
}
protected $host;

return 'bybitinverse';
function __construct(BybitV5 $platform_v5,$host){
$this->platform_v5=$platform_v5;
$this->host=$host;
}
}

Expand All @@ -38,16 +30,7 @@ class AccountBybit extends BaseBybit implements AccountInterface
/**
*
* */
function get(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}
function get(array $data){}
}

class MarketBybit extends BaseBybit implements MarketInterface
Expand All @@ -56,14 +39,6 @@ class MarketBybit extends BaseBybit implements MarketInterface
*
* */
function depth(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}
}

Expand All @@ -73,155 +48,103 @@ class TraderBybit extends BaseBybit implements TraderInterface
*
* */
function sell(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}

/**
*
* */
function buy(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}

/**
*
* */
function cancel(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}

/**
*
* */
function update(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}

/**
*
* */
function show(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return $this->platform_inverse->order()->get($data);
}
case 'bybitlinear':{
return $this->platform_linear->order()->get($data);
}
}
}

/**
*
* */
function showAll(array $data){
switch ($this->checkType()){
case 'bybitinverse':{
return ;
}
case 'bybitlinear':{
return ;
}
}
}
}

class Bybit
{
private $key;
private $secret;
private $passphrase;
private $host;

protected $type;

protected $platform_inverse;
protected $platform_linear;
protected $platform='';

function __construct($key,$secret,$passphrase,$host=''){
protected $platform_v5;

function __construct($key,$secret,$host=''){
$this->key=$key;
$this->secret=$secret;
$this->passphrase=$passphrase;
$this->host=empty($host) ? 'https://api.bybit.com' : $host ;

$this->getPlatform();
}

function account(){
return new AccountBybit($this->platform_linear,$this->platform_inverse,$this->host);
return new AccountBybit($this->platform_v5,$this->host);
}

function market(){
return new MarketBybit($this->platform_linear,$this->platform_inverse,$this->host);
return new MarketBybit($this->platform_v5,$this->host);
}

function trader(){
return new TraderBybit($this->platform_linear,$this->platform_inverse,$this->host);
return new TraderBybit($this->platform_v5,$this->host);
}

function getPlatform(string $type=''){
$this->type=strtolower($type);

switch ($this->type){
case 'bybitinverse':{
return $this->platform_inverse=new BybitInverse($this->key,$this->secret,$this->host);
}
case 'bybitlinear':{
return $this->platform_linear=new BybitLinear($this->key,$this->secret,$this->host);
}
default:{
if(stripos($this->host,'bybitlinear')!==false){
$this->type='bybitlinear';
return $this->platform_linear=new BybitLinear($this->key,$this->secret,$this->host);
}
$this->type='bybitinverse';
return $this->platform_inverse=new BybitInverse($this->key,$this->secret,$this->host);
return $this->platform_v5=new BybitV5($this->key,$this->secret,$this->host);
}
}
}

/**
Set exchange transaction category, default "spot" transaction. Other options "spot" "margin" "future" "swap"
*/
public function setPlatform(string $platform=''){
$this->platform=$platform ?? 'spot';
return $this;
}

/**
Set exchange API interface version. for example "v1" "v3" "v5"
*/
public function setVersion(string $version=''){
$this->version=$version;
return $this;
}


/**
* Support for more request Settings
* */
function setOptions(array $options=[]){

switch ($this->type){
case 'bybitinverse':{
$this->platform_inverse->setOptions($options);
break;
}
case 'bybitlinear':{
$this->platform_linear->setOptions($options);
break;
}
}
$this->options=$options;
return $this;
}
}
20 changes: 10 additions & 10 deletions tests/bybit.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
//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
Expand Down Expand Up @@ -473,6 +463,16 @@
}


case 2001:{
$result=$exchanges->getPlatform()->position()->getList([
'category'=>'linear',
'symbol'=>'BTCUSDT',
]);
print_r($result);
break;
}


default:{
echo 'nothing';
//exit;
Expand Down

0 comments on commit 1cbcb03

Please sign in to comment.