当前位置:   article > 正文

Rust : 如何通过Web API调用JoinQuant 的量化数据?_rust量化

rust量化

前面,我试了Julia调用 JoinQuant的web api 的数据。兴奋之余,也试了一下Rust。
Rust的 reqwest库不太熟悉,用了更长的一点时间。但总体上用法差不多。

一、相关的库

[dependencies]
reqwest = "0.9.10"
serde_json = "1.0"
  • 1
  • 2
  • 3

关于reqwest库,除了github上的文档,建议参考,可以了解更清楚明了。
http://siciarz.net/24-days-rust-reqwest/

二、相关代码:

use std::time::Duration;
use std::{thread, time};
extern crate reqwest;
extern crate serde_json;
use std::collections::HashMap;
use std::io::Read;
fn get_token()->String{
    let mut data = HashMap::new();
    data.insert("method", "get_token");
    data.insert("mob", "13761618222");
    data.insert("pwd", "618222");
    let mut _res= reqwest::Client::new()
        .post("https://dataapi.joinquant.com/apis")
        .json(&data)
        .send()
        .expect("Failed to send request");
    let mut buf = String::new();
    _res.read_to_string(&mut buf).expect("Failed to read response");
    buf
}
fn get_security_info(token:&str,code:&str,date:&str) -> String{
    //"method" => "get_security_info","token" => token,"code" => "502050.XSHG","date" =>"2019-01-15"
    let mut data = HashMap::new();
    data.insert("method","get_security_info");
    data.insert("token",token);
    data.insert("code",code);
    data.insert("date",date);
    let mut _res= reqwest::Client::new()
        .post("https://dataapi.joinquant.com/apis")
        .json(&data)
        .send()
        .expect("Failed to send request");
    let mut buf = String::new();
    _res.read_to_string(&mut buf).expect("Failed to read response");
    buf
}

fn get_bars(token:&str,code:&str,count:&str,unit_type:&str,end_date:&str,fq_ref_date:&str) -> String{
    //"method" => "get_security_info","token" => token,"code" => "502050.XSHG","date" =>"2019-01-15"
    let mut data = HashMap::new();
    data.insert("method","get_bars");
    data.insert("count",count);
    data.insert("unit",unit_type);
    data.insert("token",token);
    data.insert("code",code);
    data.insert("enddate",end_date);
    data.insert("fq_ref_date",fq_ref_date);
    let mut _res= reqwest::Client::new()
        .post("https://dataapi.joinquant.com/apis")
        .json(&data)
        .send()
        .expect("Failed to send request");
    let mut buf = String::new();
    _res.read_to_string(&mut buf).expect("Failed to read response");
    buf
}
//params=Dict("method" => "get_ticks","token" => token,"code"=>code,"count"=>count,"end_date"=>end_date);
fn get_ticks(token:&str,code:&str,count:&str,end_date:&str) -> String{
    let mut data = HashMap::new();
    data.insert("method","get_ticks");
    data.insert("count",count);
    data.insert("token",token);
    data.insert("code",code);
    data.insert("enddate",end_date);
    let mut _res= reqwest::Client::new()
        .post("https://dataapi.joinquant.com/apis")
        .json(&data)
        .send()
        .expect("Failed to send request");
    let mut buf = String::new();
    _res.read_to_string(&mut buf).expect("Failed to read response");
    buf
}
fn main() {
    let _token = get_token();
    println!("{:?}",_token);
    let code ="600000.XSHG";
    let date = "2019-01-15";
    let _data = get_bars(&_token,code,"10000","1m","2019-01-15","2019-02-28");
    println!("{}",_data);
    thread::sleep(Duration::from_millis(500000));
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/823979
推荐阅读
相关标签
  

闽ICP备14008679号