Browse Source

okx 完成未测试

DESKTOP-F4JN2J4\Citrus_limon 2 years ago
parent
commit
d2093d8648
1 changed files with 112 additions and 106 deletions
  1. 112 106
      exchange_data_formatter/src/okx_handle.rs

+ 112 - 106
exchange_data_formatter/src/okx_handle.rs

@@ -468,93 +468,105 @@ pub(crate) fn handle_swap_market(data: String, symbol: String) -> Market {
 /// - 接口`"/api/v5/trade/order"`
 ///
 /// struct SwapOrder
-/// `id`: String 订单编号
-/// `symbol`: String, 合约编号
-/// `order_type`: String, 类型,市价单或限价单
-/// `side`: String, 买卖方向
-/// `price`: Decimal, 下单价格
-/// `size`: Decimal, 数量
-/// `value`: Decimal, 订单价值
-/// `deal_value`: Decimal, 成交价
-/// `deal_size`: Decimal, 成交数量
-/// `stp`: Option<String>, 类型
-/// `stop`: String, 止损订单类型
-/// `stop_price_type`: String, 止损订单触发价格类型
-/// `stop_triggered`: bool, 止损订单是否触发标志
-/// `stop_price`: Option<Decimal>, 止损订单触发价格
-/// `time_in_force`: String, timeInForce类型
-/// `post_only`: bool, postOnly标志
-/// `hidden`: bool, 隐藏单标志
-/// `iceberg`: bool, 冰山单标志
-/// `leverage`: Decimal, 杠杆倍数
-/// `force_hold`: bool, 强制冻结单标志
-/// `close_order`: bool, 平仓单标志
-/// `visible_size`: Option<Decimal>, 冰山单可见数量
-/// `client_oid`: String, 客户订单编号
-/// `remark`: Option<String>, 注解
-/// `tags`: Option<String>, 订单标签
-/// `is_active`: bool, 未完成订单标志
-/// `cancel_exist`: bool, 订单存在取消数量标志
-/// `created_at`: i64, 创建时间
-/// `updated_at`: i64, 最新更新时间
-/// `end_at`: Option<i64>, 截止时间
-/// `order_time`: i128, 下单时间纳秒
-/// `settle_currency`: String, 结算币种
-/// `status`: String, 订单状态open或done
-/// `filled_value`: Decimal, 已经成交订单价值
-/// `filled_size`: Decimal, 已经成交订单数量
-/// `reduce_only`: bool, 只减仓标记
-#[derive(Debug, Deserialize, Serialize)]
+/// - `inst_type`: String, 产品类型
+/// - `inst_id`: String, 产品ID
+/// - `ccy`: String, 保证金币种
+/// - `ord_id`: String, 订单ID
+/// - `cl_ord_id`: String, 客户自定义订单ID
+/// - `tag`: String, 订单标签
+/// - `px`: Decimal, 委托价格
+/// - `px_usd`: String, 期权价格
+/// - `px_vol`: String, 期权订单的隐含波动率
+/// - `px_type`: String, 期权的价格类型
+/// - `sz`: Decimal, 委托数量
+/// - `pnl`: Decimal, 收益
+/// - `ord_type`: String, 订单类型
+/// - `side`: String, 订单方向
+/// - `pos_side`: String, 持仓方向
+/// - `td_mode`: String, 交易模式
+/// - `acc_fill_sz`: Decimal, 累计成交数量
+/// - `fill_px`: String, 最新成交价格,如果成交数量为0,该字段为""
+/// - `trade_id`: String, 最新成交ID
+/// - `fill_sz`: Decimal, 最新成交数量
+/// - `fill_time`: String, 最新成交时间
+/// - `source`: String, 订单来源
+/// - `state`: String, 订单状态
+/// - `avg_px`: Decimal, 成交均价,如果成交数量为0,该字段也为""
+/// - `lever`: Decimal, 杠杆倍数
+/// - `attach_algo_cl_ord_id`: String, 下单附带止盈止损时,客户自定义的策略订单ID
+/// - `tp_trigger_px`: Decimal, 止盈触发价
+/// - `tp_trigger_px_type`: String, 止盈触发价类型
+/// - `tp_ord_px`: Decimal, 止盈委托价
+/// - `sl_trigger_px`: Decimal, 止损触发价
+/// - `sl_trigger_px_type`: String, 止损触发价类型
+/// - `sl_ord_px`: Decimal, 止损委托价
+/// - `stp_id`: String, 自成交保护ID
+/// - `stp_mode`: String, 自成交保护模式
+/// - `fee_ccy`: String, 交易手续费币种
+/// - `fee`: Decimal, 手续费与返佣
+/// - `rebate_ccy`: String, 返佣金币种
+/// - `rebate`: String, 返佣金额,仅适用于币币和杠杆
+/// - `tgt_ccy`: String, 币币市价单委托数量sz的单位
+/// - `category`: String, 订单种类
+/// - `reduce_only`: String, 是否只减仓
+/// - `cancel_source`: String, 	订单取消来源的原因枚举值代码
+/// - `cancel_source_reason`: String, 订单取消来源的对应具体原因
+/// - `quick_mgn_type`: String, 一键借币类型,仅适用于杠杆逐仓的一键借币模式
+/// - `algo_cl_ord_id`: String, 客户自定义策略订单ID
+/// - `algo_id`: String, 策略委托单ID,策略订单触发时有值,否则为""
+/// - `u_time`: String, 订单状态更新时间
+/// - `c_time`: String, 订单创建时间
+#[derive(Debug, Clone, Deserialize, Serialize)]
 #[serde(rename_all = "camelCase")]
 struct SwapOrder {
-    inst_type:String,
-    inst_id:String,
-    ccy:String,
-    ord_id:String,
-    cl_ord_id:String,
-    tag:String,
-    px:Decimal,
-    px_usd:String,
-    px_vol:String,
-    px_type:String,
-    sz:Decimal,
-    pnl:Decimal,
-    ord_type:String,
-    side:String,
-    pos_side:String,
-    td_mode:String,
-    acc_fill_sz:Decimal,
-    fill_px:Decimal,
-    trade_id:String,
-    fill_sz:Decimal,
-    fill_time:String,
-    source: "",
-    state:String,
-    avg_px:Decimal,
-    lever:Decimal,
-    attach_algo_cl_ord_id:String,
-    tp_trigger_px:Decimal,
-    tp_trigger_px_type:String,
-    tp_ord_px:Decimal,
-    sl_trigger_px:Decimal,
-    sl_trigger_px_type:String,
-    sl_ord_px:Decimal,
-    stp_id:String,
-    stp_mode:String,
-    fee_ccy:String,
-    fee:Decimal,
-    rebate_ccy:String,
-    rebate:"",
-    tgt_ccy:"",
-    category:"",
-    reduce_only: "false",
-    cancel_source: "20",
-    cancel_source_reason: "Cancel all after triggered",
-    quick_mgn_type: "",
-    algo_cl_ord_id: "",
-    algo_id: "",
-    u_time:"1597026383085",
-    c_time:"1597026383085"
+    inst_type: String,
+    inst_id: String,
+    ccy: String,
+    ord_id: String,
+    cl_ord_id: String,
+    tag: String,
+    px: Decimal,
+    px_usd: String,
+    px_vol: String,
+    px_type: String,
+    sz: Decimal,
+    pnl: Decimal,
+    ord_type: String,
+    side: String,
+    pos_side: String,
+    td_mode: String,
+    acc_fill_sz: Decimal,
+    fill_px: String,
+    trade_id: String,
+    fill_sz: Decimal,
+    fill_time: String,
+    source: String,
+    state: String,
+    avg_px: Decimal,
+    lever: Decimal,
+    attach_algo_cl_ord_id: String,
+    tp_trigger_px: Decimal,
+    tp_trigger_px_type: String,
+    tp_ord_px: Decimal,
+    sl_trigger_px: Decimal,
+    sl_trigger_px_type: String,
+    sl_ord_px: Decimal,
+    stp_id: String,
+    stp_mode: String,
+    fee_ccy: String,
+    fee: Decimal,
+    rebate_ccy: String,
+    rebate: String,
+    tgt_ccy: String,
+    category: String,
+    reduce_only: String,
+    cancel_source: String,
+    cancel_source_reason: String,
+    quick_mgn_type: String,
+    algo_cl_ord_id: String,
+    algo_id: String,
+    u_time: String,
+    c_time: String,
 }
 
 /// 处理合约订单信息
@@ -564,30 +576,24 @@ struct SwapOrder {
 /// - `data`:String, 交易所返回账户信息,JsonString
 /// - `ct_val`:Decimal, 张数价值
 pub(crate) fn handle_swap_order(data: String, ct_val: Decimal) -> Order {
-    let order_info: SwapOrder = serde_json::from_str(&data).unwrap();
-
-    let status = order_info.status;
-    let deal_amount = order_info.filled_size * ct_val;
-    let avg_price = if deal_amount.is_zero() { dec!(0) } else { order_info.filled_value / deal_amount };
-    let custom_status = match status.as_str() {
-        "open" => { "NEW".to_string() }
-        "cancelled" => { "REMOVE".to_string() }
-        "closed" => { "REMOVE".to_string() }
-        "finished" => { "REMOVE".to_string() }
-        _ => {
-            error!("Kucoin:格式化订单状态错误!\nhandle_swap_order:status={}", status);
-            panic!("Kucoin:格式化订单状态错误!\nhandle_swap_order:status={}", status)
-        }
+    let order_info_list: Vec<SwapOrder> = serde_json::from_str(&data).unwrap();
+    let order_info = order_info_list[0].clone();
+    let custom_status = if ["canceled", "filled", "mmp_canceled"].contains(&order_info.state.as_str()) {
+        "REMOVE".to_string()
+    } else if ["live", "partially_filled"].contains(&order_info.state.as_str()) {
+        "NEW".to_string()
+    } else {
+        "NULL".to_string()
     };
 
     Order {
-        id: order_info.id,
-        custom_id: order_info.client_oid,
-        price: order_info.price,
-        amount: order_info.size * ct_val,
-        deal_amount,
-        avg_price,
+        id: order_info.ord_id,
+        custom_id: order_info.cl_ord_id,
+        price: order_info.px,
+        amount: order_info.sz * ct_val,
+        deal_amount: order_info.acc_fill_sz * ct_val,
+        avg_price: order_info.avg_px,
         status: custom_status,
-        order_type: order_info.order_type,
+        order_type: order_info.ord_type,
     }
 }