-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.js
179 lines (149 loc) · 3.94 KB
/
orders.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
const api = require('./binanceapi');
const { Binance } = api // rest api
const { BinanceWS } = api // websocket api
const { print } = require('./utils');
let env = '';
if (process.env.NODE_ENV) {
env = '.'+process.env.NODE_ENV;
}
const config = require('./config'+env);
const conf = {
api: config.api,
secret: config.secret
}
const client = new Binance(conf);
function getClient() {
return client;
}
async function get_ticker(symbol) {
try {
let response = await client.get_ticker(symbol);
//console.log(response);
return response.lastPrice;
} catch (err) {
//console.log(err);
throw new Error('Invalid get_ticker');
}
}
async function get_order_book(symbol) {
try {
let response = await client.get_order_book(symbol, 5);
//console.log(response);
let lastBid = parseFloat(response.bids[0][0]) //last buy price (bid)
let lastAsk = parseFloat(response.asks[0][0]) //last sell price (ask)
return {lastAsk:lastAsk, lastBid:lastBid};
} catch (err) {
print('Invalid Order Book');
throw new Error('Invalid Order Book');
}
}
async function get_info(symbol) {
try {
let response = await client.get_exchange_info();
//console.log(response);
if (symbol != "") {
for(let item of response.symbols) {
if (item.symbol == symbol) {
return item;
}
}
}
return response
} catch (err) {
return null;
}
}
async function buy_limit(symbol, quantity, buyPrice) {
let order = await client.buy_limit(symbol, quantity, buyPrice)
if (order.msg) {
print("%s",order.msg);
process.exit(1);
}
// Buy order created.
if (!order.orderId) {
print("Buy order without orderId");
process.exit(1);
}
return order.orderId;
}
async function get_order(symbol, orderId) {
try {
let order = await client.query_order(symbol, orderId)
if (order.msg) {
print("%s",order.msg);
process.exit(1);
}
return order
} catch (err) {
print('get_order Exception: %s' , err);
throw new Error('Invalid get_order');
}
}
async function cancel_order(symbol, orderId) {
try{
let order = await client.cancel(symbol, orderId)
if (order.msg) {
print("%s",order.msg);
process.exit(1);
}
print('Profit loss, called order, %s' , orderId)
return true
} catch (err) {
print('cancel_order Exception: %s' , err)
throw new Error('Invalid cancel_order');
}
}
async function sell_limit(symbol, quantity, sell_price){
let order = await client.sell_limit(symbol, quantity, sell_price)
if (order.msg) {
print("%s",order.msg);
process.exit(1);
}
return order
}
async function sell_market(symbol, quantity, sell_price){
let order = await client.sell_market(symbol, quantity, sell_price)
if (order.msg) {
print("%s",order.msg);
process.exit(1);
}
return order
}
async function openOrders () {
try {
let orders = await client.openOrders();
return orders;
} catch (err) {
print('openOrders Exception: %s', err);
return [];
}
}
async function get_products () {
try {
let coins = await client.get_products();
return coins;
} catch (err) {
print('get_products Exception: %s', err);
return [];
}
}
async function get_account() {
try {
let account = await client.get_account();
return account;
} catch (err) {
print('get_account Exception: %s', err);
return null;
}
}
module.exports.get_ticker = get_ticker;
module.exports.get_order_book = get_order_book;
module.exports.get_info = get_info;
module.exports.buy_limit = buy_limit;
module.exports.get_order = get_order;
module.exports.cancel_order = cancel_order;
module.exports.sell_limit = sell_limit;
module.exports.openOrders = openOrders;
module.exports.get_products = get_products;
module.exports.get_account = get_account;
module.exports.getClient = getClient;