alpaca
![Prettier(idk)](https://img.shields.io/static/v1?label=code%20style&message=prettier&color=ff51bc&style=flat-square)
a TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams
Contents
Install
From NPM:
> npm i @master-chief/alpaca
From GitHub:
> npm i 117/alpaca
Client
import * as alpaca from "@master-chief/alpaca";
let client = new alpaca.Client({
credentials: {
key: "mykey",
secret: "mysecret",
},
paper: true,
rate_limit: true,
});
The following methods are available on the client.
isAuthenticated
await client.isAuthenticated();
getAccount
await client.getAccount();
getOrder
await client.getOrder({
order_id: "6187635d-04e5-485b-8a94-7ce398b2b81c",
});
getOrders
await client.getOrders({
limit: 25,
status: "all",
});
placeOrder
await client.placeOrder({
symbol: "SPY",
qty: 1,
side: "buy",
type: "market",
time_in_force: "day",
});
replaceOrder
await client.replaceOrder({
order_id: "69a3db8b-cc63-44da-a26a-e3cca9490308",
limit_price: 9.74,
});
cancelOrder
await client.cancelOrder({
order_id: "69a3db8b-cc63-44da-a26a-e3cca9490308",
});
cancelOrders
await client.cancelOrders();
getPosition
await client.getPosition({ symbol: "SPY" });
getPositions
await client.getPositions();
closePosition
await client.closePosition({ symbol: "SPY" });
closePositions
await client.closePositions();
getAsset
await client.getAsset({ asset_id_or_symbol: "SPY" });
getAssets
await client.getAssets({ status: "active" });
getWatchlist
await client.getWatchlist({ uuid: "2000e463-6f87-41c0-a8ba-3e40cbf67128" });
getWatchlists
await client.getWatchlists();
createWatchlist
await client.createWatchlist({
name: "my watchlist",
symbols: ["SPY", "DIA", "EEM", "XLF"],
});
updateWatchlist
await client.updateWatchlist({
uuid: "2000e463-6f87-41c0-a8ba-3e40cbf67128",
name: "new watchlist name",
symbols: ["TSLA", "AAPL"],
});
addToWatchlist
await client.addToWatchlist({
uuid: "2000e463-6f87-41c0-a8ba-3e40cbf67128",
symbol: "F",
});
removeFromWatchlist
await client.removeFromWatchlist({
uuid: "2000e463-6f87-41c0-a8ba-3e40cbf67128",
symbol: "F",
});
deleteWatchlist
await client.deleteWatchlist({
uuid: "2000e463-6f87-41c0-a8ba-3e40cbf67128",
});
getCalender
await client.getCalendar({ start: new Date(), end: new Date() });
getClock
await client.getClock();
getAccountConfigurations
await client.getAccountConfigurations();
updateAccountConfigurations
await client.updateAccountConfigurations({
no_shorting: true,
suspend_trade: true,
});
getAccountActivities
await client.getAccountActivities({
activity_type: "FILL",
});
getPortfolioHistory
await client.getPortfolioHistory({
period: "1D",
timeframe: "1Min",
});
getBars
await client.getBars({
symbols: ["SPY", "DIA", "XLF"],
});
getLastTrade
await client.getLastTrade({
symbol: "SPY",
});
getLastQuote
await client.getLastQuote({
symbol: "SPY",
});
Stream
Note: Each key is allowed only 1 simultaneous connection to each host.
import * as alpaca from "@master-chief/alpaca";
let stream = new alpaca.Stream({
credentials: {
key: "mykey",
secret: "mysecret",
},
host: alpaca.URL.WSS_MARKET_DATA,
});
The following events are available on the market data stream.
aggregate_minute
stream.on("authenticated", () => stream.subscribe(["AM.SPY"]));
stream.on("aggregate_minute", console.log);
quote
stream.on("authenticated", () => stream.subscribe(["Q.SPY"]));
stream.on("quote", console.log);
trade
stream.on("authenticated", () => stream.subscribe(["T.SPY"]));
stream.on("trade", console.log);
trade
stream.on("authenticated", () => stream.subscribe(["T.SPY"]));
stream.on("trade", console.log);
The following events are available on the account stream.
trade_updates
stream.on("authenticated", () => stream.subscribe(["trade_updates"]));
stream.on("trade_updates", console.log);
account_updates
stream.on("authenticated", () => stream.subscribe(["account_updates"]));
stream.on("account_updates", console.log);
Contribute
Pull requests are encouraged. 😁