Code Monkey home page Code Monkey logo

exchanges-php's Introduction

Introduction

This SDK brings together the APIs of the many exchanges currently trading the most, allowing developers to focus only on the business layer. It is based on Bitmex Okex Huobi Binance and so on, and these underlying APIs are encapsulated again. Its advantages support multiple platforms at the same time, support unified parameter input and output, also support native parameter input, and simple quantitative trading to fully meet your needs. Even if you have special requirements, you can use the method getPlatform() to return the instance and call the underlying API.

中文文档

Other exchanges API

Exchanges It includes all of the following exchanges and is highly recommended.

Bitmex Support Websocket

Okex Support Websocket

Huobi Support Websocket

Binance Support Websocket

Kucoin

Mxc

Coinbase

ZB

Bitfinex

Bittrex

Kraken

Gate

Bigone

Bybit

Bitget

Poloniex

Coinex

If you don't find the exchange SDK you want, you can tell me and I'll join them.

Install

composer require linwj/exchanges

Exchanges initialization

//Public
$exchanges=new Exchanges('binance');
$exchanges=new Exchanges('bitmex');
$exchanges=new Exchanges('okex');
$exchanges=new Exchanges('huobi');
$exchanges=new Exchanges('kucoin');
...
...

//Private
$exchanges=new Exchanges('binance',$key,$secret);
$exchanges=new Exchanges('bitmex',$key,$secret);
$exchanges=new Exchanges('okex',$key,$secret,$passphrase);
$exchanges=new Exchanges('huobi',$key,$secret,$account_id);
$exchanges=new Exchanges('kucoin',$key,$secret,$passphrase);
...
...

Get Huobi Spot $account_id

Uniform parameter return

All submitted parameters and return as long as the first character for the underlined _ all for custom parameters.

/**
 * Buy()   Sell()   Show() Uniform parameter return
 * @return [
 *      ***Return to original data
 *      ...
 *      ...
 *      ***Returns custom data in a uniform return parameter format with '_' underscore
 *      _status=>NEW  PART_FILLED  FILLED  CANCELING  CANCELLED  FAILURE
 *      _filled_qty=>Number of transactions completed
 *      _price_avg=>Average transaction price
 *      _filed_amount=>transaction price
 *      _order_id=>system ID
 *      _client_id=>custom ID
 * ]
 *
 * */
 
 /**
 * System error
 * http request code 400 403 500 503
 * @return [
 *      _error=>[
 *          ***Return to original data
 *          ...
 *          ...
 *          ***Returns custom data in a uniform return parameter format with '_' underscore
 *          _method => POST
 *          _url => https://testnet.bitmex.com/api/v1/order
 *          _httpcode => 400
 *      ]
 * ]
 * */

The Buy Sell method has a waiting time of 2 seconds by default. Because the exchange is a matching transaction, the inquiry needs to wait. The default 2-second query can be turned off as:buy($data,false)

Buy and sell query uniform parameter return detail

System error unified parameter return binance okex huobi bitmex kucoin

The SDK currently only supports REST requests, and does not support Websocket for the time being. It will be added later.

Support for more request Settings More

$exchanges->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>10,
    
    //https://github.com/guzzle/guzzle
    'proxy'=>[],

    //https://www.php.net/manual/en/book.curl.php
    'curl'=>[],
]);

Spot Trader

Market
//binance
$exchanges->trader()->buy([
    '_symbol'=>'BTCUSDT',
    '_number'=>'0.01',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'BTCUSDT',
    'type'=>'MARKET',
    'quantity'=>'0.01',
]);

//okex default V3
//$exchanges->setPlatform('spot')->setVersion('v5');// Use V5
$exchanges->trader()->buy([
    '_symbol'=>'BTC-USDT',
    '_price'=>'10',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'instrument_id'=>'btc-usdt',
    'type'=>'market',
    'notional'=>'10'
]);

//huobi
$exchanges->trader()->buy([
    '_symbol'=>'btcusdt',
    '_price'=>'10',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'account-id'=>$account_id,
    'symbol'=>'btcusdt',
    'type'=>'buy-market',
    'amount'=>10
]);
Limit
//binance
$exchanges->trader()->buy([
    '_symbol'=>'BTCUSDT',
    '_number'=>'0.01',
    '_price'=>'2000',
]); 
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'BTCUSDT',
    'type'=>'LIMIT',
    'quantity'=>'0.01',
    'price'=>'2000',
    'timeInForce'=>'GTC',
]);

//okex default V3
//$exchanges->setPlatform('spot')->setVersion('v5');// Use V5
$exchanges->trader()->buy([
    '_symbol'=>'BTC-USDT',
    '_number'=>'0.001',
    '_price'=>'2000',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'instrument_id'=>'btc-usdt',
    'price'=>'100',
    'size'=>'0.001',
]);

//huobi
$exchanges->trader()->buy([
    '_symbol'=>'btcusdt',
    '_number'=>'0.001',
    '_price'=>'2000',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'account-id'=>$account_id,
    'symbol'=>'btcusdt',
    'type'=>'buy-limit',
    'amount'=>'0.001',
    'price'=>'2001',
]);

Future Trader

Market
//binance
$exchanges->trader()->buy([
    '_symbol'=>'BTCUSDT',
    '_number'=>'0.001',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'BTCUSDT',
    'quantity'=>'0.001',
    'type'=>'MARKET',
]);

//bitmex
$exchanges->trader()->buy([
    '_symbol'=>'XBTUSD',
    '_number'=>'1',
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'XBTUSD',
    'orderQty'=>'1',
    'ordType'=>'Market',
]);

//okex default V3
//$exchanges->setPlatform('future')->setVersion('v5');// Use V5
$exchanges->trader()->buy([
    '_symbol'=>'BTC-USD-190628',
    '_number'=>'1',
    '_entry'=>true,//buy=ture |  buy=false
]);
$exchanges->trader()->sell([
    '_symbol'=>'BTC-USD-190628',
    '_number'=>'1',
    '_entry'=>true,//sell=ture |  sell=false
]);




//Support for original parameters
$exchanges->trader()->buy([
    'instrument_id'=>'BTC-USD-190628',
    'size'=>1,
    'type'=>1,//1:open long 2:open short 3:close long 4:close short
    //'price'=>2000,
    'leverage'=>10,//10x or 20x leverage
    'match_price' => 1,
    'order_type'=>0,
]);

//huobi
$exchanges->trader()->buy([
    '_symbol'=>'ETC191227',
    '_number'=>'1',
    '_entry'=>true,//true:open  false:close
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'XRP',//string false "BTC","ETH"...
    'contract_type'=>'quarter',//string false Contract Type ("this_week": "next_week": "quarter":)
    'contract_code'=>'XRP190927',//string false BTC180914
    //'price'=>'0.3',// decimal true Price
    'volume'=>'1',//long true Numbers of orders (amount)
    //'direction'=>'buy',// string  true    Transaction direction
    'offset'=>'open',// string  true    "open", "close"
    'order_price_type'=>'opponent',//"limit", "opponent"
    'lever_rate'=>20,//int true Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
]);
Limit
//binance
$exchanges->trader()->buy([
    '_symbol'=>'BTCUSDT',
    '_number'=>'0.001',
    '_price'=>'6000'
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'BTCUSDT',
    'quantity'=>'0.001',
    'type'=>'LIMIT',
    'price'=>'6500',
    'timeInForce'=>'GTC',
]);

//bitmex
$exchanges->trader()->buy([
    '_symbol'=>'XBTUSD',
    '_number'=>'1',
    '_price'=>100
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'XBTUSD',
    'price'=>'100',
    'orderQty'=>'1',
    'ordType'=>'Limit',
]);

//okex default V3
//$exchanges->setPlatform('future')->setVersion('v5');// Use V5
$exchanges->trader()->buy([
    '_symbol'=>'BTC-USD-190628',
    '_number'=>'1',
    '_price'=>'2000',
    '_entry'=>true,//open long
]);
//Support for original parameters
$exchanges->trader()->buy([
    'instrument_id'=>'BTC-USD-190628',
    'size'=>1,
    'type'=>1,//1:open long 2:open short 3:close long 4:close short
    'price'=>2000,
    'leverage'=>10,//10x or 20x leverage
    'match_price' => 0,
    'order_type'=>0,
]);

//huobi
$exchanges->trader()->buy([
    '_symbol'=>'XRP190927',
    '_number'=>'1',
    '_price'=>'0.3',
    '_entry'=>true,//true:open  false:close
]);
//Support for original parameters
$exchanges->trader()->buy([
    'symbol'=>'XRP',//string false "BTC","ETH"...
    'contract_type'=>'quarter',//string false Contract Type ("this_week": "next_week": "quarter":)
    'contract_code'=>'XRP190927',// string  false   BTC180914
    'price'=>'0.3',//decimal true Price
    'volume'=>'1',//long true Numbers of orders (amount)
    //'direction'=>'buy',// string  true Transaction direction
    'offset'=>'open',// string  true    "open", "close"
    'order_price_type'=>'limit',//"limit", "opponent"
    'lever_rate'=>20,//int true Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
]);

Get Order Details

//binance
$exchanges->trader()->show([
    '_symbol'=>'BTCUSDT',
    '_order_id'=>'324314658',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);

//bitmex
$exchanges->trader()->show([
    '_symbol'=>'XBTUSD',
    '_order_id'=>'7d03ac2a-b24d-f48c-95f4-2628e6411927',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);

//okex default V3
//$exchanges->setPlatform('spot')->setVersion('v5');// Use V5
$exchanges->trader()->show([
    '_symbol'=>'BTC-USDT',
    '_order_id'=>'2671215997495296',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
$exchanges->trader()->show([
    '_symbol'=>'BTC-USD-190927',
    '_order_id'=>'2671566274710528',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
$exchanges->trader()->show([
    '_symbol'=>'BTC-USD-SWAP',
    '_order_id'=>'2671566274710528',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);

//huobi spot
$exchanges->trader()->show([
    '_order_id'=>'29897313869',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);
//huobi future
$exchanges->trader()->show([
    '_symbol'=>'XRP190927',
    '_order_id'=>'2715696586',
    //'_client_id'=>'1bc3e974577a6ad9ce730006eafb5522',
]);

Get accounts or positions

//binance
//Get current account information.
$exchanges->account()->get();

//bitmex
//bargaining transaction
$exchanges->account()->get([
    //Default return all
    //'_symbol'=>'XBTUSD'
]);

//okex  spot
//This endpoint supports getting the balance, amount available/on hold of a token in spot account.
$exchanges->account()->get([
    '_symbol'=>'BTC',
]);

//okex future
//Get the information of holding positions of a contract.
$exchanges->account()->get([
    '_symbol'=>'BTC-USD-190628',
]);

//okex swap
$exchanges->account()->get([
    '_symbol'=>'BTC-USD-SWAP',
]);

//huobi spot
$exchanges->account()->get([
    '_symbol'=>'btcusdt',
]);

//huobi future
$exchanges->account()->get([
    '_symbol'=>'BTC190927',
]);

Support for original parameters

Below is the call to the underlying API to initiate a new order instance

The following is the call to the underlying API to initiate a new order instance. There are two ways to call the underlying API.

Method 1: through $exchanges -> getplatform ()

//binance
$exchanges=new Exchanges('binance',$key,$secret);
$exchanges->getPlatform('spot')->trade()->postOrder([
    'symbol'=>'BTCUSDT',
    'side'=>'BUY',
    'type'=>'LIMIT',
    'quantity'=>'0.01',
    'price'=>'2000',
    'timeInForce'=>'GTC',
]);
$exchanges->getPlatform('future')->trade()->postOrder([
    'symbol'=>'BTCUSDT',
    'side'=>'BUY',
    'type'=>'LIMIT',
    'quantity'=>'0.01',
    'price'=>'2000',
    'timeInForce'=>'GTC',
]);


//bitmex
$exchanges=new Exchanges('bitmex',$key,$secret);
$exchanges->getPlatform()->order()->post([
    'symbol'=>'XBTUSD',
    'price'=>'100',
    'side'=>'Buy',
    'orderQty'=>'1',
    'ordType'=>'Limit',
]);


//okex
$exchanges=new Exchanges('okex',$key,$secret);
$exchanges->getPlatform('spot')->order()->post([
    'instrument_id'=>'btc-usdt',
    'side'=>'buy',
    'price'=>'100',
    'size'=>'0.001',
    //'type'=>'market',
    //'notional'=>'100'
]);
$exchanges->getPlatform('future')->order()->post([
    'instrument_id'=>'btc-usd-190628',
    'type'=>'1',
    'price'=>'100',
    'size'=>'1',
]);
$result=$exchanges->getPlatform('swap')->order()->post([
    'instrument_id'=>'BTC-USD-SWAP',
    'type'=>'1',
    'price'=>'5000',
    'size'=>'1',
]);


//huobi
$exchanges=new Exchanges('huobi',$key,$secret);
$exchanges->getPlatform('spot')->order()->postPlace([
    'account-id'=>$account_id,
    'symbol'=>'btcusdt',
    'type'=>'buy-limit',
    'amount'=>'0.001',
    'price'=>'100',
]);

$exchanges->getPlatform('future')->contract()->postOrder([
    'symbol'=>'XRP',//string    false   "BTC","ETH"...
    'contract_type'=>'quarter',//   string  false   Contract Type ("this_week": "next_week": "quarter":)
    'contract_code'=>'XRP190927',// string  false   BTC180914
    'price'=>'0.3',//   decimal true    Price
    'volume'=>'1',//    long    true    Numbers of orders (amount)
    'direction'=>'buy',//   string  true    Transaction direction
    'offset'=>'open',// string  true    "open", "close"
    'order_price_type'=>'limit',//"limit", "opponent"
    'lever_rate'=>20,//int  true    Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
    //'client_order_id'=>'',//long  false   Clients fill and maintain themselves, and this time must be greater than last time
]);

Method 2: directly instantiate the underlying exchange. The following code is equivalent to mode 1.

//binance
$binance=new Binance($key,$secret);
$binance->trade()->postOrder([
    'symbol'=>'BTCUSDT',
    'side'=>'BUY',
    'type'=>'LIMIT',
    'quantity'=>'0.01',
    'price'=>'2000',
    'timeInForce'=>'GTC',
]);

$binance=new BinanceFuture($key,$secret);
$binance->trade()->postOrder([
    'symbol'=>'BTCUSDT',
    'side'=>'BUY',
    'type'=>'LIMIT',
    'quantity'=>'0.01',
    'price'=>'2000',
    'timeInForce'=>'GTC',
]);


//bitmex
$bitmex=new Bitmex($key,$secret);
$bitmex->order()->post([
    'symbol'=>'XBTUSD',
    'price'=>'100',
    'side'=>'Buy',
    'orderQty'=>'1',
    'ordType'=>'Limit',
]);


//okex
$okex=new OkexSpot($key,$secret,$passphrase);
$okex->order()->post([
    'instrument_id'=>'btc-usdt',
    'side'=>'buy',
    'price'=>'100',
    'size'=>'0.001',
    //'type'=>'market',
    //'notional'=>'100'
]);

$okex=new OkexFuture($key,$secret,$passphrase);
$okex->order()->post([
    'instrument_id'=>'btc-usd-190628',
    'type'=>'1',
    'price'=>'100',
    'size'=>'1',
]);

$okex=new OkexSwap($key,$secret,$passphrase);
$okex->order()->post([
    'instrument_id'=>'BTC-USD-SWAP',
    'type'=>'1',
    'price'=>'5000',
    'size'=>'1',
]);


//huobi
$huobi=new HuobiSpot($key,$secret);
$huobi->order()->postPlace([
    'account-id'=>$account_id,
    'symbol'=>'btcusdt',
    'type'=>'buy-limit',
    'amount'=>'0.001',
    'price'=>'100',
]);

$huobi=new HuobiFuture($key,$secret);
$huobi->contract()->postOrder([
    'symbol'=>'XRP',//string    false   "BTC","ETH"...
    'contract_type'=>'quarter',//   string  false   Contract Type ("this_week": "next_week": "quarter":)
    'contract_code'=>'XRP190927',// string  false   BTC180914
    'price'=>'0.3',//   decimal true    Price
    'volume'=>'1',//    long    true    Numbers of orders (amount)
    'direction'=>'buy',//   string  true    Transaction direction
    'offset'=>'open',// string  true    "open", "close"
    'order_price_type'=>'limit',//"limit", "opponent"
    'lever_rate'=>20,//int  true    Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
    //'client_order_id'=>'',//long  false   Clients fill and maintain themselves, and this time must be greater than last time
]);

More Tests

More API

exchanges-php's People

Contributors

gittsewell avatar teswell avatar zhouaini528 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

exchanges-php's Issues

Adding exchanges

Hi, maybe you could add Poloniex exchange? Do you think about adding uniswap/pancakeswap?

发现几个小的细节问题

主要是下单的问题

RequestTraderMap.php

function buy()

> 115 $map['sz']=$data['_price'] ?? 0; => $map['sz']=$data['_number'] ?? 0;
> 118 switch ($this->platform){ => switch ($this->checkType($map['instId'])) {
> 129 if(!$data['_entry']) $map['side']='sell'; => if(!$data['_entry']) $map['posSide']='short';

How to extend binance user class for example with sub-account api calls?

How do this work with Exchanges class?
have you a little code snip for request "GET /sapi/v1/sub-account/list (HMAC SHA256)" for me?

my code until now:

`use Lin\Exchange\Exchanges;

try {

$tmp_arr = array();
if (isset($cfg_main_arr['msa_api']['binance']['key_ro'])) {
    $tmp_arr = $cfg_main_arr['msa_api']['binance']['key_ro'];
}
if (empty($tmp_arr)) {
    die('config.json error: binance api key "key_ro" not exists!');
}

$ex_binance = new Exchanges('binance', $tmp_arr['APIKEY'], $tmp_arr['APISECRET']);

$result = $ex_binance->getPlatform('spot')
    ->user()
    ->getAccount();

print_r($result);

} catch (Exception $e) {
// error mail senden!!!
echo $e;
}`

okex V5 的 websocket 能出一个相关的使用示例吗?

两个小请求
1,目前okex websocket 只有 v3 的封装,如果可以能否封装一下v5的谢谢

2,目前用工厂获取的是 v3的实例,可以适配一下 v5 的
可以类似这样 $exchanges = new Exchanges('okexV5');
这个目前使用起来问题不大,我目前是这样使用的 new \Lin\Okex\OkexV5

主要是第一个,谢谢!

Exchange API里是不是缺少个key_secret.php?

我是初学,有个疑问。

我尝试在看exchange api,发现里面有文件引用key_secret.php。但是代码包里没有这个文件。请问是需要自己建立么?内容都写什么呢?

谢谢!

Huobi Account ID

Hi! Thank you for the great work. When trying to post a sample trade with Huobi I get an error. I first get the Account ID using the $result=$huobi->account()->get(); method. I set this ID in the key_secret.php and then require the key_secret.php in my script.

use Lin\Exchange\Exchanges;
require 'exchanges/vendor/linwj/exchanges/tests/key_secret.php';

$key=$keysecret['huobi']['key'];
$secret=$keysecret['huobi']['secret'];
$host=$keysecret['huobi']['host'];
$account_id=$keysecret['huobi']['account_id'];

$exchanges=new Exchanges('huobi',$key,$secret,$account_id,$host);

I then get the following error:

"Array ( [_error] => Array ( [status] => error [err-code] => account-frozen-account-inexistent-error [err-msg] => account for id 1#,###,##6 and user id 1#,###,#9 does not exist [data] => [_method] => POST [_url] => https://api.huobi.pro/v1/order/orders/place [_httpcode] => 200 ) ) "

  1. For some reason it uses commas in the error code. My ID is an int without commas.
  2. what is user ID? I do not set this anywhere. All I have set is key, secret and ID.

Thank you for the help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.