1
+ var http = require ( 'http' ) ;
2
+ const Binance = require ( 'node-binance-api' ) ;
3
+ require ( 'dotenv' ) . config ( ) ;
4
+
5
+ // B i n a n c e - N o d e - A P I O p t i o n s
6
+ const binance = new Binance ( ) . options ( {
7
+ APIKEY : process . env . BINANCE_APIKEY ,
8
+ APISECRET : process . env . BINANCE_SECRET ,
9
+ useServerTime : true ,
10
+ recvWindow : 1500 , // Set a higher recvWindow to increase response timeout
11
+ verbose : false , // Add extra output when subscribing to WebSockets, etc
12
+ test : true ,
13
+ reconnect : true
14
+ // to do: enable Logging
15
+ } ) ;
16
+
17
+ const port = 80
18
+
19
+ async function buy ( ) {
20
+ try {
21
+ let usdtBal , qty , price ;
22
+ let balances_data = await binance . futuresBalance ( ) , assets = Object . keys ( balances_data ) ;
23
+ for ( let asset of assets ) {
24
+ let obj = balances_data [ asset ] , bal = Number ( obj . balance ) ;
25
+ if ( obj . asset == 'USDT' ) {
26
+ console . log ( `USDT balance: ${ bal } ` ) ;
27
+ usdtBal = bal ;
28
+ //const response = await binance.futuresMarketBuy( 'BTCUSDT', qty ) <-----------------------!
29
+ //console.log(response) <--------------------------------------------------------------------!
30
+ }
31
+ }
32
+ } catch ( error ) {
33
+ console . error ( error )
34
+ }
35
+ }
36
+
37
+ async function sell ( ) {
38
+ try {
39
+ let qty = 0.0016 ;
40
+ let balances_data = await binance . futuresBalance ( ) , assets = Object . keys ( balances_data ) ;
41
+ for ( let asset of assets ) {
42
+ let obj = balances_data [ asset ] , bal = Number ( obj . balance ) ;
43
+ if ( obj . asset == 'USDT' ) { // <-------------------------------------------------CHANGE TO BTC
44
+ console . log ( `BTC balance ${ bal } ` ) ;
45
+ qty = bal * 0.1 ;
46
+ console . log ( `10% of BTC balance ${ qty } ` ) ;
47
+ // response = await binance.futuresMarketSell( 'BTCUSDT', qty ) <-------------------------------UNCOMMENT
48
+ // console.log( response )
49
+ }
50
+ }
51
+ } catch ( error ) {
52
+ console . error ( error )
53
+ }
54
+ }
55
+
56
+ async function stop ( ) {
57
+ try {
58
+ let balances_data = await binance . futuresBalance ( ) , assets = Object . keys ( balances_data ) ;
59
+ for ( let asset of assets ) {
60
+ let obj = balances_data [ asset ] , bal = Number ( obj . balance ) ;
61
+ if ( obj . asset == 'BTC' ) {
62
+ console . log ( bal ) ;
63
+ //const response = await binance.futuresMarketSell( 'BTCUSDT', bal ) <-------------------------------!
64
+ //console.log(response) <----------------------------------------------------------------------------!
65
+ }
66
+ }
67
+ } catch ( error ) {
68
+ console . error ( error )
69
+ }
70
+ }
71
+
72
+ //create a server object:
73
+ http . createServer ( function ( req , res ) {
74
+ if ( req . url == '/balances' ) {
75
+ balances ( ) ;
76
+ message = 'Balances Success' ;
77
+ }
78
+ else if ( req . url == '/stop' ) {
79
+ stop ( ) ;
80
+ message = 'Stop Success' ;
81
+ }
82
+ else if ( req . url == '/buy' ) {
83
+ buy ( ) ;
84
+ message = 'Buy Success' ;
85
+ }
86
+ else if ( req . url == '/sell' ) {
87
+ sell ( ) ;
88
+ message = 'Sell Success' ;
89
+ }
90
+ else {
91
+ message = '404' ;
92
+ }
93
+
94
+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
95
+ res . write ( message ) ; //write a response to the client
96
+ res . end ( ) ; //end the response
97
+
98
+ } ) . listen ( port ) ; //the server object listens on port 80
99
+ console . log ( 'Listening on ' + port )
0 commit comments