Sfoglia il codice sorgente

加入ticker信息,ticker中有持仓量。

skyffire 1 anno fa
parent
commit
86a733e4e5

+ 4 - 2
standard/src/binance_swap.rs

@@ -177,13 +177,14 @@ impl Platform for BinanceSwap {
         if res_data.code == 200 {
             let res_data_json: serde_json::Value = res_data.data;
             let result = Ticker {
-                time: res_data_json["time"].as_i64().unwrap(),
+                time: Decimal::from_i64(res_data_json["time"].as_i64().unwrap()).unwrap(),
                 high: Decimal::from_str(res_data_json["askPrice"].as_str().unwrap()).unwrap(),
                 low: Decimal::from_str(res_data_json["bidPrice"].as_str().unwrap()).unwrap(),
                 sell: Decimal::from_str(res_data_json["askPrice"].as_str().unwrap()).unwrap(),
                 buy: Decimal::from_str(res_data_json["bidPrice"].as_str().unwrap()).unwrap(),
                 last: dec!(-1),
                 volume: dec!(-1),
+                open_interest: Default::default(),
             };
             Ok(result)
         } else {
@@ -197,13 +198,14 @@ impl Platform for BinanceSwap {
         if res_data.code == 200 {
             let res_data_json: serde_json::Value = res_data.data;
             let result = Ticker {
-                time: res_data_json["time"].as_i64().unwrap(),
+                time: Decimal::from_i64(res_data_json["time"].as_i64().unwrap()).unwrap(),
                 high: Decimal::from_str(res_data_json["askPrice"].as_str().unwrap()).unwrap(),
                 low: Decimal::from_str(res_data_json["bidPrice"].as_str().unwrap()).unwrap(),
                 sell: Decimal::from_str(res_data_json["askPrice"].as_str().unwrap()).unwrap(),
                 buy: Decimal::from_str(res_data_json["bidPrice"].as_str().unwrap()).unwrap(),
                 last: dec!(-1),
                 volume: dec!(-1),
+                open_interest: Default::default(),
             };
             Ok(result)
         } else {

+ 6 - 4
standard/src/bybit_swap.rs

@@ -193,13 +193,14 @@ impl Platform for BybitSwap {
             }
             let value = list[0].clone();
             Ok(Ticker{
-                time: chrono::Utc::now().timestamp_millis(),
+                time: Decimal::from_i64(chrono::Utc::now().timestamp_millis()).unwrap(),
                 high: value.high_price24h,
                 low: value.low_price24h,
                 sell: value.ask1_price,
                 buy: value.bid1_price,
                 last: value.last_price,
-                volume: value.volume24h
+                volume: value.volume24h,
+                open_interest: Default::default(),
             })
         } else {
             Err(Error::new(ErrorKind::Other, res_data.to_string()))
@@ -220,13 +221,14 @@ impl Platform for BybitSwap {
                 }
                 Some(value) => {
                     let result = Ticker {
-                        time: chrono::Utc::now().timestamp_millis(),
+                        time: Decimal::from_i64(chrono::Utc::now().timestamp_millis()).unwrap(),
                         high: value.high_price24h,
                         low: value.low_price24h,
                         sell: value.ask1_price,
                         buy: value.bid1_price,
                         last: value.last_price,
-                        volume: value.volume24h
+                        volume: value.volume24h,
+                        open_interest: Default::default(),
                     };
                     Ok(result)
                 }

+ 25 - 1
standard/src/bybit_swap_handle.rs

@@ -1,4 +1,5 @@
 use std::str::FromStr;
+use chrono::Utc;
 use rust_decimal::Decimal;
 use rust_decimal::prelude::FromPrimitive;
 use serde_json::{from_value, Value};
@@ -6,7 +7,7 @@ use tokio::time::Instant;
 use tracing::{error};
 use exchanges::response_base::ResponseData;
 use global::trace_stack::TraceStack;
-use crate::{Account, OrderBook, Order, Position, PositionModeEnum, SpecialOrder, Depth, Trade};
+use crate::{Account, OrderBook, Order, Position, PositionModeEnum, SpecialOrder, Depth, Trade, Ticker};
 
 // 处理账号信息
 pub fn handle_account_info(res_data: &ResponseData, symbol: &String) -> Account {
@@ -193,6 +194,29 @@ pub fn handle_book_ticker(res_data: &ResponseData, mul: &Decimal) -> Depth {
     }
 }
 
+pub fn handle_ticker(res_data: &ResponseData) -> Ticker {
+    let time = Decimal::from_i64(Utc::now().timestamp_millis()).unwrap();
+    let high = Decimal::from_str(res_data.data["highPrice24h"].as_str().unwrap()).unwrap();
+    let low = Decimal::from_str(res_data.data["lowPrice24h"].as_str().unwrap()).unwrap();
+    let sell = Decimal::from_str(res_data.data["ask1Price"].as_str().unwrap()).unwrap();
+    let buy = Decimal::from_str(res_data.data["bid1Price"].as_str().unwrap()).unwrap();
+    let last = Decimal::from_str(res_data.data["lastPrice"].as_str().unwrap()).unwrap();
+    let volume = Decimal::from_str(res_data.data["volume24h"].as_str().unwrap()).unwrap();
+    let open_interest = Decimal::from_str(res_data.data["openInterest"].as_str().unwrap()).unwrap();
+    // let s = res_data.data["symbol"].as_str().unwrap().replace("USDT", "_USDT");
+
+    Ticker {
+        time,
+        high,
+        low,
+        sell,
+        buy,
+        last,
+        volume,
+        open_interest,
+    }
+}
+
 pub fn format_depth_items(value: Value, mul: &Decimal) -> Vec<OrderBook> {
     let mut depth_items: Vec<OrderBook> = vec![];
     for val in value.as_array().unwrap() {

+ 4 - 2
standard/src/coinex_swap.rs

@@ -182,13 +182,14 @@ impl Platform for CoinexSwap {
                 }
                 Some(value) => {
                     let result = Ticker {
-                        time: chrono::Utc::now().timestamp_millis(),
+                        time: Decimal::from_i64(chrono::Utc::now().timestamp_millis()).unwrap(),
                         high: Decimal::from_str(value["high"].as_str().unwrap()).unwrap(),
                         low: Decimal::from_str(value["low"].as_str().unwrap()).unwrap(),
                         sell: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         buy: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         last: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         volume: Decimal::from_str(value["volume"].as_str().unwrap()).unwrap(),
+                        open_interest: Default::default(),
                     };
                     Ok(result)
                 }
@@ -211,13 +212,14 @@ impl Platform for CoinexSwap {
                 }
                 Some(value) => {
                     let result = Ticker {
-                        time: chrono::Utc::now().timestamp_millis(),
+                        time: Decimal::from_i64(chrono::Utc::now().timestamp_millis()).unwrap(),
                         high: Decimal::from_str(value["high"].as_str().unwrap()).unwrap(),
                         low: Decimal::from_str(value["low"].as_str().unwrap()).unwrap(),
                         sell: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         buy: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         last: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         volume: Decimal::from_str(value["volume"].as_str().unwrap()).unwrap(),
+                        open_interest: Default::default(),
                     };
                     Ok(result)
                 }

+ 13 - 3
standard/src/exchange_struct_handler.rs

@@ -4,7 +4,7 @@ use rust_decimal::prelude::FromPrimitive;
 use tracing::error;
 use exchanges::response_base::ResponseData;
 use crate::exchange::ExchangeEnum;
-use crate::{binance_swap_handle, bybit_swap_handle, coinex_swap_handle, gate_swap_handle};
+use crate::{binance_swap_handle, bybit_swap_handle, coinex_swap_handle, gate_swap_handle, Ticker};
 use crate::{Record, Trade, Depth};
 use crate::{Account, OrderBook, Position, SpecialOrder};
 
@@ -98,7 +98,6 @@ impl ExchangeStructHandler {
             symbol,
         }
     }
-
     // 处理成交信息,关于mul的问题:
     //     因为部分交易所比较特殊,返回的深度的数量是张数,这里是标准化成U量的形式;
     //     如果是不需要处理的交易所,传个Decimal::ONE就行了
@@ -158,7 +157,6 @@ impl ExchangeStructHandler {
             // }
         }
     }
-
     // 处理BookTicker信息
     pub fn book_ticker_handle(exchange: ExchangeEnum, res_data: &ResponseData, mul: &Decimal) -> Depth {
         match exchange {
@@ -231,6 +229,18 @@ impl ExchangeStructHandler {
             }
         }
     }
+    // 处理ticker信息
+    pub fn ticker_handle(exchange: ExchangeEnum, res_data: &ResponseData) -> Ticker {
+        match exchange {
+            ExchangeEnum::BybitSwap => {
+                bybit_swap_handle::handle_ticker(res_data)
+            },
+            _ => {
+                error!("未找到该交易所!trades_handle: {:?}", exchange);
+                panic!("未找到该交易所!trades_handle: {:?}", exchange);
+            }
+        }
+    }
     // 处理账号信息
     pub fn account_info_handle(exchange: ExchangeEnum, res_data: &ResponseData, symbol: &String) -> Account {
         match exchange {

+ 4 - 2
standard/src/gate_swap.rs

@@ -165,13 +165,14 @@ impl Platform for GateSwap {
                 }
                 Some(value) => {
                     let result = Ticker {
-                        time: chrono::Utc::now().timestamp_millis(),
+                        time: Decimal::from_i64(chrono::Utc::now().timestamp_millis()).unwrap(),
                         high: Decimal::from_str(value["high_24h"].as_str().unwrap()).unwrap(),
                         low: Decimal::from_str(value["low_24h"].as_str().unwrap()).unwrap(),
                         sell: Decimal::from_str(value["lowest_ask"].as_str().unwrap()).unwrap(),
                         buy: Decimal::from_str(value["highest_bid"].as_str().unwrap()).unwrap(),
                         last: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         volume: Decimal::from_str(value["volume_24h"].as_str().unwrap()).unwrap(),
+                        open_interest: Default::default(),
                     };
                     Ok(result)
                 }
@@ -195,13 +196,14 @@ impl Platform for GateSwap {
                 }
                 Some(value) => {
                     let result = Ticker {
-                        time: chrono::Utc::now().timestamp_millis(),
+                        time: Decimal::from_i64(chrono::Utc::now().timestamp_millis()).unwrap(),
                         high: Decimal::from_str(value["high_24h"].as_str().unwrap()).unwrap(),
                         low: Decimal::from_str(value["low_24h"].as_str().unwrap()).unwrap(),
                         sell: Decimal::from_str(value["lowest_ask"].as_str().unwrap()).unwrap(),
                         buy: Decimal::from_str(value["highest_bid"].as_str().unwrap()).unwrap(),
                         last: Decimal::from_str(value["last"].as_str().unwrap()).unwrap(),
                         volume: Decimal::from_str(value["volume_24h"].as_str().unwrap()).unwrap(),
+                        open_interest: Default::default(),
                     };
                     Ok(result)
                 }

+ 4 - 2
standard/src/lib.rs

@@ -416,25 +416,27 @@ impl Order {
 /// - `volume(Decimal)`: 最近成交量
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct Ticker {
-    pub time: i64,
+    pub time: Decimal,
     pub high: Decimal,
     pub low: Decimal,
     pub sell: Decimal,
     pub buy: Decimal,
     pub last: Decimal,
     pub volume: Decimal,
+    pub open_interest: Decimal,
 }
 
 impl Ticker {
     pub fn new() -> Ticker {
         Ticker {
-            time: 0,
+            time: Default::default(),
             high: Default::default(),
             low: Default::default(),
             sell: Default::default(),
             buy: Default::default(),
             last: Default::default(),
             volume: Default::default(),
+            open_interest: Default::default(),
         }
     }
 }

+ 2 - 1
strategy/src/core.rs

@@ -745,13 +745,14 @@ impl Core {
             let bp = self.tickers.get(i).unwrap().buy;
             let ap = self.tickers.get(i).unwrap().sell;
             ref_tickers.insert(i.clone(), Ticker {
-                time: 0,
+                time: Decimal::ZERO,
                 high: Default::default(),
                 low: Default::default(),
                 sell: ap,
                 buy: bp,
                 last: Default::default(),
                 volume: Default::default(),
+                open_interest: Default::default(),
             });
         }
         self.ref_price = self.avellaneda_stoikov.get_ref_price(&ref_tickers);