Last active
May 23, 2023 05:57
-
-
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 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 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 | |
// [email protected] 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. | |
} | |
}) | |
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
That's saying that a constant you've previously declared as API already
exists.
Are you bringing this into another script?
Do you have another variable named API?
Thanks,
Dallas
- - - - - - - - - - - - -
Director of Engineering @ Microventures
*Professional leader of highly performant engineering teams*
*512.923.4426 | Github <https://github.com/codeninja>| Twitter
<https://twitter.com/dallaspool>| Linkedin
<https://linkedin.com/in/dallaspool>*
…On Thu, Jun 9, 2022 at 3:36 PM Gustraa ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/7a4d13106b386d2c545d88fd1ba60313#gistcomment-4195289>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAADUQV3XNTQMNXHXPN52CLVOJIUFANCNFSM5YLKE5QA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
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
how to solve this?
Uncaught SyntaxError: Identifier 'api' has already been declared at :1:1