Coder Social home page Coder Social logo

lostnaustin / alpaca-trade-api-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alpacahq/alpaca-trade-api-js

1.0 1.0 0.0 1.56 MB

Node.js library for Alpaca Trade API.

Home Page: https://www.npmjs.com/package/@alpacahq/alpaca-trade-api

License: Apache License 2.0

JavaScript 68.77% Shell 0.03% TypeScript 31.19%

alpaca-trade-api-js's Introduction

Alpaca Trade API JS

npm version CircleCI

Node.js library for Alpaca Trade API.

API Documentation

The REST API documentation can be found in https://docs.alpaca.markets. For detailed information about an endpoint, please consult the REST API docs. Documentation specific to this library can be found below.

News

We introduced Typescript support recently, which allows you to use strongly typed data structures and better IDE experience if you are using it.

Installation

npm install --save @alpacahq/alpaca-trade-api

Runtime Dependencies

  • Node.js v14.x or newer
  • npm version 6 and above

Usage

Import the module first.

const Alpaca = require('@alpacahq/alpaca-trade-api')

Instantiate the API with config options, obtained from the dashboard at app.alpaca.markets.

const alpaca = new Alpaca({
  keyId: 'AKFZXJH121U18SHHDRFO',
  secretKey: 'pnq4YHlpMF3LhfLyOvmdfLmlz6BnASrTPQIASeiU',
  paper: true,
})

Note: keyId and secretKey may also be specified by setting the APCA_API_KEY_ID and APCA_API_SECRET_KEY environment variables, respectively. Also, rather than specifying paper, you may set APCA_API_BASE_URL as an environment variable to direct your API calls to the paper trading API.

Call methods, which will return a promise.

alpaca.getAccount().then((account) => {
  console.log('Current Account:', account)
})

The websocket api is a good way to watch and react to the market we have 2 types of websockets:

  • data websocket: get updates to data equities
  • account/trade websocket: get updates on your account

please refer to this example code to see how to use the websockets.

Data WS

The Alapca websocket service now supports V2. Make sure you update your old sample code accordingly.
You could use it even if you don't have a funded account.

Methods

As a general rule, required method parameters are passed as plain function arguments, and the final parameter is an object containing any optional parameters to the method.

Account API

Get Account

Calls GET /account and returns the current account.

getAccount() => Promise<Account>

Account Configurations API

Get Account Configurations

Calls GET /account/configurations and returns the current account configurations.

getAccountConfigurations() => Promise<AccountConfigurations>

Update Account Configurations

Calls PATCH /account/configurations to update the account configurations, and returns the updated configurations.

updateAccountConfigurations(AccountConfigurations) => Promise<AccountConfigurations>

Get Account Activities

Calls GET /account/activities and returns account actvities.

getAccountActivities({
  activityTypes: string | string[], // Any valid activity type
  until: Date,
  after: Date,
  direction: string,
  date: Date,
  pageSize: number,
  pageToken: string
}) => Promise<AccountActivity[]>

Portfolio History API

Get Portfolio History

Calls GET /account/portfolio/history and returns portfolio history.

getPortfolioHistory({
  date_start: Date,
  date_end: Date,
  period: '1M' | '3M' | '6M' | '1A' | 'all' | 'intraday',
  timeframe: '1Min' | '5Min' | '15Min' | '1H' | '1D',
  extended_hours: Boolean
}) => Promise<PortfolioHistory>

Orders API

Create Order

Calls POST /orders and creates a new order.

createOrder({
  symbol: string, // any valid ticker symbol
  qty: number, 
  notional: number, // qty or notional required, not both
  side: 'buy' | 'sell',
  type: 'market' | 'limit' | 'stop' | 'stop_limit' | 'trailing_stop',
  time_in_force: 'day' | 'gtc' | 'opg' | 'ioc',
  limit_price: number, // optional,
  stop_price: number, // optional,
  client_order_id: string, // optional,
  extended_hours: boolean, // optional,
  order_class: string, // optional,
  take_profit: object, // optional,
  stop_loss: object, // optional,
  trail_price: string, // optional,
  trail_percent: string // optional,
}) => Promise<Order>

Get Orders

Calls GET /orders and returns a list of orders.

getOrders({
  status: 'open' | 'closed' | 'all',
  after: Date,
  until: Date,
  limit: number,
  direction: 'asc' | 'desc'
}) => Promise<Order[]>

Get Order by ID

Calls GET /orders/{id} and returns an order.

getOrder(uuid) => Promise<Order>

Get Order by Client ID

Calls GET /orders:by_client_order_id and returns an order by client_order_id. You can set client_order_id upon order creation to more easily keep track of your orders.

getOrderByClientOrderId(string) => Promise<Order>

Update Order by ID

Calls PATCH /orders/{id} and updates an existing open order. The updated order will have a new ID.

replaceOrder(uuid) => Promise<Order>

Cancel Order

Calls DELETE /orders/{id} and deletes an order.

cancelOrder(uuid) => Promise

Cancel all Orders

Calls DELETE /orders and deletes all open orders.

cancelAllOrders() => Promise

Positions API

Get Position

Calls GET /positions/{symbol} and returns a position.

getPosition(symbol) => Promise<Position>

Get All Positions

Calls GET /positions and returns all positions.

getPositions() => Promise<Position[]>

Close a Position

Calls DELETE /positions/{symbol} and liquidates your position in the given symbol.

closePosition(symbol) => Promise

Close all Positions

Calls DELETE /positions and liquidates all open positions.

closeAllPositions() => Promise

Assets API

Get All Assets

Calls GET /assets and returns assets matching your parameters.

getAssets({
  status: 'active' | 'inactive',
  asset_class: string
}) => Promise<Asset[]>

Get information about an asset

Calls GET /assets/{symbol} and returns an asset entity.

getAsset(symbol) => Promise<Asset>

Calendar API

Calls GET /calendar and returns the market calendar.

getCalendar({ start: Date, end: Date }) => Promise<Calendar[]>

Watchlist API

available methods for you to use:

module.exports = {
    getWatchlists,
    getWatchlist,
    addWatchlist,
    addToWatchlist,
    updateWatchlist,
    deleteWatchlist,
    deleteFromWatchlist,
}

Get All Watchlists

this.alpaca.getWatchlists().then((response) => {
      console.log(response)
    })

Get Specific Watchlist

 // xxxx.. are the watchlist id you get on creation or with get all
 this.alpaca.getWatchlist('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx').then((response) => {
      console.log(response)
    })

Add a Watchlist

this.alpaca.addWatchlist("myWatchList", []).then((response) => {
      console.log(response)
    })

Add to Watchlist

this.alpaca.addToWatchlist('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', "AAPL").then((response) => {
  console.log(response)
})

Update a Watchlist

this.alpaca.updateWatchlist("myWatchList", ["AAPL", "GOOG"]).then((response) => {
      console.log(response)
    })

Delete a Watchlist

this.alpaca.deleteWatchlist('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx').then((response) => {
      console.log(response)
    })

Delete from Watchlist

this.alpaca.deleteFromWatchlist('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', "AAPL").then((response) => {
  console.log(response)
})

Data API

Get Bars

getBarsV2(
  symbol,
  
  {
    limit: number,
    start: date isoformat string yyyy-mm-ddThh:MM:ss-04:00,
    end: date isoformat string yyyy-mm-ddThh:MM:ss-04:00,
    timeframe: "1Min" | "1Hour" | "1Day"
  }
) => Promise<BarsObject>
example
let resp = this.alpaca.getBarsV2(
    "AAPL",
    {
        start: "2021-02-01",
        end: "2021-02-10",
        limit: 2,
        timeframe: "1Day",
        adjustment: "all",
    },
    alpaca.configuration
);
const bars = [];

for await (let b of resp) {
    console.log(b)
}

note: to get the date of response samples you could do this console.log(new Date(resp['AAPL'][0].startEpochTime*1000))

Last trade

lastTrade(
  symbol: string)
) => Promise<LastTradeObject>
example
this.alpaca.lastTrade('AAPL').then((response) => {
          console.log(response)
        })

Last quote

lastQuote(
  symbol: string)
) => Promise<LastQuoteObject>
example
this.alpaca.lastQuote('AAPL').then((response) => {
          console.log(response)
        })

Websockets

You can use data websocket with or without a funded account.

Working with websocket

  • The websocket is created when you creating the Alpaca instance
  • const websocket = alpaca.data_stream_v2: Get the websocket client instance.
  • websocket.connect(): Connect to the Alpaca server using websocket.
  • client.onConnect(function() {}): all the following code will be executed after the user authentication. You can also subscribe and unsubscribe for data outside this function.
  • websocket.subscribeForTrades(["symbol"]): Subscribe for trades data for the given symbol(s). You can do the same with quotes, bars, dailyBars, statuses and lulds.
  • websocket.onStockTrade(function(trade) {}): Get the data and process it inside this function.
  • websocket.unsubscribeFromTrades(["symbol"]): Unsunscribe from symbol(s).
  • websocket.onDisconnect(function() {}) and websocket.disconnect(): the function inside the onDisconnect will run when you disconnect, then closes the connection between the client and server.

  • const cryptoWebsocket = alpaca.crypto_stream_v2: Get the crypto websocket client instance.
  • cryptoWebsocket.subscribeForTrades(["symbol"]), cryptoWebsocket.unsubscribeFromTrades(["symbol"]), cryptoWebsocket.onCryptoTrade(function(cryptoTrade) {}) Working as the examples above.
  • cryptoWebsocket.onDisconnect(function() {}) and cryptoWebsocket.disconnect(): Same as above.

alpaca-trade-api-js's People

Contributors

shlomiku avatar ttt733 avatar andrewkim316 avatar noramehesz avatar suddjian avatar motocombotag avatar camelpac avatar umitanuki avatar sc4recoin avatar bexanderthebex avatar kalmant avatar gnvk avatar haxdds avatar krisk avatar ccnlui avatar axelmaldonado avatar bbeale avatar chamburr avatar mdkrieg avatar areyaar avatar dependabot[bot] avatar martonw avatar neeschit avatar smartchris84 avatar the-reverend avatar

Stargazers

Chad Lengefeld avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.