Skip to content

快速开始

前提条件

  • Rust(stable)及 Cargo

从源码构建

克隆仓库并构建:

bash
git clone https://github.com/longbridge/openpine.git
cd openpine
cargo build

运行测试

bash
# 所有测试
cargo test --all-features

# 特定 crate
cargo test -p openpine-parser
cargo test -p openpine-visitor
cargo test -p openpine-vm
cargo test -p openpine-lsp

你的第一个脚本

Cargo.toml 中添加 openpine-vm

toml
[dependencies]
openpine-vm = { path = "crates/vm" }

然后编写一个简单的程序:

rust
use openpine_vm::{Candlestick, Instance, TimeFrame};

#[tokio::main]
async fn main() -> Result<(), openpine_vm::Error> {
    let source = r#"
//@version=6
indicator("Hello OpenPine")
plot(close)
plot(ta.sma(close, 5), "SMA 5")
"#;

    let timeframe = TimeFrame::days(1);
    let symbol = "NASDAQ:AAPL";

    let bars = vec![
        Candlestick::new(1700000000000, 150.0, 155.0, 149.0, 153.0, 1000.0, 0.0, Default::default()),
        Candlestick::new(1700086400000, 153.0, 158.0, 152.0, 157.0, 1200.0, 0.0, Default::default()),
        Candlestick::new(1700172800000, 157.0, 160.0, 155.0, 159.0, 900.0, 0.0, Default::default()),
    ];

    let mut instance = Instance::builder(bars, source, timeframe, symbol)
        .build().await?;

    // 运行脚本,从 provider 读取所有 K 线
    instance.run_to_end(symbol, timeframe).await?;

    // 读取输出
    for (_id, graph) in instance.chart().series_graphs() {
        if let Some(plot) = graph.as_plot() {
            println!("Plot: {}", plot.title.as_deref().unwrap_or("unnamed"));
            for i in 0..plot.series.len() {
                println!("  bar {}: {:?}", i, plot.series[i]);
            }
        }
    }

    Ok(())
}

下一步

基于 MIT 许可证发布。