public class AlpacaAPI
extends java.lang.Object
Constructor and Description |
---|
AlpacaAPI()
Instantiates a new Alpaca API using properties specified in alpaca.properties file (or relevant defaults)
|
AlpacaAPI(java.lang.String apiVersion)
Instantiates a new Alpaca API using the specified apiVersion
|
AlpacaAPI(java.lang.String apiVersion,
java.lang.String keyId,
java.lang.String secret)
Instantiates a new Alpaca API using the specified apiVersion, keyId, and secret.
|
AlpacaAPI(java.lang.String apiVersion,
java.lang.String keyId,
java.lang.String secret,
java.lang.String baseAPIURL)
Instantiates a new Alpaca API using the specified apiVersion, keyId, secret, and baseAPIURL.
|
AlpacaAPI(java.lang.String apiVersion,
java.lang.String keyId,
java.lang.String secret,
java.lang.String baseAPIURL,
java.lang.String baseDataUrl)
Instantiates a new Alpaca API using the specified keyId, secret, baseAPIURL, and baseDataUrl.
|
Modifier and Type | Method and Description |
---|---|
void |
addAlpacaStreamListener(AlpacaStreamListener streamListener)
Adds the alpaca stream listener.
|
Watchlist |
addWatchlistAsset(java.lang.String watchlistID,
java.lang.String symbol)
Append an asset for the symbol to the end of watchlist asset list
|
java.util.ArrayList<CancelledOrder> |
cancelAllOrders()
Attempts to cancel all open orders.
|
boolean |
cancelOrder(java.lang.String orderId)
Attempts to cancel an open order.
|
Watchlist |
createWatchlist(java.lang.String name,
java.lang.String... symbols)
Create a new watchlist with initial set of assets.
|
boolean |
deleteWatchlist(java.lang.String watchlistID)
Delete a watchlist.
|
Account |
getAccount()
Returns the account associated with the API key.
|
java.util.ArrayList<AccountActivity> |
getAccountActivities(java.time.ZonedDateTime date,
java.time.ZonedDateTime until,
java.time.ZonedDateTime after,
Direction direction,
java.lang.Integer pageSize,
java.lang.String pageToken,
ActivityType... activityTypes)
Returns account activity entries for many or for a specific type of activity.
|
AccountConfiguration |
getAccountConfiguration()
Returns the current account configuration values.
|
Asset |
getAssetBySymbol(java.lang.String symbolOrAssetID)
Gets the asset by symbol or asset_id.
|
java.util.ArrayList<Asset> |
getAssets(AssetStatus assetStatus,
java.lang.String assetClass)
Get a list of assets.
|
java.util.Map<java.lang.String,java.util.ArrayList<Bar>> |
getBars(BarsTimeFrame timeframe,
java.lang.String[] symbols,
java.lang.Integer limit,
java.time.ZonedDateTime start,
java.time.ZonedDateTime end,
java.time.ZonedDateTime after,
java.time.ZonedDateTime until)
Retrieves a list of bars for each requested symbol.
|
java.util.Map<java.lang.String,java.util.ArrayList<Bar>> |
getBars(BarsTimeFrame timeframe,
java.lang.String symbol,
java.lang.Integer limit,
java.time.ZonedDateTime start,
java.time.ZonedDateTime end,
java.time.ZonedDateTime after,
java.time.ZonedDateTime until)
Retrieves a list of bars for each requested symbol.
|
java.util.ArrayList<Calendar> |
getCalendar()
Returns the market calendar.
|
java.util.ArrayList<Calendar> |
getCalendar(java.time.LocalDate start,
java.time.LocalDate end)
Returns the market calendar.
|
Clock |
getClock()
Returns the market clock.
|
Position |
getOpenPositionBySymbol(java.lang.String symbol)
Gets the open position by symbol.
|
java.util.ArrayList<Position> |
getOpenPositions()
Gets the open positions.
|
Order |
getOrder(java.lang.String orderID)
Retrieves a single order for the given order_id.
|
Order |
getOrderByClientID(java.lang.String clientOrderId)
Retrieves a single order for the given client_order_id.
|
java.util.ArrayList<Order> |
getOrders(OrderStatus status,
java.lang.Integer limit,
java.time.ZonedDateTime after,
java.time.ZonedDateTime until,
Direction direction)
Retrieves a list of orders for the account, filtered by the supplied query parameters.
|
Watchlist |
getWatchlist(java.lang.String watchlistID)
Returns a watchlist identified by the ID.
|
java.util.ArrayList<Watchlist> |
getWatchlists()
Returns the list of watchlists registered under the account.
|
void |
removeAlpacaStreamListener(AlpacaStreamListener streamListener)
Removes the alpaca stream listener.
|
Watchlist |
removeSymbolFromWatchlist(java.lang.String watchlistID,
java.lang.String symbol)
Delete one entry for an asset by symbol name.
|
Order |
replaceOrder(java.lang.String orderID,
java.lang.Integer quantity,
OrderTimeInForce timeInForce,
java.lang.Double limitPrice,
java.lang.Double stopPrice,
java.lang.String clientOrderId)
Replaces a single order with updated parameters.
|
Order |
requestNewLimitOrder(java.lang.String symbol,
java.lang.Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
java.lang.Double limitPrice,
java.lang.Boolean extendedHours,
java.lang.String clientOrderId)
|
Order |
requestNewMarketOrder(java.lang.String symbol,
java.lang.Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
java.lang.Boolean extendedHours,
java.lang.String clientOrderId)
|
Order |
requestNewOrder(java.lang.String symbol,
java.lang.Integer quantity,
OrderSide side,
OrderType type,
OrderTimeInForce timeInForce,
java.lang.Double limitPrice,
java.lang.Double stopPrice,
java.lang.Boolean extendedHours,
java.lang.String clientOrderId)
Places a new order for the given account.
|
Order |
requestNewStopLimitOrder(java.lang.String symbol,
java.lang.Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
java.lang.Double limitPrice,
java.lang.Double stopPrice,
java.lang.Boolean extendedHours,
java.lang.String clientOrderId)
|
Order |
requestNewStopOrder(java.lang.String symbol,
java.lang.Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
java.lang.Double stopPrice,
java.lang.Boolean extendedHours,
java.lang.String clientOrderId)
|
AccountConfiguration |
setAccountConfiguration(AccountConfiguration accountConfiguration)
Sets account configuration.
|
java.lang.String |
toString() |
Watchlist |
updateWatchlist(java.lang.String watchlistID,
java.lang.String name,
java.lang.String... symbols)
Update the name and/or content of watchlist.
|
public AlpacaAPI()
public AlpacaAPI(java.lang.String apiVersion)
apiVersion
- the api versionpublic AlpacaAPI(java.lang.String apiVersion, java.lang.String keyId, java.lang.String secret)
keyId
- the key idsecret
- the secretpublic AlpacaAPI(java.lang.String apiVersion, java.lang.String keyId, java.lang.String secret, java.lang.String baseAPIURL)
apiVersion
- the api versionkeyId
- the key idsecret
- the secretbaseAPIURL
- the api account urlpublic AlpacaAPI(java.lang.String apiVersion, java.lang.String keyId, java.lang.String secret, java.lang.String baseAPIURL, java.lang.String baseDataUrl)
apiVersion
- the api versionkeyId
- the key idsecret
- the secretbaseAPIURL
- the base api urlbaseDataUrl
- the base data urlpublic Account getAccount() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.ArrayList<AccountActivity> getAccountActivities(java.time.ZonedDateTime date, java.time.ZonedDateTime until, java.time.ZonedDateTime after, Direction direction, java.lang.Integer pageSize, java.lang.String pageToken, ActivityType... activityTypes) throws AlpacaAPIRequestException
date
- The date for which you want to see activities.until
- The response will contain only activities submitted before this date. (Cannot be used with
date.)after
- The response will contain only activities submitted after this date. (Cannot be used with
date.)direction
- asc or desc (default desc if unspecified.)pageSize
- The maximum number of entries to return in the response. (See the section on paging above.)pageToken
- The ID of the end of your current page of results. (See the section on paging above.)activityTypes
- the activity types (null for all activities)AlpacaAPIRequestException
- the alpaca api exceptionpublic AccountConfiguration getAccountConfiguration() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca api exceptionpublic AccountConfiguration setAccountConfiguration(AccountConfiguration accountConfiguration) throws AlpacaAPIRequestException
accountConfiguration
- the account configurationAlpacaAPIRequestException
- the alpaca api exceptionpublic java.util.ArrayList<Order> getOrders(OrderStatus status, java.lang.Integer limit, java.time.ZonedDateTime after, java.time.ZonedDateTime until, Direction direction) throws AlpacaAPIRequestException
status
- Order status to be queried. open, closed or all. Defaults to open.limit
- The maximum number of orders in response. Defaults to 50 and max is 500.after
- The response will include only ones submitted after this timestamp (exclusive.)until
- The response will include only ones submitted until this timestamp (exclusive.)direction
- The chronological order of response based on the submission time. asc or desc. Defaults to
desc.AlpacaAPIRequestException
- the alpaca API exceptionpublic Order requestNewOrder(java.lang.String symbol, java.lang.Integer quantity, OrderSide side, OrderType type, OrderTimeInForce timeInForce, java.lang.Double limitPrice, java.lang.Double stopPrice, java.lang.Boolean extendedHours, java.lang.String clientOrderId) throws AlpacaAPIRequestException
symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to tradeside
- buy or selltype
- market, limit, stop, or stop_limittimeInForce
- day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.limitPrice
- required if type is limit or stop_limitstopPrice
- required if type is stop or stop_limitextendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.clientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- the alpaca API exceptionpublic Order requestNewMarketOrder(java.lang.String symbol, java.lang.Integer quantity, OrderSide side, OrderTimeInForce timeInForce, java.lang.Boolean extendedHours, java.lang.String clientOrderId) throws AlpacaAPIRequestException
requestNewOrder(String, Integer, OrderSide, OrderType, OrderTimeInForce, Double, Double, Boolean,
String)
with OrderType.MARKET
.symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to tradeside
- buy or selltimeInForce
- day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.extendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.clientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- the alpaca api request exceptionpublic Order requestNewLimitOrder(java.lang.String symbol, java.lang.Integer quantity, OrderSide side, OrderTimeInForce timeInForce, java.lang.Double limitPrice, java.lang.Boolean extendedHours, java.lang.String clientOrderId) throws AlpacaAPIRequestException
requestNewOrder(String, Integer, OrderSide, OrderType, OrderTimeInForce, Double, Double, Boolean,
String)
* with OrderType.LIMIT
.symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to tradeside
- buy or selltimeInForce
- day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.limitPrice
- required if type is limit or stop_limitextendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.clientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- the alpaca api request exceptionpublic Order requestNewStopOrder(java.lang.String symbol, java.lang.Integer quantity, OrderSide side, OrderTimeInForce timeInForce, java.lang.Double stopPrice, java.lang.Boolean extendedHours, java.lang.String clientOrderId) throws AlpacaAPIRequestException
requestNewOrder(String, Integer, OrderSide, OrderType, OrderTimeInForce, Double, Double, Boolean,
String)
with OrderType.STOP
.symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to tradeside
- buy or selltimeInForce
- day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.stopPrice
- required if type is limit or stop_limitextendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.clientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- the alpaca api request exceptionpublic Order requestNewStopLimitOrder(java.lang.String symbol, java.lang.Integer quantity, OrderSide side, OrderTimeInForce timeInForce, java.lang.Double limitPrice, java.lang.Double stopPrice, java.lang.Boolean extendedHours, java.lang.String clientOrderId) throws AlpacaAPIRequestException
requestNewOrder(String, Integer, OrderSide, OrderType, OrderTimeInForce, Double, Double, Boolean,
String)
with OrderType.STOP_LIMIT
.symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to tradeside
- buy or selltimeInForce
- day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.limitPrice
- required if type is limit or stop_limitstopPrice
- required if type is stop or stop_limitextendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.clientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- the alpaca api request exceptionpublic Order getOrder(java.lang.String orderID) throws AlpacaAPIRequestException
orderID
- Order IDAlpacaAPIRequestException
- the alpaca API exceptionpublic Order getOrderByClientID(java.lang.String clientOrderId) throws AlpacaAPIRequestException
clientOrderId
- the client order idAlpacaAPIRequestException
- the alpaca API exceptionpublic Order replaceOrder(java.lang.String orderID, java.lang.Integer quantity, OrderTimeInForce timeInForce, java.lang.Double limitPrice, java.lang.Double stopPrice, java.lang.String clientOrderId) throws AlpacaAPIRequestException
orderID
- Order idquantity
- number of shares to tradetimeInForce
- day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.limitPrice
- required if type is limit or stop_limitstopPrice
- required if type is stop or stop_limitclientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- the alpaca api request exceptionpublic java.util.ArrayList<CancelledOrder> cancelAllOrders() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca api request exceptionpublic boolean cancelOrder(java.lang.String orderId) throws AlpacaAPIRequestException
orderId
- Order IDAlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.ArrayList<Position> getOpenPositions() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca API exceptionpublic Position getOpenPositionBySymbol(java.lang.String symbol) throws AlpacaAPIRequestException
symbol
- the symbol or asset_id (required)AlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.ArrayList<Asset> getAssets(AssetStatus assetStatus, java.lang.String assetClass) throws AlpacaAPIRequestException
assetStatus
- e.g. “active”. By default, all statuses are included.assetClass
- Defaults to us_equity.AlpacaAPIRequestException
- the alpaca API exceptionpublic Asset getAssetBySymbol(java.lang.String symbolOrAssetID) throws AlpacaAPIRequestException
symbolOrAssetID
- the symbol or asset_id (required)AlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.ArrayList<Watchlist> getWatchlists() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca api exceptionpublic Watchlist createWatchlist(java.lang.String name, java.lang.String... symbols) throws AlpacaAPIRequestException
name
- arbitrary name string, up to 64 characterssymbols
- set of symbol stringAlpacaAPIRequestException
- the alpaca api exceptionpublic Watchlist getWatchlist(java.lang.String watchlistID) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDAlpacaAPIRequestException
- the alpaca api exceptionpublic Watchlist updateWatchlist(java.lang.String watchlistID, java.lang.String name, java.lang.String... symbols) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDname
- the new watchlist namesymbols
- the new list of symbol names to replace the watchlist contentAlpacaAPIRequestException
- the alpaca api exceptionpublic Watchlist addWatchlistAsset(java.lang.String watchlistID, java.lang.String symbol) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDsymbol
- the symbol name to add to the watchlistAlpacaAPIRequestException
- the alpaca api exceptionpublic boolean deleteWatchlist(java.lang.String watchlistID) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDAlpacaAPIRequestException
- the alpaca api exceptionpublic Watchlist removeSymbolFromWatchlist(java.lang.String watchlistID, java.lang.String symbol) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDsymbol
- symbol name to remove from the watchlist contentAlpacaAPIRequestException
- the alpaca api exceptionpublic java.util.ArrayList<Calendar> getCalendar() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.ArrayList<Calendar> getCalendar(java.time.LocalDate start, java.time.LocalDate end) throws AlpacaAPIRequestException
start
- The first date to retrieve data for (inclusive)end
- The last date to retrieve data for (inclusive)AlpacaAPIRequestException
- the alpaca API exceptionpublic Clock getClock() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.Map<java.lang.String,java.util.ArrayList<Bar>> getBars(BarsTimeFrame timeframe, java.lang.String symbol, java.lang.Integer limit, java.time.ZonedDateTime start, java.time.ZonedDateTime end, java.time.ZonedDateTime after, java.time.ZonedDateTime until) throws AlpacaAPIRequestException
Currently, no “incomplete” bars are returned. For example, a 1 minute bar for 09:30 will not be returned until 09:31.
timeframe
- One of minute, 1Min, 5Min, 15Min, day or 1D. minute is an alias of 1Min. Similarly, day is of
1D.symbol
- One symbol name.limit
- The maximum number of bars to be returned for each symbol. It can be between 1 and 1000. Default
is 100 if parameter is unspecified or 0.start
- Filter bars equal to or after this time. Cannot be used with after.end
- Filter bars equal to or before this time. Cannot be used with until.after
- Filter bars after this time. Cannot be used with start.until
- Filter bars before this time. Cannot be used with end.AlpacaAPIRequestException
- the alpaca API exceptionpublic java.util.Map<java.lang.String,java.util.ArrayList<Bar>> getBars(BarsTimeFrame timeframe, java.lang.String[] symbols, java.lang.Integer limit, java.time.ZonedDateTime start, java.time.ZonedDateTime end, java.time.ZonedDateTime after, java.time.ZonedDateTime until) throws AlpacaAPIRequestException
Currently, no “incomplete” bars are returned. For example, a 1 minute bar for 09:30 will not be returned until 09:31.
timeframe
- One of minute, 1Min, 5Min, 15Min, day or 1D. minute is an alias of 1Min. Similarly, day is of
1D.symbols
- One or more (max 200) symbol names split by commas (“,”).limit
- The maximum number of bars to be returned for each symbol. It can be between 1 and 1000. Default
is 100 if parameter is unspecified or 0.start
- Filter bars equal to or after this time. Cannot be used with after.end
- Filter bars equal to or before this time. Cannot be used with until.after
- Filter bars after this time. Cannot be used with start.until
- Filter bars before this time. Cannot be used with end.AlpacaAPIRequestException
- the alpaca API exceptionpublic void addAlpacaStreamListener(AlpacaStreamListener streamListener)
streamListener
- the stream listenerpublic void removeAlpacaStreamListener(AlpacaStreamListener streamListener)
streamListener
- the stream listenerpublic java.lang.String toString()
toString
in class java.lang.Object