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
//! Runtime configuration loaded from environment variables.
use anyhow::{Context, Result};
#[cfg(test)]
mod tests {
use super::*;
fn paper_vars(endpoint: &str) -> Vec<(&'static str, Option<String>)> {
vec![
("PAPER_ALPACA_ENDPOINT", Some(endpoint.into())),
("PAPER_ALPACA_KEY", Some("PKTEST000".into())),
("PAPER_ALPACA_SECRET", Some("secret000".into())),
]
}
fn live_vars(endpoint: &str) -> Vec<(&'static str, Option<String>)> {
vec![
("LIVE_ALPACA_ENDPOINT", Some(endpoint.into())),
("LIVE_ALPACA_KEY", Some("AKTEST000".into())),
("LIVE_ALPACA_SECRET", Some("secret000".into())),
]
}
#[test]
fn env_label_paper() {
let cfg = AlpacaConfig {
base_url: String::new(),
key: String::new(),
secret: String::new(),
env: AlpacaEnv::Paper,
dry_run: false,
};
assert_eq!(cfg.env_label(), "PAPER");
}
#[test]
fn env_label_live() {
let cfg = AlpacaConfig {
base_url: String::new(),
key: String::new(),
secret: String::new(),
env: AlpacaEnv::Live,
dry_run: false,
};
assert_eq!(cfg.env_label(), "LIVE");
}
#[test]
fn from_env_paper_selects_paper_vars() {
temp_env::with_vars(paper_vars("https://paper-api.alpaca.markets/v2"), || {
let cfg = AlpacaConfig::from_env(AlpacaEnv::Paper).unwrap();
assert_eq!(cfg.env, AlpacaEnv::Paper);
assert_eq!(cfg.base_url, "https://paper-api.alpaca.markets/v2");
assert_eq!(cfg.key, "PKTEST000");
assert_eq!(cfg.secret, "secret000");
});
}
#[test]
fn from_env_paper_trailing_slash_stripped() {
temp_env::with_vars(paper_vars("https://paper-api.alpaca.markets/v2/"), || {
let cfg = AlpacaConfig::from_env(AlpacaEnv::Paper).unwrap();
assert_eq!(cfg.base_url, "https://paper-api.alpaca.markets/v2");
});
}
#[test]
fn from_env_live_appends_v2() {
temp_env::with_vars(live_vars("https://api.alpaca.markets"), || {
let cfg = AlpacaConfig::from_env(AlpacaEnv::Live).unwrap();
assert_eq!(cfg.env, AlpacaEnv::Live);
assert_eq!(cfg.base_url, "https://api.alpaca.markets/v2");
});
}
#[test]
fn from_env_live_no_double_slash() {
temp_env::with_vars(live_vars("https://api.alpaca.markets/"), || {
let cfg = AlpacaConfig::from_env(AlpacaEnv::Live).unwrap();
assert_eq!(cfg.base_url, "https://api.alpaca.markets/v2");
});
}
#[test]
fn from_env_missing_paper_key_errors() {
temp_env::with_vars(
[
(
"PAPER_ALPACA_ENDPOINT",
Some("https://paper-api.alpaca.markets/v2".to_string()),
),
("PAPER_ALPACA_KEY", None),
("PAPER_ALPACA_SECRET", Some("secret".to_string())),
],
|| {
assert!(AlpacaConfig::from_env(AlpacaEnv::Paper).is_err());
},
);
}
#[test]
fn from_env_missing_live_key_errors() {
temp_env::with_vars(
[
(
"LIVE_ALPACA_ENDPOINT",
Some("https://api.alpaca.markets".to_string()),
),
("LIVE_ALPACA_KEY", None),
("LIVE_ALPACA_SECRET", Some("secret".to_string())),
],
|| {
assert!(AlpacaConfig::from_env(AlpacaEnv::Live).is_err());
},
);
}
}
/// Credentials resolved from env vars, OS keychain, or an interactive prompt.
///
/// Produced by the binary-crate's `credentials::resolve()` and consumed by
/// [`AlpacaConfig::from_credentials`].
#[derive(Debug, Clone)]
pub struct ResolvedCredentials {
/// Raw endpoint URL (without `/v2` normalisation).
///
/// For live trading this is typically `https://api.alpaca.markets`.
/// For paper trading it is `https://paper-api.alpaca.markets/v2` (already
/// contains `/v2` — [`AlpacaConfig::from_credentials`] handles both forms).
pub endpoint: String,
/// Alpaca API key ID (`APCA-API-KEY-ID` header value).
pub key: String,
/// Alpaca API secret key (`APCA-API-SECRET-KEY` header value).
pub secret: String,
/// Which trading environment these credentials belong to.
pub env: AlpacaEnv,
}
/// Selects which Alpaca trading environment to connect to.
#[derive(Debug, Clone, PartialEq)]
pub enum AlpacaEnv {
/// Alpaca paper-trading environment — uses simulated funds with no real money.
Paper,
/// Alpaca live-trading environment — uses real funds; handle with care.
Live,
}
/// Runtime configuration loaded from environment variables.
///
/// Construct via [`AlpacaConfig::from_env`]; the individual fields are
/// exposed so downstream code can read the resolved values without
/// re-parsing the environment.
#[derive(Debug, Clone)]
pub struct AlpacaConfig {
/// REST base URL including `/v2`, without a trailing slash.
///
/// Example: `https://paper-api.alpaca.markets/v2`
pub base_url: String,
/// Alpaca API key ID (`APCA-API-KEY-ID` header value).
pub key: String,
/// Alpaca API secret key (`APCA-API-SECRET-KEY` header value).
pub secret: String,
/// Which environment (paper / live) this config targets.
pub env: AlpacaEnv,
/// When `true`, order submissions are simulated locally and never sent to
/// the Alpaca API. All read-only calls (account, positions, watchlist …)
/// still use live or paper data from the selected environment.
pub dry_run: bool,
}
impl AlpacaConfig {
/// Load configuration from environment variables for the specified environment.
///
/// Only the variables for the requested environment are read and validated —
/// the opposing set is ignored entirely. The environment is determined by the
/// `--paper` CLI flag: pass [`AlpacaEnv::Paper`] when `--paper` is supplied,
/// or [`AlpacaEnv::Live`] otherwise (the default).
///
/// | Env | Variables required |
/// |-----|--------------------|
/// | [`AlpacaEnv::Paper`] | `PAPER_ALPACA_ENDPOINT`, `PAPER_ALPACA_KEY`, `PAPER_ALPACA_SECRET` |
/// | [`AlpacaEnv::Live`] | `LIVE_ALPACA_ENDPOINT`, `LIVE_ALPACA_KEY`, `LIVE_ALPACA_SECRET` |
///
/// Returns an error if any required variable for the chosen environment is missing.
pub fn from_env(env: AlpacaEnv) -> Result<Self> {
match env {
AlpacaEnv::Live => {
let endpoint = std::env::var("LIVE_ALPACA_ENDPOINT")
.context("LIVE_ALPACA_ENDPOINT not set")?;
let key = std::env::var("LIVE_ALPACA_KEY").context("LIVE_ALPACA_KEY not set")?;
let secret =
std::env::var("LIVE_ALPACA_SECRET").context("LIVE_ALPACA_SECRET not set")?;
// Live endpoint does not include /v2
let base_url = format!("{}/v2", endpoint.trim_end_matches('/'));
Ok(Self {
base_url,
key,
secret,
env: AlpacaEnv::Live,
dry_run: false,
})
}
AlpacaEnv::Paper => {
let endpoint = std::env::var("PAPER_ALPACA_ENDPOINT")
.context("PAPER_ALPACA_ENDPOINT not set")?;
let key = std::env::var("PAPER_ALPACA_KEY").context("PAPER_ALPACA_KEY not set")?;
let secret =
std::env::var("PAPER_ALPACA_SECRET").context("PAPER_ALPACA_SECRET not set")?;
// Paper endpoint already includes /v2
let base_url = endpoint.trim_end_matches('/').to_string();
Ok(Self {
base_url,
key,
secret,
env: AlpacaEnv::Paper,
dry_run: false,
})
}
}
}
/// Build configuration from pre-resolved credentials.
///
/// Applies the same URL normalisation as [`AlpacaConfig::from_env`]:
/// live endpoints have `/v2` appended; paper endpoints are used as-is
/// (with any trailing slash stripped).
///
/// # Errors
///
/// Returns an error if the endpoint string is empty.
pub fn from_credentials(creds: ResolvedCredentials) -> Result<Self> {
if creds.endpoint.is_empty() {
return Err(anyhow::anyhow!("endpoint must not be empty"));
}
let base_url = match creds.env {
AlpacaEnv::Live => {
format!("{}/v2", creds.endpoint.trim_end_matches('/'))
}
AlpacaEnv::Paper => creds.endpoint.trim_end_matches('/').to_string(),
};
Ok(Self {
base_url,
key: creds.key,
secret: creds.secret,
env: creds.env,
dry_run: false,
})
}
/// Set the dry-run flag, consuming and returning `self`.
///
/// When `dry_run` is `true`, order submission calls will be intercepted
/// and simulated locally without contacting the Alpaca API.
///
/// ```
/// # use alpaca_trader_rs::config::{AlpacaConfig, AlpacaEnv};
/// let config = AlpacaConfig {
/// base_url: "http://localhost".into(),
/// key: "k".into(),
/// secret: "s".into(),
/// env: AlpacaEnv::Paper,
/// dry_run: false,
/// }.with_dry_run(true);
/// assert!(config.dry_run);
/// ```
pub fn with_dry_run(mut self, dry_run: bool) -> Self {
self.dry_run = dry_run;
self
}
/// Returns a short uppercase label for the current environment.
///
/// Returns `"PAPER"` or `"LIVE"`. Useful for status-bar display.
pub fn env_label(&self) -> &'static str {
match self.env {
AlpacaEnv::Paper => "PAPER",
AlpacaEnv::Live => "LIVE",
}
}
}