Browse Source

接入行情仓库

JiahengHe 1 năm trước cách đây
mục cha
commit
9cfe2cb950
3 tập tin đã thay đổi với 84 bổ sung2 xóa
  1. 1 0
      strategy/Cargo.toml
  2. 17 0
      strategy/src/model.rs
  3. 66 2
      strategy/src/strategy.rs

+ 1 - 0
strategy/Cargo.toml

@@ -19,6 +19,7 @@ tracing-subscriber = "0.3.17"
 standard = { path = "../standard" }
 global = { path = "../global" }
 exchanges = { path = "../exchanges" }
+reqwest = { version = "0.11.14", features = ["json"] }
 
 futures-util = { version = "0.3.28", default-features = false, features = ["sink", "std"] }
 futures-channel = "0.3.28"

+ 17 - 0
strategy/src/model.rs

@@ -130,3 +130,20 @@ pub struct OriginalTicker {
     pub A: Decimal
 }
 
+#[allow(non_snake_case)]
+#[derive(Serialize, Deserialize, Debug)]
+pub struct DealRecord {
+    // 参考价
+    pub refPrice: Decimal,
+    // 挂单价
+    pub regPrice: Decimal,
+    // 买单最优挂单数量
+    pub num: Decimal,
+    // 触发时间
+    pub triggerTime: i64,
+    // 机器名称
+    pub robotName: String,
+    // 方向
+    pub side: String
+}
+

+ 66 - 2
strategy/src/strategy.rs

@@ -1,15 +1,18 @@
 use std::cmp::{max, min};
 use std::collections::HashMap;
 use std::ops::{Div, Mul};
+use std::str::FromStr;
 use chrono::Utc;
 use rust_decimal::Decimal;
 use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
 use rust_decimal_macros::dec;
-use crate::model::{LocalPosition, OrderInfo, TraderMsg};
+use crate::model::{DealRecord, LocalPosition, OrderInfo, TraderMsg};
 use crate::utils;
 use tracing::{info, error, warn, instrument};
+use reqwest::{Client};
+use tokio::spawn;
 use global::params::Params;
-use standard::OrderCommand;
+use standard::{OrderCommand};
 
 #[derive(Debug)]
 pub struct Strategy {
@@ -1222,10 +1225,71 @@ impl Strategy {
         self._refresh_request_limit();                      // 刷新频率限制
         self._update_request_num(&mut command);             // 统计刷新频率
 
+
+        if command.limits_open.len() > 0 || command.limits_close.len() > 0{
+            let time = chrono::Utc::now().timestamp_millis();
+            let name = self.params.account_name.clone();
+            // 参考卖价
+            let ref_ap = self.ref_ap;
+            // 参考买价
+            let ref_bp = self.ref_bp;
+            let limits_open = command.limits_open.clone();
+            let limits_close = command.limits_close.clone();
+            spawn(async move {
+                let param_list = paras_limit_command(name.clone(), time.clone(), ref_ap.clone(), ref_bp.clone(), limits_open, limits_close);
+                let param_json_obj = serde_json::to_string(&param_list).unwrap();
+                market_warehouse_request(param_json_obj).await;
+            });
+        }
+
+
         return command;
     }
 }
 
+async fn market_warehouse_request(body_params: String) {
+    /****请求接口与 地址*/
+    let url = "http://as.skyfffire.com:8848/basic/saveDealRecords";
+
+    let client = Client::new();
+    let req = client.post(url).header("auth", "43626546liangjiang")
+        .header("Content-Type", "application/json").body(body_params.clone());
+
+    let response = req.send().await.unwrap();
+    if !response.status().is_success()  {
+        error!("行情数据------仓库挂单数据存储失败--------!{}", response.status());
+        error!(body_params);
+    }
+}
+
+fn paras_limit_command (robot_name: String, time: i64, ref_ap: Decimal, ref_bp: Decimal, limits_open: HashMap<String, Vec<String>>, limits_close: HashMap<String, Vec<String>>) -> Vec<DealRecord>{
+    let mut limits = HashMap::new();
+    limits.extend(limits_open);
+    limits.extend(limits_close);
+    let mut list: Vec<DealRecord> = Vec::with_capacity(limits.len());
+    for item in limits.keys() {
+        let item_clone = item.clone();
+        let value = limits[&item_clone].clone();
+        let amount = Decimal::from_str(value.get(0).unwrap_or(&"0".to_string())).unwrap();
+        let side = value.get(1).unwrap();
+        let price = Decimal::from_str(value.get(2).unwrap_or(&"0".to_string())).unwrap();
+        let mut ref_price = ref_ap;
+        if "kd" == side {
+            ref_price = ref_bp;
+        }
+        let deal_recode = DealRecord {
+            refPrice: ref_price,
+            regPrice: price,
+            num: amount,
+            triggerTime: time,
+            robotName: robot_name.clone(),
+            side: side.to_string(),
+        };
+        list.push(deal_recode);
+    }
+    return list;
+}
+
 #[cfg(test)]
 mod tests {
     use rust_decimal::Decimal;