forked from Petersoj/alpaca-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlpacaAPI.java
311 lines (273 loc) · 11.6 KB
/
AlpacaAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package net.jacobpeterson.alpaca;
import devcsrj.okhttp3.logging.HttpLoggingInterceptor;
import net.jacobpeterson.alpaca.model.properties.DataAPIType;
import net.jacobpeterson.alpaca.model.properties.EndpointAPIType;
import net.jacobpeterson.alpaca.properties.AlpacaProperties;
import net.jacobpeterson.alpaca.rest.AlpacaClient;
import net.jacobpeterson.alpaca.rest.endpoint.accountactivities.AccountActivitiesEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.accountconfiguration.AccountConfigurationEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.account.AccountEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.AlpacaEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.assets.AssetsEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.calendar.CalendarEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.clock.ClockEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.orders.OrdersEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.portfoliohistory.PortfolioHistoryEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.positions.PositionsEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.watchlist.WatchlistEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.marketdata.crypto.CryptoMarketDataEndpoint;
import net.jacobpeterson.alpaca.rest.endpoint.marketdata.stock.StockMarketDataEndpoint;
import net.jacobpeterson.alpaca.websocket.AlpacaWebsocket;
import net.jacobpeterson.alpaca.websocket.marketdata.MarketDataWebsocketInterface;
import net.jacobpeterson.alpaca.websocket.marketdata.crypto.CryptoMarketDataWebsocket;
import net.jacobpeterson.alpaca.websocket.marketdata.stock.StockMarketDataWebsocket;
import net.jacobpeterson.alpaca.websocket.streaming.StreamingWebsocket;
import net.jacobpeterson.alpaca.websocket.streaming.StreamingWebsocketInterface;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* The {@link AlpacaAPI} class contains several instances of various {@link AlpacaEndpoint}s and {@link
* AlpacaWebsocket}s to interface with Alpaca. You will generally only need one instance of this class in your
* application. Note that many methods inside the various {@link AlpacaEndpoint}s allow <code>null<code/> to be passed
* in as a parameter if it is optional.
*
* @see <a href="https://docs.alpaca.markets/api-documentation/api-v2/">Alpaca API Documentation</a>
*/
public class AlpacaAPI {
private static final Logger LOGGER = LoggerFactory.getLogger(AlpacaAPI.class);
private static final String VERSION_2_PATH_SEGMENT = "v2";
private static final String VERSION_1_BETA_1_PATH_SEGMENT = "v1beta1";
private final OkHttpClient okHttpClient;
private final AlpacaClient brokerClient;
private final AlpacaClient cryptoDataClient;
private final AlpacaClient stockDataClient;
// Ordering of fields/methods below are analogous to the ordering in the Alpaca documentation
private final AccountEndpoint accountEndpoint;
private final CryptoMarketDataEndpoint cryptoMarketDataEndpoint;
private final StockMarketDataEndpoint stockMarketDataEndpoint;
private final OrdersEndpoint ordersEndpoint;
private final PositionsEndpoint positionsEndpoint;
private final AssetsEndpoint assetsEndpoint;
private final WatchlistEndpoint watchlistEndpoint;
private final CalendarEndpoint calendarEndpoint;
private final ClockEndpoint clockEndpoint;
private final AccountConfigurationEndpoint accountConfigurationEndpoint;
private final AccountActivitiesEndpoint accountActivitiesEndpoint;
private final PortfolioHistoryEndpoint portfolioHistoryEndpoint;
private final StreamingWebsocket streamingWebsocket;
private final CryptoMarketDataWebsocket cryptoMarketDataWebsocket;
private final StockMarketDataWebsocket stockMarketDataWebsocket;
/**
* Instantiates a new {@link AlpacaAPI} using properties specified in <code>alpaca.properties</code> file (or their
* associated defaults).
*/
public AlpacaAPI() {
this(AlpacaProperties.KEY_ID,
AlpacaProperties.SECRET_KEY,
AlpacaProperties.ENDPOINT_API_TYPE,
AlpacaProperties.DATA_API_TYPE);
}
/**
* Instantiates a new {@link AlpacaAPI}.
*
* @param keyID the key ID
* @param secretKey the secret key
*/
public AlpacaAPI(String keyID, String secretKey) {
this(null, keyID, secretKey, null,
AlpacaProperties.ENDPOINT_API_TYPE,
AlpacaProperties.DATA_API_TYPE);
}
/**
* Instantiates a new {@link AlpacaAPI}.
*
* @param keyID the key ID
* @param secretKey the secret key
* @param endpointAPIType the {@link EndpointAPIType}
* @param dataAPIType the {@link DataAPIType}
*/
public AlpacaAPI(String keyID, String secretKey, EndpointAPIType endpointAPIType, DataAPIType dataAPIType) {
this(null, keyID, secretKey, null, endpointAPIType, dataAPIType);
}
/**
* Instantiates a new {@link AlpacaAPI}.
*
* @param oAuthToken the OAuth token. Note that the Data API v2 does not work with OAuth tokens.
*/
public AlpacaAPI(String oAuthToken) {
this(null, null, null, oAuthToken,
AlpacaProperties.ENDPOINT_API_TYPE,
AlpacaProperties.DATA_API_TYPE);
}
/**
* Instantiates a new {@link AlpacaAPI}.
*
* @param okHttpClient the {@link OkHttpClient} or <code>null</code> to create a default instance
* @param keyID the key ID
* @param secretKey the secret key
* @param oAuthToken the OAuth token
* @param endpointAPIType the {@link EndpointAPIType}
* @param dataAPIType the {@link DataAPIType}
*/
public AlpacaAPI(OkHttpClient okHttpClient, String keyID, String secretKey, String oAuthToken,
EndpointAPIType endpointAPIType, DataAPIType dataAPIType) {
checkArgument((keyID != null && secretKey != null) ^ oAuthToken != null,
"You must specify a (KeyID (%s) and Secret Key (%s)) or an OAuthToken (%s)!",
keyID, secretKey, oAuthToken);
checkNotNull(endpointAPIType);
checkNotNull(dataAPIType);
// Create default 'okHttpClient'
if (okHttpClient == null) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.cache(null); // Ensure response caching is disabled
if (LOGGER.isDebugEnabled()) {
clientBuilder.addInterceptor(new HttpLoggingInterceptor(LOGGER));
}
okHttpClient = clientBuilder.build();
}
this.okHttpClient = okHttpClient;
String brokerHostSubdomain;
switch (endpointAPIType) {
case LIVE:
brokerHostSubdomain = "api";
break;
case PAPER:
brokerHostSubdomain = "paper-api";
break;
default:
throw new UnsupportedOperationException();
}
if (oAuthToken == null) {
brokerClient = new AlpacaClient(okHttpClient, keyID, secretKey,
brokerHostSubdomain, VERSION_2_PATH_SEGMENT);
cryptoDataClient = new AlpacaClient(okHttpClient, keyID, secretKey, "data", VERSION_1_BETA_1_PATH_SEGMENT);
stockDataClient = new AlpacaClient(okHttpClient, keyID, secretKey, "data", VERSION_2_PATH_SEGMENT);
} else {
brokerClient = new AlpacaClient(okHttpClient, oAuthToken, brokerHostSubdomain, VERSION_2_PATH_SEGMENT);
cryptoDataClient = null;
stockDataClient = null;
}
accountEndpoint = new AccountEndpoint(brokerClient);
cryptoMarketDataEndpoint = cryptoDataClient == null ? null : new CryptoMarketDataEndpoint(cryptoDataClient);
stockMarketDataEndpoint = stockDataClient == null ? null : new StockMarketDataEndpoint(stockDataClient);
ordersEndpoint = new OrdersEndpoint(brokerClient);
positionsEndpoint = new PositionsEndpoint(brokerClient);
assetsEndpoint = new AssetsEndpoint(brokerClient);
watchlistEndpoint = new WatchlistEndpoint(brokerClient);
calendarEndpoint = new CalendarEndpoint(brokerClient);
clockEndpoint = new ClockEndpoint(brokerClient);
accountConfigurationEndpoint = new AccountConfigurationEndpoint(brokerClient);
accountActivitiesEndpoint = new AccountActivitiesEndpoint(brokerClient);
portfolioHistoryEndpoint = new PortfolioHistoryEndpoint(brokerClient);
streamingWebsocket = new StreamingWebsocket(okHttpClient, brokerHostSubdomain, keyID, secretKey, oAuthToken);
cryptoMarketDataWebsocket = cryptoDataClient == null ? null :
new CryptoMarketDataWebsocket(okHttpClient, keyID, secretKey);
stockMarketDataWebsocket = stockDataClient == null ? null :
new StockMarketDataWebsocket(okHttpClient, dataAPIType, keyID, secretKey);
}
/**
* @return the {@link AccountEndpoint}
*/
public AccountEndpoint account() {
return accountEndpoint;
}
/**
* @return the {@link CryptoMarketDataEndpoint}
*/
public CryptoMarketDataEndpoint cryptoMarketData() {
return cryptoMarketDataEndpoint;
}
/**
* @return the {@link StockMarketDataEndpoint}
*/
public StockMarketDataEndpoint stockMarketData() {
return stockMarketDataEndpoint;
}
/**
* @return the {@link OrdersEndpoint}
*/
public OrdersEndpoint orders() {
return ordersEndpoint;
}
/**
* @return the {@link PositionsEndpoint}
*/
public PositionsEndpoint positions() {
return positionsEndpoint;
}
/**
* @return the {@link AssetsEndpoint}
*/
public AssetsEndpoint assets() {
return assetsEndpoint;
}
/**
* @return the {@link WatchlistEndpoint}
*/
public WatchlistEndpoint watchlist() {
return watchlistEndpoint;
}
/**
* @return the {@link CalendarEndpoint}
*/
public CalendarEndpoint calendar() {
return calendarEndpoint;
}
/**
* @return the {@link ClockEndpoint}
*/
public ClockEndpoint clock() {
return clockEndpoint;
}
/**
* @return the {@link AccountConfigurationEndpoint}
*/
public AccountConfigurationEndpoint accountConfiguration() {
return accountConfigurationEndpoint;
}
/**
* @return the {@link AccountActivitiesEndpoint}
*/
public AccountActivitiesEndpoint accountActivities() {
return accountActivitiesEndpoint;
}
/**
* @return the {@link PortfolioHistoryEndpoint}
*/
public PortfolioHistoryEndpoint portfolioHistory() {
return portfolioHistoryEndpoint;
}
/**
* @return the {@link StreamingWebsocketInterface}
*/
public StreamingWebsocketInterface streaming() {
return streamingWebsocket;
}
/**
* @return the Crypto {@link MarketDataWebsocketInterface}
*/
public MarketDataWebsocketInterface cryptoMarketDataStreaming() {
return cryptoMarketDataWebsocket;
}
/**
* @return the Stock {@link MarketDataWebsocketInterface}
*/
public MarketDataWebsocketInterface stockMarketDataStreaming() {
return stockMarketDataWebsocket;
}
public OkHttpClient getOkHttpClient() {
return okHttpClient;
}
public AlpacaClient getBrokerClient() {
return brokerClient;
}
public AlpacaClient getCryptoDataClient() {
return cryptoDataClient;
}
public AlpacaClient getStockDataClient() {
return stockDataClient;
}
}