Skip to content

Commit 64dbe1f

Browse files
authored
Merge pull request #11 from you21979/leverage_support
Leverage support
2 parents 0dad911 + 51cc0ad commit 64dbe1f

File tree

7 files changed

+250
-14
lines changed

7 files changed

+250
-14
lines changed

example/future_api.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const zaif = require('..')
2+
const sleep = require('@you21979/promise-sleep')
3+
4+
const api = zaif.FuturesPublicApi
5+
6+
const initialize = () => {
7+
zaif.Constant.OPT_KEEPALIVE = true
8+
zaif.Constant.OPT_TIMEOUT_SEC = 30
9+
}
10+
11+
const main_loop = async () => {
12+
const groups = await api.groups('all')
13+
console.log(groups)
14+
const depth = await api.depth('1', 'btc_jpy')
15+
console.log(depth)
16+
const ticker = await api.ticker('1', 'btc_jpy')
17+
console.log(ticker)
18+
const trades = await api.trades('1', 'btc_jpy')
19+
console.log(trades.length)
20+
const lastprice = await api.lastPrice('1', 'btc_jpy')
21+
console.log(lastprice)
22+
}
23+
24+
const main = async () => {
25+
initialize()
26+
return await main_loop()
27+
}
28+
29+
main()

example/margin_trade.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const zaif = require('..');
2+
const errors = require('../errors')
3+
const fs = require('fs');
4+
const sleep = require("@you21979/promise-sleep")
5+
const config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
6+
7+
const initialize = (config) => {
8+
zaif.Constant.OPT_KEEPALIVE = true
9+
const api = zaif.createFuturesPrivateApi(config.apikey, config.secretkey, '');
10+
return api
11+
}
12+
13+
const main_loop = async (api) => {
14+
const type = "margin"
15+
const pair = "btc_jpy"
16+
const leverage = 7.77
17+
const price = 1000000
18+
const amount = 0.1
19+
20+
const activepos = await api.activePositions(type, {currency_pair: pair})
21+
console.log(activepos)
22+
23+
await sleep(1000)
24+
25+
const createpos = await api.createPosition(type, pair, "bid", price, amount, leverage)
26+
console.log(createpos)
27+
}
28+
29+
const main = async (config) => {
30+
const api = initialize(config)
31+
try{
32+
await main_loop(api)
33+
}catch(e){
34+
console.log(e)
35+
}
36+
}
37+
38+
main(config)

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
exports.Constant = require('./lib/constant');
22
exports.PublicApi = require('./lib/public_api');
3+
exports.FuturesPublicApi = require('./lib/futures_public_api');
4+
exports.createFuturesPrivateApi = require('./lib/futures_private_api');
35
exports.createPrivateApi = require('./lib/private_api');
46
exports.createStreamApi = require('./lib/stream_api');

lib/constant.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
exports.OPT_TAPI_URL = 'https://api.zaif.jp/tapi';
22
exports.OPT_APIV1_URL = 'https://api.zaif.jp/api/1';
3+
exports.OPT_TLAPI_URL = 'https://api.zaif.jp/tlapi';
4+
exports.OPT_FAPI_URL = 'https://api.zaif.jp/fapi/1';
35
exports.OPT_WEBSOCKET_URL = 'wss://ws.zaif.jp:8888';
46
exports.OPT_LIMIT_SEC = 0.02;
57
exports.OPT_LIMIT_HOST = 'https://api.zaif.jp';

lib/futures_private_api.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"use strict";
2+
var querystring = require('querystring');
3+
var verify = require('@you21979/simple-verify');
4+
var objectutil = require('@you21979/object-util');
5+
var constant = require('./constant');
6+
var lp = require('./system').lp;
7+
var HttpApiError = require('@you21979/http-api-error');
8+
9+
var createHeader = function(api_key, secret_key, user_agent, postdata){
10+
var qstring = querystring.stringify(postdata);
11+
return {
12+
'Content-Type': 'application/x-www-form-urlencoded',
13+
'Content-Length': qstring.length,
14+
'User-Agent': user_agent,
15+
'Sign': verify.sign('sha512', secret_key, qstring),
16+
'Key': api_key,
17+
};
18+
}
19+
20+
var createPostOption = function(url, api_key, secret_key, user_agent, nonce, timeout, method, params){
21+
var post = objectutil.keyMerge({nonce:nonce, method:method}, params);
22+
return {
23+
url: url,
24+
method: 'POST',
25+
forever: constant.OPT_KEEPALIVE,
26+
form: post,
27+
headers: createHeader(api_key, secret_key, user_agent, post),
28+
timeout: timeout,
29+
transform2xxOnly : true,
30+
transform: function(body){
31+
return JSON.parse(body)
32+
},
33+
};
34+
}
35+
36+
var req = function(params){
37+
return lp.req(params)
38+
}
39+
40+
var FuturesTradeApi = function(api_key, secret_key, user_agent, nonce_func){
41+
this.name = "ZAIF-FUTURES";
42+
this.url = constant.OPT_TLAPI_URL;
43+
this.api_key = api_key;
44+
this.secret_key = secret_key;
45+
this.user_agent = user_agent;
46+
this.timeout = Math.floor(constant.OPT_TIMEOUT_SEC * 1000);
47+
48+
var initnonce = new Date() / 1000;
49+
this.nonce_func = nonce_func || function(){ return initnonce = initnonce + 0.01; }
50+
}
51+
52+
FuturesTradeApi.prototype.query = function(method, mustparams, options){
53+
var params = objectutil.keyMerge(mustparams, options);
54+
return req(createPostOption(this.url, this.api_key, this.secret_key, this.user_agent, this.nonce_func(), this.timeout, method, params)).
55+
then(function(v){
56+
if(v.success === 1){
57+
return v['return'];
58+
}else{
59+
var error_code = (v.error).toUpperCase().replace(' ', '_');
60+
throw new HttpApiError(v.error, "API", error_code, v);
61+
}
62+
});
63+
}
64+
65+
FuturesTradeApi.prototype.getPositions = function(type, options){
66+
return this.query('get_positions', {
67+
type : type,
68+
}, options)
69+
}
70+
71+
FuturesTradeApi.prototype.activePositions = function(type, options){
72+
return this.query('active_positions', {
73+
type : type,
74+
}, options)
75+
}
76+
77+
FuturesTradeApi.prototype.positionHistory = function(type, leverage_id, options){
78+
return this.query('position_history', {
79+
type : type,
80+
leverage_id : leverage_id,
81+
}, options)
82+
}
83+
84+
FuturesTradeApi.prototype.createPosition = function(type, currency_pair, action, price, amount, leverage, options){
85+
return this.query('create_position', {
86+
type : type,
87+
currency_pair : currency_pair.toLowerCase(),
88+
action : action.toLowerCase(),
89+
price : price,
90+
amount : amount,
91+
leverage : leverage
92+
}, options)
93+
}
94+
95+
FuturesTradeApi.prototype.changePosition = function(type, leverage_id, price, options){
96+
return this.query('change_position', {
97+
type : type,
98+
leverage_id : leverage_id,
99+
price : price,
100+
}, options)
101+
}
102+
103+
FuturesTradeApi.prototype.cancelPosition = function(type, leverage_id, options){
104+
return this.query('cancel_position', {
105+
type : type,
106+
leverage_id : leverage_id,
107+
}, options)
108+
}
109+
110+
var createFuturesPrivateApi = module.exports = function(api_key, secret_key, user_agent, nonce_func){
111+
return new FuturesTradeApi(api_key, secret_key, user_agent, nonce_func)
112+
}
113+

lib/futures_public_api.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
var lp = require('./system').lp;
3+
var constant = require('./constant');
4+
var HttpApiError = require('@you21979/http-api-error');
5+
6+
var makeapi = function(api){
7+
return constant.OPT_FAPI_URL + '/' + api;
8+
}
9+
var createEndPoint = function(apiv1, pair){
10+
return apiv1 + '/' + pair.toLowerCase();
11+
}
12+
13+
var createGetOption = function(url){
14+
return {
15+
url: url,
16+
method: 'GET',
17+
forever: constant.OPT_KEEPALIVE,
18+
timeout: Math.floor(constant.OPT_TIMEOUT_SEC * 1000),
19+
transform2xxOnly : true,
20+
transform: function(body){
21+
return JSON.parse(body)
22+
},
23+
};
24+
}
25+
26+
var query = exports.query = function(method, pair){
27+
return lp.req(createGetOption(createEndPoint(makeapi(method), pair))).
28+
then(function(result){
29+
if('error' in result){
30+
var error_code = (result.error).toUpperCase().replace(' ', '_');
31+
throw new HttpApiError(result.error, "API", error_code, result);
32+
}else{
33+
return result;
34+
}
35+
});
36+
}
37+
38+
exports.groups = function(group_id){
39+
return query('groups', group_id);
40+
}
41+
exports.lastPrice = function(group_id, pair){
42+
return query('last_price', [group_id, pair].join('/'));
43+
}
44+
exports.ticker = function(group_id, pair){
45+
return query('ticker', [group_id, pair].join('/'));
46+
}
47+
exports.trades = function(group_id, pair){
48+
return query('trades', [group_id, pair].join('/'));
49+
}
50+
exports.depth = function(group_id, pair){
51+
return query('depth', [group_id, pair].join('/'));
52+
}

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
22
"author": "Yuki Akiyama <you2197901 [at] gmail.com>",
33
"name": "zaif.jp",
4-
"version": "0.1.15",
4+
"version": "0.1.16",
55
"private": false,
66
"dependencies": {
7-
"@you21979/simple-verify" : "0.0.x",
8-
"@you21979/object-util" : "0.0.x",
9-
"@you21979/http-api-error" : "0.0.x",
10-
"limit-request-promise" : "0.1.x",
11-
"ws" : "1.1.x",
7+
"@you21979/simple-verify": "0.0.x",
8+
"@you21979/object-util": "0.0.x",
9+
"@you21979/http-api-error": "0.0.x",
10+
"limit-request-promise": "0.1.x",
11+
"ws": "1.1.x",
1212
"request": "^2.34"
1313
},
1414
"readmeFilename": "README.md",
1515
"description": "Promise-base Cryptocurrency Exchange zaif.jp API for node.js",
1616
"main": "index.js",
1717
"devDependencies": {
18-
"@you21979/promise-sleep" : "",
19-
"local-echoserver" : "",
20-
"moment" : "",
21-
"bluebird" : "",
22-
"istanbul":"",
23-
"coveralls":"",
24-
"power-assert":"",
25-
"mocha":""
18+
"@you21979/promise-sleep": "",
19+
"local-echoserver": "",
20+
"moment": "",
21+
"bluebird": "",
22+
"istanbul": "",
23+
"coveralls": "",
24+
"power-assert": "",
25+
"mocha": ""
2626
},
2727
"scripts": {
2828
"test": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha",

0 commit comments

Comments
 (0)