This repository has been archived by the owner on Jul 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·133 lines (111 loc) · 3.98 KB
/
index.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
'use strict';
const _ = require('lodash');
const qs = require('querystring');
const Joi = require('joi');
const url = require('url');
const util = require('util');
const oauth = require('oauth');
const moment = require('moment');
const urljoin = require('url-join');
const constants = require('./lib/constants');
class Tradeking {
constructor(config) {
this.config = config;
this.oauth = new oauth.OAuth(
config.requestUrl || null, // https://developers.tradeking.com/oauth/request_token
config.accessUrl || null, // https://developers.tradeking.com/oauth/access_token
config.consumerKey || null,
config.consumerSecret || null,
config.version || '1.0',
config.authorize_callback || null,
config.signatureMethod || 'HMAC-SHA1',
config.nonceSize || null,
config.customHeaders || null
);
this.marketClock(
(err, data, response) => {
this.clock = JSON.parse(data).response;
const servertime = this.clock.unixtime;
const localtime = moment().unix();
this.clockSkew = localtime - servertime;
}
);
}
marketClock(callback) {
return this.request(constants.ENDPOINTS.MARKET_CLOCK, callback);
}
accountSummary(callback) {
return this.request(constants.ENDPOINTS.ACCOUNT_SUMMARY, callback);
}
accountBalances(callback) {
return this.request(constants.ENDPOINTS.ACCOUNT_BALANCE, callback);
}
streamQuote(symbols, callback) {
if(!_.isArray(symbols)) {
throw TypeError("symbols is not an array");
}
var request = this.request(constants.ENDPOINTS.STREAM_QUOTE, symbols);
request
.on('response', response => {
response
.setEncoding('utf8')
.on('data', data => {
callback(null, data);
})
.on('error', error => {
callback(error);
});
})
.on('error', error => {
callback(error);
})
.end();
return request;
}
// --- low-level requests ------------------------------------------
request(endpoint, payload, callback) {
if(!_.isArray(endpoint)) {
throw TypeError("endpoint is not an array");
} else if(_.isFunction(payload)) {
callback = payload;
payload = {};
}
var request;
const [method, uri] = endpoint;
switch (method.toLowerCase()) {
case 'get':
request =
this.oauth.get(
urljoin(constants.API_URL, uri),
this.config.accessToken,
this.config.accessSecret,
callback
);
break;
case 'post':
throw Error("not implemented");
// request =
// this.oauth.post(
// urljoin(constants.API_URL, uri),
// this.config.accessToken,
// this.config.accessSecret,
// payload,'text/plain', callback
// );
break;
case 'stream':
const _url = urljoin(constants.STREAM_URL, uri);
const _qs = qs.stringify({symbols: payload.toString()});
request =
this.oauth.get(
url.parse(_url + '?' + _qs).href,
this.config.accessToken,
this.config.accessSecret
);
break;
default:
throw TypeError("unknown request method");
}
return request;
}
}
module.exports = Tradeking;