-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
231 lines (200 loc) · 5.94 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
require('dotenv').config();
const util = require('util');
const binance = require('node-binance-api');
const readline = require('readline-promise');
const rl = new readline.default.Interface({
input: process.stdin,
output: process.stdout,
terminal: true
});
binance.options({
APIKEY: process.env.BINANCEPUB,
APISECRET: process.env.BINANCESEC,
useServerTime: true, // If you get timestamp errors, synchronize to server time at startup
// Toggle below when ready!
//test: true // If you want to use sandbox mode where orders are simulated
});
/*
List available commands and any other documentation relevant to a new user
*/
function help() {
console.log("You can use the commands: balances, cancel, help, open, price, or quit");
}
/*
See all balances, no matter how small.
Also, display USDT and BTC while we're still testing.
*/
function balance() {
binance.balance((error, balances) => {
console.log("balances()", balances);
console.log("");
console.log("USDT balance: ", balances.USDT.available);
console.log("BTC balance: ", balances.BTC.available);
console.log("");
});
}
/*
Second piece of ticker (USDT) is the trading pair
e.g. XMRBTC means BTC market, XMR
*/
async function cancel_orders(req) {
// binance.cancelOrders("BTCUSDT", (error, response, symbol) => {
// console.log(symbol+" cancel response:", response);
// console.log("");
// });
const cancelOrders = util.promisify(binance.cancelOrders);
try {
const orders = await cancelOrders(req);
console.log("Here are your cancelled orders:\n\n", orders);
console.log("");
} catch(err) {
console.error("error: " + JSON.parse(err.body).msg);
console.log("");
return;
}
}
/*
List all open orders:
const orders = await openOrders("");
For specific ticker:
const orders = await openOrders(BTCUSDT);
*/
async function open_orders() {
// binance.openOrders(false, (error, openOrders) => {
// console.log("Here are your open orders:\n\n", openOrders);
// console.log("");
// });
const openOrders = util.promisify(binance.openOrders);
try {
const orders = await openOrders("");
// Verbose, print all info
// console.log("Here are your open orders:\n\n", orders);
// console.log("");
console.log("Open orders:\n=========================\n");
orders.forEach(order => {
var dateFormat = require('dateformat');
console.log("Ticker: " + order.symbol);
console.log("Date placed: " + dateFormat(order.time, "yyyy-mm-dd h:MM:ss"));
console.log("Buy/Sell?: " + order.side);
console.log("Type: " + order.type);
console.log("");
console.log("Original Qty: " + order.origQty);
console.log("Executed Qty: " + order.origQty);
console.log("");
if (order.type == "TAKE_PROFIT_LIMIT") {
console.log("=-=-=-=-=-=\nSTOP LIMIT\n=-=-=-=-=-=");
console.log("Stop price: " + order.stopPrice);
}
console.log("Limit price: " + order.price);
console.log("");
console.log("=========================");
console.log("");
});
} catch(err) {
console.error("error: " + JSON.parse(err.body).msg);
return;
}
}
/*
Get current price of requested ticker pairing
*/
async function price(req) {
// binance.prices(req, (error, ticker) => {
// console.log("Price of "+req+":", ticker[req]);
// ticker = await prices(req);
const prices = util.promisify(binance.prices);
try {
const ticker = await prices(req);
console.log("Price of "+req+":", ticker[req]);
console.log("");
} catch (err) {
console.error("error: " + JSON.parse(err.body).msg);
console.log("");
return;
}
}
// // Prompt for ticker to check price for
// async function price_prompt() {
// const answer = await rl.questionAsync('View prices for which ticker? Put the pairing second (e.g. BNBBTC): ');
// console.log("");
// //console.log('TICKER = ',answer +"\n");
// await price(answer);
// }
/*
Replaces above commented code, used to have a separate function for each prompt.
This still isn't very DRY, but it's all in one place instead of several functions
paired with their partner functions across the codebase.
*/
async function prompt(cmd) {
switch(cmd) {
// Prompt for ticker to cancel orders for
case 'cancel':
const cancel_answer = await rl.questionAsync('View prices for which ticker? Put the pairing second (e.g. BNBBTC): ');
console.log("");
await cancel_orders(cancel_answer);
break;
// Prompt for ticker to check prices for
case 'price':
const price_answer = await rl.questionAsync('Cancel orders for which ticker? Put the pairing second (e.g. BNBBTC): ');
console.log("");
await price(price_answer);
break;
default:
//
break;
}
}
async function run() {
console.log("-------------Welcome-------------");
console.log("");
console.log("Issue a command (you can type help if you're unsure):")
while (true) {
const d = await rl.questionAsync('> ');
console.log("");
switch(d.toString().trim()) {
case 'help':
case 'list':
case 'commands':
case '?':
case 'h':
help();
break;
case 'balance':
case 'balances':
case 'b':
balance();
break;
case 'cancel':
case 'c':
await prompt('cancel');
break;
case 'open':
case 'orders':
case 'o':
await open_orders();
break;
case 'price':
case 'p':
await prompt('price');
//price("BNBBTC");
//price("WTCBTC");
break;
/*
Exit the program
*/
case 'quit':
case 'q':
case 'exit':
process.exit(0)
break;
/*
Inform the user of an error
*/
default:
console.log("Command does not exist or failed. Please type help for a list of commands.")
break;
}
}
}
// Run the program
run();