-
-
Save fsboehme/89f5c239a3248df6443327b54f7f989b to your computer and use it in GitHub Desktop.
Node JS Script to check Binance for open orders and notify when they are filled.
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
// This script will monitor your open orders on Binance. | |
// You'll get alerted (in Terminal and via voice prompt) when an order changes status (e,g, filled, canceled). | |
// Save locally, add your api key, and run with "node <script name>" in Terminal. | |
// If you don't have node installed: | |
// brew install node | |
// Install other packages used: | |
// npm i requirejs binance-api-node say dateformat | |
binanceNode = require('binance-api-node')["default"]; | |
const api = binanceNode({apiKey: '*****', apiSecret: '****'}); | |
const say = require('say'); // for voice prompts | |
const df = require('dateformat'); // for human-readable time stamps | |
var frequency = 60*1000; // 60 secs - frequency at which to update orders | |
var orders = {}; | |
var openOrders = async function(){ | |
// api query wrapped in try/catch to prevent crashing | |
try { | |
var res = await api.openOrders(); | |
} catch (err) { | |
console.log('ERROR in openOrders, trying again:', err); | |
setTimeout(openOrders, frequency); // retry | |
return false; | |
} | |
for( i in res){ | |
// save the open order that we | |
var order = res[i]; | |
var orderId = order['orderId']; | |
if (!(orderId in orders)) { | |
console.log(order); | |
orders[orderId] = order; | |
// check back in 'frequency' seconds, offsetting each order by 1/10 sec to avoid rate limits | |
setTimeout(checkOrderStatus, frequency+i*100, orderId); | |
} | |
} | |
console.log(df(new Date(), 'HH:MM:ss.l'), '---', Object.keys(orders).length, 'orders'); | |
// recheck our open orders every n seconds | |
setTimeout(openOrders, frequency); | |
} | |
// updates an order and notifies when it's status changes. | |
var checkOrderStatus = async function(orderId){ | |
var o = orders[orderId]; | |
// api query wrapped in try/catch to prevent crashing | |
try { | |
var res = await api.getOrder({symbol:o['symbol'], orderId: orderId}); | |
} catch (err) { | |
console.log('ERROR in checkOrderStatus, trying again:', err); | |
setTimeout(checkOrderStatus, frequency, orderId); // retry | |
return false; | |
} | |
// alert if our status has changed. | |
if(res['status'] != o['status']){ | |
say.speak('buynance order '+res['status']); | |
console.log(res['status'], '---', o['orderId'], o['side'], o['origQty'], o['symbol'], 'at', o['price']) | |
} | |
o[orderId] = res; | |
// call ourselves in 10 more seconds if the status isn't filled | |
if(res['status'].includes('NEW', "PARTIALLY_FILLED")){ | |
setTimeout(checkOrderStatus, frequency, orderId) | |
} else { | |
delete orders[orderId] | |
} | |
} | |
openOrders(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment