Skip to content

Commit

Permalink
wrote getPositions
Browse files Browse the repository at this point in the history
  • Loading branch information
sg-s committed Mar 20, 2021
1 parent 29b8d00 commit b3d7228
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 18 deletions.
41 changes: 41 additions & 0 deletions @TDAmeritrade/getPositions.m
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
18 changes: 1 addition & 17 deletions @TDAmeritrade/getTransactions.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,7 @@


% 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);
response = self.requestAccessToken();


% use the new access token to get transactions
Expand Down
27 changes: 27 additions & 0 deletions @TDAmeritrade/requestAccessToken.m
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);
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ transactions = T.getTransactions;

Using OAuth2, get a list of transactions from a specified account. You need to tell it what account to read from (see below).

`transactions` is a table that could look like this:

```
Symbol Quantity Price Date
_______________ ________ ______ ____________________
MOON -500 127 19-Mar-2021 13:18:12
FTFT -60 18.151 19-Mar-2021 14:38:06
TSLA -14 15.17 11-Mar-2021 14:15:19
GME 14 18 11-Mar-2021 15:10:11
NVDA 5 125 03-Mar-2021 14:19:00
GME -1 122.6 02-Mar-2021 14:21:40
GME 700 123.14 22-Feb-2021 14:44:30
GME -700 125 22-Feb-2021 14:31:28
```


### Get positions for account

```
positions = T.getPositions();
```

returns a table with positions.

## Installation

Expand Down Expand Up @@ -154,7 +180,7 @@ T.set('AccountID','YOUR_ACCOUNT_ID');

1. Realtime quotes (using authenticated requests)
2. Variable resolution historical data
3. Get account balances, positions & orders
3. Get account balances & orders
4. Place orders
5. Cancel orders
6. Get new refresh token using access token
Expand Down

0 comments on commit b3d7228

Please sign in to comment.