Java API for Alpaca - a commission-free API-first stock brokerage
- Clone this repository
- Run
./mvn clean install
in the project folder to build the project and install it to the local Maven repository
Add alpaca-trade-api-java
as a dependency:
<dependency>
<groupId>io.github.maseev</groupId>
<artifactId>alpaca-trade-api-java</artifactId>
<version>1.0</version>
</dependency>
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 account = api.account().get().get();
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();
OrderRequest request =
ImmutableOrderRequest.builder()
.symbol("AAPL")
.qty(1)
.side(BUY)
.type(MARKET)
.timeInForce(DAY)
.build();
Order order = api.orders().place(request).get();
Order order = api.orders().get("id").get();
Order order = api.orders().getByClientOrderId("id").get();
api.orders().cancel("id").get();
List<Position> positions = api.positions().get().get();
Position position = api.positions().get("AAPL").get();
List<Asset> assets = api.assets().get(ACTIVE, US_EQUITY).get();
Asset asset = api.assets().get("AAPL").get();
LocalDate start = LocalDate.now();
LocalDate end = start.plusDays(10);
List<Calendar> calendars = api.calendar().get(start, end).get();
Clock clock = api.clock().get().get();
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();
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.