Package net.jacobpeterson.alpaca
Class AlpacaAPI
java.lang.Object
net.jacobpeterson.alpaca.AlpacaAPI
AlpacaAPI
is the main class used to interface with the various Alpaca API endpoints. If you are using the
Trading or Market Data APIs for a single Alpaca account or if you are using the Broker API, you will generally only
need one instance of this class. However, if you are using the Trading API with OAuth to act on behalf of an Alpaca
account, this class is optimized so that it can be instantiated quickly, especially when an existing
OkHttpClient
is given in the constructor. Additionally, all API endpoint instances are instantiated lazily.
This class is thread-safe.- See Also:
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionAlpacaAPI
(String traderKeyID, String traderSecretKey, String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType, MarketDataWebsocketSourceType marketDataWebsocketSourceType, String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
.AlpacaAPI
(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType) Instantiates a newAlpacaAPI
.AlpacaAPI
(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
.AlpacaAPI
(String traderKeyID, String traderSecretKey, TraderAPIEndpointType traderAPIEndpointType, MarketDataWebsocketSourceType marketDataWebsocketSourceType) Instantiates a newAlpacaAPI
.AlpacaAPI
(String traderKeyID, String traderSecretKey, TraderAPIEndpointType traderAPIEndpointType, MarketDataWebsocketSourceType marketDataWebsocketSourceType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
.AlpacaAPI
(String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType) Instantiates a newAlpacaAPI
.AlpacaAPI
(String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
. -
Method Summary
Modifier and TypeMethodDescriptionbroker()
Gets theAlpacaBrokerAPI
.static AlpacaAPI.Builder
builder()
Creates aAlpacaAPI.Builder
forAlpacaAPI
.void
Closes theOkHttpClient
.Gets theCryptoMarketDataWebsocketInterface
.okhttp3.OkHttpClient
Gets theOkHttpClient
.Gets theAlpacaMarketDataAPI
.Gets theNewsMarketDataWebsocketInterface
.Gets theStockMarketDataWebsocketInterface
.trader()
Gets theAlpacaTraderAPI
.Gets theUpdatesWebsocketInterface
.
-
Constructor Details
-
AlpacaAPI
public AlpacaAPI(String traderKeyID, String traderSecretKey, TraderAPIEndpointType traderAPIEndpointType, MarketDataWebsocketSourceType marketDataWebsocketSourceType) Instantiates a newAlpacaAPI
. Use this constructor if you are using the Trading or Market Data APIs for a single Alpaca account.- Parameters:
traderKeyID
- the Trader key IDtraderSecretKey
- the Trader secret keytraderAPIEndpointType
- theTraderAPIEndpointType
marketDataWebsocketSourceType
- theMarketDataWebsocketSourceType
-
AlpacaAPI
public AlpacaAPI(String traderKeyID, String traderSecretKey, TraderAPIEndpointType traderAPIEndpointType, MarketDataWebsocketSourceType marketDataWebsocketSourceType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
. Use this constructor if you are using the Trading or Market Data APIs for a single Alpaca account and a customOkHttpClient
instance.- Parameters:
traderKeyID
- the Trader key IDtraderSecretKey
- the Trader secret keytraderAPIEndpointType
- theTraderAPIEndpointType
marketDataWebsocketSourceType
- theMarketDataWebsocketSourceType
okHttpClient
- an existingOkHttpClient
ornull
to create a new default instance
-
AlpacaAPI
Instantiates a newAlpacaAPI
. Use this constructor if you are using the Trading API with OAuth to act on behalf of an Alpaca account.- Parameters:
traderOAuthToken
- the Trader OAuth tokentraderAPIEndpointType
- theTraderAPIEndpointType
-
AlpacaAPI
public AlpacaAPI(String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
. Use this constructor if you are using the Trading API with OAuth to act on behalf of an Alpaca account and a customOkHttpClient
instance.- Parameters:
traderOAuthToken
- the Trader OAuth tokentraderAPIEndpointType
- theTraderAPIEndpointType
okHttpClient
- an existingOkHttpClient
ornull
to create a new default instance
-
AlpacaAPI
public AlpacaAPI(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType) Instantiates a newAlpacaAPI
. Use this constructor if you are using the Broker API.- Parameters:
brokerAPIKey
- the Broker API keybrokerAPISecret
- the Broker API secretbrokerAPIEndpointType
- theBrokerAPIEndpointType
-
AlpacaAPI
public AlpacaAPI(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
. Use this constructor if you are using the Broker API and a customOkHttpClient
instance.- Parameters:
brokerAPIKey
- the Broker API keybrokerAPISecret
- the Broker API secretbrokerAPIEndpointType
- theBrokerAPIEndpointType
okHttpClient
- an existingOkHttpClient
ornull
to create a new default instance
-
AlpacaAPI
public AlpacaAPI(String traderKeyID, String traderSecretKey, String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType, MarketDataWebsocketSourceType marketDataWebsocketSourceType, String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType, okhttp3.OkHttpClient okHttpClient) Instantiates a newAlpacaAPI
.- Parameters:
traderKeyID
- the Trader key IDtraderSecretKey
- the Trader secret keytraderOAuthToken
- the Trader OAuth tokentraderAPIEndpointType
- theTraderAPIEndpointType
marketDataWebsocketSourceType
- theMarketDataWebsocketSourceType
brokerAPIKey
- the Broker API keybrokerAPISecret
- the Broker API secretbrokerAPIEndpointType
- theBrokerAPIEndpointType
okHttpClient
- an existingOkHttpClient
ornull
to create a new default instance
-
-
Method Details
-
closeOkHttpClient
public void closeOkHttpClient()Closes theOkHttpClient
. -
getOkHttpClient
public okhttp3.OkHttpClient getOkHttpClient()Gets theOkHttpClient
.- Returns:
- the
OkHttpClient
-
trader
Gets theAlpacaTraderAPI
. Lazily instantiated.- Returns:
- the
AlpacaTraderAPI
-
marketData
Gets theAlpacaMarketDataAPI
. Lazily instantiated.- Returns:
- the
AlpacaMarketDataAPI
-
broker
Gets theAlpacaBrokerAPI
. Lazily instantiated.- Returns:
- the
AlpacaBrokerAPI
-
updatesStream
Gets theUpdatesWebsocketInterface
. Lazily instantiated.- Returns:
- the
UpdatesWebsocketInterface
-
stockMarketDataStream
Gets theStockMarketDataWebsocketInterface
. Lazily instantiated.- Returns:
- the
StockMarketDataWebsocketInterface
-
cryptoMarketDataStream
Gets theCryptoMarketDataWebsocketInterface
. Lazily instantiated.- Returns:
- the
CryptoMarketDataWebsocketInterface
-
newsMarketDataStream
Gets theNewsMarketDataWebsocketInterface
. Lazily instantiated.- Returns:
- the
NewsMarketDataWebsocketInterface
-
builder
Creates aAlpacaAPI.Builder
forAlpacaAPI
.- Returns:
- the
AlpacaAPI.Builder
-