-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbinanceapi.js
219 lines (184 loc) · 5.17 KB
/
binanceapi.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
const ccxt = require('ccxt')
var underscore = require('underscore');
require('dotenv').config()
const { env } = process
const HttpsProxyAgent = require('https-proxy-agent')
const fetch = require('node-fetch')
const proxy = env.PROXYURL // HTTP/HTTPS proxy to connect to
const agent = new HttpsProxyAgent(proxy)
const jingdu = require('./xiadanjingdu')
const requestMethod = function(url, options) {
return fetch(url, Object.assign({}, options, { agent: agent }))
}
let bian = null
if(env.PROXYUSE == '1')
{
bian = new ccxt.binance({
'apiKey': process.env.APIKEY,
'secret': process.env.SECRETKEY,
//'timeout': 30000,
'hostname':process.env.BINANHOSTNAME,
'enableRateLimit': true,
'options': {
'defaultType': 'future',
},
'fetchImplementation':requestMethod,
})
}
else
{
bian = new ccxt.binance({
'apiKey': process.env.APIKEY,
'secret': process.env.SECRETKEY,
//'timeout': 30000,
'hostname':process.env.BINANHOSTNAME,
'enableRateLimit': true,
'options': {
'defaultType': 'future',
},
})
}
const getnowprice = async(symbol) =>{
let res = await bian.fetchFundingRate(symbol);
return res;
}
//获取持仓信息
async function getaccount() {
let res = await bian.fapiPrivateGetAccount();
//console.log(res);
let result = new Map();
for (var i = 0; i < res.positions.length; i++) {
let chicang = res.positions[i];
if (Number(chicang.initialMargin) != 0) {
//console.log(chicang);
var symbol = chicang.symbol;
var startprice = chicang.entryPrice;
var leverage = chicang.leverage;
//保证金
var baozhengjin = chicang.initialMargin;
//盈利或者亏损
var win = chicang.unrealizedProfit;
var positionAmt = chicang.positionAmt;
var winpercent = Number(win)/(Number(leverage)*Number(baozhengjin))*100;
result.set(symbol,{ symbol, startprice, leverage, baozhengjin, win,positionAmt ,winpercent})
}
}
return result;
}
async function getbalance() {
let res = await bian.fapiPrivateGetBalance();
//console.log(res);
let result = 0;
for (var i = 0; i < res.length; i++) {
let chicang = res[i];
if ((chicang.asset == "USDT") ||(chicang.asset == "BUSD")) {
//console.log(chicang);
result += Number(chicang.withdrawAvailable)
}
}
return result;
}
//获得当前最大杠杆水平
async function getleverageBracket(symbol)
{
let result = await bian.fapiPrivateGetLeverageBracket({
symbol,
})
var leverage = 1;
if(result.length >0)
{
for(var i =0;i<result[0].brackets.length;i++)
{
var tmp = Number(result[0].brackets[i].initialLeverage);
if(tmp >leverage)
{
leverage = tmp;
}
}
}
return leverage;
}
//设置杠杆水平
async function setLeverage(symbol, leverage = 1) {
await bian.fapiPrivatePostLeverage({
leverage,
symbol,
})
}
function getzuixiaoxiadanliang(symbol)
{
var tmp = jingdu[symbol];
if(tmp)
{
tmp = tmp.toString();
var pos = tmp.indexOf(".");
if(pos==-1)
{
return 0;
}
else
{
var length = tmp.length - 1 - pos;
return length;
}
}
else
{
return 0;
}
}
//市价平仓
async function FanFangxiangOrder(symbol, side = 'SELL', type = 'MARKET',quantity) {
quantity = Number(quantity).toFixed(getzuixiaoxiadanliang(symbol));
let res = await bian.fapiPrivatePostOrder({ symbol, side, type, quantity })
console.log(res);
}
//测试用例
async function test() {
try {
//获得账户详情
let balance = await getbalance()
console.log("账户价值:" + balance )
//await getleverageBracket("WAVESUSDT")
//测试开单
//
//await Order('BTCUSDT');
//Order('BTSsUSDT');
//getfuturepprice("BTCUSDT")
//Order("UNFIUSDT")
//getxianhuo()
//getohcv("RLC/BUSD")
//getglobalLongShortAccountRatio("1000SHIB/USDT")
}
catch (e) {
console.log(e);
}
}
async function getfuturepprice(symbol)
{
var futureprice = await (await bian.fetchTicker(symbol)).info.lastPrice;
return futureprice;
}
async function getbalance() {
let res = await bian.fapiPrivateGetBalance();
//console.log(res);
let result = 0;
for (var i = 0; i < res.length; i++) {
let chicang = res[i];
if ((chicang.asset == "USDT") ||(chicang.asset == "BUSD")) {
//console.log(chicang);
result += Number(chicang.withdrawAvailable)
}
}
return result;
}
test();
module.exports = {
setLeverage:setLeverage,
getaccount:getaccount,
getfuturepprice:getfuturepprice,
FanFangxiangOrder:FanFangxiangOrder,
getnowprice:getnowprice,
getleverageBracket:getleverageBracket,
getbalance:getbalance,
}