model.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. use rust_decimal::Decimal;
  2. use rust_decimal_macros::dec;
  3. use serde_derive::{Deserialize, Serialize};
  4. use tokio::time::Instant;
  5. use global::trace_stack::TraceStack;
  6. use standard::Order;
  7. pub struct TokenParam {
  8. // 平台币名称
  9. pub token: String,
  10. // 最低持有价值(u)
  11. pub limit_value: Decimal
  12. }
  13. #[derive(Debug, Clone, PartialEq, Eq)]
  14. pub struct LocalPosition {
  15. // 做多仓位
  16. pub long_pos: Decimal,
  17. // 做空仓位
  18. pub short_pos: Decimal,
  19. //
  20. pub long_avg: Decimal,
  21. //
  22. pub short_avg: Decimal
  23. }
  24. impl LocalPosition {
  25. pub fn new() -> LocalPosition {
  26. LocalPosition{
  27. long_pos: dec!(0),
  28. short_pos: dec!(0),
  29. long_avg: dec!(0),
  30. short_avg: dec!(0),
  31. }
  32. }
  33. }
  34. // #[derive(Debug, Clone)]
  35. // pub struct TraderMsg {
  36. // pub position: LocalPosition,
  37. // pub cash: Decimal,
  38. // pub coin: Decimal,
  39. // pub orders: HashMap<String, OrderInfo>,
  40. // pub ref_price: Vec<Vec<Decimal>>,
  41. // pub market: Vec<Decimal>,
  42. // pub predict: Decimal,
  43. // pub side: String,
  44. // }
  45. // impl TraderMsg {
  46. // pub fn new() -> TraderMsg {
  47. // TraderMsg{
  48. // position: LocalPosition {
  49. // long_pos: Default::default(),
  50. // short_pos: Default::default(),
  51. // long_avg: Default::default(),
  52. // short_avg: Default::default(),
  53. // },
  54. // cash: Default::default(),
  55. // coin: Default::default(),
  56. // orders: Default::default(),
  57. // ref_price: Default::default(),
  58. // market: vec![],
  59. // predict: Default::default(),
  60. // side: "normal".to_string(),
  61. // }
  62. // }
  63. // }
  64. #[derive(Debug, Clone)]
  65. pub struct OrderInfo {
  66. pub symbol: String,
  67. pub amount: Decimal,
  68. // 方向 kd kk
  69. pub side: String,
  70. // 价格
  71. pub price: Decimal,
  72. // 自定义订单号
  73. pub client_id: String,
  74. // 实际价格
  75. pub filled_price: Decimal,
  76. //
  77. pub filled: Decimal,
  78. // 订单号
  79. pub order_id: String,
  80. // 时间
  81. pub local_time: i64,
  82. // 时间
  83. pub create_time: i64,
  84. pub status: String,
  85. pub fee: Decimal,
  86. // 追踪体系
  87. pub trace_stack: TraceStack
  88. }
  89. impl OrderInfo {
  90. pub fn parse_order_to_order_info(order: &mut Order) -> OrderInfo {
  91. OrderInfo {
  92. symbol: "".to_string(),
  93. amount: order.amount.abs(),
  94. side: "".to_string(),
  95. price: order.price,
  96. client_id: order.custom_id.clone(),
  97. filled_price: order.avg_price,
  98. filled: order.deal_amount.abs(),
  99. order_id: order.id.clone(),
  100. local_time: 0,
  101. create_time: 0,
  102. status: order.status.clone(),
  103. fee: Default::default(),
  104. trace_stack: TraceStack::new(0, Instant::now()),
  105. }
  106. }
  107. }
  108. #[derive(Serialize, Deserialize, Clone, Debug)]
  109. pub struct OriginalTradeBa {
  110. // 成交价格
  111. pub p: Decimal,
  112. // 成交数量
  113. pub q: Decimal,
  114. // 成交时间
  115. #[serde(rename = "T")]
  116. pub t: Decimal,
  117. // 买方是否是做市方。如true,则此次成交是一个主动卖出单,否则是一个主动买入单。
  118. pub m: bool
  119. }
  120. #[derive(Serialize, Deserialize)]
  121. pub struct OriginalTradeGa {
  122. pub size: Decimal,
  123. pub price: Decimal
  124. }
  125. #[derive(Serialize, Deserialize)]
  126. pub struct OriginalTradeBy {
  127. pub v: Decimal,
  128. pub p: Decimal
  129. }
  130. #[derive(Serialize, Deserialize)]
  131. pub struct OriginalTradeOK {
  132. // 数量
  133. pub sz: Decimal,
  134. // 价格
  135. pub px: Decimal
  136. }
  137. #[allow(non_snake_case)]
  138. #[derive(Serialize, Deserialize, Debug)]
  139. pub struct OriginalTicker {
  140. // 更新ID
  141. pub u: i64,
  142. // 买单最优挂单价格
  143. pub b: Decimal,
  144. // 买单最优挂单数量
  145. pub B: Decimal,
  146. // 卖单最优挂单价格
  147. pub a: Decimal,
  148. // 卖单最优挂单数量
  149. pub A: Decimal
  150. }
  151. #[allow(non_snake_case)]
  152. #[derive(Serialize, Deserialize, Debug)]
  153. pub struct DealRecord {
  154. // 参考价
  155. pub refPrice: String,
  156. // 挂单价
  157. pub regPrice: String,
  158. // 买单最优挂单数量
  159. pub num: String,
  160. // 触发时间
  161. pub triggerTime: i64,
  162. // 机器名称
  163. pub robotName: String,
  164. // 方向
  165. pub side: String
  166. }