#经济学 #股票 #API #API绑定 #延迟 #eodhd

eodhd_rs

EODHD API 的社区 Rust 包装器

3 个不稳定版本

0.2.0 2022 年 9 月 17 日
0.1.1 2022 年 9 月 14 日
0.1.0 2022 年 9 月 14 日

#7 in #经济学

MIT 许可证

40KB
876

eodhd-rs 💹

EODHD API 的社区 Rust 包装器!你正在用 AI 打造下一个沃伦·巴菲特,或者正在为中央银行提供下一步的建议?那么如果你喜欢 Rust,那么你就有福了,因为必要的数据可以通过 eodhd 获取,而这个仓库有一些方便的包装结构/函数。

功能

目前我们支持以下 API 操作

  • 期末数据
  • 日内历史数据
  • 实时报价/交易/外汇/加密货币
  • 实时延迟
  • 新闻情绪
  • 经济事件

所有功能都提供异步操作,因此您需要安装 tokio 运行时 或利用 std:future 等。

用法

通过 cargo add eodhd_rs 安装或将其添加到您的 Cargo.toml

eodhd_rs = "0.2.0"

出于认证目的,将环境变量 EODHD_TOKEN 设置为您的令牌。此外,以下依赖项将使您的生活更加轻松

env_logger = "0.9"
chrono = "0.4.22"
tokio = { version = "1", features = ["full"] }

示例

期末,HistoricIntraday

use eodhd_rs::datetime::{EODHDDate, EODHDInterval};
use eodhd_rs::end_of_period::{EODHDEndOfPeriodFilter, EODHDPeriod};
use eodhd_rs::historic_intraday::HistoricIntradayOptions;


#[tokio::main]
async fn main() {
    let options = HistoricIntradayOptions {
        from: None,
        to: None,
        interval: EODHDInterval::Minute
    };

    match eodhd_rs::historic_intraday::get_historic_intraday("AAPL", options).await {
        Ok(o) => {
            // o is Vec<EODHDHistoricIntraday>
            println!("{:#?}", o);
        },
        Err(e) => {
            // e is Box<dyn Error> (mostly serde, reqwest errors)
            println!("{:#?}", e);
        }
    }

    // EODHDDate (year, month, day) is utility type for easy construction of chrono's NaiveDates
    let from = chrono::naive::NaiveDate::from(EODHDDate(2022, 9, 12));

    let filter = EODHDEndOfPeriodFilter {
        from: Some(from),
        to: None, // defaults of eodhd are used instead
        period: Some(EODHDPeriod::Daily)
    };
    match eodhd_rs::end_of_period::get_end_of_period("AAPL", Some(filter)).await {
        Ok(o) => {
            println!("{:#?}", o);
        },
        Err(e) => {
            println!("{:#?}", e);
        }
    }
}

实时


use futures::{Stream, StreamExt};
use eodhd_rs::realtime::{socket::{
    EODHDSocketKind, 
    subscribe_rt, 
    unsubscribe_rt,
    create_socket_channel
}, us::EODHDUSQuote, forex::{EODHDForexRT, EODHDCryptoRT}};

#[tokio::main]
async fn main() {
    // depending on the log level
    // you can get insight what is happening
    // behind the scenes (all messages received
    // instead of only the ticks)
    env_logger::init();

    let mut channel = create_socket_channel::<EODHDCryptoRT>(
        2, // size of the buffer for received ticks
        EODHDSocketKind::Crypto // must fit the generic parameter
    ).await.expect("Failed to create channel");
    subscribe_rt("BTC-USD", &mut channel).await.expect("Failed to subscribe to ticker");
    let mut counter = 0;
    while let Some(tick) = channel.tick_channel.recv().await {
        println!("Got a forex tick {:#?}", tick);
        if counter > 10 {
            break;
        }
        counter += 1;
    }
    unsubscribe_rt("EURUSD", &mut channel);

    let mut channel = create_socket_channel::<EODHDUSQuote>(2, EODHDSocketKind::Quote).await.expect("Failed to create channel");
    subscribe_rt("AAPL", &mut channel).await.expect("Failed to subscribe to ticker");
    let mut counter = 0;
    while let Some(tick) = channel.tick_channel.recv().await {
        println!("Got a quote {:#?}", tick);
        if counter > 10 {
            break;
        }
        counter += 1;
    }

    unsubscribe_rt("AAPL", &mut channel);

}

作者: Niklas Jona Lohmann

依赖项

~17–33MB
~584K SLoC