three_commas_types/bots/
deals.rs

1use crate::{Pair, ProfitCurrency, StopLossType, Strategy, TakeProfitType, VolumeType};
2use chrono::{DateTime, Utc};
3use rust_decimal::Decimal;
4use serde::{Deserialize, Serialize};
5use smol_str::SmolStr;
6
7#[derive(Debug, Deserialize, Clone)]
8pub struct Deal {
9  pub id: usize,
10  pub bot_id: usize,
11  pub max_safety_orders: usize,
12  pub deal_has_error: bool,
13  pub account_id: usize,
14  pub active_safety_orders_count: usize,
15  pub created_at: DateTime<Utc>,
16  pub updated_at: Option<DateTime<Utc>>,
17  pub closed_at: Option<DateTime<Utc>>,
18  #[serde(rename = "finished?")]
19  pub is_finished: bool,
20  pub current_active_safety_orders_count: usize,
21  /// completed safeties (not including manual)
22  pub completed_safety_orders_count: usize,
23  /// completed manual safeties
24  pub completed_manual_safety_orders_count: usize,
25  pub pair: Pair,
26  pub status: DealStatus,
27  pub take_profit: Decimal,
28  pub base_order_volume: Decimal,
29  pub safety_order_volume: Decimal,
30  pub safety_order_step_percentage: Decimal,
31  pub bought_amount: Option<Decimal>,
32  pub bought_volume: Option<Decimal>,
33  pub bought_average_price: Option<Decimal>,
34  pub sold_amount: Option<Decimal>,
35  pub sold_volume: Option<Decimal>,
36  pub sold_average_price: Option<Decimal>,
37  pub take_profit_type: TakeProfitType,
38  pub final_profit: Decimal,
39  pub martingale_coefficient: Decimal,
40  pub martingale_volume_coefficient: Decimal,
41  pub martingale_step_coefficient: Decimal,
42  pub profit_currency: ProfitCurrency,
43  pub stop_loss_type: StopLossType,
44  pub safety_order_volume_type: VolumeType,
45  pub base_order_volume_type: VolumeType,
46  pub from_currency: SmolStr,
47  pub to_currency: SmolStr,
48  pub current_price: Decimal,
49  pub take_profit_price: Option<Decimal>,
50  pub stop_loss_price: Option<Decimal>,
51  pub final_profit_percentage: Decimal,
52  pub actual_profit_percentage: Decimal,
53  pub bot_name: String,
54  pub account_name: String,
55  pub usd_final_profit: Decimal,
56  pub actual_profit: Decimal,
57  pub actual_usd_profit: Decimal,
58  pub failed_message: Option<String>,
59  pub reserved_base_coin: Decimal,
60  pub reserved_second_coin: Decimal,
61  pub trailing_deviation: Decimal,
62  /// Highest price met in case of long deal, lowest price otherwise
63  pub trailing_max_price: Option<Decimal>,
64  /// Highest price met in TSL in case of long deal, lowest price otherwise
65  pub tsl_max_price: Option<Decimal>,
66  pub strategy: Strategy,
67}
68
69impl Deal {
70  pub fn id(&self) -> usize {
71    self.id
72  }
73
74  pub fn account_id(&self) -> usize {
75    self.account_id
76  }
77
78  pub fn bot_id(&self) -> usize {
79    self.bot_id
80  }
81
82  pub fn created_at(&self) -> DateTime<Utc> {
83    self.created_at
84  }
85
86  pub fn status(&self) -> DealStatus {
87    self.status
88  }
89
90  pub fn is_finished(&self) -> bool {
91    self.is_finished
92  }
93
94  pub fn is_active(&self) -> bool {
95    !self.is_finished
96  }
97
98  pub fn pair(&self) -> &Pair {
99    &self.pair
100  }
101
102  pub fn strategy(&self) -> Strategy {
103    self.strategy
104  }
105
106  pub fn max_safety_orders(&self) -> usize {
107    self.max_safety_orders
108  }
109
110  pub fn completed_safety_orders_count(&self) -> usize {
111    self.completed_safety_orders_count
112  }
113
114  pub fn completed_manual_safety_orders_count(&self) -> usize {
115    self.completed_manual_safety_orders_count
116  }
117
118  pub fn bought_volume(&self) -> Option<Decimal> {
119    self.bought_volume
120  }
121
122  pub fn reserved_base_coin(&self) -> Decimal {
123    self.reserved_base_coin
124  }
125
126  pub fn actual_profit(&self) -> Decimal {
127    self.actual_profit
128  }
129
130  pub fn actual_usd_profit(&self) -> Decimal {
131    self.actual_usd_profit
132  }
133}
134
135#[non_exhaustive]
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
137pub enum DealStatus {
138  #[serde(rename = "created")]
139  Created,
140
141  #[serde(rename = "base_order_placed")]
142  BaseOrderPlaced,
143
144  #[serde(rename = "bought")]
145  Bought,
146
147  #[serde(rename = "cancelled")]
148  Cancelled,
149
150  #[serde(rename = "completed")]
151  Completed,
152
153  #[serde(rename = "failed")]
154  Failed,
155
156  #[serde(rename = "panic_sell_pending")]
157  PanicSellPending,
158
159  #[serde(rename = "panic_sell_order_placed")]
160  PanicSellOrderPlaced,
161
162  #[serde(rename = "panic_sold")]
163  PanicSold,
164
165  #[serde(rename = "cancel_pending")]
166  CancelPending,
167
168  #[serde(rename = "stop_loss_pending")]
169  StopLossPending,
170
171  #[serde(rename = "stop_loss_finished")]
172  StopLossFinished,
173
174  #[serde(rename = "stop_loss_order_placed")]
175  StopLossOrderPlaced,
176
177  #[serde(rename = "switched")]
178  Switched,
179
180  #[serde(rename = "switched_take_profit")]
181  SwitchedTakeProfit,
182
183  #[serde(rename = "ttp_activated")]
184  TtpActivated,
185
186  #[serde(rename = "ttp_order_placed")]
187  TtpOrderPlaced,
188
189  #[serde(rename = "liquidated")]
190  Liquidated,
191
192  #[serde(rename = "bought_safety_pending")]
193  BoughtSafetyPending,
194
195  #[serde(rename = "bought_take_profit_pending")]
196  BoughtTakeProfitPending,
197
198  #[serde(rename = "settled")]
199  Settled,
200}