Skip to content

Instantly share code, notes, and snippets.

@codeninja
Last active May 23, 2023 05:57
Show Gist options
  • Save codeninja/7a4d13106b386d2c545d88fd1ba60313 to your computer and use it in GitHub Desktop.
Save codeninja/7a4d13106b386d2c545d88fd1ba60313 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 script will monitor for all pairs and find any open orders for that pair.
// I've done my best to account for throttling and make the script robust.
// save locally, add your api key, and run with "node <script name>" in console.
// You'll get alerted (in console) when an order fills.
// if you'd like to send a donation my way:
// BTC: 1JD595vphvXmWEwpjiUmfw3QzsBtE9jUP7
// LTC: LcaHE2dqrH73xLtoKXwDZEUtDiY42iAvk4
// ETH: 0x110191093ffab1f0d3d6012cc0de15f42257b7f6
// BNB: 0x110191093ffab1f0d3d6012cc0de15f42257b7f6
// codeninja@gmail.com for questions... I'll do my best to support the script if you find any bugs.
binanceNode = require('binance-api-node')["default"];
const api = binanceNode({apiKey: '*****', apiSecret: '****'});
var orders = {}
var openOrders = async function(pair){
var res = await api.openOrders({symbol: pair})
for( i in res){
// save the open order that we know about
orders[res[i]['orderId']] = res[i]
// check back in 10 seconds, modify this to check more frequently,
// but note that there's a limit of 10 requests per second to the API
setTimeout(checkOrderStatus, 10*1000, res[i]['orderId'])
}
// recheck our open orders every 1 seconds
setTimeout(openOrders, 10*1000, pair);
}
// updates an order and notifies when it's status changes.
var checkOrderStatus = async function(orderId){
o = orders[orderId];
var res = await api.getOrder({symbol:o['symbol'], orderId: orderId})
// alert if our status has changed.
if(res['status'] != o['status']){
// this could play a sound, send a text, or rickroll
console.log('ALERT! Status for', o['orderId'], o['symbol'], o['price'], 'has changed to', res['status`'])
}
o[orderId] = res;
// call ourselves in 10 more seconds if the status isn't filled
if(res['status'].includes('NEW', "PARTIALLY_FILLED")){
setTimeout(checkOrderStatus, 10*1000, orderId)
}
}
var delay = 0;
// if you don't want to check all pairs, then use this instead
// pairs= ['ETHBTC', 'LTCBTC']
// for(i in pairs){
// // start a query, on a delay, for each pair.
// const pair = res['symbols'][i]['symbol']
// // openOrders(pair)
// var new_check = setTimeout(openOrders, 1000+delay, res['symbols'][i]['symbol'])
// delay += 1000 //100 ms added to delay for each symbol to avoid spamming the API and being banned.
// }
// checks all pairs on a delay
api.exchangeInfo().then((res)=>{
for(i in res['symbols']){
// start a query, on a delay, for each pair.
const pair = res['symbols'][i]['symbol']
// openOrders(pair)
var new_check = setTimeout(openOrders, 1000+delay, res['symbols'][i]['symbol'])
delay += 1000 //1s added to delay for each symbol to avoid spamming the API and being banned.
}
})
@linglung
Copy link

how to solve this?
Uncaught SyntaxError: Identifier 'api' has already been declared at :1:1

@Gustraa
Copy link

Gustraa commented Jun 9, 2022

how to solve this? Uncaught SyntaxError: Identifier 'api' has already been declared at :1:1

Easy to use, Try This https://github.com/PiyushDixit96/binance-spot-order-notification-heroku

@codeninja
Copy link
Author

codeninja commented Jun 9, 2022 via email

@tufac2
Copy link

tufac2 commented May 23, 2023

Does it work with binance testnet? I always got [] empty but I have checked the account's balance and orders are being place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment