├── .gitignore ├── .env ├── lib ├── getDownTrendingStock.js ├── getAccountValue.js ├── getBars.js └── order.js ├── package.json ├── LICENSE └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .env -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | APIKEY=ABCDEFGHIJKLMNOP 2 | SECRET=1234567890 -------------------------------------------------------------------------------- /lib/getDownTrendingStock.js: -------------------------------------------------------------------------------- 1 | const getDownTrendingStock = () => { 2 | return "PM"; 3 | }; 4 | 5 | module.exports = getDownTrendingStock; 6 | -------------------------------------------------------------------------------- /lib/getAccountValue.js: -------------------------------------------------------------------------------- 1 | const getAccountValue = () => { 2 | // hit account api 3 | return 5000; 4 | }; 5 | 6 | module.exports = getAccountValue; 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "trade-bot", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Kyle Goss", 10 | "license": "ISC", 11 | "dependencies": { 12 | "@alpacahq/alpaca-trade-api": "1.3.2", 13 | "dotenv": "8.2.0", 14 | "node-fetch": "2.6.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lib/getBars.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const getBars = async ({symbol, start, end}) => { 4 | try { 5 | const resp = await fetch(`https://data.alpaca.markets/v1/bars/minute?symbols=${symbol}&start=${start}&end=${end}`, { 6 | headers: { 7 | 'APCA-API-KEY-ID': process.env.APIKEY, 8 | 'APCA-API-SECRET-KEY': process.env.SECRET 9 | } 10 | }); 11 | return resp.json(); 12 | } catch (e) { 13 | console.log(e); 14 | } 15 | } 16 | 17 | module.exports = getBars; 18 | -------------------------------------------------------------------------------- /lib/order.js: -------------------------------------------------------------------------------- 1 | const buyMarket = async ({symbol, amt}) => { 2 | // hit api to buy a market order of symbol @ amt 3 | await new Promise(res => { 4 | setTimeout(() => { 5 | res({price: 10.35}); 6 | }); 7 | }); 8 | }; 9 | 10 | const sellStop = async ({symbol, price, amt}) => { 11 | // hit api to set a stop sell order of symbol @ amt @ price 12 | await new Promise(res => { 13 | setTimeout(() => { 14 | res({price: 10.35}); 15 | }); 16 | }); 17 | }; 18 | 19 | const sellLimit = async ({symbol, price, amt}) => { 20 | // hit api to set a stop sell order of symbol @ amt @ price 21 | await new Promise(res => { 22 | setTimeout(() => { 23 | res({price: 10.35}); 24 | }); 25 | }); 26 | }; 27 | 28 | module.exports = { 29 | buyMarket, sellStop, sellLimit 30 | }; 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Web Dev Profesh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const getDownTrendingStock = require('./lib/getDownTrendingStock'); 3 | const getBars = require('./lib/getBars'); 4 | const getAccountValue = require('./lib/getAccountValue'); 5 | const { buyMarket, sellStop, sellLimit } = require('./lib/order'); 6 | 7 | const init = async () => { 8 | // recognize bullish engulfing pattern 9 | // find downtrending stock 10 | const symbol = await getDownTrendingStock(); 11 | // Check every minute 12 | const checkAndOrder = async () => { 13 | // look at last 2 interval candles before the current minute, for example if the time is 9:32am, 9:30am is bar1, 9:31am is bar2 14 | const oneMinuteMS = 60000; 15 | const now = new Date(); 16 | const start = new Date(now - (2 * oneMinuteMS)).toISOString(); 17 | const end = new Date(now - oneMinuteMS).toISOString(); 18 | console.log('Checking bars', end, start); 19 | const bars = await getBars({symbol, start, end}); 20 | // if: 21 | // - bar1 closes < bar1 open 22 | // - bar2 closes > bar2 opening 23 | // - bar2 closes > bar1 open 24 | // - bar2 opens < bar1 close 25 | // - bar2 volume > bar1 volume 26 | const bar1 = bars[symbol][0]; 27 | const bar2 = bars[symbol][1]; 28 | if ( 29 | (bar1 && bar2) && 30 | (bar1.c < bar1.o) && 31 | (bar2.c > bar2.o) && 32 | (bar2.c > bar1.o) && 33 | (bar2.o < bar1.c) && 34 | (bar2.v > bar1.v) 35 | ) { 36 | // Get 10% of account value 37 | const willingToSpend = getAccountValue() * .1; 38 | // Find how many shares we can buy with 10% of account value 39 | const amt = Math.floor(willingToSpend / bar2.c); 40 | // Buy this stock 41 | const purchase = await buyMarket({symbol, amt}); 42 | // set stop at bar1 low price 43 | sellStop({symbol, price: bar1.l, amt}); 44 | // set 100% limit sell at 2:1 profit ratio, which comes out to ((purchase price - stop price) * 2) + purchase price 45 | const profitTarget = ((purchase.price - bar1.l) * 2) + purchase.price; 46 | sellLimit({symbol, price: profitTarget, amt}); 47 | } 48 | } 49 | checkAndOrder(); 50 | setInterval(checkAndOrder, 60 * 1000); 51 | } 52 | init(); 53 | --------------------------------------------------------------------------------