Skip to content

Commit eebe244

Browse files
committed
Merge
2 parents 6a1d806 + 3f04413 commit eebe244

File tree

9 files changed

+1037
-65
lines changed

9 files changed

+1037
-65
lines changed

.gitignore

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
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
10-
node_modules
8+
node_modules
9+
10+
# Personal
11+
test.js

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# 'lil Bot
22
## The littlest crypto bot
33

4-
Don't you wish you could use TrandingView charts, strategies and indicators to trade REAL crypto? You can! Using TradingView alerts. When an alert is triggered on your chart a webhook contacts this app.js script and tells it to place a buy/sell market order on the binance exchange.
4+
Don't you wish you could use TrandingView charts, strategies and indicators to trade REAL crypto? You can! Using TradingView alerts, this repo, and Binance's robust API.
5+
6+
When an alert is triggered on your chart a webhook contacts this app.js script and tells it to place a buy/sell market order on the binance exchange.
57

68
## Requirements:
79

@@ -13,7 +15,7 @@ Extract it to a convenient location.
1315
### Binance Trading Account
1416

1517
[API key](https://www.binance.com/en/support/articles/360002502072)
16-
Log in and create a new api key. Beginners should disable withrawls and restrict access to your device's external ip. Copy/paste your info into options.json.
18+
Log in and create a new api key. Beginners should disable withdrawls. Remember to restrict access to your device's external ip. Copy/paste your info into options.json.
1719

1820
### NodeJS
1921

@@ -24,8 +26,6 @@ Log in and create a new api key. Beginners should disable withrawls and restrict
2426
[Download](https://ngrok.com/download)
2527
Sign up for an account and follow the instructions. Ngrok should be in the same folder as app.js.
2628

27-
**Important:** The ngrok.exe must reside in the same folder as app.js.
28-
2929
## TradingView Charts
3030

3131
To use webhooks we need TradingView **Pro** ($10.95/month).
@@ -38,7 +38,7 @@ After the pre-requisits are installed open a terminal in the cloned repository a
3838
``` bash
3939
npm init -y
4040

41-
npm install node-binance-api --save
41+
npm install
4242
```
4343

4444
## Start:
@@ -49,7 +49,7 @@ To start run the following in your terminal.
4949

5050
./ngrok http 80
5151

52-
node app.js
52+
nodemon app.js
5353
```
5454

5555
## Tips
@@ -59,4 +59,4 @@ Ngrok changes your url on startup, so you'll have to copy/paste the new url into
5959
For testing webhooks go to http://localhost:4040 and select "replay". You can feed your bot any
6060
value you want without having to wait for your alerts to be triggered.
6161

62-
To use the example bollinger ribbon pine script just copy/paste it into the tradingview pine editor at the bottom of your chart. By clicking 'new' a drop down menu displays many default scripts you can easily clone and edit to create your own custom indicators.
62+
To use the example pine scripts just copy/paste them into the tradingview pine editor at the bottom of your chart. By clicking 'new' a drop down menu displays many default scripts you can easily clone and edit to create your own custom indicators.

app.js

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
const Binance = require( 'node-binance-api' );
1313
const http = require('http');
1414
const events = require('events');
15-
16-
17-
const binance = new Binance().options('./options.json'); // <---- TODO: tweak recvWindo
18-
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: false,
25+
reconnect: true
26+
// to do: enable Logging
27+
});
1928

2029
const symbol = 'BTCUSDT';
21-
const quantity = 0.015;
30+
const quantity = 0.0016;
2231
const hostname = '127.0.0.1';
2332
const port = 80;
2433

@@ -34,53 +43,54 @@ eventEmitter.on('error', (err) => {
3443
eventEmitter.on('buy', () => {
3544

3645
binance.balance((error, balances) => {
37-
38-
if (error) {
39-
console.error(error);
40-
return;
46+
if ( error ) return console.error(error);
47+
const usdtBal = balances.USDT.available;
48+
if (balances.USDT.available > 20.00) {
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')
4159
}
42-
43-
if (balances.USDT.available > 15.00) {
44-
45-
/*
46-
* M A R K E T O R D E R - B U Y
47-
*/
48-
binance.marketBuy(symbol, quantity, (error, response) => {
49-
if (error) {
50-
console.error(error);
51-
}
52-
console.log(response)
53-
console.log("Bought " + quantity + " Order Id: " + response.orderId);
54-
}); // marketBuy
55-
} // if
5660
}) // binance.balance
5761
}) // eventemitter.on('buy')
5862

59-
6063
eventEmitter.on('sell', () => {
6164

6265
binance.balance((error, balances) => {
63-
64-
if (error) {
65-
console.error(error);
66-
return false;
67-
}
66+
if ( error ) return console.error(error);
6867

69-
if ( balances.BTC.available > quantity ) {
70-
/*
71-
* M A R K E T O R D E R - S E L L
72-
*/
73-
binance.marketSell(symbol, quantity, (error, response) => {
74-
if (error) {
75-
console.error(error);
76-
}
77-
console.log(response)
78-
console.log('Sold ' + quantity + ' Order Id: ', + response.orderId);
79-
}); // marketSell
80-
} // if
68+
if ( balances.BTC.available > quantity ) {
69+
binance.marketSell(symbol, quantity);
70+
}
71+
72+
else {
73+
74+
binance.marketSell(symbol, balances.BTC.available);
75+
}
76+
console.log(balances.BTC.available);
8177
}) // binance.balance
8278
}) // end eventemitter.on('sell')
8379

80+
// S T O P
81+
eventEmitter.on('stop', () => {
82+
83+
binance.balance((error, balances) => {
84+
if ( error ) return console.error(error);
85+
86+
if( balances.BTC.available > 0) {
87+
btcBalance = parseFloat(balances.BTC.available);
88+
console.log(btcBalance);
89+
binance.marketSell(symbol, btcBalance);
90+
}
91+
}) // binance.balance
92+
93+
}) // end eventemitter.on('stop')
8494

8595
const server = http.createServer((req, res) => {
8696
//const { headers, method, url } = req;
@@ -99,15 +109,17 @@ const server = http.createServer((req, res) => {
99109
if(body === 'sell') {
100110
eventEmitter.emit('sell'); // <---------------------- SELL
101111
}
102-
112+
113+
if(body === 'stop') {
114+
eventEmitter.emit('stop'); // <---------------------- SELL
115+
}
103116
console.log(body);
104117
res.statusCode = 200;
105118
res.end();
106119
}
107120
)}
108121
);
109122

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

options.json

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

0 commit comments

Comments
 (0)