Skip to content

Commit 2d82d45

Browse files
committed
added nodemon
1 parent 7dda360 commit 2d82d45

File tree

7 files changed

+980
-23
lines changed

7 files changed

+980
-23
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
# Options file with pub/priv keys
2-
# options.json
3-
41
# ngrok.exe
52
ngrok.exe
63

7-
# Personal Modules
4+
# Binance Keys
5+
.env
86

97
# Nodejs Modules
108
node_modules

app.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@
1212
const Binance = require( 'node-binance-api' );
1313
const http = require('http');
1414
const events = require('events');
15-
15+
require('dotenv').config();
16+
17+
// B i n a n c e - N o d e - A P I O p t i o n s
18+
const binance = new Binance().options({
19+
APIKEY: process.env.BINANCE_APIKEY,
20+
APISECRET: process.env.BINANCE_SECRET,
21+
useServerTime: true,
22+
recvWindow: 1500, // Set a higher recvWindow to increase response timeout
23+
verbose: false, // Add extra output when subscribing to WebSockets, etc
24+
test: true,
25+
reconnect: true
26+
// to do: enable Logging
27+
});
1628

1729
const binance = new Binance().options('./options.json'); // <---- TODO: tweak recvWindo
1830

19-
2031
const symbol = 'BTCUSDT';
2132
const quantity = 0.015;
2233
const hostname = '127.0.0.1';
@@ -40,11 +51,11 @@ eventEmitter.on('buy', () => {
4051
return;
4152
}
4253

43-
if (balances.USDT.available > 200.00) {
54+
if (balances.USDT.available > 20.00) {
4455

45-
/*
46-
* M A R K E T O R D E R - B U Y
47-
*/
56+
/*
57+
* M A R K E T O R D E R - B U Y
58+
*/
4859
binance.marketBuy(symbol, quantity, (error, response) => {
4960
if (error) {
5061
console.error(error);
@@ -56,7 +67,6 @@ eventEmitter.on('buy', () => {
5667
}) // binance.balance
5768
}) // eventemitter.on('buy')
5869

59-
6070
eventEmitter.on('sell', () => {
6171

6272
binance.balance((error, balances) => {
@@ -81,7 +91,6 @@ eventEmitter.on('sell', () => {
8191
}) // binance.balance
8292
}) // end eventemitter.on('sell')
8393

84-
8594
const server = http.createServer((req, res) => {
8695
//const { headers, method, url } = req;
8796
let body = [];
@@ -107,7 +116,6 @@ const server = http.createServer((req, res) => {
107116
)}
108117
);
109118

110-
111119
server.listen(port, hostname, () => {
112120
console.log(`Server running at http://${hostname}:${port}/`);
113121
});

index.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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)

options.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)