-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathAlpacaAPI.java
368 lines (331 loc) · 15.8 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package net.jacobpeterson.alpaca;
import net.jacobpeterson.alpaca.model.util.apitype.BrokerAPIEndpointType;
import net.jacobpeterson.alpaca.model.util.apitype.MarketDataWebsocketSourceType;
import net.jacobpeterson.alpaca.model.util.apitype.TraderAPIEndpointType;
import net.jacobpeterson.alpaca.rest.broker.AlpacaBrokerAPI;
import net.jacobpeterson.alpaca.rest.marketdata.AlpacaMarketDataAPI;
import net.jacobpeterson.alpaca.rest.trader.AlpacaTraderAPI;
import net.jacobpeterson.alpaca.websocket.marketdata.streams.crypto.CryptoMarketDataWebsocket;
import net.jacobpeterson.alpaca.websocket.marketdata.streams.crypto.CryptoMarketDataWebsocketInterface;
import net.jacobpeterson.alpaca.websocket.marketdata.streams.news.NewsMarketDataWebsocket;
import net.jacobpeterson.alpaca.websocket.marketdata.streams.news.NewsMarketDataWebsocketInterface;
import net.jacobpeterson.alpaca.websocket.marketdata.streams.stock.StockMarketDataWebsocket;
import net.jacobpeterson.alpaca.websocket.marketdata.streams.stock.StockMarketDataWebsocketInterface;
import net.jacobpeterson.alpaca.websocket.updates.UpdatesWebsocket;
import net.jacobpeterson.alpaca.websocket.updates.UpdatesWebsocketInterface;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static net.jacobpeterson.alpaca.model.util.apitype.BrokerAPIEndpointType.SANDBOX;
import static net.jacobpeterson.alpaca.model.util.apitype.MarketDataWebsocketSourceType.IEX;
import static net.jacobpeterson.alpaca.model.util.apitype.TraderAPIEndpointType.PAPER;
import static okhttp3.logging.HttpLoggingInterceptor.Level.BODY;
/**
* {@link 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
* {@link OkHttpClient} is given in the constructor. Additionally, all API endpoint instances are instantiated lazily.
* This class is thread-safe.
*
* @see <a href="https://docs.alpaca.markets">Alpaca Docs</a>
*/
public class AlpacaAPI {
private static final Logger LOGGER = LoggerFactory.getLogger(AlpacaAPI.class);
private final String traderKeyID;
private final String traderSecretKey;
private final String traderOAuthToken;
private final TraderAPIEndpointType traderAPIEndpointType;
private final MarketDataWebsocketSourceType marketDataWebsocketSourceType;
private final String brokerAPIKey;
private final String brokerAPISecret;
private final BrokerAPIEndpointType brokerAPIEndpointType;
private final OkHttpClient okHttpClient;
private AlpacaTraderAPI trader;
private AlpacaMarketDataAPI marketData;
private AlpacaBrokerAPI broker;
private UpdatesWebsocket updatesWebsocket;
private StockMarketDataWebsocket stockMarketDataWebsocket;
private CryptoMarketDataWebsocket cryptoMarketDataWebsocket;
private NewsMarketDataWebsocket newsMarketDataWebsocket;
/**
* Instantiates a new {@link AlpacaAPI}. Use this constructor if you are using the Trading or Market Data APIs for a
* single Alpaca account.
*
* @param traderKeyID the Trader key ID
* @param traderSecretKey the Trader secret key
* @param traderAPIEndpointType the {@link TraderAPIEndpointType}
* @param marketDataWebsocketSourceType the {@link MarketDataWebsocketSourceType}
*/
public AlpacaAPI(String traderKeyID, String traderSecretKey, TraderAPIEndpointType traderAPIEndpointType,
MarketDataWebsocketSourceType marketDataWebsocketSourceType) {
this(traderKeyID, traderSecretKey, null, traderAPIEndpointType, marketDataWebsocketSourceType, null, null, null,
null);
}
/**
* Instantiates a new {@link AlpacaAPI}. Use this constructor if you are using the Trading or Market Data APIs for a
* single Alpaca account and a custom {@link OkHttpClient} instance.
*
* @param traderKeyID the Trader key ID
* @param traderSecretKey the Trader secret key
* @param traderAPIEndpointType the {@link TraderAPIEndpointType}
* @param marketDataWebsocketSourceType the {@link MarketDataWebsocketSourceType}
* @param okHttpClient an existing {@link OkHttpClient} or <code>null</code> to create a new
* default instance
*/
public AlpacaAPI(String traderKeyID, String traderSecretKey, TraderAPIEndpointType traderAPIEndpointType,
MarketDataWebsocketSourceType marketDataWebsocketSourceType, OkHttpClient okHttpClient) {
this(traderKeyID, traderSecretKey, null, traderAPIEndpointType, marketDataWebsocketSourceType, null, null, null,
okHttpClient);
}
/**
* Instantiates a new {@link AlpacaAPI}. Use this constructor if you are using the Trading API with OAuth to act on
* behalf of an Alpaca account.
*
* @param traderOAuthToken the Trader OAuth token
* @param traderAPIEndpointType the {@link TraderAPIEndpointType}
*/
public AlpacaAPI(String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType) {
this(null, null, traderOAuthToken, traderAPIEndpointType, null, null, null, null, null);
}
/**
* Instantiates a new {@link AlpacaAPI}. Use this constructor if you are using the Trading API with OAuth to act on
* behalf of an Alpaca account and a custom {@link OkHttpClient} instance.
*
* @param traderOAuthToken the Trader OAuth token
* @param traderAPIEndpointType the {@link TraderAPIEndpointType}
* @param okHttpClient an existing {@link OkHttpClient} or <code>null</code> to create a new default
* instance
*/
public AlpacaAPI(String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType, OkHttpClient okHttpClient) {
this(null, null, traderOAuthToken, traderAPIEndpointType, null, null, null, null, okHttpClient);
}
/**
* Instantiates a new {@link AlpacaAPI}. Use this constructor if you are using the Broker API.
*
* @param brokerAPIKey the Broker API key
* @param brokerAPISecret the Broker API secret
* @param brokerAPIEndpointType the {@link BrokerAPIEndpointType}
*/
public AlpacaAPI(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType) {
this(null, null, null, null, null, brokerAPIKey, brokerAPISecret, brokerAPIEndpointType, null);
}
/**
* Instantiates a new {@link AlpacaAPI}. Use this constructor if you are using the Broker API and a custom
* {@link OkHttpClient} instance.
*
* @param brokerAPIKey the Broker API key
* @param brokerAPISecret the Broker API secret
* @param brokerAPIEndpointType the {@link BrokerAPIEndpointType}
* @param okHttpClient an existing {@link OkHttpClient} or <code>null</code> to create a new default
* instance
*/
public AlpacaAPI(String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType,
OkHttpClient okHttpClient) {
this(null, null, null, null, null, brokerAPIKey, brokerAPISecret, brokerAPIEndpointType, okHttpClient);
}
/**
* Instantiates a new {@link AlpacaAPI}.
*
* @param traderKeyID the Trader key ID
* @param traderSecretKey the Trader secret key
* @param traderOAuthToken the Trader OAuth token
* @param traderAPIEndpointType the {@link TraderAPIEndpointType}
* @param marketDataWebsocketSourceType the {@link MarketDataWebsocketSourceType}
* @param brokerAPIKey the Broker API key
* @param brokerAPISecret the Broker API secret
* @param brokerAPIEndpointType the {@link BrokerAPIEndpointType}
* @param okHttpClient an existing {@link OkHttpClient} or <code>null</code> to create a new
* default instance
*/
public AlpacaAPI(String traderKeyID, String traderSecretKey,
String traderOAuthToken, TraderAPIEndpointType traderAPIEndpointType,
MarketDataWebsocketSourceType marketDataWebsocketSourceType,
String brokerAPIKey, String brokerAPISecret, BrokerAPIEndpointType brokerAPIEndpointType,
OkHttpClient okHttpClient) {
this.traderKeyID = traderKeyID;
this.traderSecretKey = traderSecretKey;
this.traderOAuthToken = traderOAuthToken;
this.traderAPIEndpointType = traderAPIEndpointType != null ?
traderAPIEndpointType : PAPER;
this.marketDataWebsocketSourceType = marketDataWebsocketSourceType != null ?
marketDataWebsocketSourceType : IEX;
this.brokerAPIKey = brokerAPIKey;
this.brokerAPISecret = brokerAPISecret;
this.brokerAPIEndpointType = brokerAPIEndpointType != null ?
brokerAPIEndpointType : SANDBOX;
// Create default OkHttpClient instance
if (okHttpClient == null) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
if (LOGGER.isDebugEnabled()) {
final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(LOGGER::debug);
loggingInterceptor.setLevel(BODY);
clientBuilder.addInterceptor(loggingInterceptor);
}
okHttpClient = clientBuilder.build();
}
this.okHttpClient = okHttpClient;
}
/**
* Closes the {@link OkHttpClient}.
*/
public void closeOkHttpClient() {
okHttpClient.dispatcher().executorService().shutdown();
okHttpClient.connectionPool().evictAll();
}
/**
* Gets the {@link OkHttpClient}.
*
* @return the {@link OkHttpClient}
*/
public OkHttpClient getOkHttpClient() {
return okHttpClient;
}
/**
* Gets the {@link AlpacaTraderAPI}. Lazily instantiated.
*
* @return the {@link AlpacaTraderAPI}
*/
public synchronized AlpacaTraderAPI trader() {
if (trader == null) {
trader = new AlpacaTraderAPI(traderKeyID, traderSecretKey, traderOAuthToken, traderAPIEndpointType,
okHttpClient);
}
return trader;
}
/**
* Gets the {@link AlpacaMarketDataAPI}. Lazily instantiated.
*
* @return the {@link AlpacaMarketDataAPI}
*/
public synchronized AlpacaMarketDataAPI marketData() {
if (marketData == null) {
marketData = new AlpacaMarketDataAPI(traderKeyID, traderSecretKey, brokerAPIKey, brokerAPISecret,
okHttpClient);
}
return marketData;
}
/**
* Gets the {@link AlpacaBrokerAPI}. Lazily instantiated.
*
* @return the {@link AlpacaBrokerAPI}
*/
public synchronized AlpacaBrokerAPI broker() {
if (broker == null) {
broker = new AlpacaBrokerAPI(brokerAPIKey, brokerAPISecret, brokerAPIEndpointType, okHttpClient);
}
return broker;
}
/**
* Gets the {@link UpdatesWebsocketInterface}. Lazily instantiated.
*
* @return the {@link UpdatesWebsocketInterface}
*/
public synchronized UpdatesWebsocketInterface updatesStream() {
if (updatesWebsocket == null) {
updatesWebsocket = new UpdatesWebsocket(okHttpClient, traderAPIEndpointType,
traderKeyID, traderSecretKey, traderOAuthToken);
}
return updatesWebsocket;
}
/**
* Gets the {@link StockMarketDataWebsocketInterface}. Lazily instantiated.
*
* @return the {@link StockMarketDataWebsocketInterface}
*/
public synchronized StockMarketDataWebsocketInterface stockMarketDataStream() {
if (stockMarketDataWebsocket == null) {
stockMarketDataWebsocket = new StockMarketDataWebsocket(okHttpClient,
traderKeyID, traderSecretKey, brokerAPIKey, brokerAPISecret, marketDataWebsocketSourceType);
}
return stockMarketDataWebsocket;
}
/**
* Gets the {@link CryptoMarketDataWebsocketInterface}. Lazily instantiated.
*
* @return the {@link CryptoMarketDataWebsocketInterface}
*/
public synchronized CryptoMarketDataWebsocketInterface cryptoMarketDataStream() {
if (cryptoMarketDataWebsocket == null) {
cryptoMarketDataWebsocket = new CryptoMarketDataWebsocket(okHttpClient,
traderKeyID, traderSecretKey, brokerAPIKey, brokerAPISecret);
}
return cryptoMarketDataWebsocket;
}
/**
* Gets the {@link NewsMarketDataWebsocketInterface}. Lazily instantiated.
*
* @return the {@link NewsMarketDataWebsocketInterface}
*/
public synchronized NewsMarketDataWebsocketInterface newsMarketDataStream() {
if (newsMarketDataWebsocket == null) {
newsMarketDataWebsocket = new NewsMarketDataWebsocket(okHttpClient,
traderKeyID, traderSecretKey, brokerAPIKey, brokerAPISecret);
}
return newsMarketDataWebsocket;
}
/**
* Creates a {@link Builder} for {@link AlpacaAPI}.
*
* @return the {@link Builder}
*/
public static Builder builder() {
return new Builder();
}
/**
* A builder for {@link AlpacaAPI}
*/
public static final class Builder {
private String traderKeyID;
private String traderSecretKey;
private String traderOAuthToken;
private TraderAPIEndpointType traderAPIEndpointType;
private MarketDataWebsocketSourceType marketDataWebsocketSourceType;
private String brokerAPIKey;
private String brokerAPISecret;
private BrokerAPIEndpointType brokerAPIEndpointType;
private OkHttpClient okHttpClient;
private Builder() {}
public Builder withTraderKeyID(String traderKeyID) {
this.traderKeyID = traderKeyID;
return this;
}
public Builder withTraderSecretKey(String traderSecretKey) {
this.traderSecretKey = traderSecretKey;
return this;
}
public Builder withTraderOAuthToken(String traderOAuthToken) {
this.traderOAuthToken = traderOAuthToken;
return this;
}
public Builder withTraderAPIEndpointType(TraderAPIEndpointType traderAPIEndpointType) {
this.traderAPIEndpointType = traderAPIEndpointType;
return this;
}
public Builder withMarketDataWebsocketSourceType(MarketDataWebsocketSourceType marketDataWebsocketSourceType) {
this.marketDataWebsocketSourceType = marketDataWebsocketSourceType;
return this;
}
public Builder withBrokerAPIKey(String brokerAPIKey) {
this.brokerAPIKey = brokerAPIKey;
return this;
}
public Builder withBrokerAPISecret(String brokerAPISecret) {
this.brokerAPISecret = brokerAPISecret;
return this;
}
public Builder withBrokerAPIEndpointType(BrokerAPIEndpointType brokerAPIEndpointType) {
this.brokerAPIEndpointType = brokerAPIEndpointType;
return this;
}
public Builder withOkHttpClient(OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
return this;
}
public AlpacaAPI build() {
return new AlpacaAPI(traderKeyID, traderSecretKey, traderOAuthToken, traderAPIEndpointType,
marketDataWebsocketSourceType, brokerAPIKey, brokerAPISecret, brokerAPIEndpointType, okHttpClient);
}
}
}