alpaca-sdk 0.1.0

Unofficial Rust SDK for the Alpaca trading, market data, and broker APIs
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
//! [Instant funding](https://docs.alpaca.markets/us/reference/get-v1-instant-funding-list):
//! trading against money that has not landed yet, and settling the debt.
//!
//! A correspondent fronts an account cash before the deposit clears; the
//! account trades on it immediately, and the correspondent owes Alpaca until the
//! [deadline](InstantFunding::deadline). Interest accrues past it.
//!
//! Spec-derived, and unverified against a live response.
//! Settlements are shared with [JIT](crate::broker::jit); see
//! [`crate::broker::settlements`].

use chrono::{DateTime, NaiveDate, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::broker::settlements::TransmitterInfo;
use crate::types::wire::wire_enum;

wire_enum! {
    /// Where an instant funding request stands.
    pub enum InstantFundingStatus {
        /// Submitted.
        Pending => "PENDING",
        /// Withdrawn before execution.
        Canceled => "CANCELED",
        /// The cash has been advanced.
        Executed => "EXECUTED",
        /// It could not be advanced.
        Failed => "FAILED",
        /// Advanced and settled.
        Completed => "COMPLETED",
    }
}

wire_enum! {
    /// Who charges a fee on an instant funding request.
    pub enum InstantFundingFeeType {
        /// The correspondent.
        Partner => "partner",
        /// Alpaca.
        Alpaca => "alpaca",
    }
}

wire_enum! {
    /// What an instant funding list is sorted by.
    pub enum InstantFundingSortBy {
        /// When the request was made.
        CreatedAt => "created_at",
        /// How much was advanced.
        Amount => "amount",
        /// When it must be settled.
        Deadline => "deadline",
    }
}

wire_enum! {
    /// Which way a sort runs.
    ///
    /// Upper-case here, unlike the lower-case `asc`/`desc` every other route in
    /// this crate takes — which is why this is its own enum rather than
    /// [`Sort`](crate::types::Sort).
    pub enum SortOrder {
        /// Ascending.
        Asc => "ASC",
        /// Descending.
        Desc => "DESC",
    }
}

/// A fee charged on an instant funding request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstantFundingFee {
    /// Alpaca's identifier for the fee.
    pub id: Uuid,
    /// Who charges it.
    #[serde(rename = "type")]
    pub fee_type: InstantFundingFeeType,
    /// How much.
    #[serde(with = "crate::types::decimal")]
    pub amount: Decimal,
}

/// A day's interest on an unsettled advance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstantFundingInterest {
    /// Alpaca's identifier for the charge.
    pub id: Uuid,
    /// The day it accrued for.
    pub date: NaiveDate,
    /// How much.
    #[serde(with = "crate::types::decimal")]
    pub amount: Decimal,
    /// Where the charge stands.
    pub status: InstantFundingStatus,
    /// When it was raised.
    pub created_at: DateTime<Utc>,
    /// When it was reconciled.
    #[serde(default)]
    pub reconciled_at: Option<DateTime<Utc>>,
}

/// An advance of cash against a deposit that has not cleared.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstantFunding {
    /// Alpaca's identifier for the advance.
    pub id: Uuid,
    /// The account credited.
    pub account_no: String,
    /// The account the money is owed from.
    pub source_account_no: String,
    /// How much was advanced.
    #[serde(with = "crate::types::decimal")]
    pub amount: Decimal,
    /// How much is still owed.
    #[serde(with = "crate::types::decimal")]
    pub remaining_payable: Decimal,
    /// Interest accrued so far.
    #[serde(with = "crate::types::decimal")]
    pub total_interest: Decimal,
    /// Where the advance stands.
    pub status: InstantFundingStatus,
    /// The business day it was booked on.
    pub system_date: NaiveDate,
    /// The day it must be settled by.
    pub deadline: NaiveDate,
    /// Fees charged.
    #[serde(
        default,
        deserialize_with = "crate::types::serde_util::null_as_default"
    )]
    pub fees: Vec<InstantFundingFee>,
    /// Interest charges raised.
    #[serde(
        default,
        deserialize_with = "crate::types::serde_util::null_as_default"
    )]
    pub interests: Vec<InstantFundingInterest>,
    /// When the advance was requested.
    pub created_at: DateTime<Utc>,
}

/// How much instant funding a correspondent may have outstanding.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstantFundingLimits {
    /// The ceiling.
    #[serde(default, with = "crate::types::option_decimal")]
    pub amount_limit: Option<Decimal>,
    /// How much of it is committed.
    #[serde(default, with = "crate::types::option_decimal")]
    pub amount_in_use: Option<Decimal>,
    /// How much is left.
    #[serde(default, with = "crate::types::option_decimal")]
    pub amount_available: Option<Decimal>,
}

/// One account's share of the correspondent's limit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AccountInstantFundingLimits {
    /// The account.
    pub account_no: String,
    /// Its ceiling.
    #[serde(with = "crate::types::decimal")]
    pub amount_limit: Decimal,
    /// How much of it is committed.
    #[serde(with = "crate::types::decimal")]
    pub amount_in_use: Decimal,
    /// How much is left.
    #[serde(with = "crate::types::decimal")]
    pub amount_available: Decimal,
}

/// A day's instant funding position for one account.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InstantFundingReport {
    /// The account.
    pub account_no: String,
    /// The business day.
    pub system_date: NaiveDate,
    /// The settlement deadline.
    pub deadline: NaiveDate,
    /// Everything owed on that day.
    #[serde(with = "crate::types::decimal")]
    pub total_amount_owed: Decimal,
    /// Interest charged for missing the deadline.
    #[serde(with = "crate::types::decimal")]
    pub total_interest_penalty: Decimal,
    /// The advances behind the total.
    #[serde(
        default,
        deserialize_with = "crate::types::serde_util::null_as_default"
    )]
    pub instant_funding_transfers: Vec<InstantFunding>,
}

/// Filters for listing instant funding requests.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GetInstantFundingRequest {
    /// Only requests in this state.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<InstantFundingStatus>,
    /// Only requests booked on this business day.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub system_date: Option<NaiveDate>,
    /// Only requests due on this day.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deadline: Option<NaiveDate>,
    /// Only requests made before this time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub before: Option<DateTime<Utc>>,
    /// What to sort by.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sort_by: Option<InstantFundingSortBy>,
    /// Which way to sort.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sort_order: Option<SortOrder>,
    /// How many to return.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// How many to skip. This family pages by offset.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub offset: Option<u32>,
}

impl GetInstantFundingRequest {
    /// A request with no filters.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Only requests in this state.
    #[must_use]
    pub fn status(mut self, status: InstantFundingStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Sorts the results.
    #[must_use]
    pub fn sort(mut self, sort_by: InstantFundingSortBy, order: SortOrder) -> Self {
        self.sort_by = Some(sort_by);
        self.sort_order = Some(order);
        self
    }
}

/// A request to advance cash against an uncleared deposit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CreateInstantFundingRequest {
    /// The account to credit.
    pub account_no: String,
    /// The account the money is owed from.
    pub source_account_no: String,
    /// How much to advance.
    #[serde(with = "crate::types::decimal")]
    pub amount: Decimal,
}

impl CreateInstantFundingRequest {
    /// Advances `amount` to `account_no` from `source_account_no`.
    pub fn new(
        account_no: impl Into<String>,
        source_account_no: impl Into<String>,
        amount: Decimal,
    ) -> Self {
        Self {
            account_no: account_no.into(),
            source_account_no: source_account_no.into(),
            amount,
        }
    }

    /// The one check a request cannot pass without contradicting itself.
    ///
    /// This guards a money-movement route, so it catches a sign error before it
    /// becomes an advance.
    ///
    /// # Errors
    /// Returns [`Error::InvalidRequest`](crate::Error::InvalidRequest) if
    /// `amount` is not positive.
    pub fn validate(&self) -> crate::Result<()> {
        if self.amount <= Decimal::ZERO {
            return Err(crate::Error::InvalidRequest(
                "amount must be greater than zero".to_owned(),
            ));
        }
        Ok(())
    }
}

/// One advance to settle, and who sent the money.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct SettlementTransfer {
    /// The advance being settled.
    pub instant_transfer_id: Uuid,
    /// Who sent the money, for travel-rule reporting.
    pub transmitter_info: TransmitterInfo,
}

impl SettlementTransfer {
    /// Settles the advance `instant_transfer_id`, sent by `transmitter_info`.
    #[must_use]
    pub fn new(instant_transfer_id: Uuid, transmitter_info: TransmitterInfo) -> Self {
        Self {
            instant_transfer_id,
            transmitter_info,
        }
    }
}

/// A request to settle one or more advances.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CreateInstantFundingSettlementRequest {
    /// The advances to settle.
    pub transfers: Vec<SettlementTransfer>,
    /// Free-form notes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub additional_info: Option<String>,
}

impl CreateInstantFundingSettlementRequest {
    /// Settles `transfers`.
    #[must_use]
    pub fn new(transfers: Vec<SettlementTransfer>) -> Self {
        Self {
            transfers,
            additional_info: None,
        }
    }

    /// Rejects a settlement that would settle nothing.
    ///
    /// # Errors
    /// Returns [`Error::InvalidRequest`](crate::Error::InvalidRequest) if no
    /// transfers are named.
    pub fn validate(&self) -> crate::Result<()> {
        if self.transfers.is_empty() {
            return Err(crate::Error::InvalidRequest(
                "a settlement must name at least one transfer".to_owned(),
            ));
        }
        Ok(())
    }
}

/// Filters for the instant funding report.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GetInstantFundingReportRequest {
    /// Which report to run.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub report_type: Option<String>,
    /// The business day to report on.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub system_date: Option<NaiveDate>,
}

/// A request for several accounts' instant funding limits.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct GetAccountLimitsRequest {
    /// The accounts to ask about, sent as one comma-separated parameter.
    ///
    /// The spec draws this as an array, which reads as a repeated parameter;
    /// the reference says "comma-separated account numbers" in as many words,
    /// and it is the reference that describes the live route. It also has to be
    /// joined to be sent at all — a bare `Vec` in a query struct fails the
    /// request before it leaves the process.
    #[serde(serialize_with = "crate::types::serde_util::comma_separated_required")]
    pub account_numbers: Vec<String>,
}

impl GetAccountLimitsRequest {
    /// Limits for `account_numbers`.
    #[must_use]
    pub fn new(account_numbers: Vec<String>) -> Self {
        Self { account_numbers }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_zero_advance_is_refused_before_it_is_sent() {
        let request = CreateInstantFundingRequest::new("acct", "source", Decimal::ZERO);
        assert!(request.validate().is_err());

        let request = CreateInstantFundingRequest::new("acct", "source", Decimal::ONE);
        assert!(request.validate().is_ok());
    }

    #[test]
    fn a_settlement_must_settle_something() {
        assert!(
            CreateInstantFundingSettlementRequest::new(Vec::new())
                .validate()
                .is_err()
        );
    }

    #[test]
    fn the_sort_order_is_upper_case_on_this_family_alone() {
        // Every other route in this crate takes `asc`/`desc`.
        assert_eq!(SortOrder::Asc.as_str(), "ASC");
        assert_eq!(crate::types::Sort::Asc.as_str(), "asc");
    }

    #[test]
    fn an_advance_with_no_fees_or_interest_yet_decodes() {
        let funding: InstantFunding = serde_json::from_value(serde_json::json!({
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "account_no": "123",
            "source_account_no": "456",
            "amount": "1000.00",
            "remaining_payable": "1000.00",
            "total_interest": "0",
            "status": "EXECUTED",
            "system_date": "2026-01-02",
            "deadline": "2026-01-05",
            "created_at": "2026-01-02T15:04:05Z",
        }))
        .unwrap();

        assert!(funding.fees.is_empty());
        assert!(funding.interests.is_empty());
    }
}