-
Notifications
You must be signed in to change notification settings - Fork 1
/
tradestation.js
137 lines (112 loc) · 3.56 KB
/
tradestation.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
#!/usr/bin/nodejs
/* Imports */
var $ = require('jquery');
var sugar = require('sugar');
var exec = require('child_process').exec;
var USER = 'Your username';
var PASS = 'Your password';
var KEY = 'Your Web API public key';
var SECRET = 'Your Web API secret';
var SECURITY_QUESTIONS = [
{question: '', answer: ''},
{question: '', answer: ''},
{question: '', answer: ''}
];
var BASEURL = 'https://api.tradestation.com/v2/';
var JSONHEADER = 'Content-Type: application/json';
var objectFormSerialize = function (o) {
return Object.keys(o).map(function (k) {
return '{0}={1}'.assign({0: encodeURIComponent(k), 1: encodeURIComponent(o[k])});
}).join('&');
};
var _authHeader;
var authorize = function (callback) {
if (_authHeader) {
callback(_authHeader);
return;
}
var authorizePage = '{0}authorize/?client_id={1}&response_type=code'.assign({0: BASEURL, 1: KEY});
$.get(authorizePage, function (data) {
var $data = $(data);
exec('curl "{0}" --data "{1}" --compressed -s'.assign({
0: authorizePage,
1: objectFormSerialize({
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE': $data.find('#__VIEWSTATE').val(),
'__EVENTVALIDATION': $data.find('#__EVENTVALIDATION').val(),
'username': USER,
'password': PASS,
'btnLogin': 'Login',
'deviceTrustOption': ''
})
}), function (err, data) {
var $data = $(data);
var question = $data.find('#ucQuestionAndAnswer_lblQuestion').text().toLowerCase();
var answer = question.has(SECURITY_QUESTIONS[0].question.toLowerCase()) ?
SECURITY_QUESTIONS[0].answer :
question.has(SECURITY_QUESTIONS[1].question.toLowerCase()) ?
SECURITY_QUESTIONS[1].answer :
SECURITY_QUESTIONS[2].answer
;
exec('curl "{0}" --data "{1}" --compressed -D - -s -o /dev/null'.assign({
0: authorizePage,
1: objectFormSerialize({
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE': $data.find('#__VIEWSTATE').val(),
'__EVENTVALIDATION': $data.find('#__EVENTVALIDATION').val(),
'ucQuestionAndAnswer$txtAnswer': answer,
'ucQuestionAndAnswer$btnLogin': 'Login',
'deviceTrustOption': false
})
}), function (err, data) {
var authCode = data.
split('\n').
filter(function (s) { return s.startsWith('Location: '); })[0].
split('code=')[1]
;
exec('curl "{0}security/authorize" --data "{1}" --compressed -s'.assign({
0: BASEURL,
1: objectFormSerialize({
'grant_type': 'authorization_code',
'client_id': KEY,
'client_secret': SECRET,
'response_type': 'token',
'code': authCode
})
}), function (err, data) {
var o = JSON.parse(data);
var authHeader = 'Authorization: Bearer {0}'.assign({0: o['access_token']});
_authHeader = authHeader;
setTimeout(function () {
if (_authHeader == authHeader) {
_authHeader = null;
}
},
/* Convert seconds to milliseconds, but cut expire time by 10% to be safe */
parseInt(o['expires_in'], 10) * 900);
callback(authHeader);
});
});
});
});
};
exports.getAccount = function (callback) {
authorize(function (authHeader) {
exec('curl "{0}users/{1}/accounts" -H "{2}" --compressed -s'.assign({
0: BASEURL,
1: USER,
2: authHeader
}), function (err, data) {
var key = JSON.parse(data)[0]['Key'];
exec('curl "{0}accounts/{1}/balances" -H "{2}" --compressed -s'.assign({
0: BASEURL,
1: key,
2: authHeader
}), function (err, data) {
callback({key: key, balance: parseFloat(JSON.parse(data)[0].RealTimeAccountBalance || 0)});
});
});
});
};