Skip to content

Commit

Permalink
Added User.getOptionPositions()
Browse files Browse the repository at this point in the history
  • Loading branch information
torreyleonard committed Mar 20, 2019
1 parent 0fa5f50 commit a8714cd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
8 changes: 8 additions & 0 deletions docs/ROBINHOOD.md
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,7 @@ Represents the user that is logged in while accessing the Robinhood API.
* [.cancelOpenOrders()](#User+cancelOpenOrders) ⇒ <code>Promise</code>
* [.getOptionOrders()](#User+getOptionOrders) ⇒ <code>Promise.&lt;Array&gt;</code>
* [.getPortfolio()](#User+getPortfolio) ⇒ <code>Promise.&lt;Object&gt;</code>
* [.getOptionPositions()](#User+getOptionPositions) ⇒ <code>Promise.&lt;any&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>
Expand Down Expand Up @@ -1621,6 +1622,13 @@ Returns an array of 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)
**Author**: Torrey Leonard <https://github.com/Ladinn>
<a name="User+getOptionPositions"></a>

### user.getOptionPositions() ⇒ <code>Promise.&lt;any&gt;</code>
Returns an array of options that the user holds.

**Kind**: instance method of [<code>User</code>](#User)
**Author**: Torrey Leonard <https://github.com/Ladinn>
<a name="User+getHistoricals"></a>
Expand Down
20 changes: 13 additions & 7 deletions objects/broker/robinhood/OptionOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class OptionOrder extends Robinhood {
* @property {String} type - market/limit. Note: market orders are not allowed if side = buy.
* @property {Number} price
* @property {String} timeInForce - gtc/gfd/ioc/opg
* @property {OptionInstrument} option
* @property {OptionInstrument|Null} option - Required if no legs are provided
* @property {Array|Null} legs - Required if no option is provided
* @property {Number} quantity
*/
constructor(user, object) {
Expand All @@ -29,15 +30,19 @@ class OptionOrder extends Robinhood {
if (object.state === undefined && object.cancel_url === undefined) { // This should be a new order
_validate();
this.executed = false;
this.form = {
account: this.url + "/accounts/" + this.user.getAccountNumber() + "/",
direction: object.side === 'buy' ? 'debit' : 'credit',
legs: [{
let legs = null;
if (!object.legs) {
legs = [{
position_effect: object.side === "buy" ? "open" : "close",
side: object.side,
ratio_quantity: 1,
option: object.option.instrumentURL
}],
}];
} else legs = object.legs;
this.form = {
account: this.url + "/accounts/" + this.user.getAccountNumber() + "/",
direction: object.side === 'buy' ? 'debit' : 'credit',
legs: legs,
price: object.price,
time_in_force: object.timeInForce,
trigger: 'immediate',
Expand All @@ -64,7 +69,8 @@ class OptionOrder extends Robinhood {
assert(typeof object.price === 'number', new Error("Object property 'price' must be a number"));
assert(typeof object.timeInForce === 'string', new Error("Object property 'timeInForce' must be a string"));
assert(['gfd', 'gtc', 'ioc', 'opg'].indexOf(object.timeInForce.toLowerCase()) !== -1, new Error("Object property 'timeInForce' must be either GFD, GTC, IOC, or OPG"));
assert(object.option instanceof OptionInstrument, new Error("Object property 'optionInstrument' must be an instance of the OptionInstrument class"));
assert(object.option instanceof OptionInstrument || typeof object.legs !== 'undefined', new Error("Object property 'optionInstrument' must be an instance of the OptionInstrument class"));
assert(Array.isArray(object.legs) || typeof object.option !== 'undefined', new Error("Object property 'legs' must be an array. Required parameter if 'option' is undefined"));
assert(typeof object.quantity === 'number', new Error("Object property 'quantity' must be a number"))
}
}
Expand Down
38 changes: 38 additions & 0 deletions objects/broker/robinhood/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Instrument = require('./Instrument');
const Portfolio = require('./Portfolio');
const Order = require('./Order');
const OptionOrder = require('./OptionOrder');
const OptionInstrument = require('./OptionInstrument');

const request = require('request');
const fs = require('fs');
Expand Down Expand Up @@ -584,6 +585,9 @@ class User extends Robinhood {
uri: _this.url + "/accounts/" + _this.account + "/positions/",
headers: {
'Authorization': 'Bearer ' + _this.token
},
qs: {
nonzero: true
}
}, (error, response, body) => {
Robinhood.handleResponse(error, response, body, _this.token, res => {
Expand All @@ -605,6 +609,40 @@ class User extends Robinhood {
})
}

/**
* Returns an array of options that the user holds.
* @author Torrey Leonard <https://github.com/Ladinn>
* @returns {Promise<any>}
*/
getOptionPositions() {
const _this = this;
return new Promise((resolve, reject) => {
request({
uri: _this.url + "/options/positions/",
headers: {
'Authorization': 'Bearer ' + _this.token
},
qs: {
nonzero: 'True'
}
}, (error, response, body) => {
Robinhood.handleResponse(error, response, body, _this.token, res => {
if (!Array.isArray(res)) res = [res];
let array = [];
async.forEachOf(res, (position, key, callback) => {
OptionInstrument.getByURL(_this, position.option).then(option => {
position.option = option;
array.push(position);
callback();
}).catch(reject);
}, () => {
resolve(array);
})
}, reject);
})
})
}

/**
* Returns an object that can be used to create a chart, show total return, etc.
* @author Torrey Leonard <https://github.com/Ladinn>
Expand Down

0 comments on commit a8714cd

Please sign in to comment.