Login
Historical
Streaming
Alpaca API
Command History
Account
Node Graph
// get account info
getAccount().then(d => console.log(d))
getPositions().then(d => console.log(d))
getAssetInfo('NVDA').then(d => console.log(d))
// place trades and close position
placeTrade({
"symbol": "GOOGL",
"qty": 1,
"side": "buy",
"type": "market",
"time_in_force": "day"
})
.then(d => console.log(d))
closePosition('GOOGL')
// get order
var orderId = '1db007bd-88f3-445a-a49b-18de71705fc4'
fetch(`https://paper-api.alpaca.markets/v2/orders/${orderId}`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// get list of orders
var endpoint = 'orders'
fetch(`https://paper-api.alpaca.markets/v2/${endpoint}`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// cancel order
var endpoint = 'orders/e43c203d-4452-44d8-8ef4-a77b3b51df86'
fetch(`https://paper-api.alpaca.markets/v2/${endpoint}`, {
method: 'DELETE',
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => {
console.log(res)
return res
})
// get stock snapshot
var endpoint = 'stocks/NVDA/snapshot'
fetch(`https://data.alpaca.markets/v2/${endpoint}`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// get multiple stock snapshots
var endpoint = 'stocks/snapshots'
var symbols = "NVDA,MSFT,AAPL"
fetch(`https://data.alpaca.markets/v2/${endpoint}?symbols=${symbols}`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// fetch bars data v1
fetch(`https://data.alpaca.markets/v1/bars/day?symbols=AAPL&limit=10&start=2019-02-01T00:00:00Z&end=2019-02-10T00:00:00Z`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// API call from plot
const stocks = ["NVDA","AAPL","MSFT","SPY"]
const start = "2021-01-01T00:00:00Z"
const end = new Date().toISOString()
fetch(`https://data.alpaca.markets/v1/bars/day?symbols=${stocks.join(',')}&limit=1000&start=${start}&end=${end}`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
})
// minute bars
fetch(`https://data.alpaca.markets/v2/stocks/NVDA/bars?start=2021-06-28&end=2021-06-28&timeframe=1Min`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// minutes bars, multiple stocks (uses v1 API)
fetch(`https://data.alpaca.markets/v1/bars/1Min?symbols=NVDA,MSFT,SPY&start=${dayStart()}&end=${fifteenMinutesAgo()}`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// get minute bars from start of day to 15 minutes ago
fetch(`https://data.alpaca.markets/v2/stocks/NVDA/bars?start=${dayStart()}&end=${fifteenMinutesAgo()}&timeframe=1Min`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
.then(res => res.json())
.then(data => {
console.log(data)
return data
})
// get minute bars for today, for multiple stocks (use Promise.all)
let requests = ["NVDA", "SPY"].map(sym => {
return fetch(`https://data.alpaca.markets/v2/stocks/${sym}/bars?start=${dayStart()}&end=${fifteenMinutesAgo()}&timeframe=1Min`, {
headers: {
'APCA-API-KEY-ID': apiKey,
'APCA-API-SECRET-KEY': secretKey
}
})
})
console.log(requests)
Promise.all(requests)
.then(res => {
return Promise.all(res.map(res => res.json()))
})
.then(data => console.log(data))
// use alpaca api library
// client and stream are available
client.getAccount()
.then(d => console.log(d))
client.getAssets({
status: 'active'
})
.then(assets => {
console.log(assets)
assetsList = assets
})
// subscribe to bars
stream.once('authenticated', () => {
stream.subscribe('bars', ['SPY', 'NVDA'])
stream.on('bar', bar => console.log(bar))
})
stream.on('message', (message) => console.log(message))
stream.on('bar', bar => console.log(bar))
// use raw websockets
ws.send(JSON.stringify({"action":"subscribe","bars":["NVDA", "SPY"]}))