Skip to content

Commit 4b074d1

Browse files
committed
added stop, and now buys 100% of avail. balance
1 parent 2d82d45 commit 4b074d1

File tree

6 files changed

+90
-139
lines changed

6 files changed

+90
-139
lines changed

app.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ const binance = new Binance().options({
2121
useServerTime: true,
2222
recvWindow: 1500, // Set a higher recvWindow to increase response timeout
2323
verbose: false, // Add extra output when subscribing to WebSockets, etc
24-
test: true,
24+
test: false,
2525
reconnect: true
2626
// to do: enable Logging
2727
});
2828

29-
const binance = new Binance().options('./options.json'); // <---- TODO: tweak recvWindo
30-
3129
const symbol = 'BTCUSDT';
32-
const quantity = 0.015;
30+
const quantity = 0.0016;
3331
const hostname = '127.0.0.1';
3432
const port = 80;
3533

@@ -45,52 +43,51 @@ eventEmitter.on('error', (err) => {
4543
eventEmitter.on('buy', () => {
4644

4745
binance.balance((error, balances) => {
48-
49-
if (error) {
50-
console.error(error);
51-
return;
52-
}
53-
46+
if ( error ) return console.error(error);
47+
const usdtBal = balances.USDT.available;
5448
if (balances.USDT.available > 20.00) {
55-
56-
/*
57-
* M A R K E T O R D E R - B U Y
58-
*/
59-
binance.marketBuy(symbol, quantity, (error, response) => {
60-
if (error) {
61-
console.error(error);
62-
}
63-
console.log(response)
64-
console.log("Bought " + quantity + " Order Id: " + response.orderId);
65-
}); // marketBuy
66-
} // if
49+
binance.bookTickers('BTCUSDT', (error, ticker) => {
50+
tickAsk = ticker.askPrice;
51+
let qty = usdtBal/tickAsk;
52+
qty = qty.toFixed(5);
53+
console.log(tickAsk, usdtBal, qty);
54+
binance.marketBuy(symbol, qty);
55+
});
56+
}
57+
else {
58+
console.log('Balance < 20.00')
59+
}
6760
}) // binance.balance
6861
}) // eventemitter.on('buy')
6962

7063
eventEmitter.on('sell', () => {
7164

7265
binance.balance((error, balances) => {
73-
74-
if (error) {
75-
console.error(error);
76-
return false;
77-
}
66+
if ( error ) return console.error(error);
7867

79-
if ( balances.BTC.available > quantity ) {
80-
/*
81-
* M A R K E T O R D E R - S E L L
82-
*/
83-
binance.marketSell(symbol, quantity, (error, response) => {
84-
if (error) {
85-
console.error(error);
86-
}
87-
console.log(response)
88-
console.log('Sold ' + quantity + ' Order Id: ', + response.orderId);
89-
}); // marketSell
90-
} // if
68+
if ( balances.BTC.available > quantity ) {
69+
binance.marketSell(symbol, quantity);
70+
}
71+
72+
else {
73+
let btcBalance = balances.BTC.available;
74+
binance.marketSell(symbol, btcBalance);
75+
}
9176
}) // binance.balance
9277
}) // end eventemitter.on('sell')
9378

79+
// S T O P
80+
eventEmitter.on('stop', () => {
81+
82+
binance.balance((error, balances) => {
83+
84+
if ( error ) return console.error(error);
85+
86+
let btcBalance = balances.BTC.available;
87+
binance.marketSell(symbol, btcBalance);
88+
}) // binance.balance
89+
}) // end eventemitter.on('stop')
90+
9491
const server = http.createServer((req, res) => {
9592
//const { headers, method, url } = req;
9693
let body = [];
@@ -108,7 +105,10 @@ const server = http.createServer((req, res) => {
108105
if(body === 'sell') {
109106
eventEmitter.emit('sell'); // <---------------------- SELL
110107
}
111-
108+
109+
if(body === 'stop') {
110+
eventEmitter.emit('stop'); // <---------------------- SELL
111+
}
112112
console.log(body);
113113
res.statusCode = 200;
114114
res.end();

index.js

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

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"author": "EM Greeff",
2121
"license": "MIT",
2222
"dependencies": {
23+
"dotenv": "^8.2.0",
2324
"node-binance-api": "^0.9.8",
2425
"nodemon": "^2.0.4"
2526
}

stop.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
eventEmitter.on('stop', () => {
3+
binance.balance((error, balances) => {
4+
if ( error ) return console.error(error);
5+
let btcBalance = balances.BTC.available;
6+
binance.marketSell(symbol, btcBalance);
7+
}) // binance.balance
8+
}) // end eventemitter.on('stop')
9+

test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const http = require('http');
2+
const events = require('events');
3+
const Binance = require( 'node-binance-api' );
4+
require('dotenv').config();
5+
6+
// B i n a n c e - N o d e - A P I O p t i o n s
7+
const binance = new Binance().options({
8+
APIKEY: process.env.BINANCE_APIKEY,
9+
APISECRET: process.env.BINANCE_SECRET,
10+
useServerTime: true,
11+
recvWindow: 2500, // Set a higher recvWindow to increase response timeout
12+
verbose: false, // Add extra output when subscribing to WebSockets, etc
13+
test: true,
14+
reconnect: true
15+
// to do: enable Logging
16+
});
17+
18+
const symbol = 'BTCUSDT';
19+
const quantity = 0.0016;
20+
21+
binance.balance((error, balances) => {
22+
if ( error ) return console.error(error);
23+
const usdtBal = balances.USDT.available;
24+
if (balances.USDT.available > 20.00) {
25+
binance.bookTickers('BTCUSDT', (error, ticker) => {
26+
tickAsk = ticker.askPrice;
27+
let qty = usdtBal/tickAsk;
28+
console.log(tickAsk, usdtBal, qty);
29+
binance.marketBuy(symbol, qty);
30+
});
31+
}
32+
else {
33+
console.log('Balance < 20.00')
34+
}
35+
}) // binance.balance

0 commit comments

Comments
 (0)