Another gem right up my alley. Video here.
Backend server with Firebase cloud functions, Node function running on a chron job during market hours. Used Alpaca API for commission-free trades and real-time market data.
Gets started with firebase-tools
Need a firebase account to do this.
Installing alpaca dependency
npm i @alpacahq/alpaca-trade-api
The default firebase cloud function looks like this:
// functions/index.js
const functions = require('firebase-functions')
exports.helloWorld = functions.https.onRequest((request, response) => {})
He uses it to connect to GPT-3 and get the stocks that Cramer likes.
Run “npm run serve” to run the functions locally. This will give you a link to the browser with the function URL.
He also uses puppeteer to scrape Cramer’s twitter for some ideas and feeds this into the GPT-3 query.
In order to get started with Alpaca you need to make a deposit and get a SECRET API key.
const Alpaca = require("@alpacahq/alpaca-trade-api")
const alpaca = new Alpaca({
keyId: functions.config().alpaca.id, // REPLACE with your API creds
secretKey: functions.config.alpaca.key, // REPLACE with your API creds
paper: true, // Trade with real money or not
})
// get account with important info like buying power
const account = await alpaca.getAccount()
console.log(`dry powder: ${account.buying_power}`)
// place order
// supports fractional shares with notional
const order = await alpaca.createOrder({
symbol: "SPY",
qty: 1,
side: "buy",
type: "market",
time_in_force: "day"
})
Now sends the order as the response and so running the function should make a trade and return a confirmation number. Congratulations you are now an algo trader.
Wow.
Run the code every day. A “Cron Job” is a schedule for doing work in the background on the server. Easily set one up with firebase using a pub/sub function, (scalable cloud function, firebase docs here).
Note: use a site like crontab.guru to help convert your desired interval into cron schedule expressions.
Also Note: He allocated memory because puppeteer uses a lot of memory.
exports.getRichQuick = functions.runWith({memory: "4GB"}).pubsub
.schedule('0 10 * * 1-5')
.timezone('America/New_York')
.onRun(async (ctx) => {
console.log("This will run M-F at 10:00am Eastern!")
// pastes in the code from his cloud function
return null
})
He also cancels all orders and close out any open positions first just above creating account constant. Everyday he sells all his stocks and then buys a new one.
const cancel = await alpaca.cancelAllOrders()
const liquidate = await alpaca.closeAllPositions()
Run “firebase deploy” to push the code to the cloud.