alpaca-trader-rs 0.6.0

Alpaca Markets trading toolkit — async REST client library and interactive TUI trading terminal
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Async HTTP client wrapping the Alpaca Markets REST API.
use std::collections::HashMap;

use anyhow::{Context, Result};
use reqwest::header::{HeaderMap, HeaderValue};

use crate::config::{AlpacaConfig, AlpacaEnv};
use crate::types::{
    AccountInfo, BarsResponse, MarketClock, MinuteBar, Order, OrderRequest, PortfolioHistory,
    Position, Snapshot, Watchlist, WatchlistSummary,
};

/// Async HTTP client for the Alpaca Markets REST API.
///
/// All methods require valid credentials in the [`AlpacaConfig`] provided at
/// construction time. Each call sets the `APCA-API-KEY-ID` and
/// `APCA-API-SECRET-KEY` request headers automatically.
pub struct AlpacaClient {
    http: reqwest::Client,
    config: AlpacaConfig,
}

impl AlpacaClient {
    /// Create a new client using the given configuration.
    pub fn new(config: AlpacaConfig) -> Self {
        Self {
            http: reqwest::Client::new(),
            config,
        }
    }

    /// Returns `true` when this client is configured for the paper trading environment.
    ///
    /// Useful for skipping API calls that are unsupported on the paper endpoint
    /// (e.g., `/v2/watchlists`).
    pub fn is_paper(&self) -> bool {
        self.config.env == AlpacaEnv::Paper
    }

    /// Returns `true` when dry-run mode is active.
    ///
    /// In dry-run mode, order-submission calls are simulated locally and never
    /// forwarded to the Alpaca API. All read-only calls still reach the
    /// configured endpoint.
    pub fn is_dry_run(&self) -> bool {
        self.config.dry_run
    }

    fn auth_headers(&self) -> Result<HeaderMap> {
        let mut headers = HeaderMap::new();
        headers.insert(
            "APCA-API-KEY-ID",
            HeaderValue::from_str(&self.config.key)
                .context("API key contains invalid header characters")?,
        );
        headers.insert(
            "APCA-API-SECRET-KEY",
            HeaderValue::from_str(&self.config.secret)
                .context("API secret contains invalid header characters")?,
        );
        Ok(headers)
    }

    fn url(&self, path: &str) -> String {
        format!("{}{}", self.config.base_url, path)
    }

    /// Build a URL against the Alpaca market-data API (`data.alpaca.markets`).
    ///
    /// In production the data API lives on a separate host from the broker API.
    /// For local tests (base URL is not `alpaca.markets`) we fall back to the
    /// configured base URL so wiremock mocks work without a second server.
    fn data_url(&self, path: &str) -> String {
        if self.config.base_url.contains("alpaca.markets") {
            format!("https://data.alpaca.markets/v2{}", path)
        } else {
            format!("{}{}", self.config.base_url, path)
        }
    }

    /// Fetch the current account snapshot (`GET /account`).
    pub async fn get_account(&self) -> Result<AccountInfo> {
        self.http
            .get(self.url("/account"))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /account request failed")?
            .json::<AccountInfo>()
            .await
            .context("GET /account parse failed")
    }

    /// Fetch all current open positions (`GET /positions`).
    pub async fn get_positions(&self) -> Result<Vec<Position>> {
        self.http
            .get(self.url("/positions"))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /positions request failed")?
            .json::<Vec<Position>>()
            .await
            .context("GET /positions parse failed")
    }

    /// Fetch orders filtered by `status` (`GET /orders?status=<status>&limit=100`).
    ///
    /// Common values for `status`: `"open"`, `"closed"`, `"all"`.
    pub async fn get_orders(&self, status: &str) -> Result<Vec<Order>> {
        self.http
            .get(self.url("/orders"))
            .query(&[("status", status), ("limit", "100")])
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /orders request failed")?
            .json::<Vec<Order>>()
            .await
            .context("GET /orders parse failed")
    }

    /// Submit a new order (`POST /orders`).
    ///
    /// Returns the created [`Order`] with its assigned `id` and initial status.
    pub async fn submit_order(&self, req: &OrderRequest) -> Result<Order> {
        self.http
            .post(self.url("/orders"))
            .headers(self.auth_headers()?)
            .json(req)
            .send()
            .await
            .context("POST /orders request failed")?
            .json::<Order>()
            .await
            .context("POST /orders parse failed")
    }

    /// Cancel an open order by its ID (`DELETE /orders/{id}`).
    pub async fn cancel_order(&self, id: &str) -> Result<()> {
        self.http
            .delete(self.url(&format!("/orders/{}", id)))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("DELETE /orders/{id} request failed")?;
        Ok(())
    }

    /// Fetch the current market clock (`GET /clock`).
    ///
    /// Returns whether the market is open and the next open/close times.
    pub async fn get_clock(&self) -> Result<MarketClock> {
        self.http
            .get(self.url("/clock"))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /clock request failed")?
            .json::<MarketClock>()
            .await
            .context("GET /clock parse failed")
    }

    /// List all watchlists for the account (`GET /watchlists`).
    ///
    /// Returns summaries (id + name only). Use [`get_watchlist`] to fetch full asset lists.
    ///
    /// [`get_watchlist`]: AlpacaClient::get_watchlist
    pub async fn list_watchlists(&self) -> Result<Vec<WatchlistSummary>> {
        self.http
            .get(self.url("/watchlists"))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /watchlists request failed")?
            .json::<Vec<WatchlistSummary>>()
            .await
            .context("GET /watchlists parse failed")
    }

    /// Fetch a watchlist including its full asset list (`GET /watchlists/{id}`).
    pub async fn get_watchlist(&self, id: &str) -> Result<Watchlist> {
        self.http
            .get(self.url(&format!("/watchlists/{}", id)))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /watchlists/{id} request failed")?
            .json::<Watchlist>()
            .await
            .context("GET /watchlists/{id} parse failed")
    }

    /// Add a symbol to a watchlist (`POST /watchlists/{id}`).
    ///
    /// Returns the updated [`Watchlist`] with the new symbol included.
    pub async fn add_to_watchlist(&self, id: &str, symbol: &str) -> Result<Watchlist> {
        let body = serde_json::json!({ "symbol": symbol });
        self.http
            .post(self.url(&format!("/watchlists/{}", id)))
            .headers(self.auth_headers()?)
            .json(&body)
            .send()
            .await
            .context("POST /watchlists/{id} request failed")?
            .json::<Watchlist>()
            .await
            .context("POST /watchlists/{id} parse failed")
    }

    /// Remove a symbol from a watchlist (`DELETE /watchlists/{id}/{symbol}`).
    ///
    /// Returns the updated [`Watchlist`] with the symbol removed.
    pub async fn remove_from_watchlist(&self, id: &str, symbol: &str) -> Result<Watchlist> {
        self.http
            .delete(self.url(&format!("/watchlists/{}/{}", id, symbol)))
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("DELETE /watchlists/{id}/{symbol} request failed")?
            .json::<Watchlist>()
            .await
            .context("DELETE /watchlists/{id}/{symbol} parse failed")
    }

    /// Fetch intraday portfolio equity history (`GET /account/portfolio/history`).
    ///
    /// Requests 1-minute bars for the current trading day. Equity values are
    /// `None` for buckets when the market was closed.
    pub async fn get_portfolio_history(&self) -> Result<PortfolioHistory> {
        self.http
            .get(self.url("/account/portfolio/history"))
            .query(&[("timeframe", "1Min"), ("period", "1D")])
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /account/portfolio/history request failed")?
            .json::<PortfolioHistory>()
            .await
            .context("GET /account/portfolio/history parse failed")
    }

    /// Fetch latest market snapshots for multiple symbols from the data API
    /// (`GET /v2/stocks/snapshots?symbols=...&feed=iex`).
    ///
    /// Returns a map of symbol → [`Snapshot`] with daily bar data (volume) and
    /// the previous day's bar (previous close for Change% computation).
    /// Returns an empty map if `symbols` is empty.
    pub async fn get_snapshots(&self, symbols: &[String]) -> Result<HashMap<String, Snapshot>> {
        if symbols.is_empty() {
            return Ok(HashMap::new());
        }
        let symbols_param = symbols.join(",");
        self.http
            .get(self.data_url("/stocks/snapshots"))
            .query(&[("symbols", symbols_param.as_str()), ("feed", "iex")])
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /stocks/snapshots request failed")?
            .json::<HashMap<String, Snapshot>>()
            .await
            .context("GET /stocks/snapshots parse failed")
    }

    /// Fetch intraday 1-minute bars for a single symbol from the data API.
    ///
    /// Returns bars for the current UTC calendar date, IEX feed, oldest first.
    /// The caller converts the raw bars to sparkline-ready `u64` cent values.
    pub async fn get_intraday_bars(&self, symbol: &str) -> Result<Vec<MinuteBar>> {
        use chrono::Utc;
        let today = Utc::now().format("%Y-%m-%d").to_string();
        Ok(self
            .http
            .get(self.data_url(&format!("/stocks/{symbol}/bars")))
            .query(&[
                ("timeframe", "1Min"),
                ("start", today.as_str()),
                ("feed", "iex"),
                ("limit", "400"),
            ])
            .headers(self.auth_headers()?)
            .send()
            .await
            .context("GET /stocks/{symbol}/bars request failed")?
            .json::<BarsResponse>()
            .await
            .context("GET /stocks/{symbol}/bars parse failed")?
            .bars)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{AlpacaConfig, AlpacaEnv};
    use serde_json::json;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn paper_config(base_url: String) -> AlpacaConfig {
        AlpacaConfig {
            base_url,
            key: "PKTEST".into(),
            secret: "secret".into(),
            env: AlpacaEnv::Paper,
            dry_run: false,
        }
    }

    fn live_config(base_url: String) -> AlpacaConfig {
        AlpacaConfig {
            base_url,
            key: "AKTEST".into(),
            secret: "secret".into(),
            env: AlpacaEnv::Live,
            dry_run: false,
        }
    }

    // ── Unit tests ────────────────────────────────────────────────────────────

    #[test]
    fn is_paper_true_for_paper_env() {
        let client = AlpacaClient::new(paper_config("http://localhost".into()));
        assert!(client.is_paper());
    }

    #[test]
    fn is_paper_false_for_live_env() {
        let client = AlpacaClient::new(live_config("http://localhost".into()));
        assert!(!client.is_paper());
    }

    #[test]
    fn is_dry_run_false_by_default() {
        let client = AlpacaClient::new(paper_config("http://localhost".into()));
        assert!(!client.is_dry_run());
    }

    #[test]
    fn is_dry_run_true_when_set() {
        let config = AlpacaConfig {
            base_url: "http://localhost".into(),
            key: "k".into(),
            secret: "s".into(),
            env: AlpacaEnv::Paper,
            dry_run: true,
        };
        let client = AlpacaClient::new(config);
        assert!(client.is_dry_run());
    }

    #[test]
    fn data_url_uses_data_alpaca_markets_for_production() {
        let config = AlpacaConfig {
            base_url: "https://paper-api.alpaca.markets".into(),
            key: "k".into(),
            secret: "s".into(),
            env: AlpacaEnv::Paper,
            dry_run: false,
        };
        let client = AlpacaClient::new(config);
        assert_eq!(
            client.data_url("/stocks/snapshots"),
            "https://data.alpaca.markets/v2/stocks/snapshots"
        );
    }

    #[test]
    fn data_url_uses_base_url_for_non_production() {
        let client = AlpacaClient::new(paper_config("http://localhost:9999".into()));
        assert_eq!(
            client.data_url("/stocks/snapshots"),
            "http://localhost:9999/stocks/snapshots"
        );
    }

    #[tokio::test]
    async fn get_snapshots_returns_empty_map_without_request_when_no_symbols() {
        // No mock server — any HTTP would panic/fail; proves no request is made.
        let client = AlpacaClient::new(paper_config("http://127.0.0.1:1".into()));
        let result = client.get_snapshots(&[]).await.unwrap();
        assert!(result.is_empty());
    }

    // ── HTTP integration tests ────────────────────────────────────────────────

    #[tokio::test]
    async fn get_account_parses_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/account"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "status": "ACTIVE", "equity": "100000", "buying_power": "200000",
                "cash": "50000", "long_market_value": "50000",
                "daytrade_count": 0, "pattern_day_trader": false, "currency": "USD"
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let acct = client.get_account().await.unwrap();
        assert_eq!(acct.status, "ACTIVE");
        assert_eq!(acct.equity, "100000");
    }

    #[tokio::test]
    async fn get_account_returns_error_on_bad_json() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/account"))
            .respond_with(ResponseTemplate::new(200).set_body_string("not json"))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let err = client.get_account().await.unwrap_err();
        assert!(err.to_string().contains("parse failed"));
    }

    #[tokio::test]
    async fn get_positions_parses_empty_list() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/positions"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let positions = client.get_positions().await.unwrap();
        assert!(positions.is_empty());
    }

    #[tokio::test]
    async fn get_orders_parses_empty_list() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/orders"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let orders = client.get_orders("open").await.unwrap();
        assert!(orders.is_empty());
    }

    #[tokio::test]
    async fn submit_order_sends_post_and_parses_response() {
        use crate::types::OrderRequest;
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/orders"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "id": "ord-1", "symbol": "AAPL", "side": "buy",
                "order_type": "market", "status": "accepted",
                "filled_qty": "0", "time_in_force": "day"
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let req = OrderRequest {
            symbol: "AAPL".into(),
            qty: Some("1".into()),
            notional: None,
            side: "buy".into(),
            order_type: "market".into(),
            time_in_force: "day".into(),
            limit_price: None,
        };
        let order = client.submit_order(&req).await.unwrap();
        assert_eq!(order.symbol, "AAPL");
        assert_eq!(order.status, "accepted");
    }

    #[tokio::test]
    async fn cancel_order_succeeds_on_204() {
        let server = MockServer::start().await;
        Mock::given(method("DELETE"))
            .and(path("/orders/order-abc"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        client.cancel_order("order-abc").await.unwrap();
    }

    #[tokio::test]
    async fn get_clock_parses_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/clock"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "is_open": true,
                "next_open": "2026-05-13T13:30:00Z",
                "next_close": "2026-05-12T20:00:00Z",
                "timestamp": "2026-05-12T15:00:00Z"
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let clock = client.get_clock().await.unwrap();
        assert!(clock.is_open);
    }

    #[tokio::test]
    async fn list_watchlists_parses_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/watchlists"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!([
                {"id": "wl-1", "name": "My List"}
            ])))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(live_config(server.uri()));
        let lists = client.list_watchlists().await.unwrap();
        assert_eq!(lists.len(), 1);
        assert_eq!(lists[0].name, "My List");
    }

    #[tokio::test]
    async fn get_watchlist_parses_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/watchlists/wl-1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "id": "wl-1", "name": "My List", "assets": []
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(live_config(server.uri()));
        let wl = client.get_watchlist("wl-1").await.unwrap();
        assert_eq!(wl.name, "My List");
        assert!(wl.assets.is_empty());
    }

    #[tokio::test]
    async fn add_to_watchlist_returns_updated_watchlist() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/watchlists/wl-1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "id": "wl-1", "name": "My List",
                "assets": [{
                    "id": "a-1", "symbol": "AAPL", "name": "Apple Inc.",
                    "exchange": "NASDAQ", "class": "us_equity",
                    "tradable": true, "shortable": true, "fractionable": true
                }]
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(live_config(server.uri()));
        let wl = client.add_to_watchlist("wl-1", "AAPL").await.unwrap();
        assert_eq!(wl.assets.len(), 1);
        assert_eq!(wl.assets[0].symbol, "AAPL");
    }

    #[tokio::test]
    async fn remove_from_watchlist_returns_updated_watchlist() {
        let server = MockServer::start().await;
        Mock::given(method("DELETE"))
            .and(path("/watchlists/wl-1/AAPL"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "id": "wl-1", "name": "My List", "assets": []
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(live_config(server.uri()));
        let wl = client.remove_from_watchlist("wl-1", "AAPL").await.unwrap();
        assert!(wl.assets.is_empty());
    }

    #[tokio::test]
    async fn get_portfolio_history_parses_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/account/portfolio/history"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "equity": [100000.0, 100100.0, null],
                "timestamp": [1000, 2000, 3000],
                "base_value": 100000.0
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let history = client.get_portfolio_history().await.unwrap();
        assert_eq!(history.equity.len(), 3);
        assert_eq!(history.equity[0], Some(100000.0));
        assert_eq!(history.equity[2], None);
    }

    #[tokio::test]
    async fn get_snapshots_with_symbols_calls_endpoint() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/stocks/snapshots"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let result = client.get_snapshots(&["AAPL".to_string()]).await.unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn get_intraday_bars_parses_response() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/stocks/AAPL/bars"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "bars": [{"c": 195.5}],
                "symbol": "AAPL",
                "next_page_token": null
            })))
            .mount(&server)
            .await;

        let client = AlpacaClient::new(paper_config(server.uri()));
        let bars = client.get_intraday_bars("AAPL").await.unwrap();
        assert_eq!(bars.len(), 1);
        assert_eq!(bars[0].c, 195.5);
    }
}