From b4bea3441b2bdb8e8c16490183916beeb16a7d31 Mon Sep 17 00:00:00 2001 From: Chris Vanderloo Date: Sat, 30 Oct 2021 12:29:54 -0400 Subject: [PATCH] changes --- examples/example.js | 28 ++++----- src/index.js | 149 +++++++++----------------------------------- test/index.js | 3 +- 3 files changed, 41 insertions(+), 139 deletions(-) diff --git a/examples/example.js b/examples/example.js index 17efcab..ec88bae 100644 --- a/examples/example.js +++ b/examples/example.js @@ -8,7 +8,8 @@ const client = new Robinhood(); client.init({ robinhood: { username: process.env.RH_USERNAME, - password: process.env.RH_PASSWORD + password: process.env.RH_PASSWORD, + device_token: process.env.RH_DEVICE_TOKEN }, alphaVantage: process.env.ALPHA_VANTAGE, newsAPI: process.env.NEWS_API @@ -25,13 +26,6 @@ client.init({ // // These algorithms should be designed to take data from ONE // source, aka AAPL or GE, and not try to balance multiple. - // - // Algorithms will automatically be wrapped with data getters - // Before the algorithm runs, it will fetch the data necessary - // using getters in a dataset. Because of the way dependencies - // are grabbed, and to keep down the amount of requests being made - // to Alpha Vantage and other APIs, the data for each should be - // grabbed at the beginning and not live in the function. client.addAlgorithm('algoOne', emaAlgorithm); @@ -40,12 +34,14 @@ client.init({ client.execute({ name: 'algoOne', symbol: 'AAPL', - max_usd: '134.32', // maximum amount of capital in your account the - // algorithm will be allowed utilize + max_usd: '134.32', + dependencies: { + intraday: { + + } + }, }).catch((err) => { console.log(err); - }).then(() => { - client.logout(); }); @@ -54,7 +50,7 @@ client.init({ // An algorithm that will take in data based on a particular signal // This algorithm is meant to be run once, straight throw. Multiple executions // should be defined in the main trading method. -function emaAlgorithm(buy, sell, data, is_check) { +function emaAlgorithm(buy, sell, data) { var ema_40 = data.ema.daily(40); // current EMA over 15 day period var ema_120 = data.ema.daily(120); // current EMA over 60 day period @@ -63,8 +59,6 @@ function emaAlgorithm(buy, sell, data, is_check) { // the algorithm isn't being checked for dependencies but is being // delivered necessary functions - if (!is_check) { - if (ema_40 < ema_120) sell(); - else buy(); - } + if (ema_40 < ema_120) sell(); + else buy(); } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 273f57b..8b32379 100644 --- a/src/index.js +++ b/src/index.js @@ -11,27 +11,31 @@ function handleRequestErrors(error, res, callback, callbackError) { function login(rh, callback, err) { if (rh.token) { - var res = { + callback({ type: 'no_request', body: { token: rh.token } - }; - callback(res); + }); + } else { + request({ + uri: 'https://api.robinhood.com/oauth2/token/', + method: 'POST', + form: { + username: rh.username, + password: rh.password, + grant_type: 'password', + scope: 'internal', + client_id: 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS', + device_token: rh.device_token + }, + json: true + }, (error, res) => handleRequestErrors(error, res, () => { + if (res.body.access_token) callback(res); + else if (res.body.mfa_type) err('Requires MFA'); + else err(res.body); + }, err)); } - else request({ - uri: 'https://api.robinhood.com/api-token-auth/', - method: 'POST', - form: { - username: rh.username, - password: rh.password - }, - json: true - }, (error, res) => handleRequestErrors(error, res, () => { - if (res.body.token) callback(res); - else if (res.body.mfa_type) err('Requires MFA'); - else err(res.body); - }, err)); } function reqWithAuth(token, url, callback, err, type) { @@ -39,7 +43,7 @@ function reqWithAuth(token, url, callback, err, type) { return rp({ uri: url, method: rpMethod, - headers: { Authorization: `Token ${token}` }, + headers: { Authorization: `Bearer ${token}` }, json: true }).then(res => callback(res)).catch(error => err(error)); } @@ -48,7 +52,7 @@ function getPaginated(token, url, callback, err) { return rp({ uri: url, method: 'GET', - headers: { Authorization: `Token ${token}` }, + headers: { Authorization: `Bearer ${token}` }, json: true }).then(async (res) => { if (res.next && res.next !== null) { @@ -61,11 +65,6 @@ let alphaVantage; // let newsAPI; let token; -// const get = (url, callback, err) => reqWithAuth(token, url, (res) => { -// if (res.results) callback(res.results); -// else callback(res); -// }, err); - module.exports = { Robinhood: function transaction() { this.portfolio = []; @@ -74,14 +73,9 @@ module.exports = { if (error.error && error.error.detail === 'Invalid Token.') this.authorize(opts, callback, this); else callback(error); }; - if (token) this.logout(this.authorize(opts, callback, err)); - else this.authorize(opts, callback, err); + this.authorize(opts, callback, err); }; - this.logout = (callback) => reqWithAuth(token, 'https://api.robinhood.com/api-token-logout/', callback, (err) => { - throw new Error(err); - }, 'POST'); - this.authorize = (opts, callback, err) => { if (opts.alphaVantage) alphaVantage = opts.alphaVantage; // if (opts.newsAPI) newsAPI = opts.newsAPI; @@ -90,7 +84,7 @@ module.exports = { const requests = []; // Saves the authorization token - token = res.body.token; + token = res.body.access_token; // Gathers the current account's past and present positions getPaginated(token, 'https://api.robinhood.com/positions/', (pos) => { @@ -121,7 +115,7 @@ module.exports = { callback(); }); }, err); - }); + }, err); }; this.get = (url, callback, err) => reqWithAuth(token, url, (res) => { @@ -153,7 +147,7 @@ module.exports = { return rp({ uri: 'https://api.robinhood.com/orders/', method: 'POST', - headers: { Authorization: `Token ${token}` }, + headers: { Authorization: `Bearer ${token}` }, json: true, form: { account: this.account.url, @@ -175,7 +169,7 @@ module.exports = { return rp({ uri: 'https://api.robinhood.com/orders/', method: 'POST', - headers: { Authorization: `Token ${token}` }, + headers: { Authorization: `Bearer ${token}` }, json: true, form: { account: this.account.url, @@ -240,16 +234,6 @@ module.exports = { weekly: size => data.sma_data.weekly[size], monthly: size => data.sma_data.monthly[size], }, - ema_data: { - daily: {}, - weekly: {}, - monthly: {} - }, - sma_data: { - daily: {}, - weekly: {}, - monthly: {} - }, macd: {}, available_money: this.account.buying_power, price: quote.last_trade_price, @@ -288,85 +272,8 @@ module.exports = { if (stock) data.investment = stock.quantity; else data.investment = '0.0000'; - const getters = { - get available_money() { - return data.available_money; - }, - get investment() { - return data.investment; - }, - get price() { - return data.price; - }, - get quote() { - data.quote = quote; - return data.quote; - }, - get instrument() { - return data.instrument; - }, - get sentiment() { - return undefined; - }, - sma: { - daily: (size) => { - promiseArray.push(getAV('SMA', 'daily', size, opts.sma_series_type, (res) => { - data.sma_data.daily[size] = res; - })); - }, - weekly: (size) => { - promiseArray.push(getAV('SMA', 'weekly', size, opts.sma_series_type, (res) => { - data.sma_data.weekly[size] = res; - })); - }, - monthly: (size) => { - promiseArray.push(getAV('SMA', 'monthly', size, opts.sma_series_type, (res) => { - data.sma_data.monthly[size] = res; - })); - } - }, - ema: { - daily: (size) => { - promiseArray.push(getAV('EMA', 'daily', size, opts.ema_series_type, (res) => { - data.ema_data.daily[size] = res; - })); - }, - weekly: (size) => { - promiseArray.push(getAV('EMA', 'weekly', size, opts.ema_series_type, (res) => { - data.ema_data.weekly[size] = res; - })); - }, - monthly: (size) => { - promiseArray.push(getAV('EMA', 'monthly', size, opts.ema_series_type, (res) => { - data.ema_data.monthly[size] = res; - })); - } - }, - macd: { - get daily() { - promiseArray.push(getAV('MACD', 'daily', undefined, opts.macd_series_type, (res) => { - data.macd.daily = res; - })); - return undefined; - }, - get weekly() { - promiseArray.push(getAV('MACD', 'weekly', undefined, opts.macd_series_type, (res) => { - data.macd.weekly = res; - })); - return undefined; - }, - get monthly() { - promiseArray.push(getAV('MACD', 'monthly', undefined, opts.macd_series_type, (res) => { - data.macd.monthly = res; - })); - return undefined; - } - } - }; - - callback(() => data, () => data, getters, true); await Promise.all(promiseArray); - callback(data.buy, data.sell, data, false); + callback(data.buy, data.sell, data); }, }; diff --git a/test/index.js b/test/index.js index 9997769..5f99f54 100644 --- a/test/index.js +++ b/test/index.js @@ -8,7 +8,8 @@ describe('Login to Robinhood', function() { client.init({ robinhood: { username: process.env.RH_USERNAME, - password: process.env.RH_PASSWORD + password: process.env.RH_PASSWORD, + device_token: process.env.RH_DEVICE_TOKEN }, alphaVantage: process.env.ALPHA_VANTAGE, }, () => { -- 2.51.2