maseev
alpaca-trade-api-java
Java

Java API for Alpaca - a commission-free API-first stock brokerage

Last updated Nov 13, 2025
13
Stars
13
Forks
3
Issues
0
Stars/day
Attention Score
19
Language breakdown
Java 100.0%
Files click to expand
README

Alpaca Trade API Java ===================== Build Status Coverage Status Total alerts Language grade: Java GitHub

Java API for Alpaca - a commission-free API-first stock brokerage


How to build


  • Clone this repository
  • Run ./mvn clean install in the project folder to build the project and install it to the local Maven repository
How to use

Add alpaca-trade-api-java as a dependency:

Maven
<dependency>
  <groupId>io.github.maseev</groupId>
  <artifactId>alpaca-trade-api-java</artifactId>
  <version>1.0</version>
</dependency>

Initialization

String keyId = "Your API key ID";
String secretKey = "Your secret key";

AlpacaAPI api = new AlpacaAPI(TEST, V1, keyId, secretKey);

alpaca-trade-api-java provides two versions of API, asynchronous:

api.account().get().whenComplete((account, throwable) -> {});

and synchronous:

Account account = api.account().get().get();

Account

Get the account

Account account = api.account().get().get();

Orders

Get a list of orders

Status status = Status.OPEN;
int limit = 10;
LocalDateTime after = of(2007, Month.DECEMBER, 1, 10, 00, 10);
LocalDateTime until = of(2009, Month.DECEMBER, 1, 10, 00, 10);
Direction direction = Direction.ASC;
    
List<Order> orders =
  api.orders()
    .get(status, limit, after, until, direction)
    .get();

Request a new order

OrderRequest request =
  ImmutableOrderRequest.builder()
    .symbol("AAPL")
    .qty(1)
    .side(BUY)
    .type(MARKET)
    .timeInForce(DAY)
    .build();

Order order = api.orders().place(request).get();

Get an order

Order order = api.orders().get("id").get();

Get an order by client order id

Order order = api.orders().getByClientOrderId("id").get();

Cancel an order

api.orders().cancel("id").get();

Positions

Get open positions

List<Position> positions = api.positions().get().get();

Get an open position

Position position = api.positions().get("AAPL").get();

Assets

Get assets

List<Asset> assets = api.assets().get(ACTIVE, US_EQUITY).get();

Get an asset

Asset asset = api.assets().get("AAPL").get();

Calendar

Get the calendar

LocalDate start = LocalDate.now();
LocalDate end = start.plusDays(10);

List<Calendar> calendars = api.calendar().get(start, end).get();

Clock

Get the clock

Clock clock = api.clock().get().get();

Market Data

Get a list of bars

String symbol = "AAPL";
Timeframe timeframe = Timeframe.DAY;
OffsetDateTime start = of(2019, Month.FEBRUARY.getValue(), 10, 12, 30, 00, 0, ZoneOffset.UTC);
OffsetDateTime end = start.plusWeeks(3);
boolean timeInclusive = false;
int limit = 10;
    
Map<String, List<Bar>> bars =
  api.bars()
    .get(symbol, timeframe, start, end, timeInclusive, 10)
    .get();

Streaming

There are four types of events you can subscribe on AccountUpdate, TradeUpdate, ConnectionClose, and ConnectionCrash.

You can subscribe to these events by calling the subscribe method:

api.streaming().subscribe((AccountUpdate event) -> {});
api.streaming().subscribe((TradeUpdate event) -> {});
api.streaming().subscribe((ConnectionClose event) -> {});
api.streaming().subscribe((ConnectionCrash event) -> {});

In order to connect to the Alpaca Streaming API, you need to call the connect method:

api.streaming().connect();

You also have to handle the situation when the connection gets closed. In order to reconnect to the Streaming API in this situation, you have to subscribe to the ConectionClose event and call the connect method from the event handler:

api.streaming().subscribe((ConnectionClose event) -> { api.streaming().connect(); });

Notice, that you don't have to resubscribe to all events because all your subscriptions are stored separately from the connection to the Streaming API.

🔗 More in this category

© 2026 GitRepoTrend · maseev/alpaca-trade-api-java · Updated daily from GitHub