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
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Clone, Debug)]
pub struct Config {
    pub api_key: String,
    pub api_secret: String,
}

#[derive(Clone, Debug)]
pub struct Stock {
    pub symbol: String,
    pub config: Config,
}

#[derive(Deserialize, Debug)]
pub struct StockLastTradeResponse {
    status: String,
    symbol: String,
    last: LastTrade,
}

#[derive(Deserialize, Debug)]
pub struct LastTrade {
    price: f64,
    size: f64,
    exchange: u16,
    cond1: u64,
    cond2: u64,
    cond3: u64,
    cond4: u64,
    timestamp: u64,
}

#[derive(Deserialize, Debug)]
pub struct StockLastQuoteResponse {
    status: String,
    symbol: String,
    last: LastQuote,
}

#[derive(Deserialize, Debug)]
pub struct LastQuote {
    askprice: f64,
    asksize: u16,
    askexchange: u16,
    bidprice: f64,
    bidsize: u16,
    bidexchange: u16,
    timestamp: u64,
}

#[derive(Deserialize, Debug)]
pub struct Bar {
    t: u64, // timestamp
    o: f64, // open
    h: f64, // high
    l: f64, // low
    c: f64, // close
    v: u64, // volume
}

type BarResponse = HashMap<String, Vec<Bar>>;

#[allow(dead_code)]
enum Duration {
    Minute,
    Min1,
    Min5,
    Min15,
    Day,
}

/*
* TODO:
  - ADD DURATION PARAMETERS
  - ADD QUERY PARAMETERS
  - Refactor 3 API calls into abstract. Passing URL, Parameters, Deserializer
*/

impl Stock {
    pub async fn bars(self) -> Result<(), Box<dyn std::error::Error>> {
        let url = format!("{:}", "https://data.alpaca.markets/v1/bars/1D");

        let client = reqwest::Client::new();
        let res = client
            .get(&url)
            .header("APCA-API-KEY-ID", self.config.api_key)
            .header("APCA-API-SECRET-KEY", self.config.api_secret)
            .query(&[("symbols", self.symbol)])
            .send()
            .await?
            .json::<BarResponse>()
            .await?;

        for item in res.iter() {
            println!("{:?}", item);
        }

        Ok(())
    }

    pub async fn last_trade(self) -> Result<StockLastTradeResponse, Box<dyn std::error::Error>> {
        let url = format!(
            "{:}/{:}",
            "https://data.alpaca.markets/v1/last/stocks", self.symbol
        );

        let client = reqwest::Client::new();
        let res = client
            .get(&url)
            .header("APCA-API-KEY-ID", self.config.api_key)
            .header("APCA-API-SECRET-KEY", self.config.api_secret)
            .send()
            .await?
            .json::<StockLastTradeResponse>()
            .await?;

        Ok(res)
    }

    pub async fn last_quote(self) -> Result<StockLastQuoteResponse, Box<dyn std::error::Error>> {
        let url = format!(
            "{:}/{:}",
            "https://data.alpaca.markets/v1/last_quote/stocks", self.symbol
        );

        let client = reqwest::Client::new();
        let res = client
            .get(&url)
            .header("APCA-API-KEY-ID", self.config.api_key)
            .header("APCA-API-SECRET-KEY", self.config.api_secret)
            .send()
            .await?
            .json::<StockLastQuoteResponse>()
            .await?;

        Ok(res)
    }
}