params.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. use std::fs::File;
  2. use std::io::Read;
  3. use rust_decimal::Decimal;
  4. use rust_decimal_macros::dec;
  5. use toml::from_str;
  6. use serde_derive::Deserialize;
  7. use serde_json::Value;
  8. #[derive(Debug, Deserialize, Clone)]
  9. pub struct Params {
  10. // 经纪商id
  11. pub broker_id: String,
  12. // 账号昵称
  13. pub account_name: String,
  14. // ak
  15. pub access_key: String,
  16. // sk
  17. pub secret_key: String,
  18. // pk
  19. pub pass_key: String,
  20. // 交易盘口
  21. pub exchange: String,
  22. // 交易币对
  23. pub pair: String,
  24. // 开仓
  25. pub open: Decimal,
  26. // 最小挂单值
  27. pub min_open: Decimal,
  28. // 激活平仓挂单
  29. pub close_activate: Decimal,
  30. // 平仓
  31. pub close: Decimal,
  32. // 杠杆大小
  33. pub lever_rate: Decimal,
  34. // 现货底仓
  35. pub hold_coin: Decimal,
  36. // core的run_strategy函数,用于定期检查使用
  37. pub interval: u64,
  38. // 参考盘口
  39. pub ref_exchange: Vec<String>,
  40. // 参考币种
  41. pub ref_pair: Vec<String>,
  42. // 账户资金使用比例
  43. pub used_pct: Decimal,
  44. // 止损比例 默认0.02 0.02 = 2%
  45. pub stop_loss: Decimal,
  46. // 平滑系数 默认0.999
  47. pub gamma: Decimal,
  48. // 分批建仓功能 小资金建议1 大资金建议3 默认 1
  49. pub grid: i8,
  50. // 是否启用colocation技术, 1开启,0关闭 默认0
  51. pub colo: i8,
  52. // 日志级别,从低到高依次是:[trace, debug, info, warn, error]
  53. pub log_level: String,
  54. // 中控端口
  55. pub port: u32,
  56. // 运行模式 0.正常策略运行, 1.清理挂单及仓位
  57. pub run_mode: i8,
  58. // 机器人id
  59. pub r_id: String,
  60. }
  61. impl Params {
  62. pub fn new(file_path: &str) -> Result<Params, Box<dyn std::error::Error>> {
  63. // 打开文件并读取内容
  64. let mut file = File::open(file_path)?;
  65. let mut contents = String::new();
  66. file.read_to_string(&mut contents)?;
  67. // 解析 TOML 数据到 Params 结构体
  68. let params: Params = from_str(&contents)?;
  69. Ok(params)
  70. }
  71. pub fn new_json(file_path: &str, call_port: u32) -> Result<Params, Box<dyn std::error::Error>> {
  72. // 打开文件并读取内容
  73. let json_contents = std::fs::read_to_string(file_path)?;
  74. let json_value: Value = serde_json::from_str(&json_contents).unwrap();
  75. let acc = json_value["account"].clone();
  76. // 使用serde_json库来解析JSON文件内容,并将其转换为Ship结构体
  77. // let params: Params = serde_json::from_str(&json_contents)?;
  78. let params: Params = Params {
  79. account_name: acc["name"].as_str().unwrap().to_string(),
  80. access_key: acc["accessKey"].as_str().unwrap().to_string(),
  81. secret_key: acc["secretKey"].as_str().unwrap().to_string(),
  82. pass_key: acc["pass"].as_str().unwrap().to_string(),
  83. exchange: json_value["exchange"].as_str().unwrap().to_string(),
  84. pair: json_value["pair"].as_str().unwrap().to_string(),
  85. open: Decimal::try_from(json_value["open"].as_f64().unwrap_or_default()).unwrap(),
  86. min_open: Decimal::try_from(json_value["min_open"].as_f64().unwrap()).unwrap(),
  87. close: Decimal::try_from(json_value["close"].as_f64().unwrap_or_default()).unwrap(),
  88. lever_rate: Decimal::try_from(json_value["lever_rate"].as_f64().unwrap_or_default()).unwrap(),
  89. ref_exchange: serde_json::from_str(json_value["ref_exchanges"].clone().as_str().unwrap()).unwrap(),
  90. ref_pair: serde_json::from_str(json_value["ref_pairs"].clone().as_str().unwrap()).unwrap(),
  91. stop_loss: Decimal::try_from(json_value["stop_loss"].as_f64().unwrap_or_default()).unwrap(),
  92. // 接下来是写死的参数
  93. close_activate: Decimal::ZERO,
  94. hold_coin: Decimal::ZERO,
  95. grid: 1,
  96. colo: 0,
  97. interval: 100,
  98. broker_id: json_value["exchange"].as_str().unwrap().to_string().split("_").collect::<Vec<_>>()[0].to_string(),
  99. used_pct: dec!(1),
  100. gamma: dec!(0.999),
  101. log_level: "info".to_string(),
  102. port: call_port,
  103. run_mode: 0,
  104. r_id: "-1".to_string()
  105. };
  106. Ok(params)
  107. }
  108. }