-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtcturk.js
241 lines (224 loc) · 8.63 KB
/
btcturk.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
232
233
234
235
236
237
238
239
240
241
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class btcturk extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'btcturk',
'name': 'BTCTurk',
'countries': 'TR', // Turkey
'rateLimit': 1000,
'has': {
'CORS': true,
'fetchTickers': true,
'fetchOHLCV': true,
},
'timeframes': {
'1d': '1d',
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27992709-18e15646-64a3-11e7-9fa2-b0950ec7712f.jpg',
'api': 'https://www.btcturk.com/api',
'www': 'https://www.btcturk.com',
'doc': 'https://github.com/BTCTrader/broker-api-docs',
},
'api': {
'public': {
'get': [
'ohlcdata', // ?last=COUNT
'orderbook',
'ticker',
'trades', // ?last=COUNT (max 50)
],
},
'private': {
'get': [
'balance',
'openOrders',
'userTransactions', // ?offset=0&limit=25&sort=asc
],
'post': [
'buy',
'cancelOrder',
'sell',
],
},
},
'markets': {
'BTC/TRY': { 'id': 'BTCTRY', 'symbol': 'BTC/TRY', 'base': 'BTC', 'quote': 'TRY', 'maker': 0.002 * 1.18, 'taker': 0.0035 * 1.18 },
'ETH/TRY': { 'id': 'ETHTRY', 'symbol': 'ETH/TRY', 'base': 'ETH', 'quote': 'TRY', 'maker': 0.002 * 1.18, 'taker': 0.0035 * 1.18 },
'ETH/BTC': { 'id': 'ETHBTC', 'symbol': 'ETH/BTC', 'base': 'ETH', 'quote': 'BTC', 'maker': 0.002 * 1.18, 'taker': 0.0035 * 1.18 },
},
});
}
async fetchBalance (params = {}) {
let response = await this.privateGetBalance ();
let result = { 'info': response };
let base = {
'free': response['bitcoin_available'],
'used': response['bitcoin_reserved'],
'total': response['bitcoin_balance'],
};
let quote = {
'free': response['money_available'],
'used': response['money_reserved'],
'total': response['money_balance'],
};
let symbol = this.symbols[0];
let market = this.markets[symbol];
result[market['base']] = base;
result[market['quote']] = quote;
return this.parseBalance (result);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
let market = this.market (symbol);
let orderbook = await this.publicGetOrderbook (this.extend ({
'pairSymbol': market['id'],
}, params));
let timestamp = parseInt (orderbook['timestamp'] * 1000);
return this.parseOrderBook (orderbook, timestamp);
}
parseTicker (ticker, market = undefined) {
let symbol = undefined;
if (market)
symbol = market['symbol'];
let timestamp = parseInt (ticker['timestamp']) * 1000;
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': parseFloat (ticker['high']),
'low': parseFloat (ticker['low']),
'bid': parseFloat (ticker['bid']),
'ask': parseFloat (ticker['ask']),
'vwap': undefined,
'open': parseFloat (ticker['open']),
'close': undefined,
'first': undefined,
'last': parseFloat (ticker['last']),
'change': undefined,
'percentage': undefined,
'average': parseFloat (ticker['average']),
'baseVolume': parseFloat (ticker['volume']),
'quoteVolume': undefined,
'info': ticker,
};
}
async fetchTickers (symbols = undefined, params = {}) {
await this.loadMarkets ();
let tickers = await this.publicGetTicker (params);
let result = {};
for (let i = 0; i < tickers.length; i++) {
let ticker = tickers[i];
let symbol = ticker['pair'];
let market = undefined;
if (symbol in this.markets_by_id) {
market = this.markets_by_id[symbol];
symbol = market['symbol'];
}
result[symbol] = this.parseTicker (ticker, market);
}
return result;
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
let tickers = await this.fetchTickers ();
let result = undefined;
if (symbol in tickers)
result = tickers[symbol];
return result;
}
parseTrade (trade, market) {
let timestamp = trade['date'] * 1000;
return {
'id': trade['tid'],
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': market['symbol'],
'type': undefined,
'side': undefined,
'price': trade['price'],
'amount': trade['amount'],
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
let market = this.market (symbol);
// let maxCount = 50;
let response = await this.publicGetTrades (this.extend ({
'pairSymbol': market['id'],
}, params));
return this.parseTrades (response, market, since, limit);
}
parseOHLCV (ohlcv, market = undefined, timeframe = '1d', since = undefined, limit = undefined) {
let timestamp = this.parse8601 (ohlcv['Time']);
return [
timestamp,
ohlcv['Open'],
ohlcv['High'],
ohlcv['Low'],
ohlcv['Close'],
ohlcv['Volume'],
];
}
async fetchOHLCV (symbol, timeframe = '1d', since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
let market = this.market (symbol);
let request = {};
if (typeof limit !== 'undefined')
request['last'] = limit;
let response = await this.publicGetOhlcdata (this.extend (request, params));
return this.parseOHLCVs (response, market, timeframe, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
let method = 'privatePost' + this.capitalize (side);
let order = {
'Type': (side === 'buy') ? 'BuyBtc' : 'SelBtc',
'IsMarketOrder': (type === 'market') ? 1 : 0,
};
if (type === 'market') {
if (side === 'buy')
order['Total'] = amount;
else
order['Amount'] = amount;
} else {
order['Price'] = price;
order['Amount'] = amount;
}
let response = await this[method] (this.extend (order, params));
return {
'info': response,
'id': response['id'],
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
return await this.privatePostCancelOrder ({ 'id': id });
}
nonce () {
return this.milliseconds ();
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
if (this.id === 'btctrader')
throw new ExchangeError (this.id + ' is an abstract base API for BTCExchange, BTCTurk');
let url = this.urls['api'] + '/' + path;
if (api === 'public') {
if (Object.keys (params).length)
url += '?' + this.urlencode (params);
} else {
this.checkRequiredCredentials ();
let nonce = this.nonce ().toString ();
body = this.urlencode (params);
let secret = this.base64ToBinary (this.secret);
let auth = this.apiKey + nonce;
headers = {
'X-PCK': this.apiKey,
'X-Stamp': nonce,
'X-Signature': this.stringToBase64 (this.hmac (this.encode (auth), secret, 'sha256', 'binary')),
'Content-Type': 'application/x-www-form-urlencoded',
};
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
};