Skip to content

Latest commit

 

History

History
192 lines (145 loc) · 5.91 KB

README.md

File metadata and controls

192 lines (145 loc) · 5.91 KB

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

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

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();
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();

Positions

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

Assets

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

Calendar

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

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

Clock

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

Market Data

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.