#financial-data #download #http #yahoo #csv #chart

app yfinance-rust

一个用于下载Yahoo Finance V8图表数据的Rust库

2个版本

0.1.1 2024年2月14日
0.1.0 2024年2月14日

#84 in 金融

MIT 许可证

1MB
564

yfinance-rust

yfinance-rust是一个简单的命令行工具,用于从Yahoo Finance下载财务数据,类似于流行的Python包yfinance,但功能较少。

这不仅仅是重造轮子,而是一个为了熟悉Rust编程的个人项目。它为我提供了学习Rust许多方面的机会,例如

  • 移动语义
  • 生命周期
  • 函数式编程
  • 使用boxes的递归
  • 异步编程
  • 错误和选项处理
  • 宏(非常喜欢rust-analyzer的宏展开——这是一个梦想。)
  • cargo
  • 测试
  • 内联文档

除了Rust的语言特性外,Rust的社区和生态系统也非常迷人——活跃、强大且充满高质量的项目,因此我也探索了一些,如cargo.toml中列出的一样:

  • chrono:解析和处理日期时间对象
  • clap:构建命令行工具
  • hyper:高性能HTTP服务器和客户端
  • tokio:使异步编程变得简单
  • log/env_logger:出色的日志工具
  • serde:出色的序列化和反序列化工具

项目结构

src
├── http.rs: download from yahoo
├── main.rs: program entry + write to csv
├── options.rs: cmd args
└── v8chart.rs: data class and conversion to csv

命令行选项

✗ target/debug/yfinance-rust 
yfinance-rust 1.0
Hongze Xia hongzex@gmail.com>

USAGE:
    yfinance-rust <SUBCOMMAND>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

SUBCOMMANDS:
    convert     Convert yahoo finance v8 json into csv
    download    Download historical data from yahoo finance
    help        Prints this message or the help of the given subcommand(s)
✗ target/debug/yfinance-rust download -h
yfinance-rust-download 
Download historical data from yahoo finance

USAGE:
    yfinance-rust download [FLAGS] [OPTIONS] [symbols]...

ARGS:
    <symbols>...    List of symbols to download. Required

FLAGS:
        --convert             Convert JSON to CSV
    -h, --help                Prints help information
        --include-pre-post    Include pre & post market data
    -V, --version             Prints version information

OPTIONS:
        --end <end>                  An end date. Default to Now
        --interval <interval>        select a proper interval for the data 1m goes back to 4-5 days
                                     5m goes back to ~80 days others goes back to the initial
                                     trading date [default: 1d] [possible values: 1m, 5m, 1d, 5d,
                                     1wk, 1mo, 3mo]
    -o, --output-dir <output-dir>    Sets a output directory. The format of the output JSON looks
                                     like `SYMBOL_20200202_20200303.json` [default: .]
        --rate <rate>                Request rate in terms of ms [default: 100]
        --start <start>              A start date to download from. Default to the initial trading
                                     day
✗ target/debug/yfinance-rust convert -h 
yfinance-rust-convert 
Convert yahoo finance v8 json into csv

USAGE:
    yfinance-rust convert [FLAGS] <input-dir>

ARGS:
    <input-dir>    input_dir where the JSONs live

FLAGS:
    -h, --help         Prints help information
        --recursive    whether to walk the input_dir recursively
    -V, --version      Prints version information

示例

RUST_LOG=info target/debug/yfinance-rust download --convert -o target/output AAPL MSFT NVDA TSLA
[2021-01-26T05:05:24Z INFO  yfinance_rust::options] Opts { subcmd: Download(DownloadOpts { symbols: ["AAPL", "MSFT", "NVDA", "TSLA"], start: None, end: None, include_pre_post: false, output_dir: "target/output", interval: "1d", rate: MyDuration { duration: 100ms }, convert: true }) }
[2021-01-26T05:05:27Z INFO  yfinance_rust::http] downloaded "target/output/NVDA_init_20210126.json"
[2021-01-26T05:05:28Z INFO  yfinance_rust::http] downloaded "target/output/TSLA_init_20210126.json"
[2021-01-26T05:05:29Z INFO  yfinance_rust::http] downloaded "target/output/MSFT_init_20210126.json"
[2021-01-26T05:05:29Z INFO  yfinance_rust::http] downloaded "target/output/AAPL_init_20210126.json"
[2021-01-26T05:05:29Z INFO  yfinance_rust::http] have successfully download 4 of 4
[2021-01-26T05:05:30Z INFO  yfinance_rust] successfully converted to "target/output/AAPL_init_20210126.csv"
[2021-01-26T05:05:30Z INFO  yfinance_rust] successfully converted to "target/output/MSFT_init_20210126.csv"
[2021-01-26T05:05:31Z INFO  yfinance_rust] successfully converted to "target/output/NVDA_init_20210126.csv"
[2021-01-26T05:05:31Z INFO  yfinance_rust] successfully converted to "target/output/TSLA_init_20210126.csv"

依赖关系

~14–28MB
~414K SLoC