如何通过推API(WAMP协议)更新poloniex订单 [英] How to up to date poloniex orderbook via push api (WAMP protocol)

查看:108
本文介绍了如何通过推API(WAMP协议)更新poloniex订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在poloniex.com上制作剥头皮软件 为此,我需要有关订单的最新信息. API文档谈到了推api. 据我了解,它的工作原理是这样的:

I making software for scalping on poloniex.com To do this, I need to have fresh information about order book. API DOCUMENTATION said about push api. As i understand right it work like that:

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

  1. Use returnOrderBook (public method API) for getting snapshot
  2. Take sequence number (seq key) from responce
  3. Subscribe to push api with sequence number from snapshot
  4. Recive fresh data and make correction on snapshot data.

<?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();
    }
}

这是脚本日志:

    ./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,...

As you can see sequence number in snapshot is: 106470819 But sequence number recived from push API is not correlation with snapshot sequence number: 106307669, 106307670, ...

对于WAMP,我使用Thruway.我阅读了文档并进行了谷歌搜索,但找不到解决方案.

For working with WAMP i use Thruway. I read docs and googling, but can't found solution.

P.S.现在我认为我不明白poloniex api的工作原理-( P.P.S对不起,我英语不好.这不是我的祖国

P.S. Now i think that i not understand right how poloniex api work -( P.P.S sorry for my ugly English. It is not my native

推荐答案

WAMP事情现在似乎完全没有用,但是无论如何您做错了:首先,您需要订阅频道(您不需要使用任何序列号进行订阅,仅使用频道名称(例如BTC_ETH),开始接收更新(具有序列号),然后仅通过REST API获取订单,因此您可以立即使用您输入的条目开始更新通过WAMP连接重新接收(您可以从完整的订购书下载中删除之前收到的带有序列号的任何内容.)

The WAMP thing seems to be completely useless right now, but you're doing it wrong anyway: first you need to subscribe to the channel (you don't use any seq numbers for subscribing, just the channel name, e.g. BTC_ETH), start receiving the updates (with the seq numbers), and only get the orderbook through the REST API then, so you can immediately start updating it with the entries you're receiving through the WAMP connection (you can discard whatever you received with seq numbers before that from the full order book download.)

这篇关于如何通过推API(WAMP协议)更新poloniex订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆