#alpaca #stock

alpaca-finance

An API to interact with alpaca.markets

3 unstable releases

0.2.1 May 12, 2020
0.2.0 May 12, 2020
0.1.0 Apr 27, 2020

#362 in Finance

Download history 4/week @ 2024-07-19 5/week @ 2024-07-26 5/week @ 2024-09-20 1/week @ 2024-09-27

54 downloads per month

MIT license

43KB
484 lines

Alpaca Finance

An API for interacting with Alpaca.

Package Documentation Build Status

  • Account information
use alpaca_finance::{ Account, Alpaca };

#[tokio::main]
async fn main() {
   // Get a connection to the live API
   let alpaca = Alpaca::live("My KEY ID", "My Secret Key").await.unwrap();
   let account = Account::get(&alpaca).await.unwrap();

   println!("I have ${:.2} in my account.", account.cash)
}
  • To place an order
use alpaca_finance::{ Account, Alpaca };

#[tokio::main]
async fn main() {
   // Get a connection to the live API
   let alpaca = Alpaca::paper("My KEY ID", "My Secret Key").await.unwrap();
   let order = Order::buy("AAPL", 100, OrderType::Limit, TimeInForce::DAY)
      .limit_price(100.0)
      .place(sandbox).await.unwrap();
}
  • Listening on account or order changes
use alpaca_finance::{ Alpaca, Streamer, StreamMessage };
use futures::{ future, StreamExt };

#[tokio::main]
async fn main() {
   // Get a connection to the live API
   let alpaca = Alpaca::paper("My KEY ID", "My Secret Key").await.unwrap();

   let streamer = Streamer:new(&alpaca);
   streamer.start().await
      .for_each(|msg| {
         match msg {
            StreamMessage::Account(_) => println!("Got an account update!"),
            StreamMessage::Order(_) => println!("Got an order update!"),
            _ => println!("Got an unexpected msg")
         }
         future::ready(())
      })
      .await;
}

Usage

Add this to your Cargo.toml:

alpaca-finance = "0.2"

Dependencies

~12–26MB
~387K SLoC