core/rustybot/src/positions.rs
Giulio De Pasquale 78b57b3899 traits and shit
2021-01-05 12:58:47 +00:00

103 lines
2.2 KiB
Rust

use crate::currency::{Symbol, SymbolPair};
#[derive(Clone)]
pub struct Position {
pair: SymbolPair,
state: PositionState,
amount: f64,
base_price: f64,
pl: f64,
pl_perc: f64,
price_liq: f64,
position_id: u64,
creation_date: Option<u64>,
creation_update: Option<u64>,
}
impl Position {
pub fn new(
pair: SymbolPair,
state: PositionState,
amount: f64,
base_price: f64,
pl: f64,
pl_perc: f64,
price_liq: f64,
position_id: u64,
) -> Self {
Position {
pair,
state,
amount,
base_price,
pl,
pl_perc,
price_liq,
position_id,
creation_date: None,
creation_update: None,
}
}
pub fn with_creation_date(mut self, creation_date: u64) -> Self {
self.creation_date = Some(creation_date);
self.creation_update = Some(creation_date);
self
}
pub fn pair(&self) -> &SymbolPair {
&self.pair
}
pub fn state(&self) -> PositionState {
self.state
}
pub fn amount(&self) -> f64 {
self.amount
}
pub fn base_price(&self) -> f64 {
self.base_price
}
pub fn pl(&self) -> f64 {
self.pl
}
pub fn pl_perc(&self) -> f64 {
self.pl_perc
}
pub fn price_liq(&self) -> f64 {
self.price_liq
}
pub fn position_id(&self) -> u64 {
self.position_id
}
pub fn creation_date(&self) -> Option<u64> {
self.creation_date
}
pub fn creation_update(&self) -> Option<u64> {
self.creation_update
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum PositionState {
Critical,
Loss,
BreakEven,
MinimumProfit,
Profit,
Closed,
Any,
}
impl PositionState {
fn color(self) -> String {
match self {
PositionState::Any => "blue",
PositionState::Critical | PositionState::Loss => "red",
PositionState::BreakEven => "yellow",
PositionState::MinimumProfit | PositionState::Profit => "green",
PositionState::Closed => "gray",
}
.into()
}
}