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

futures #342

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 65 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@
[![Code consistency](https://squizlabs.github.io/PHP_CodeSniffer/analysis/jaggedsoft/php-binance-api/grade.svg?style=flat-square)](https://squizlabs.github.io/PHP_CodeSniffer/analysis/jaggedsoft/php-binance-api)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/683459a5a71c4875956cf23078a0c39b)](https://www.codacy.com/app/dmzoneill/php-binance-api?utm_source=github.com&utm_medium=referral&utm_content=jaggedsoft/php-binance-api&utm_campaign=Badge_Grade)
-->
# Updates - Futures

- userDataStream (you can add func as third parameter which starts on ws connect)
- userDataStreamF (same but for futures)

```php
global $api;
$quantity = 1.00;
$price = 59000.58;
$api->sell("BTCUSDT", $quantity, $price, "LIMIT", [], true);
// true paramater is for futures
// if its true then it using futures server else default
```

# PHP Binance API

This project is designed to help you make your own projects that interact with the [Binance API](https://github.com/binance-exchange/binance-official-api-docs). You can stream candlestick chart data, market depth, or use other advanced features such as setting stop losses and iceberg orders. This project seeks to have complete API coverage including WebSockets.

#### Installation
Expand Down Expand Up @@ -40,10 +55,25 @@ php composer.phar require "jaggedsoft/php-binance-api @dev"

</details>

#### Getting started
#### Getting started FAPI
```php
require 'vendor/autoload.php';
require 'php-binance-fapi.php';
// 1. config in home directory
$fapi = new Binance\FAPI();
// 2. config in specified file
$fapi = new Binance\FAPI( "somefile.json" );
// 3. config by specifying api key and secret
$fapi = new Binance\FAPI("<api key>","<secret>");
// 4. config by specifying api key, api secret and testnet flag. By default the testnet is disabled
$fapi = new Binance\FAPI("<testnet api key>","<testnet secret>", true);
```

#### Getting started API
`composer require jaggedsoft/php-binance-api`
```php
require 'vendor/autoload.php';
require 'php-binance-api.php';
// 1. config in home directory
$api = new Binance\API();
// 2. config in specified file
Expand Down Expand Up @@ -1193,34 +1223,43 @@ bid: 0.00022258

#### User Data: Account Balance Updates, Trade Updates, New Orders, Filled Orders, Cancelled Orders via WebSocket
```php
$balance_update = function($api, $balances) {
print_r($balances);
echo "Balance update".PHP_EOL;
$run = function () {
global $api;
$quantity = 1.00;
$price = 59000.58;
$api->sell("BTCUSDT", $quantity, $price, "LIMIT", [], true);
echo "Startup Function Completed";
};

$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;
$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);

$api->userData($balance_update, $order_update, $run);
```

<details>
Expand Down
6 changes: 5 additions & 1 deletion php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2359,12 +2359,13 @@ public function keepAlive()
* @return null
* @throws \Exception
*/
public function userData(&$balance_callback, &$execution_callback = false)
public function userData(&$balance_callback, &$execution_callback = false, &$runFunction = false)
{
$response = $this->httpRequest("v1/userDataStream", "POST", []);
$this->listenKey = $response['listenKey'];
$this->info['balanceCallback'] = $balance_callback;
$this->info['executionCallback'] = $execution_callback;
$this->info['run'] = $runFunction;

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

Expand All @@ -2378,6 +2379,9 @@ public function userData(&$balance_callback, &$execution_callback = false)
// @codeCoverageIgnoreStart
// phpunit can't cover async function
$connector($this->getWsEndpoint() . $this->listenKey)->then(function ($ws) {
if ($this->info['run'] != false) {
$this->info['run']();
}
$ws->on('message', function ($data) use ($ws) {
if ($this->subscriptions['@userdata'] === false) {
//$this->subscriptions[$endpoint] = null;
Expand Down
Loading