Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kucoin
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rongjun
kucoin
Commits
a669f277
Commit
a669f277
authored
May 27, 2019
by
zihan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
代码基本改完了
parent
306c5779
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
94 additions
and
136 deletions
+94
-136
api_kucoin.js
api_kucoin.js
+48
-46
kucoinCollector.js
kucoinCollector.js
+6
-62
kucoinStrategy3.js
kucoinStrategy3.js
+6
-7
order_kucoin.js
order_kucoin.js
+21
-17
test_kucoin.js
test_kucoin.js
+13
-4
No files found.
api_kucoin.js
View file @
a669f277
...
...
@@ -3,8 +3,10 @@ const SocksProxyAgent = require('socks-proxy-agent');
const
proxy
=
process
.
env
.
agent
;
const
agent
=
proxy
?
new
SocksProxyAgent
(
proxy
)
:
null
;
const
request
=
require
(
'request'
);
const
host
=
'https://api.kucoin.com'
;
const
proHost
=
'https://api.kucoin.com'
;
const
testHost
=
'https://openapi-sandbox.kucoin.com'
;
const
CryptoJS
=
require
(
'crypto-js'
);
const
crypto
=
require
(
'crypto'
);
const
constants
=
require
(
'./constants'
);
const
{
IPs
,
mergeDepthAsk
,
mergeDepthBids
,
mergeDepth
}
=
require
(
'./util'
);
let
totalOrderbook
=
{};
...
...
@@ -44,8 +46,12 @@ let getUpdatedData = function (currentData, response) {
class
biboxApi
{
constructor
()
{
this
.
apiKey
=
'5ba054d2e0abb830dee81e1d'
;
this
.
apiSecret
=
'baa55ba9-7290-431e-9fd4-42aa4598887f'
;
this
.
apiKey
=
'5ce8b7ae134ab72e8c06b112'
;
this
.
apiSecret
=
'd355354d-eac5-46fb-a2cf-4342f5bc7055'
;
this
.
pass
=
'Kinming0123@#$%'
;
// this.apiKey = '5c2db93503aa674c74a31734';
// this.apiSecret = 'f03a5284-5c39-4aaa-9b20-dea10bdcf8e3';
// this.pass = 'Abc123456';
this
.
allowRequest
=
true
;
this
.
index
=
0
;
}
...
...
@@ -248,33 +254,36 @@ class biboxApi {
});
}
_request
(
method
,
path
,
params
,
callback
)
{
_genSign
(
timestamp
,
method
,
path
,
params
){
const
signStr
=
timestamp
+
method
+
path
+
JSON
.
stringify
(
params
);
const
signed
=
crypto
.
createHmac
(
'sha256'
,
this
.
apiSecret
).
update
(
signStr
).
digest
(
'base64'
);
return
signed
}
_request
(
method
,
path
,
params
,
callback
,
host
=
proHost
)
{
if
(
!
this
.
allowRequest
)
{
callback
({
code
:
"-2"
,
message
:
"出现超频情况,暂停提交请求"
});
return
;
}
let
url
=
host
+
path
;
const
nonce
=
new
Date
().
getTime
();
const
strParams
=
this
.
transform
(
this
.
objKeySort
(
params
));
const
strForSign
=
path
+
'/'
+
nonce
+
'/'
+
strParams
;
const
strForSign2
=
CryptoJS
.
enc
.
Base64
.
stringify
(
CryptoJS
.
enc
.
Utf8
.
parse
(
strForSign
))
const
sign
=
CryptoJS
.
HmacSHA256
(
strForSign2
,
this
.
apiSecret
).
toString
(
CryptoJS
.
enc
.
Hex
);
const
timestamp
=
Date
.
now
()
+
''
;
const
sign
=
this
.
_genSign
(
timestamp
,
method
,
path
,
params
);
const
headers
=
{
"Accept-Language"
:
"zh_CN"
,
'KC-API-KEY'
:
this
.
apiKey
,
'KC-API-NONCE'
:
nonce
,
'KC-API-SIGNATURE'
:
sign
'KC-API-SIGN'
:
sign
,
'KC-API-TIMESTAMP'
:
timestamp
,
'KC-API-PASSPHRASE'
:
this
.
pass
,
"Content-Type"
:
"application/json"
}
const
requestParams
=
{};
let
form
=
params
if
(
method
===
'GET'
)
{
if
(
method
===
'GET'
&&
params
)
{
url
+=
'?'
+
this
.
transform
(
params
);
form
=
{}
}
else
{
requestParams
[
"body"
]
=
JSON
.
stringify
(
params
);
}
requestParams
[
"url"
]
=
url
;
requestParams
[
"method"
]
=
method
;
requestParams
[
"headers"
]
=
headers
;
requestParams
[
"form"
]
=
form
;
requestParams
[
"timeout"
]
=
10000
;
requestParams
[
"forever"
]
=
true
;
request
(
requestParams
,
(
error
,
response
,
body
)
=>
{
...
...
@@ -283,7 +292,7 @@ class biboxApi {
}
else
{
try
{
const
result
=
JSON
.
parse
(
body
);
if
(
result
&&
result
.
success
&&
result
.
code
===
'OK
'
)
{
if
(
result
&&
result
.
code
===
'200000
'
)
{
callback
(
null
,
result
);
}
else
{
callback
(
result
,
null
);
...
...
@@ -307,50 +316,43 @@ class biboxApi {
this
.
_publicRequest
(
'/api/v1/symbols'
,
{},
callback
);
}
order
(
price
,
amount
,
symbol
,
side
,
callback
)
{
//账户类型,0-普通账户,1-信用账户
//交易类型,1-市价单,2-限价单
//交易方向,1-买,2-卖
order
(
cliordId
,
price
,
amount
,
symbol
,
side
,
timeInForce
,
callback
)
{
const
params
=
{
symbol
:
symbol
,
type
:
side
===
constants
.
OrderSideBuy
?
'BUY'
:
'SELL'
,
pay_bix
:
1
,
clientOid
:
cliordId
,
side
,
symbol
,
price
,
amount
,
size
:
amount
,
timeInForce
};
this
.
_request
(
"POST"
,
"/
v1/order"
,
params
,
callback
)
this
.
_request
(
"POST"
,
"/
api/v1/orders"
,
params
,
callback
);
}
balance
(
limit
,
page
,
callback
)
{
this
.
_request
(
"GET"
,
"/
v1/account/balances"
,
{
limit
:
limit
||
20
,
page
:
page
||
1
}
,
callback
);
balance
(
callback
)
{
this
.
_request
(
"GET"
,
"/
api/v1/accounts"
,
null
,
callback
);
}
coins_info
(
callback
)
{
this
.
_publicRequest
(
"/v1/market/open/coins"
,
{},
callback
);
}
//
coins_info(callback) {
//
this._publicRequest("/v1/market/open/coins", {}, callback);
//
}
getTrades
(
symbol
,
callback
)
{
callback
(
null
,
null
)
}
searchOrder
(
orderId
,
symbol
,
side
,
callback
)
{
const
params
=
{
symbol
:
symbol
,
type
:
side
===
constants
.
OrderSideBuy
?
'BUY'
:
'SELL'
,
orderOid
:
orderId
}
this
.
_request
(
"GET"
,
"/v1/order/detail"
,
params
,
callback
);
searchOrder
(
orderId
,
callback
)
{
this
.
_request
(
"GET"
,
`/api/v1/orders/
${
orderId
}
`
,
null
,
callback
);
}
cancelOrder
(
orderId
,
symbol
,
side
,
callback
)
{
const
params
=
{
symbol
:
symbol
,
orderOid
:
orderId
,
type
:
side
===
constants
.
OrderSideBuy
?
'BUY'
:
'SELL'
,
}
this
.
_request
(
"POST"
,
"/v1/cancel-order"
,
params
,
callback
);
}
//
cancelOrder(orderId, symbol, side, callback) {
//
const params = {
//
symbol: symbol,
//
orderOid: orderId,
//
type: side === constants.OrderSideBuy ? 'BUY' : 'SELL',
//
}
//
this._request("POST", "/v1/cancel-order", params, callback);
//
}
fetchHistorOrders
(
page
,
size
,
symbol
,
side
,
callback
)
{
const
params
=
{
...
...
kucoinCollector.js
View file @
a669f277
...
...
@@ -105,77 +105,21 @@ class KucoinCollector extends BaseCollector{
}
}
_getCoinsInfo
(){
console
.
log
(
'start get coin info...'
);
this
.
api
.
coins_info
((
error
,
result
)
=>
{
if
(
error
){
console
.
error
(
'get coins info error'
);
this
.
_getCoinsInfo
();
}
else
{
if
(
result
.
data
){
coinInfoMap
=
{}
for
(
let
d
of
result
.
data
){
coinInfoMap
[
d
.
coin
]
=
d
}
console
.
log
(
'get coin info ok'
);
}
}
});
}
_runMonitor
(
callback
){
callback
({});
return
;
//todo 接口也改了
let
balanceMap
=
{};
let
need
=
1
;
// if(!coinInfoMap){
// this._getCoinsInfo()
// }
this
.
api
.
balance
(
20
,
1
,
(
error
,
result
)
=>
{
this
.
api
.
balance
(
(
error
,
result
)
=>
{
if
(
error
){
console
.
error
(
"get balance by rest error:"
);
if
(
!
error
.
data
){
console
.
error
(
error
);
}
callback
(
null
);
return
;
}
else
{
const
balanceList
=
result
.
data
.
datas
;
for
(
let
detail
of
balanceList
){
balanceMap
[
detail
.
coinType
]
=
{
available
:
detail
.
balance
,
onOrder
:
detail
.
freezeBalance
};
}
let
pageNos
=
result
.
data
.
pageNos
if
(
pageNos
>
1
){
need
=
pageNos
-
1
;
let
amount
=
0
for
(
let
i
=
2
;
i
<=
pageNos
;
i
++
){
this
.
api
.
balance
(
20
,
i
,
(
error
,
result
)
=>
{
amount
+=
1
;
if
(
error
){
console
.
error
(
"get balance by rest error:"
);
if
(
!
error
.
data
){
console
.
error
(
error
);
}
callback
(
null
);
return
;
}
else
{
const
balanceList
=
result
.
data
.
datas
;
for
(
let
detail
of
balanceList
){
balanceMap
[
detail
.
coinType
]
=
{
available
:
detail
.
balance
,
onOrder
:
detail
.
freezeBalance
};
}
}
if
(
need
===
amount
){
callback
(
balanceMap
)
}
})
}
}
else
{
const
balanceList
=
result
.
data
.
datas
;
for
(
let
detail
of
balanceList
){
balanceMap
[
detail
.
coinType
]
=
{
available
:
detail
.
balance
,
onOrder
:
detail
.
freezeBalance
};
}
callback
(
balanceMap
)
const
balanceData
=
result
.
data
;
const
balanceMap
=
{};
for
(
const
balance
of
balanceData
){
balanceMap
[
balance
.
currency
]
=
{
available
:
balance
.
available
,
onOrder
:
balance
.
holds
};
}
callback
(
balanceMap
);
}
})
}
...
...
kucoinStrategy3.js
View file @
a669f277
...
...
@@ -89,22 +89,21 @@ class BiboxStrategy3 extends Strategy3 {
if
(
order
.
executedQty
===
order
.
origQty
)
{
console
.
log
(
"买入全部成交"
);
createdBuyOrder
=
true
;
amount
=
collector
.
processAmount
(
midCurrency
,
baseCurrency2
,
parseFloat
(
order
.
executedQty
*
0.999
));
setTimeout
(()
=>
{
//
amount = collector.processAmount(midCurrency, baseCurrency2, parseFloat(order.executedQty * 0.999));
//
setTimeout(() => {
sellOrder
();
},
120
);
//
}, 120);
returnOrder
();
}
else
if
(
parseFloat
(
order
.
executedQty
)
>
0
)
{
returnAmount
=
parseFloat
(
returnAmount
)
*
parseFloat
(
order
.
executedQty
)
/
parseFloat
(
amount
);
returnAmount
=
collector
.
processAmount
(
baseCurrency2
,
baseCurrency1
,
returnAmount
);
amount
=
collector
.
processAmount
(
midCurrency
,
baseCurrency2
,
parseFloat
(
order
.
executedQty
*
0.999
)
);
amount
=
collector
.
processAmount
(
midCurrency
,
baseCurrency2
,
order
.
executedQty
);
console
.
log
(
`买入部分成交
${
order
.
executedQty
}
回归量调整为
${
returnAmount
}
`
);
order
.
remark
=
"部分成交@amount "
+
order
.
executedQty
+
";"
;
createdBuyOrder
=
true
;
if
(
parseFloat
(
amount
)
>
0
)
setTimeout
(()
=>
{
//
setTimeout(() => {
sellOrder
();
},
120
);
//
}, 120);
if
(
parseFloat
(
returnAmount
)
>
0
)
returnOrder
();
else
{
...
...
order_kucoin.js
View file @
a669f277
...
...
@@ -22,10 +22,10 @@ function returnFakeOrder(symbol, price, amount) {
function
convertToRecordOrder
(
order
,
price
)
{
let
status
=
constants
.
OrderStatusNew
;
if
(
order
.
pendingAmount
===
0
){
if
(
order
.
dealSize
==
order
.
size
){
status
=
constants
.
OrderStatusFilled
;
}
else
{
if
(
order
.
deal
Amount
>
0
){
if
(
order
.
deal
Size
>
0
){
status
=
constants
.
OrderStatusPartiallyFilled
;
}
if
(
!
order
.
isActive
){
...
...
@@ -33,14 +33,15 @@ function convertToRecordOrder(order, price) {
}
}
return
{
orderId
:
order
.
orderO
id
+
''
,
orderId
:
order
.
id
+
''
,
price
:
price
,
status
:
status
,
transactTime
:
order
.
createdAt
,
origQty
:
order
.
pendingAmount
+
order
.
dealAmount
,
executedQty
:
order
.
dealAmount
,
symbol
:
order
.
coinType
+
'-'
+
order
.
coinTypePair
,
type
:
order
.
type
,
origQty
:
order
.
size
,
executedQty
:
order
.
dealSize
,
symbol
:
order
.
symbol
,
side
:
order
.
side
,
fee
:
order
.
fee
,
}
}
...
...
@@ -101,17 +102,14 @@ class Order {
})
}
order
(
symbol
,
price
,
amount
,
side
,
callback
,
mustSuccess
=
false
)
{
if
(
!
constants
.
RealOrder
)
{
callback
(
null
,
returnFakeOrder
(
symbol
,
price
,
amount
));
return
;
}
this
.
api
.
order
(
price
,
amount
,
symbol
,
side
,
(
error
,
result
)
=>
{
order
(
symbol
,
price
,
amount
,
side
,
timeInForce
,
callback
)
{
const
cliordId
=
Date
.
now
()
+
Math
.
floor
(
Math
.
random
()
*
100
)
+
''
;
this
.
api
.
order
(
cliordId
,
price
,
amount
,
symbol
,
side
,
timeInForce
,
(
error
,
result
)
=>
{
if
(
error
)
{
callback
(
error
,
null
);
return
;
}
const
orderId
=
result
.
data
.
order
Oi
d
;
const
orderId
=
result
.
data
.
order
I
d
;
const
api2
=
this
.
api
;
function
handleSearchResult
(
error
,
result
)
{
...
...
@@ -120,15 +118,21 @@ class Order {
console
.
error
(
error
);
// let timeout = 100;
// setTimeout(() => {
api2
.
searchOrder
(
orderId
,
symbol
,
side
,
handleSearchResult
.
bind
(
this
));
api2
.
searchOrder
(
orderId
,
handleSearchResult
.
bind
(
this
));
// }, timeout);
}
if
(
!
result
||
!
result
.
data
){
console
.
error
(
"没有返回数据,再次搜索"
);
api2
.
searchOrder
(
orderId
,
symbol
,
side
,
handleSearchResult
.
bind
(
this
));
api2
.
searchOrder
(
orderId
,
handleSearchResult
.
bind
(
this
));
return
;
}
callback
(
null
,
convertToRecordOrder
(
result
.
data
,
price
));
if
(
timeInForce
===
constants
.
TimeInForceFOK
&&
result
.
data
.
isActive
){
setTimeout
(()
=>
{
api2
.
searchOrder
(
orderId
,
handleSearchResult
.
bind
(
this
));
},
200
);
}
else
{
callback
(
null
,
convertToRecordOrder
(
result
.
data
,
price
));
}
}
this
.
api
.
searchOrder
(
orderId
,
symbol
,
side
,
handleSearchResult
.
bind
(
this
));
...
...
test_kucoin.js
View file @
a669f277
...
...
@@ -43,7 +43,7 @@ function testCollector(){
// const strategy3 = new BiboxStrategy3(collector);
// strategy3.run();
}
testCollector
();
//
testCollector();
function
printCurrency
(){
const
currentArray
=
[,
'ETH'
,
'BTC'
,
'LTC'
,
'BCH'
,
'USDT'
,
'USD'
,
'RMB'
,
"RCN"
,
"WINGS"
,
"TRX"
,
"LEND"
,
"CMT"
,
"POWR"
,
"HSR"
,
"GAS"
,
"RDN"
,
"TNT"
,
"OAX"
...
...
@@ -124,11 +124,20 @@ function printCurrency(){
// printCurrency()
function
testOrder
(){
const
order
=
new
Order
();
order
.
order
(
"BTC_USDT"
,
"0.01"
,
"0.01"
,
constants
.
OrderSideBuy
,(
error
,
result
)
=>
{
// const order = new Order();
const
biboxApi
=
require
(
'./api_kucoin'
);
const
api
=
new
biboxApi
();
const
ordid
=
Date
.
now
()
+
''
;
// api.order(ordid, "1","1","BTC-USDT",'buy',constants.TimeInForceFOK,(error,result)=>{
// console.log(error);
// console.log(result);
// })
api
.
balance
((
error
,
result
)
=>
{
console
.
log
(
error
);
console
.
log
(
result
);
})
// const sign = api._genSign('1547015186532','POST', '/api/v1/deposit-addresses',{'currency':'BTC'})
// console.log(sign === '7QP/oM0ykidMdrfNEUmng8eZjg/ZvPafjIqmxiVfYu4=');
// api.balance((error,data)=>{
// console.log(error);
// console.log(data);
...
...
@@ -146,7 +155,7 @@ function testOrder(){
// console.log(result);
// })
}
//
testOrder();
testOrder
();
// const amount = (0.0917 * parseFloat(0.00001005)).toFixed(10);
// console.log(amount);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment