Skip to content

Instantly share code, notes, and snippets.

@Enalmada
Forked from codeninja/openOrderMonitor.js
Created January 12, 2021 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Enalmada/c2b20e437e0c9062290959ae25b872c7 to your computer and use it in GitHub Desktop.
Save Enalmada/c2b20e437e0c9062290959ae25b872c7 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.
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment