-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
|
||
function positions = getPositions(self) | ||
|
||
|
||
response = self.requestAccessToken(); | ||
|
||
|
||
% use the new access token to get transactions | ||
disp('Requesting position info using OAuth...') | ||
curl_str = 'curl -X GET --header "Authorization: " --header "Authorization: Bearer $ACCESS_TOKEN" "https://api.tdameritrade.com/v1/accounts/$ACCOUNTID?fields=positions"'; | ||
|
||
|
||
|
||
curl_str = strrep(curl_str,'$ACCOUNTID',strip(self.AccountID)); | ||
curl_str = strrep(curl_str,'$ACCESS_TOKEN',strip(response.access_token)); | ||
|
||
[e,o] = system(curl_str); | ||
|
||
assert(e==0,'curl failed. Cannot get positions') | ||
|
||
pos_raw = jsondecode(o); | ||
pos_raw = pos_raw.securitiesAccount.positions; | ||
|
||
positions = table(categorical(NaN),0,0,0,'VariableNames',{'Symbol','Quantity','Price','MarketValue'}); | ||
positions = repmat(positions,length(pos_raw),1); | ||
|
||
for i = 1:length(pos_raw) | ||
this_pos = pos_raw(i); | ||
|
||
positions.Symbol(i) = this_pos.instrument.symbol; | ||
|
||
positions.Quantity(i) = this_pos.longQuantity; | ||
positions.Price(i) = this_pos.averagePrice; | ||
|
||
positions.MarketValue(i) = this_pos.marketValue; | ||
|
||
|
||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
% requestAccessToken.m | ||
% request a new access token using the refresh token | ||
% This may fail if your refresh token is stale | ||
% This method is not meant to be publicly accessible | ||
% | ||
|
||
function response = requestAccessToken(self) | ||
|
||
|
||
% get a new access token | ||
|
||
disp('Requesting access token using OAuth...') | ||
curl_str = 'curl -X POST --header "Content-Type: application/x-www-form-urlencoded" -d "grant_type=refresh_token&refresh_token=$REFRESH_token&access_type=&code=&client_id=$APIKey%40AMER.OAUTHAP" "https://api.tdameritrade.com/v1/oauth2/token"'; | ||
|
||
curl_str = strrep(curl_str, '$REFRESH_token',urlencode(self.RefreshToken)); | ||
curl_str = strrep(curl_str, '$APIKey',self.APIKey); | ||
|
||
[e,o] = system(curl_str); | ||
|
||
if e ~= 0 | ||
disp('Something went wrong with getting the access token. The error was:') | ||
disp(o) | ||
disp('Try getting a new refresh token') | ||
error('Cannot read access token') | ||
end | ||
|
||
response = jsondecode(o); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters