Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket UserData Futures #468

Open
faca5 opened this issue Feb 23, 2022 · 0 comments
Open

Websocket UserData Futures #468

faca5 opened this issue Feb 23, 2022 · 0 comments

Comments

@faca5
Copy link

faca5 commented Feb 23, 2022

Hello.

How to change PHP code below to start working with FUTURES. Right now it works only on SPOT.

Thank you

PHP code:
`/**

  • userData Issues userDataStream token and keepalive, subscribes to userData WebSocket

  • $balance_update = function($api, $balances) {

  • print_r($balances);

  • echo "Balance update".PHP_EOL;

  • };

  • $order_update = function($api, $report) {

  • echo "Order update".PHP_EOL;

  • print_r($report);

  • $price = $report['price'];

  • $quantity = $report['quantity'];

  • $symbol = $report['symbol'];

  • $side = $report['side'];

  • $orderType = $report['orderType'];

  • $orderId = $report['orderId'];

  • $orderStatus = $report['orderStatus'];

  • $executionType = $report['orderStatus'];

  • if( $executionType == "NEW" ) {

  • if( $executionType == "REJECTED" ) {

  • echo "Order Failed! Reason: {$report['rejectReason']}".PHP_EOL;

  • }

  • echo "{$symbol} {$side} {$orderType} ORDER #{$orderId} ({$orderStatus})".PHP_EOL;

  • echo "..price: {$price}, quantity: {$quantity}".PHP_EOL;

  • return;

  • }

  • //NEW, CANCELED, REPLACED, REJECTED, TRADE, EXPIRED

  • echo "{$symbol} {$side} {$executionType} {$orderType} ORDER #{$orderId}".PHP_EOL;

  • };

  • $api->userData($balance_update, $order_update);

  • @param $balance_callback callable function

  • @param bool $execution_callback callable function

  • @return null

  • @throws \Exception
    */
    public function userData(&$balance_callback, &$execution_callback = false)
    {
    $response = $this->httpRequest("v1/userDataStream", "POST", []);
    $this->listenKey = $response['listenKey'];

    $this->info['balanceCallback'] = $balance_callback;
    $this->info['executionCallback'] = $execution_callback;

    $this->subscriptions['@userdata'] = true;

    $loop = \React\EventLoop\Factory::create();
    $loop->addPeriodicTimer(30*60, function () {
    $listenKey = $this->listenKey;
    $this->httpRequest("v1/listenKey={$listenKey}", "PUT", []);
    });
    $connector = new \Ratchet\Client\Connector($loop);

    // @codeCoverageIgnoreStart
    // phpunit can't cover async function
    $connector($this->getWsEndpoint() . $this->listenKey)->then(function ($ws) {
    $ws->on('message', function ($data) use ($ws) {
    if ($this->subscriptions['@userdata'] === false) {
    //$this->subscriptions[$endpoint] = null;
    $ws->close();
    return; //return $ws->close();
    }
    $json = json_decode($data);
    $type = $json->e;
    if ($type === "outboundAccountPosition") {
    $balances = $this->balanceHandler($json->B);
    $this->info['balanceCallback']($this, $balances);
    } elseif ($type === "executionReport") {
    $report = $this->executionHandler($json);
    if ($this->info['executionCallback']) {
    $this->info['executionCallback']($this, $report);
    }
    }
    });
    $ws->on('close', function ($code = null, $reason = null) {
    // WPCS: XSS OK.
    echo "userData: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
    });
    }, function ($e) {
    // WPCS: XSS OK.
    echo "userData: Could not connect: {$e->getMessage()}" . PHP_EOL;
    });

    $loop->run();
    }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant