|
|
@@ -12,9 +12,8 @@ use serde::{Deserialize, Serialize};
|
|
|
use tokio::time::Instant;
|
|
|
use tracing::{error, info, trace};
|
|
|
use exchanges::bybit_swap_rest::BybitSwapRest;
|
|
|
-use crate::{Platform, ExchangeEnum, Account, Position, Ticker, Market, Order, OrderCommand};
|
|
|
+use crate::{Platform, ExchangeEnum, Account, Position, Ticker, Market, Order, OrderCommand, PositionModeEnum};
|
|
|
use global::trace_stack::TraceStack;
|
|
|
-use crate::bybit_swap_handle::format_position_item;
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
@@ -162,7 +161,7 @@ impl Platform for BybitSwap {
|
|
|
let ct_val = self.market.multiplier;
|
|
|
let res_data = self.request.get_positions(symbol, "".to_string()).await;
|
|
|
if res_data.code == 200 {
|
|
|
- let result = res_data.data["list"].as_array().unwrap().iter().map(|item| { format_position_item(item, &ct_val) }).collect();
|
|
|
+ let result = res_data.data["list"].as_array().unwrap().iter().map(|item| { format_position_item(item, ct_val) }).collect();
|
|
|
Ok(result)
|
|
|
} else {
|
|
|
Err(Error::new(ErrorKind::Other, res_data.to_string()))
|
|
|
@@ -175,7 +174,7 @@ impl Platform for BybitSwap {
|
|
|
let res_data = self.request.get_positions("".to_string(), symbol_array[1].to_string().to_uppercase()).await;
|
|
|
if res_data.code == 200 {
|
|
|
info!("{}", res_data.data.to_string());
|
|
|
- let result = res_data.data["list"].as_array().unwrap().iter().map(|item| { format_position_item(item, &ct_val) }).collect();
|
|
|
+ let result = res_data.data["list"].as_array().unwrap().iter().map(|item| { format_position_item(item, ct_val) }).collect();
|
|
|
Ok(result)
|
|
|
} else {
|
|
|
Err(Error::new(ErrorKind::Other, res_data.to_string()))
|
|
|
@@ -651,6 +650,49 @@ impl Platform for BybitSwap {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+pub fn format_position_item(position: &Value, ct_val: Decimal) -> Position {
|
|
|
+ let position_idx = position["positionIdx"].to_string();
|
|
|
+ let mut position_mode = match position_idx.as_str() {
|
|
|
+ "0" => PositionModeEnum::Both,
|
|
|
+ "1" => PositionModeEnum::Long,
|
|
|
+ "2" => PositionModeEnum::Short,
|
|
|
+ _ => {
|
|
|
+ error!("bybit_swap:格式化持仓模式错误!\nformat_position_item:position={:?}", position);
|
|
|
+ panic!("bybit_swap:格式化持仓模式错误!\nformat_position_item:position={:?}", position)
|
|
|
+ }
|
|
|
+ };
|
|
|
+ let size_str: String = from_value(position["size"].clone()).unwrap();
|
|
|
+ let size = Decimal::from_str(size_str.as_str()).unwrap();
|
|
|
+ let side = position["side"].as_str().unwrap().to_string();
|
|
|
+ let amount = size * ct_val;
|
|
|
+ let mut profit = Decimal::ZERO;
|
|
|
+ let profit_str = position["unrealisedPnl"].as_str().unwrap_or("0");
|
|
|
+ if profit_str != "" {
|
|
|
+ profit = Decimal::from_str(profit_str).unwrap();
|
|
|
+ }
|
|
|
+
|
|
|
+ match position_mode {
|
|
|
+ PositionModeEnum::Both => {
|
|
|
+ position_mode = match side.as_str() {
|
|
|
+ "Buy" => PositionModeEnum::Long,
|
|
|
+ "Sell" => PositionModeEnum::Short,
|
|
|
+ _ => { PositionModeEnum::Both }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _ => {}
|
|
|
+ }
|
|
|
+ Position {
|
|
|
+ symbol: position["symbol"].as_str().unwrap_or("").parse().unwrap(),
|
|
|
+ margin_level: Decimal::from_str(position["leverage"].as_str().unwrap()).unwrap(),
|
|
|
+ amount,
|
|
|
+ frozen_amount: Decimal::ZERO,
|
|
|
+ price: Decimal::from_str(position["avgPrice"].as_str().unwrap()).unwrap(),
|
|
|
+ profit,
|
|
|
+ position_mode,
|
|
|
+ margin: Decimal::from_str(position["positionBalance"].as_str().unwrap()).unwrap(),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn format_cancel_order_item(order: Value) -> Order {
|
|
|
Order {
|
|
|
id: format!("{}", order["orderId"].as_str().unwrap()),
|