public class AlpacaAPI extends Object
AlpacaAPI
contains several methods to interface with Alpaca. You will generally only need one instance of it
in your application. Note that many methods allow null
to be passed in as a parameter if it is
optional.
Note that most of these methods are blocking methods and this class in not thread-safe.
Constructor and Description |
---|
AlpacaAPI()
Instantiates a new
AlpacaAPI using properties specified in alpaca.properties file (or their
associated defaults). |
AlpacaAPI(String oAuthToken)
Instantiates a new
AlpacaAPI . |
AlpacaAPI(String keyID,
String secret)
Instantiates a new
AlpacaAPI . |
AlpacaAPI(String keyID,
String secret,
EndpointAPIType endpointAPIType,
DataAPIType dataAPIType)
Instantiates a new
AlpacaAPI . |
AlpacaAPI(String keyID,
String secret,
String oAuthToken,
EndpointAPIType endpointAPIType,
DataAPIType dataAPIType)
Instantiates a new
AlpacaAPI . |
Modifier and Type | Method and Description |
---|---|
void |
addAlpacaStreamListener(AlpacaStreamListener streamListener)
Adds the
AlpacaStreamListener . |
void |
addMarketDataStreamListener(MarketDataListener streamListener)
Adds the
MarketDataListener . |
Watchlist |
addWatchlistAsset(String watchlistID,
String symbol)
Append an asset for the symbol to the end of watchlist asset list
|
ArrayList<CancelledOrder> |
cancelAllOrders()
Attempts to cancel all open orders.
|
boolean |
cancelOrder(String orderId)
Attempts to cancel an open order.
|
ArrayList<ClosePositionOrder> |
closeAllPositions()
Closes (liquidates) all of the account’s open long and short positions.
|
Order |
closePosition(String symbol)
Closes (liquidates) the account’s open position for the given symbol.
|
Watchlist |
createWatchlist(String name,
String... symbols)
Create a new watchlist with initial set of assets.
|
boolean |
deleteWatchlist(String watchlistID)
Delete a watchlist.
|
Account |
getAccount()
Returns the
Account associated with the API key. |
List<AccountActivity> |
getAccountActivities(ZonedDateTime date,
ZonedDateTime until,
ZonedDateTime after,
SortDirection sortDirection,
Integer pageSize,
String pageToken,
ActivityType... activityTypes)
Returns
AccountActivity entries for many or for a specific type of activity. |
AccountConfiguration |
getAccountConfiguration()
Returns the current
AccountConfiguration values. |
Asset |
getAssetBySymbol(String symbolOrAssetID)
Gets the asset by symbol or asset_id.
|
ArrayList<Asset> |
getAssets(AssetStatus assetStatus,
String assetClass)
Get a list of assets.
|
BarsResponse |
getBars(String symbol,
ZonedDateTime start,
ZonedDateTime end,
Integer limit,
String pageToken,
BarsTimeFrame timeFrame)
Gets
Bar aggregate historical data for the requested security. |
List<Calendar> |
getCalendar()
Returns the market
Calendar from 1970 to 2029. |
List<Calendar> |
getCalendar(LocalDate start,
LocalDate end)
Returns the market
Calendar . |
Clock |
getClock()
Returns the market
Clock . |
Position |
getOpenPositionBySymbol(String symbol)
Gets the open position by symbol.
|
ArrayList<Position> |
getOpenPositions()
Gets the open positions.
|
Order |
getOrder(String orderID,
Boolean nested)
Retrieves a single order for the given order_id.
|
Order |
getOrderByClientID(String clientOrderId)
Retrieves a single order for the given client_order_id.
|
List<Order> |
getOrders(OrderStatus status,
Integer limit,
ZonedDateTime after,
ZonedDateTime until,
SortDirection sortDirection,
Boolean nested,
List<String> symbols)
Retrieves a list of orders for the account, filtered by the supplied query parameters.
|
PortfolioHistory |
getPortfolioHistory(Integer periodLength,
PortfolioPeriodUnit periodUnit,
PortfolioTimeFrame timeFrame,
LocalDate dateEnd,
Boolean extendedHours)
Returns timeseries data about equity and profit/loss (P/L) of the account in requested timespan.
|
QuotesResponse |
getQuotes(String symbol,
ZonedDateTime start,
ZonedDateTime end,
Integer limit,
String pageToken)
Gets
Quote (NBBO or National Best Bid and Offer) historical data for the requested security. |
TradesResponse |
getTrades(String symbol,
ZonedDateTime start,
ZonedDateTime end,
Integer limit,
String pageToken)
Gets
Trade historical data for the requested security. |
Watchlist |
getWatchlist(String watchlistID)
Returns a watchlist identified by the ID.
|
ArrayList<Watchlist> |
getWatchlists()
Returns the list of watchlists registered under the account.
|
void |
removeAlpacaStreamListener(AlpacaStreamListener streamListener)
Removes the
AlpacaStreamListener . |
void |
removeMarketDataStreamListener(MarketDataListener streamListener)
Removes the
MarketDataListener . |
Watchlist |
removeSymbolFromWatchlist(String watchlistID,
String symbol)
Delete one entry for an asset by symbol name.
|
Order |
replaceOrder(String orderID,
Integer quantity,
OrderTimeInForce timeInForce,
Double limitPrice,
Double stopPrice,
Double trail,
String clientOrderId)
Replaces a single order with updated parameters.
|
Order |
requestNewFractionalMarketOrder(String symbol,
Double quantity,
OrderSide side)
A market order is a request to buy or sell a security at the currently available market price.
|
Order |
requestNewLimitBracketOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double limitPrice,
Boolean extendedHours,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
A bracket order is a chain of three orders that can be used to manage your position entry and exit.
|
Order |
requestNewLimitOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double limitPrice,
Boolean extendedHours)
A limit order is an order to buy or sell at a specified price or better.
|
Order |
requestNewMarketBracketOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
A bracket order is a chain of three orders that can be used to manage your position entry and exit.
|
Order |
requestNewMarketOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce)
A market order is a request to buy or sell a security at the currently available market price.
|
Order |
requestNewNotionalMarketOrder(String symbol,
Double notional,
OrderSide side)
A market order is a request to buy or sell a security at the currently available market price.
|
Order |
requestNewOCOOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Boolean extendedHours,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
OCO (One-Cancels-Other) is another type of advanced order type.
|
Order |
requestNewOrder(String symbol,
Double quantity,
Double notional,
OrderSide side,
OrderType type,
OrderTimeInForce timeInForce,
Double limitPrice,
Double stopPrice,
Double trailPrice,
Double trailPercent,
Boolean extendedHours,
String clientOrderId,
OrderClass orderClass,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
Places a new order for the given account.
|
Order |
requestNewOTOLimitOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double limitPrice,
Boolean extendedHours,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
OTO (One-Triggers-Other) is a variant of bracket order.
|
Order |
requestNewOTOMarketOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
OTO (One-Triggers-Other) is a variant of bracket order.
|
Order |
requestNewOTOStopLimitOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double limitPrice,
Double stopPrice,
Boolean extendedHours,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
OTO (One-Triggers-Other) is a variant of bracket order.
|
Order |
requestNewOTOStopOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double stopPrice,
Boolean extendedHours,
Double takeProfitLimitPrice,
Double stopLossStopPrice,
Double stopLossLimitPrice)
OTO (One-Triggers-Other) is a variant of bracket order.
|
Order |
requestNewStopLimitOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double limitPrice,
Double stopPrice,
Boolean extendedHours)
A stop-limit order is a conditional trade over a set time frame that combines the features of a stop order with
those of a limit order and is used to mitigate risk.
|
Order |
requestNewStopOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double stopPrice,
Boolean extendedHours)
A stop (market) order is an order to buy or sell a security when its price moves past a particular point,
ensuring a higher probability of achieving a predetermined entry or exit price.
|
Order |
requestNewTrailingStopPercentOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double trailPercent,
Boolean extendedHours)
Trailing stop orders allow you to continuously and automatically keep updating the stop price threshold based on
the stock price movement.
|
Order |
requestNewTrailingStopPriceOrder(String symbol,
Integer quantity,
OrderSide side,
OrderTimeInForce timeInForce,
Double trailPrice,
Boolean extendedHours)
Trailing stop orders allow you to continuously and automatically keep updating the stop price threshold based on
the stock price movement.
|
AccountConfiguration |
setAccountConfiguration(AccountConfiguration accountConfiguration)
Sets
AccountConfiguration . |
String |
toString() |
Watchlist |
updateWatchlist(String watchlistID,
String name,
String... symbols)
Update the name and/or content of watchlist.
|
public AlpacaAPI()
AlpacaAPI
using properties specified in alpaca.properties
file (or their
associated defaults).public AlpacaAPI(String keyID, String secret)
AlpacaAPI
.keyID
- the key IDsecret
- the secretpublic AlpacaAPI(String keyID, String secret, EndpointAPIType endpointAPIType, DataAPIType dataAPIType)
AlpacaAPI
.keyID
- the key IDsecret
- the secretendpointAPIType
- the EndpointAPIType
dataAPIType
- the DataAPIType
public AlpacaAPI(String oAuthToken)
AlpacaAPI
.oAuthToken
- the OAuth tokenpublic AlpacaAPI(String keyID, String secret, String oAuthToken, EndpointAPIType endpointAPIType, DataAPIType dataAPIType)
AlpacaAPI
.keyID
- the key IDsecret
- the secretoAuthToken
- the OAuth tokenendpointAPIType
- the EndpointAPIType
dataAPIType
- the DataAPIType
public Account getAccount() throws AlpacaAPIRequestException
Account
associated with the API key.Account
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic List<AccountActivity> getAccountActivities(ZonedDateTime date, ZonedDateTime until, ZonedDateTime after, SortDirection sortDirection, Integer pageSize, String pageToken, ActivityType... activityTypes) throws AlpacaAPIRequestException
AccountActivity
entries for many or for a specific type of activity.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.)sortDirection
- asc or desc (default desc if unspecified.)pageSize
- The maximum number of entries to return in the response. (See the section on paging.)pageToken
- The ID of the end of your current page of results. (See the section on paging.)activityTypes
- the activity types (null for all activities)AccountActivity
List
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic AccountConfiguration getAccountConfiguration() throws AlpacaAPIRequestException
AccountConfiguration
values.AccountConfiguration
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic AccountConfiguration setAccountConfiguration(AccountConfiguration accountConfiguration) throws AlpacaAPIRequestException
AccountConfiguration
.accountConfiguration
- the account configurationAccountConfiguration
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic List<Order> getOrders(OrderStatus status, Integer limit, ZonedDateTime after, ZonedDateTime until, SortDirection sortDirection, Boolean nested, List<String> symbols) 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)sortDirection
- The chronological order of response based on the submission time. asc or desc. Defaults to
desc.nested
- If true, the result will roll up multi-leg orders under the legs field of primary order.symbols
- A list of symbols to filter the result by (null for no filter).AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewOrder(String symbol, Double quantity, Double notional, OrderSide side, OrderType type, OrderTimeInForce timeInForce, Double limitPrice, Double stopPrice, Double trailPrice, Double trailPercent, Boolean extendedHours, String clientOrderId, OrderClass orderClass, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to trade. Can be fractional.notional
- dollar amount to trade. Cannot work with qty. Can only work for market order types
and day for time in force.side
- 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_limittrailPrice
- this or trail_percent is required if type is trailing_stoptrailPercent
- this or trail_price is required if type is trailing_stopextendedHours
- (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.orderClass
- Simple, bracket, oco, or oto. For details of non-simple order classes, please see
Bracket Order Overview on the Alpaca Docs.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders. Required for bracket
orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders. Required for bracket
orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewMarketOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
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.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewFractionalMarketOrder(String symbol, Double quantity, OrderSide side) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
with OrderType.MARKET
and OrderTimeInForce.DAY
with a fractional quantity.symbol
- symbol or asset ID to identify the asset to tradequantity
- number of shares to trade. Can be fractional.side
- buy or sellAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewNotionalMarketOrder(String symbol, Double notional, OrderSide side) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
with OrderType.MARKET
and OrderTimeInForce.DAY
with a notional dollar amount.symbol
- symbol or asset ID to identify the asset to tradenotional
- dollar amount to tradeside
- buy or sellAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewLimitOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double limitPrice, Boolean extendedHours) throws AlpacaAPIRequestException
requestNewOrder(String, Double,
Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass,
Double, Double, Double)
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.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewStopOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double stopPrice, Boolean extendedHours) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
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 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.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewStopLimitOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double limitPrice, Double stopPrice, Boolean extendedHours) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
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.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewMarketBracketOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
with OrderType.MARKET
and with parameters for a
bracket order.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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders. Required for bracket
orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders. Required for bracket
orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewLimitBracketOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double limitPrice, Boolean extendedHours, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType, OrderTimeInForce, Double, Double, Double, Double,
Boolean, String, OrderClass, Double, Double, Double)
with OrderType.LIMIT
and with parameters for a
bracket order.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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders. Required for bracket
orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders. Required for bracket
orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewOCOOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Boolean extendedHours, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType,
OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double, Double)
with
parameters for an OCO order type.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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewOTOMarketOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide,
OrderType, OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double,
Double)
with OrderType.LIMIT
and with parameters for an OTO order type.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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewOTOLimitOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double limitPrice, Boolean extendedHours, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide,
OrderType, OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double,
Double)
with OrderType.LIMIT
and with parameters for an OTO order type.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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewOTOStopOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double stopPrice, Boolean extendedHours, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide,
OrderType, OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double,
Double)
with OrderType.STOP
and with parameters for an OTO order type.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 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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewOTOStopLimitOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double limitPrice, Double stopPrice, Boolean extendedHours, Double takeProfitLimitPrice, Double stopLossStopPrice, Double stopLossLimitPrice) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide,
OrderType, OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double,
Double)
with OrderType.STOP_LIMIT
and with parameters for an OTO order type.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.takeProfitLimitPrice
- Additional parameter for take-profit leg of advanced orders.stopLossStopPrice
- Additional parameters for stop-loss leg of advanced orders.stopLossLimitPrice
- Additional parameters for stop-loss leg of advanced orders. The stop-loss order
becomes a stop-limit order if specified.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewTrailingStopPriceOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double trailPrice, Boolean extendedHours) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType,
OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double, Double)
with
OrderType.TRAILING_STOP
and with parameters for a Trailing Stop Price order type.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.trailPrice
- this or trail_percent is required if type is trailing_stopextendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order requestNewTrailingStopPercentOrder(String symbol, Integer quantity, OrderSide side, OrderTimeInForce timeInForce, Double trailPercent, Boolean extendedHours) throws AlpacaAPIRequestException
requestNewOrder(String, Double, Double, OrderSide, OrderType,
OrderTimeInForce, Double, Double, Double, Double, Boolean, String, OrderClass, Double, Double, Double)
with
OrderType.TRAILING_STOP
and with parameters for a Trailing Stop Percent order type.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.trailPercent
- this or trail_price is required if type is trailing_stopextendedHours
- (default) false. If true, order will be eligible to execute in premarket/afterhours. Only
works with type limit and time_in_force day.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order getOrder(String orderID, Boolean nested) throws AlpacaAPIRequestException
orderID
- Order IDnested
- If true, the result will roll up multi-leg orders under the legs field of primary order.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order getOrderByClientID(String clientOrderId) throws AlpacaAPIRequestException
clientOrderId
- the client order idAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order replaceOrder(String orderID, Integer quantity, OrderTimeInForce timeInForce, Double limitPrice, Double stopPrice, Double trail, 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_limittrail
- the new value of the trail_price or trail_percent value (works only for type=trailing_stop)clientOrderId
- A unique identifier for the order. Automatically generated if not sent.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic ArrayList<CancelledOrder> cancelAllOrders() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic boolean cancelOrder(String orderId) throws AlpacaAPIRequestException
orderId
- Order IDAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic ArrayList<Position> getOpenPositions() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Position getOpenPositionBySymbol(String symbol) throws AlpacaAPIRequestException
symbol
- the symbol or asset_id (required)AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic ArrayList<ClosePositionOrder> closeAllPositions() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Order closePosition(String symbol) throws AlpacaAPIRequestException
symbol
- symbol or asset_idAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic ArrayList<Asset> getAssets(AssetStatus assetStatus, String assetClass) throws AlpacaAPIRequestException
assetStatus
- e.g. “active”. By default, all statuses are included.assetClass
- Defaults to us_equity.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Asset getAssetBySymbol(String symbolOrAssetID) throws AlpacaAPIRequestException
symbolOrAssetID
- the symbol or asset_id (required)AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic ArrayList<Watchlist> getWatchlists() throws AlpacaAPIRequestException
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Watchlist createWatchlist(String name, String... symbols) throws AlpacaAPIRequestException
name
- arbitrary name string, up to 64 characterssymbols
- set of symbol stringAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Watchlist getWatchlist(String watchlistID) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Watchlist updateWatchlist(String watchlistID, String name, String... symbols) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDname
- the new watchlist namesymbols
- the new list of symbol names to replace the watchlist contentAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Watchlist addWatchlistAsset(String watchlistID, String symbol) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDsymbol
- the symbol name to add to the watchlistAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic boolean deleteWatchlist(String watchlistID) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Watchlist removeSymbolFromWatchlist(String watchlistID, String symbol) throws AlpacaAPIRequestException
watchlistID
- Watchlist IDsymbol
- symbol name to remove from the watchlist contentAlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic PortfolioHistory getPortfolioHistory(Integer periodLength, PortfolioPeriodUnit periodUnit, PortfolioTimeFrame timeFrame, LocalDate dateEnd, Boolean extendedHours) throws AlpacaAPIRequestException
periodLength
- The duration of the data in "number + unit", such as 1D, where unit can be D for day, W for
week, M for month and A for year. Defaults to 1M. This parameter is the number.periodUnit
- The duration of the data in "number + unit", such as 1D, where unit can be D for day, W for
week, M for month and A for year. Defaults to 1M. This parameter is the unit.timeFrame
- The resolution of time window. 1Min, 5Min, 15Min, 1H, or 1D. If omitted, 1Min for less than
7 days period, 15Min for less than 30 days, or otherwise 1D.dateEnd
- The date the data is returned up to, in "YYYY-MM-DD" format. Defaults to the current market
date (rolls over at the market open if extended_hours is false, otherwise at 7am ET)extendedHours
- If true, include extended hours in the result. This is effective only for timeframe less
than 1D.AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic List<Calendar> getCalendar() throws AlpacaAPIRequestException
Calendar
from 1970 to 2029.Calendar
List
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic List<Calendar> getCalendar(LocalDate start, LocalDate end) throws AlpacaAPIRequestException
Calendar
.start
- the first date to retrieve data for (inclusive)end
- the last date to retrieve data for (inclusive)Calendar
List
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic Clock getClock() throws AlpacaAPIRequestException
Clock
.Clock
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic TradesResponse getTrades(String symbol, ZonedDateTime start, ZonedDateTime end, Integer limit, String pageToken) throws AlpacaAPIRequestException
Trade
historical data for the requested security.symbol
- the symbol to query forstart
- filter data equal to or after this ZonedDateTime
. Fractions of a second are not
acceptedend
- filter data equal to or before this ZonedDateTime
. Fractions of a second are not
acceptedlimit
- number of data points to return. Must be in range 1-10000, defaults to 1000 if null is givenpageToken
- pagination token to continue fromTradesResponse
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic QuotesResponse getQuotes(String symbol, ZonedDateTime start, ZonedDateTime end, Integer limit, String pageToken) throws AlpacaAPIRequestException
Quote
(NBBO or National Best Bid and Offer) historical data for the requested security.symbol
- the symbol to query forstart
- filter data equal to or after this ZonedDateTime
. Fractions of a second are not
acceptedend
- filter data equal to or before this ZonedDateTime
. Fractions of a second are not
acceptedlimit
- number of data points to return. Must be in range 1-10000, defaults to 1000 if null is givenpageToken
- pagination token to continue fromQuotesResponse
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic BarsResponse getBars(String symbol, ZonedDateTime start, ZonedDateTime end, Integer limit, String pageToken, BarsTimeFrame timeFrame) throws AlpacaAPIRequestException
Bar
aggregate historical data for the requested security.symbol
- the symbol to query forstart
- filter data equal to or after this ZonedDateTime
. Fractions of a second are not
acceptedend
- filter data equal to or before this ZonedDateTime
. Fractions of a second are not
acceptedlimit
- number of data points to return. Must be in range 1-10000, defaults to 1000 if null is givenpageToken
- pagination token to continue fromtimeFrame
- the BarsTimeFrame
for the aggregationBarsResponse
AlpacaAPIRequestException
- thrown for AlpacaAPIRequestException
spublic void addAlpacaStreamListener(AlpacaStreamListener streamListener) throws WebsocketException
AlpacaStreamListener
. Note that when the first AlpacaStreamListener
is added, the
Websocket connection is created.streamListener
- the stream listenerWebsocketException
- thrown for WebsocketException
spublic void removeAlpacaStreamListener(AlpacaStreamListener streamListener) throws WebsocketException
AlpacaStreamListener
. Note that when the last AlpacaStreamListener
is removed, the
Websocket connection is closed.streamListener
- the AlpacaStreamListener
WebsocketException
- thrown for WebsocketException
spublic void addMarketDataStreamListener(MarketDataListener streamListener) throws WebsocketException
MarketDataListener
. Note that when the first AlpacaStreamListener
is added, the
Websocket connection is created.streamListener
- the MarketDataListener
WebsocketException
- thrown for WebsocketException
spublic void removeMarketDataStreamListener(MarketDataListener streamListener) throws WebsocketException
MarketDataListener
. Note that when the last MarketDataListener
is removed, the
Websocket connection is closed.streamListener
- the MarketDataListener
WebsocketException
- thrown for WebsocketException
s