-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtradestationwebapi.m
More file actions
118 lines (97 loc) · 3.81 KB
/
tradestationwebapi.m
File metadata and controls
118 lines (97 loc) · 3.81 KB
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
function [] = tradestationwebapi()
% TRADESTATIONWEBAPI Access data from TradeStation
APIKEY = '';
APISECRET = '';
BASEURL = 'https://sim.api.tradestation.com/v2/';
CALLBACK = 'http://www.tradestation.com';
[~,browser] = web(getAuthorizationUrl());
% poll for the authorization code
while(isempty(strfind(getCurrentLocation(browser),'code=')) == 1)
pause(1);
end
code = getAuthorizationCodeFromUrl(getCurrentLocation(browser));
close(browser);
token = getTokenFromAuthorizationCode(code);
disp('Getting access token');
disp(token.access_token);
disp('Getting msft quote');
quote = getQuote('msft',token);
disp(quote);
disp('Getting access token from refresh token');
token = getRefreshToken(token);
disp(token.access_token);
disp('Getting aapl quote');
quote = getQuote('aapl',token);
disp(quote);
disp('Getting spy barchart stream');
displayStreamingBarcharts('spy','1','Minute','10',token);
function [url] = getAuthorizationUrl()
url = strcat(BASEURL, ...
sprintf( ...
'authorize?client_id=%s&response_type=code&redirect_uri=%s' ...
,APIKEY,urlencode(CALLBACK)));
end
function [token] = getTokenFromAuthorizationCode(code)
url = strcat(BASEURL,'security/authorize');
body = ['grant_type=authorization_code&code=' code ...
'&client_id=' APIKEY '&redirect_uri=' urlencode(CALLBACK) ...
'&client_secret=' APISECRET];
response = char(urlread2(url,'POST',body));
token = loadjson(response);
end
function [token] = getRefreshToken(token)
url = strcat(BASEURL,'security/authorize');
body = ['grant_type=refresh_token&client_id=' APIKEY ...
'&redirect_uri=' urlencode(CALLBACK) ...
'&client_secret=' APISECRET ...
'&refresh_token=' token.refresh_token];
response = char(urlread2(url,'POST',body));
token = loadjson(response);
end
function [quote] = getQuote(symbol,token)
url = strcat(BASEURL,sprintf('data/quote/%s',symbol));
params = {'oauth_token',token.access_token};
queryString = http_paramsToString(params,1);
url = [url '?' queryString];
response = char(urlread2(url));
quote = loadjson(response);
end
function [] = displayStreamingBarcharts(symbol,interval, ...
intervaltype,barsback,token)
import java.net.URL;
import java.io.*;
url = strcat(BASEURL,sprintf('stream/barchart/%s/%s/%s/%s', ...
symbol,interval,intervaltype,barsback));
params = {'oauth_token',token.access_token};
queryString = http_paramsToString(params,1);
theURL = URL([],[url '?' queryString], ...
sun.net.www.protocol.https.Handler);
% Open http connection:
httpConn = theURL.openConnection;
httpConn.setRequestProperty('Content-Type', ...
'application/x-www-form-urlencoded');
% open the connection:
try
inputStream = BufferedReader(InputStreamReader( ...
httpConn.getInputStream));
catch ME
error(ME.message);
end
% start reading from the connection:
sLine = inputStream.readLine;
while (~isempty(sLine))
if ~(sLine.isEmpty)
% convert the response from json string to a MATLAB type.
response = loadjson(char(sLine));
disp(response);
end
sLine = inputStream.readLine;
end
% close the connection:
inputStream.close;
end
end
function [code] = getAuthorizationCodeFromUrl(url)
url = char(url);
code = strcat(url(strfind(url,'code=')+5:end),'=');
end