Last active
November 27, 2021 11:44
-
-
Save magicdude4eva/b910562342d2085ece184e09345b8685 to your computer and use it in GitHub Desktop.
Binance PushBullet integration via Websocket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Uses the API as from https://github.com/binance-exchange/php-binance-api | |
Uses Pushbullet API from https://github.com/ivkos/Pushbullet-for-PHP | |
Install: | |
1) curl -s http://getcomposer.org/installer | php | |
2) php composer.phar require "jaggedsoft/php-binance-api @dev" "ivkos/pushbullet @dev" | |
3) php binance-notifi.php | |
If you like what I did, a small donation will make all my kittens happen | |
(Ripple) rPz4YgyxPpk7xqQQ9P7CqNFvK17nhBdfoy | |
(BTC) 1Mhq9SY6DzPhs7PNDx7idXFDWsGtyn7GWM | |
(ETH) 0xb0f2d091dcdd036cd26017bb0fbd6c1488fc8d04 | |
(LTC) LTfP7yJSpGFvuPqjSEKaqcjue6KSA9118y | |
(XVG) D5nBpFBaD6vmVJ5CBUhkz8E4SNWscf6pMu | |
(BNB) 0xb0f2d091dcdd036cd26017bb0fbd6c1488fc8d04 | |
Or at least follow me on Twitter: http://twitter.com/gerdnaschenweng | |
Signup to Binance: https://www.binance.com/?ref=13896895 | |
Read my guide to safe yourself some money when trading: https://www.naschenweng.info/2017/12/26/start-trading-cryptocurrency-binance/ | |
*/ | |
require './vendor/autoload.php'; | |
// Pushbullet config | |
$PUSHBULLET_ACCESS_TOKEN = "<ACCESS_TOKEN>"; | |
// Binance Config | |
$API_KEY = "<BINANCE_API_KEY>"; | |
$API_SECRET = "<BINANCE_API_SECRET>"; | |
// ----- Do not change from here (unless you really must) | |
$RECENT_ORDER_STACK = array(); // keep a history of recent orders | |
$MAX_ORDER_STACK = 500; // we keep a max of orders on the stack to skip notifications | |
echo date("Y-m-d H:i:s ") . "Starting Binance Pushbullet Notifier:" . PHP_EOL; | |
echo date("Y-m-d H:i:s ") . "To the moon... Follow me on Twitter: \033[31mhttps://twitter.com/gerdnaschenweng\033[0m" . PHP_EOL; | |
// Initialise Binance | |
echo date("Y-m-d H:i:s ") . "Initialising Binance Websocket" . PHP_EOL; | |
$api = new Binance\API($API_KEY, $API_SECRET); | |
// Initialise Pushbullet - Get your access token here: https://www.pushbullet.com/account | |
echo date("Y-m-d H:i:s ") . "Initialising PushBullet" . PHP_EOL; | |
$pushbullet = new Pushbullet\Pushbullet($PUSHBULLET_ACCESS_TOKEN); | |
$pushbullet->allDevices()->pushNote("\xF0\x9F\x9A\x80 {$NOTIFY_NAME}-notify started", "We have lift-off and are listening for trades!\nStarted @ " . date("Y-m-d H:i:s ")); | |
echo date("Y-m-d H:i:s ") . "Waiting for Order updates" . PHP_EOL; | |
// Get the balance update | |
$balance_update = function($api, $balances) { | |
// echo "Balance update".PHP_EOL; | |
// print_r($balances); | |
}; | |
$order_update = function($api, $report) { | |
global $pushbullet, $RECENT_ORDER_STACK, $MAX_ORDER_STACK, $NOTIFY_NAME; | |
$price = $report['price']; | |
$quantity = $report['quantity']; | |
$symbol = $report['symbol']; | |
$side = $report['side']; | |
$orderType = $report['orderType']; | |
$orderId = $report['orderId']; | |
$orderStatus = $report['orderStatus']; | |
$executionType = $report['executionType']; | |
$rejectionStatus = $report['rejectReason']; | |
$total = doubleval($price) * doubleval($quantity); | |
echo date("Y-m-d H:i:s ") . "-- ORDER UPDATE FOR {$symbol}: {$orderStatus} {$side}-{$orderType}" . PHP_EOL . | |
" Price={$price}, Quantity={$quantity}, OrderTotal={$total}" . PHP_EOL | |
. print_r($report, true). PHP_EOL . | |
" ---------------------------------------------------------------------" . PHP_EOL; | |
// Since Binance sends updates to partially filled orders, we will only push the first one | |
$pushguid = $executionType . "-" . $orderStatus . "-" . $orderId; | |
if (in_array($pushguid, $RECENT_ORDER_STACK)) { | |
echo date("Y-m-d H:i:s ") . "-- Skipping notification as GUID has been sent: {$pushguid}" . PHP_EOL; | |
return; | |
} | |
array_push($RECENT_ORDER_STACK, $pushguid); | |
if (count($RECENT_ORDER_STACK) >= $MAX_ORDER_STACK) { | |
array_shift($RECENT_ORDER_STACK); | |
} | |
// We use some cool emojis from from https://apps.timwhitlock.info/emoji/tables/unicode | |
$pd_title = "{$NOTIFY_NAME} Update"; $emoji = "\xF0\x9F\x98\xBC"; $price_trend=" "; | |
$order_status = $executionType . "-" . $orderStatus; | |
if ( $side == "SELL" ) { | |
switch ($order_status) { | |
case "NEW-NEW": | |
$emoji = "\xF0\x9F\x98\xBC"; // cat with wry face | |
break; | |
case "TRADE-PARTIALLY_FILLED": | |
$emoji = "\xF0\x9F\x98\xBE"; // pouting cat face | |
break; | |
case "TRADE-FILLED": | |
$emoji = "\xF0\x9F\x98\xB9"; // cat with tears of joy | |
break; | |
} | |
$pd_title = $emoji . " {$NOTIFY_NAME} {$executionType}-SELL: ${symbol}"; | |
$price_trend = "\xF0\x9F\x93\x89"; | |
} else if ( $side == "BUY" ) { | |
switch ($order_status) { | |
case "NEW-NEW": | |
$emoji = "\xF0\x9F\x98\xBC"; // cat with wry face | |
break; | |
case "TRADE-PARTIALLY_FILLED": | |
$emoji = "\xF0\x9F\x98\xBE"; // pouting cat face | |
break; | |
case "TRADE-FILLED": | |
$emoji = "\xF0\x9F\x99\x80"; // weary cat face | |
break; | |
case "EXPIRED-EXPIRED": | |
$emoji = "\xF0\x9F\x98\xBF"; // crying cat face | |
break; | |
} | |
$pd_title = $emoji . " {$NOTIFY_NAME} {$executionType}-BUY: ${symbol}"; | |
$price_trend = "\xF0\x9F\x93\x88"; | |
} | |
$pd_body = "{$price_trend} {$price}" . | |
"\n\xF0\x9F\x92\xB1 {$quantity}" . | |
"\n\xF0\x9F\x9A\xA6 {$orderStatus} {$side}-{$orderType}" . | |
"\n\xF0\x9F\x92\xB0 {$total}" . | |
(($rejectionStatus != "NONE") ? "\n\xE2\x9D\x97 {$rejectionStatus}" : "") . | |
"\n\xF0\x9F\x95\x98 " . date("Y-m-d H:i:s ") | |
; | |
echo date("Y-m-d H:i:s ") . "-- Sending notification with GUID: {$pushguid}" . PHP_EOL; | |
$pushbullet->allDevices()->pushNote($pd_title, $pd_body); | |
}; | |
$api->userData($balance_update, $order_update); |
Rev#6:
- Added some bugfixes and formatting
- Included time-stamp for logging
Rev#8:
- Bug fixes
- Who does not like emoji?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Donations are always welcome
🍺 Please support me: If the above helped you in any way, then follow me on Twitter or send me some coins:
Go to Curve.com to add your Crypto.com card to ApplePay and signup to Crypto.com for a staking and free Crypto debit card.
Use Binance Exchange to trade #altcoins. Sign up with Coinbase and instantly get $10 in BTC. I also accept old-school PayPal.
If you have no crypto, follow me at least on Twitter.
If you want to run the above under supervisord, do the following
sudo apt-get install -y supervisor
and then copy the following config (adjust path-names) to /etc/supervisor/conf.d:after that: