Skip to content

Commit

Permalink
Added getHistoricals method to Robinhood User
Browse files Browse the repository at this point in the history
  • Loading branch information
torreyleonard committed Jan 9, 2019
1 parent c1aee52 commit cc2aadd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
15 changes: 11 additions & 4 deletions docs/ROBINHOOD.md
Original file line number Diff line number Diff line change
Expand Up @@ -924,10 +924,10 @@ Represents all of the user's holdings on Robinhood and allows for various querie
Creates a new Portfolio object.


| Param | Type |
| --- | --- |
| user | [<code>User</code>](#User) |
| array | <code>Array</code> |
| Param | Type | Description |
| --- | --- | --- |
| user | [<code>User</code>](#User) | |
| array | <code>Array</code> | Created via User.getPortfolio() |

<a name="Portfolio+sellAll"></a>

Expand Down Expand Up @@ -1166,6 +1166,7 @@ Represents the user that is logged in while accessing the Robinhood API.
* [.cancelOpenOrders()](#User+cancelOpenOrders) ⇒ <code>Promise</code>
* [.getRecentOptionOrders()](#User+getRecentOptionOrders) ⇒ <code>Promise.&lt;Array&gt;</code>
* [.getPortfolio()](#User+getPortfolio) ⇒ <code>Promise.&lt;Object&gt;</code>
* [.getHistoricals()](#User+getHistoricals) ⇒ <code>Promise.&lt;Object&gt;</code>
* [.getLinkedBanks()](#User+getLinkedBanks) ⇒ <code>Promise.&lt;Object&gt;</code>
* [.addDeposit(bankID, amount, frequency)](#User+addDeposit) ⇒ <code>Promise.&lt;Object&gt;</code>
* [.getDocuments()](#User+getDocuments) ⇒ <code>Promise.&lt;Array&gt;</code>
Expand Down Expand Up @@ -1277,6 +1278,12 @@ Returns an array of recent option orders.
### user.getPortfolio() ⇒ <code>Promise.&lt;Object&gt;</code>
Returns a Portfolio object containing all open positions in a user's portfolio.

**Kind**: instance method of [<code>User</code>](#User)
<a name="User+getHistoricals"></a>

### user.getHistoricals() ⇒ <code>Promise.&lt;Object&gt;</code>
Returns an object that can be used to create a chart, show total return, etc.

**Kind**: instance method of [<code>User</code>](#User)
<a name="User+getLinkedBanks"></a>

Expand Down
2 changes: 1 addition & 1 deletion objects/broker/robinhood/Portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Portfolio extends Robinhood {
/**
* Creates a new Portfolio object.
* @param {User} user
* @param {Array} array
* @param {Array} array - Created via User.getPortfolio()
*/
constructor(user, array) {
if (!array instanceof Array) throw new Error("Parameter 'array' must be an array.");
Expand Down
6 changes: 1 addition & 5 deletions objects/broker/robinhood/Robinhood.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ class Robinhood {
if (error) reject(error);
else if (response.statusCode > 299 || response.statusCode < 200) {
if (body.indexOf("{") === 0) {

let json = JSON.parse(body);
let keys = Object.keys(json);
if (keys.length === 1) body = " | " + json[keys[0]];
else body = ".\n\n" + JSON.stringify(json, null, 2);

body = ".\n\n" + JSON.stringify(json, null, 2);
} else body = " | " + body;
reject(new LibraryError("Robinhood responded with code " + response.statusCode + body));
} else {
Expand Down
24 changes: 24 additions & 0 deletions objects/broker/robinhood/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,30 @@ class User extends Robinhood {
})
}

/**
* Returns an object that can be used to create a chart, show total return, etc.
* @returns {Promise<Object>}
*/
getHistoricals(span, interval) {
const _this = this;
return new Promise((resolve, reject) => {
request({
uri: _this.url + "/portfolios/historicals/" + _this.account,
headers: {
'Authorization': 'Bearer ' + _this.token
},
qs: {
span: span,
interval: interval
}
}, (error, response, body) => {
Robinhood.handleResponse(error, response, body, _this.token, res => {
resolve(res);
}, reject);
})
})
}

// Invalid token?
//
// getNotifications() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "algotrader",
"version": "1.4.0",
"version": "1.4.1",
"description": "Algorithmically trade stocks and options using Robinhood, Yahoo Finance, and more.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit cc2aadd

Please sign in to comment.