如何通过push api(WAMP协议)获得最新的poloniex订单

时间:2017-06-17 19:14:45

标签: php api autobahn wamp-protocol poloniex

我在poloniex.com上制作剥皮软件 为此,我需要获得有关订单的新信息。 API DOCUMENTATION说关于推送api。 据我所知,它的工作原理如下:

  1. 使用returnOrderBook(公共方法API)获取快照
  2. 从响应中获取序列号(seq键)
  3. 使用快照中的序列号订阅push api
  4. 重新获取新数据并对快照数据进行更正。

    <?php
    namespace Crypto\Scalper\Cli;
    use AppConfig;
    use Monolog\Logger;
    use Monolog\Handler\StreamHandler;
    use AndreasGlaser\PPC\PPC;
    
    use Thruway\ClientSession;
    use Thruway\Peer\Client;
    use Thruway\Transport\PawlTransportProvider;
    
    use Psr\Log\NullLogger;
    
    
    
    /**
     * Class PoloniexSyncCli
     * @package Crypto\Scalper\Cli
     */
    class PoloniexSyncCli
    {
        private $log;
        private $orderbooks;
    
        /**
         * Constructor.
         */
        public function __construct()
        {
            // Logging
            $this->log = new Logger('PoloniexSyncCli');
            $this->log->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
        }
    
        public function loop()
        {
            $this->log->info('Sync poloniex data');
            while (true) {
                $this->getOrderbooks();
                $this->subscribe();
                sleep(10);
            }
        }
    
    
        /**
         * Get orderbook snapshot
         */
        private function getOrderbooks()
        {
            $this->log->info('Getting order book snapshot (REST API)');
            $poloniex = AppConfig::get('poloniex');
            $ppc = new PPC($poloniex['apiKey'], $poloniex['secret']);
            $result = $ppc->getOrderBook('USDT_BTC', 50);
            if (array_key_exists('error', $result->decoded)) {
                $this->log->error("Error on REST API request: {$result->decoded['error']}");
                exit;
            }
    
            $this->orderbooks = $result->decoded;
            $this->log->info("Seq: {$this->orderbooks['seq']}"); // THIS IS sequence number
        }
    
    
        /**
         * Subscribe to feed for getting fresh orderbook data
         */
        private function subscribe() {
            $this->log->info('Subscribe to feed (WAMP)');
            $client = new Client("realm1");
            $client->addTransportProvider(new PawlTransportProvider("wss://api.poloniex.com"));
    
            $client->on('open', function (ClientSession $session) {
                $marketEvent = function ($args, $argsKw, $details, $publicationId) {
                    echo "Orderbook update: seq: $argsKw->seq, args count: ".count($args)."\n";
                };
    
    
                /**
                 * All problem here
                 * As i understand right i need send seq number on subscribe
                 * and start recive data from that number
                 * But i recive data with another numbers -(
                 */
                $session->subscribe('USDT_BTC', $marketEvent, ['seq' => $this->orderbooks['seq']]);
    
            });
    
            $client->on('close', function ($reason){
                $this->log->info("Соединение с Web socket было закрыто со стороны сервера, причина: $reason");
            });
    
            $client->on('error', function ($errorUri){
                $this->log->error("Произошла ошибка во время синхронизации по Web socket, причина: $errorUri");
                exit;
            });
    
    
            $client->start();
        }
    }
    
  5. 这是脚本日志:

        ./poloniex-sync.php
        PoloniexSyncCli.INFO: Sync poloniex data
        PoloniexSyncCli.INFO: Getting order book snapshot (REST API)
        PoloniexSyncCli.INFO: Seq: 106470819
        PoloniexSyncCli.INFO: Subscribe to feed (WAMP)
        Orderbook update: seq: 106307669, args count: 2
        Orderbook update: seq: 106307670, args count: 2
        Orderbook update: seq: 106307671, args count: 1
        Orderbook update: seq: 106307672, args count: 5
        Orderbook update: seq: 106307673, args count: 2
        Orderbook update: seq: 106307674, args count: 2
        Orderbook update: seq: 106307675, args count: 1
        Orderbook update: seq: 106307676, args count: 2
        Orderbook update: seq: 106307677, args count: 1
        Orderbook update: seq: 106307678, args count: 1
        Orderbook update: seq: 106307679, args count: 2
        Orderbook update: seq: 106307680, args count: 1
        Orderbook update: seq: 106307681, args count: 2
        Orderbook update: seq: 106307682, args count: 1
        Orderbook update: seq: 106307683, args count: 1
        Orderbook update: seq: 106307684, args count: 1
    

    如您所见,快照中的序列号为:106470819 但是从推送API获得的序列号与快照序列号不相关:106307669,106307670,......

    对于使用WAMP,我使用Thruway。我阅读了文档和谷歌搜索,但无法找到解决方案。

    P.S。现在我认为我不明白poloniex api是如何工作的 - ( P.P.S对不起我丑陋的英语。这不是我的母语

2 个答案:

答案 0 :(得分:2)

WAMP现在似乎完全没用了,但你无论如何都做错了:首先你需要订阅频道(你没有使用任何seq号码进行订阅,只有频道名称,例如BTC_ETH ),开始接收更新(使用seq编号),然后只通过REST API获取订单,这样你就可以立即开始用你通过WAMP连接收到的条目更新它(你可以丢弃你收到的任何内容)从完整订单下载之前的seq编号。)

答案 1 :(得分:1)

(这可能是评论而不是答案,但我还不能发表评论)

Poloniex似乎与他们的WAMP服务器有问题。大多数流式消息不仅落后大约3小时,有时它们也完全不按顺序排列,并且在一对之间跳跃100和39或更多。这已经提升到Poloniex的支持,但到目前为止没有回应。