| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- use std::fs::File;
- use std::io::Read;
- use rust_decimal::Decimal;
- use rust_decimal_macros::dec;
- use toml::from_str;
- use serde_derive::Deserialize;
- use serde_json::Value;
- #[derive(Debug, Deserialize, Clone)]
- pub struct Params {
- // 经纪商id
- pub broker_id: String,
- // 账号昵称
- pub account_name: String,
- // ak
- pub access_key: String,
- // sk
- pub secret_key: String,
- // pk
- pub pass_key: String,
- // 交易盘口
- pub exchange: String,
- // 交易币对
- pub pair: String,
- // 开仓
- pub open: Decimal,
- // 最小挂单值
- pub min_open: Decimal,
- // 激活平仓挂单
- pub close_activate: Decimal,
- // 平仓
- pub close: Decimal,
- // 杠杆大小
- pub lever_rate: Decimal,
- // 现货底仓
- pub hold_coin: Decimal,
- // core的run_strategy函数,用于定期检查使用
- pub interval: u64,
- // 参考盘口
- pub ref_exchange: Vec<String>,
- // 参考币种
- pub ref_pair: Vec<String>,
- // 账户资金使用比例
- pub used_pct: Decimal,
- // 止损比例 默认0.02 0.02 = 2%
- pub stop_loss: Decimal,
- // 平滑系数 默认0.999
- pub gamma: Decimal,
- // 分批建仓功能 小资金建议1 大资金建议3 默认 1
- pub grid: i8,
- // 是否启用colocation技术, 1开启,0关闭 默认0
- pub colo: i8,
- // 日志级别,从低到高依次是:[trace, debug, info, warn, error]
- pub log_level: String,
- // 中控端口
- pub port: u32,
- // 运行模式 0.正常策略运行, 1.清理挂单及仓位
- pub run_mode: i8,
- // 机器人id
- pub r_id: String,
- }
- impl Params {
- pub fn new(file_path: &str) -> Result<Params, Box<dyn std::error::Error>> {
- // 打开文件并读取内容
- let mut file = File::open(file_path)?;
- let mut contents = String::new();
- file.read_to_string(&mut contents)?;
- // 解析 TOML 数据到 Params 结构体
- let params: Params = from_str(&contents)?;
- Ok(params)
- }
- pub fn new_json(file_path: &str, call_port: u32) -> Result<Params, Box<dyn std::error::Error>> {
- // 打开文件并读取内容
- let json_contents = std::fs::read_to_string(file_path)?;
- let json_value: Value = serde_json::from_str(&json_contents).unwrap();
- let acc = json_value["account"].clone();
- // 使用serde_json库来解析JSON文件内容,并将其转换为Ship结构体
- // let params: Params = serde_json::from_str(&json_contents)?;
- let params: Params = Params {
- account_name: acc["name"].as_str().unwrap().to_string(),
- access_key: acc["accessKey"].as_str().unwrap().to_string(),
- secret_key: acc["secretKey"].as_str().unwrap().to_string(),
- pass_key: acc["pass"].as_str().unwrap().to_string(),
- exchange: json_value["exchange"].as_str().unwrap().to_string(),
- pair: json_value["pair"].as_str().unwrap().to_string(),
- open: Decimal::try_from(json_value["open"].as_f64().unwrap_or_default()).unwrap(),
- min_open: Decimal::try_from(json_value["min_open"].as_f64().unwrap()).unwrap(),
- close: Decimal::try_from(json_value["close"].as_f64().unwrap_or_default()).unwrap(),
- lever_rate: Decimal::try_from(json_value["lever_rate"].as_f64().unwrap_or_default()).unwrap(),
- ref_exchange: serde_json::from_str(json_value["ref_exchanges"].clone().as_str().unwrap()).unwrap(),
- ref_pair: serde_json::from_str(json_value["ref_pairs"].clone().as_str().unwrap()).unwrap(),
- stop_loss: Decimal::try_from(json_value["stop_loss"].as_f64().unwrap_or_default()).unwrap(),
- // 接下来是写死的参数
- close_activate: Decimal::ZERO,
- hold_coin: Decimal::ZERO,
- grid: 1,
- colo: 0,
- interval: 100,
- broker_id: json_value["exchange"].as_str().unwrap().to_string().split("_").collect::<Vec<_>>()[0].to_string(),
- used_pct: dec!(1),
- gamma: dec!(0.999),
- log_level: "info".to_string(),
- port: call_port,
- run_mode: 0,
- r_id: "-1".to_string()
- };
- Ok(params)
- }
- }
|