PHP 高效的开发量化交易程序,不输 go c++ python
前言
这个SDK是我们团队的成果,现在开源它,让php做虚拟货币量化交易这块更为简单,目前已经大量运用在我们的项目上,效果良好,在请求时间上与 go c++ python相差不大,因为主要网络IO问题。 当然硬件资源的利用率静态语言肯定比动态强很多。中前期用PHP来开发(就算后期也木有压力),后期用静态语言分离模块做微服务化,与PHP结合非常强悍,目前我们正在用GO开始做部分分离了。
这SDK集合了目前交易量最大的多家交易所的API,让开发人员只关注业务层。它是基于Bitmex Okex Huobi Binance等等,这些底层API再次封装。它的优点同时支持多平台,支持统一参数输入与输出,也支持原生参数输入,简单的量化交易完全满足你的需求。就算你有特殊的需求你可以单独通过该方法getPlatform()返回实例,调用底层API。
2019.9.11修改:添加了kucoin交易所,以及各种小BUG的修复等等
2019.12.2修改:市价深度、买卖查询默认关闭、稳定版本发布等等已经其他BUG
2020.1.2:添加币安期货接口
2020.5.22:新增多个交易所,mxc coinbase bitfinex zb
2020.8.28:新增多个交易所,Crex24 Bigone Gate等交易所
2020.11.26:添加多个交易所的websocket
其他交易所API
Exchanges 它包含以下所有交易所,强烈推荐使用该SDK。
Gate Support V2/V4
安装方式
composer require linwj/exchanges
交易所初始化
//公共接口初始化对象
$exchanges=new Exchanges('binance');
$exchanges=new Exchanges('bitmex');
$exchanges=new Exchanges('okex');
$exchanges=new Exchanges('huobi');
$exchanges=new Exchanges('kucoin');
...
...
//私有接口初始化对象
$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);
...
...
统一参数返回
所有提交参数与返回参数只要第一个字符为下划线的_
全部为自定义参数。
/**
* Buy() Sell() Show() 三个方法都返回相同参数
* @return [
* ***返回原始数据
* ...
* ...
* ***返回自定义数据,带'_'下划线的是统一返回参数格式。
* _status=>NEW 进行中 PART_FILLED 部分成交 FILLED 完全成交 CANCELING:撤销中 CANCELLED 已撤销 FAILURE 下单失败
* _filled_qty=>已交易完成数量
* _price_avg=>成交均价
* _filed_amount=>交易价格
* _order_id=>系统ID
* _client_id=>自定义ID
* ]
*
* */
/**
* 系统错误
* http request code 400 403 500 503
* @return [
* _error=>[
* ***返回原始数据
* ...
* ...
* ***返回自定义数据,带'_'下划线的是统一返回参数格式。
* _method => POST
* _url => https://testnet.bitmex.com/api/v1/order
* _httpcode => 400
* ]
* ]
* */
Buy Sell 方法默认有2秒的等待查询,因为交易所是撮合交易所以查询需要等待。该默认2秒查询可以关闭如:buy($data,false)
买卖查询统一参数返回 详情
系统错误统一参数返回
binance
okex
huobi
bitmex
kucoin
该SDK目前只支持REST请求,暂时不支持Websocket,后期会加入这块。
支持更多的请求设置 More
$exchanges->setOptions([
//设置请求超时时间,默认60s
'timeout'=>10,
//如果您正在本地开发需要代理,您可以这样设置
'proxy'=>true,
//更灵活的代理设置
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//是否开启证书
//'verify'=>false,
]);
现货
市价交易
//binance
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'type'=>'MARKET',
'quantity'=>'0.01',
]);
//okex
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'BTC-USDT',
'_price'=>'10',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'instrument_id'=>'btc-usdt',
'type'=>'market',
'notional'=>'10'
]);
//huobi
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'btcusdt',
'_price'=>'10',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'account-id'=>$account_id,
'symbol'=>'btcusdt',
'type'=>'buy-market',
'amount'=>10
]);
限价交易
//binance
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'BTCUSDT',
'_number'=>'0.01',
'_price'=>'2000',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'symbol'=>'BTCUSDT',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'2000',
'timeInForce'=>'GTC',
]);
//okex
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'BTC-USDT',
'_number'=>'0.001',
'_price'=>'2000',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'instrument_id'=>'btc-usdt',
'price'=>'100',
'size'=>'0.001',
]);
//huobi
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'btcusdt',
'_number'=>'0.001',
'_price'=>'2000',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'account-id'=>$account_id,
'symbol'=>'btcusdt',
'type'=>'buy-limit',
'amount'=>'0.001',
'price'=>'2001',
]);
期货
市价交易
//bitmex
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'XBTUSD',
'_number'=>'1',
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'symbol'=>'XBTUSD',
'orderQty'=>'1',
'ordType'=>'Market',
]);
//okex
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'BTC-USD-190628',
'_number'=>'1',
'_entry'=>true,//open long
]);
//也支持原生参数,与上等同
$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
]);
//也支持原生参数,与上等同
$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
]);
限价交易
//bitmex
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'XBTUSD',
'_number'=>'1',
'_price'=>100
]);
//也支持原生参数,与上等同
$exchanges->trader()->buy([
'symbol'=>'XBTUSD',
'price'=>'100',
'orderQty'=>'1',
'ordType'=>'Limit',
]);
//okex
//统一提交参数
$exchanges->trader()->buy([
'_symbol'=>'BTC-USD-190628',
'_number'=>'1',
'_price'=>'2000',
'_entry'=>true,//open long
]);
//也支持原生参数,与上等同
$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
]);
//也支持原生参数,与上等同
$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
]);
订单查询
//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
$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',
]);
账号余额与仓位获取
//binance
//Get current account information.
$exchanges->account()->get();
//bitmex
//bargaining transaction
$exchanges->account()->get([
//不填写默认返回所有仓位
//'_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',
]);
支持更底层API对象请求
使用前建议先去看看这些Bitmex Okex Huobi Binance底层已经封装过的SDK。
以下是调用底层API的发起一个新的订单实例
//binance
$exchanges->getPlatform()->trade()->postOrder([
'symbol'=>'BTCUSDT',
'side'=>'BUY',
'type'=>'LIMIT',
'quantity'=>'0.01',
'price'=>'2000',
'timeInForce'=>'GTC',
]);
//bitmex
$exchanges->getPlatform()->order()->post([
'symbol'=>'XBTUSD',
'price'=>'100',
'side'=>'Buy',
'orderQty'=>'1',
'ordType'=>'Limit',
]);
//okex
$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->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
]);
本作品采用《CC 协议》,转载必须注明作者和本文链接
你有什么特殊的优化速度技巧嘛?
@ibucoin 单从接口上的优化,还真的没什么可以优化的,网络IO以及各个平台的限制,这个硬性的条件。 主要优化的方向是你用PHP如果 怎么来进行操作,比如队列,php-cli模式,你用的框架自带的一些优化方案等等。
文档更新
请问一下。你们拿来做量化的基准是什么?什么情况下触发买入卖出。之前我有考虑过php 量化 但是,php trader 计算的macd ema 有偏差。 一般的话 量化程序都是根据 指标切入买卖点。请问您的包中指标是如何集成的
@laaa 我理解你的意思是精度问题? 如果是精度问题你用PHP高精度函数,和金钱相关的也必须用高精度函数,不然会有精度丢失的。 再说你这个问题是业务层面的东西了,对于接口来说,我这边是不做任何操作的,只返回你交易所的数据给你,得到数据由你自己处理判断的。 就目前来说 PHP开发这一块请求接口得到数据~就算做到后期也没压力的
@laaa 不同项目策略不同,我们的基准说简单点就是在什么点位买进,什么点位卖出,是基于行情数据的,交易所都提供了websocket。 现在这个SDK 还差一个行情数据的获取,后面我们也会开源, 也会集成多个交易所的数据。
我觉得我可以提供各种指标类接口 :joy:
@巴啦啦 可以啊,支持开源。让PHP做量化这块更完美。
@一个菜鸟 :joy: 首先感谢您的回复,请问您那边的交易策略基准是什么?人工输入买卖点位? 还是根据交易所数据计算macd kdj 等指标金叉死叉作为买卖点? 我的想法是根据指标进行系统自动托管量化,但是又很难找到php相关的指标资料
@巴啦啦 您可以出一些教程 或者 接口 ,我觉得量化是趋势 ,咱们phper做量化相比较python go node 还是相关资料很少。如果您愿意出一些教程 那真是感激不尽 :joy:
@一个菜鸟 https://github.com/ccxt/ccxt 这边有一个交易所接口整合包您可以参考一下
@laaa 前期还是需要人的干预,完全自动化其实是很难的。主要还是你的策略指标等等是什么。比如说高卖低买这个策略你就可以一直玩下去,所以这个没有范围的讨论是很难说的。 教程或者接口这个不好说看后期吧,其实任何一门语言都可以做量化交易程序的,就是看你有没有好的”产品人“,毕竟我们都是做技术的。 ccxt我看过了的,它确实很好很棒,但是毕竟不是我们自己的,文档也很少,还有一些特别的需求不好扩展
mark
添加 PHP 开发量化交易的工具,模拟交易所 API 数据。
添加okex永续合约,添加huobi 期货接口
我最近也是有用PHP写量化的想法,本来自己也考虑了很久,突然看到楼主已经在做SDK,所以想着用楼主的设计方案来做着试试
@lanshao 欢迎使用,如果遇到BUG或者改进意见可以提交issues进行交流
@一个菜鸟 好的,有什么问题随时交流
添加了kucoin交易所,以及各种小BUG的修复等等
修改市价深度、买卖查询默认关闭等等已经其他BUG
添加币安期货接口
新增多个交易所,mxc coinbase bitfinex zb
boss看到你们招PHP :joy:
@j475523225 最近忙,没来论坛。 欢迎来骚扰
添加多个交易所的 websocket
建议将常用的几个指标整合进sdk里。 @巴啦啦 ,兄弟,求一份你的指标计算的代码。