Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerfloyd committed Jan 6, 2019
1 parent 218e450 commit b7a651f
Show file tree
Hide file tree
Showing 15 changed files with 848 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ typings/

# next.js build output
.next

# IDE
*.vscode

#OSX
.DS_Store
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Tastyworks Node API

NodeJS Framework for basic account information, balances, positions and orders with [TastyWorks's](https://www.tastyworks.com/) private API. This API has been reversed engineered. The stability and accuracy of this API cannot be garunteed.

- [Features](#features)
- [Installation](#installation)
- [Getting Started](#gettingStarted)
- [API](#API)
- [`User`](#user)
- [`Authorization`](#authorization)
- [`Accounts`](#accounts)
- [`Balance`](#balances)
- [`Positions`](#positions)
- [`LiveOrders`](#liveorders)
- [`History`](#history)
- [TODOs](#todos)

## Features

- Account Balances
- Account History
- Current Orders
- Current Positions

## Installation

```bash
$ npm install tastyworks --save
```

## Getting Started

`/sample/index.js` has an example of each call configured. To run and see each response, replace `YOUR_USERNAME' and 'YOUR_PASSWORD' and 'ACCOUNT_ID' with your Tastyworks credentials. Then run`\$ cd sample && node index.js` in your Terminal.

## API

### `User`

Set account variables after authentication is complete so the variables can be used later. The object has four keys, `username`, `password`, `authorization_token` and `accounts`. By deafult, you are only required to set the `username` and `password`. In order to make sure the proper credentials are set as well as, to view the associated account(s) with the user, the `getUser()` function is provided.

> **NOTE:** you must set the user object's `username` and `password` before other endpoints will work
```js
const TastyWorks = require('tastyworks');
const credentials = {
username: 'username',
password: 'password'
};

TastyWorks.setAccount(credentials);
console.log(TastyWorks.getUser());
```

### `Authorization()`

The authorization endpoint will return a session token that needs to be set into headers that are used in subsequent calls. Also, this call assumes that you have previously set the `username` and `password` in the users object.

```js
TastyWorks.authorization().then(token => {
// REQUIRED: Apply the new session token to the headers object passed into each call
TastyWorks.setAuthorizationToken(token);

// OPTIONAL: Set the session token in the user object
TastyWorks.setUser({
authorization_token: token
});
return true;
});
```

### `Accounts()`

Get all accounts associated with the logged in user. The response is an array of account objects. While it is easy to add these to the user object to be referenced when needed. You can hardcode the account that you wish to reference and pass that into the other calls, too.

```js
TastyWorks.accounts().then(accounts =>
TastyWorks.setUser({
accounts
})
);
```

### `Balances(ACCOUNT_ID)`

Fetch the balances for a single account

```js
TastyWorks.balances('ACCOUNT_ID').then(balances => console.log(balances));
```

### `Positions(ACCOUNT_ID)`

Fetch the current positions for a single account

```js
TastyWorks.positions('ACCOUNT_ID').then(positions => console.log(positions));
```

### `LiveOrders(ACCOUNT_ID)`

Fetch the current (non-complete) orders for a single account. This can include orders that have been partially fulfilled

```js
TastyWorks.liveOrders('ACCOUNT_ID').then(liveOrders => console.log(liveOrders));
```

### `History(ACCOUNT_ID, START_DATE, END_DATE)`

Fetch the accounts order history for a given time period. Date format is `yyyy-mm-dd`.

```js
TastyWorks.history('ACCOUNT_ID', '2018-01-01', '2018-01-05').then(history => console.log(history));
```

## TODOs

- Position groupings
- Fetch news for an equity
- Current option prices
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const index = require('./lib/index');

module.exports = index;
37 changes: 37 additions & 0 deletions lib/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

/**
* Get Accounts
* @param {object} headers
* @return {object} array of accounts
*/

const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (headers) => {
const endpoint = endpoints['accounts']();
return request
.get(`${endpoint}`)
.set(headers)
.then(res => {
const {
body: {
data: {
items
}
}
} = res;

const accounts = items.map(data => {
const {
account
} = data;

return account;
});

return accounts;
})
.catch(err => err.message);
}
31 changes: 31 additions & 0 deletions lib/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

/**
* Get Authorization Token
* @param {string} username
* @param {string} passworde
* @return {string} session token
*/

const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (username, password, headers) => {
const endpoint = endpoints['login']();
return request
.post(`${endpoint}`)
.set(headers)
.send({
login: username,
password: password
})
.then(res => {
const {
body: {
data
}
} = res;
return data['session-token'];
})
.catch(err => err.message);
}
29 changes: 29 additions & 0 deletions lib/balances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

/**
* Get Account Balances
* @param {object} headers
* @param {string} accountId
* @return {object} account balances
*/

const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (headers, account_id) => {
const endpoint = endpoints['balances'](account_id);
return request
.get(`${endpoint}`)
.set(headers)
.send()
.then(res => {
const {
body: {
data
}
} = res;

return data;
})
.catch(err => err.message);
}
41 changes: 41 additions & 0 deletions lib/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

/**
* Get Account History
* @param {object} headers
* @param {string} account_id
* @param {date} start_date (yyyy-mm-dd)
* @param {date} end_date (yyyy-mm-dd)
* @return {object} account history
*/

const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (headers, account_id, start_date, end_date) => {
const endpoint = endpoints['history'](account_id);
return request
.get(`${endpoint}`)
.query({
'start-date': `${start_date}T07:00:00.000Z`
})
.query({
'end-date': `${end_date}T07:00:00.000Z`
})
.set(headers)
.send()
.then(res => {
const {
body: {
data: {
items
}
}
} = res;

return items;
})
.catch(err => {
return err.message;
});
}
36 changes: 36 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const _authorization = require('./authorization');
const _accounts = require('./accounts');
const _balances = require('./balances');
const _positions = require('./positions');
const _liveOrders = require('./liveOrders');
const _history = require('./history');

let _headers = require('../util/defaultHeaders');

let _user = {
username: null,
password: null,
authorization_token: null,
accounts: []
};

module.exports = {
setUser: user => {
_user = {
..._user,
...user
};
},
getUser: () => _user,
setAuthorizationToken: (authorization_token) => _headers['Authorization'] = authorization_token,
getHeaders: () => _headers,

authorization: () => _authorization(_user.username, _user.password, _headers),
accounts: () => _accounts(_headers),
balances: (account_id) => _balances(_headers, account_id),
positions: (account_id) => _positions(_headers, account_id),
liveOrders: (account_id) => _liveOrders(_headers, account_id),
history: (account_id, start_date, end_date) => _history(_headers, account_id, start_date, end_date)
}
31 changes: 31 additions & 0 deletions lib/liveOrders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

/**
* Get Account Orders
* @param {object} headers
* @param {string} accountId
* @return {object} orders
*/

const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (headers, account_id) => {
const endpoint = endpoints['liveOrders'](account_id);
return request
.get(`${endpoint}`)
.set(headers)
.send()
.then(res => {
const {
body: {
data: {
items
}
}
} = res;

return items;
})
.catch(err => err.message);
}
29 changes: 29 additions & 0 deletions lib/positions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

/**
* Get Account Positions
* @param {object} headers
* @param {string} accountId
* @return {object} account positions
*/

const request = require('superagent');
const endpoints = require('../util/endpoints');

module.exports = (headers, account_id) => {
const endpoint = endpoints['positions'](account_id);
return request
.get(`${endpoint}`)
.set(headers)
.send()
.then(res => {
const {
body: {
data
}
} = res;

return data;
})
.catch(err => err.message);
}
Loading

0 comments on commit b7a651f

Please sign in to comment.