added OrderForm struct

This commit is contained in:
Giulio De Pasquale 2021-01-15 10:49:44 +00:00
parent befa1d4bec
commit c754708213

View File

@ -1,4 +1,6 @@
use crate::currency::SymbolPair; use crate::currency::SymbolPair;
use crate::BoxError;
use std::fmt::Display;
/*************** /***************
* Prices * Prices
@ -62,6 +64,79 @@ pub enum OrderKind {
ExchangeIoc, ExchangeIoc,
} }
#[derive(Serialize, Clone)]
pub struct OrderForm {
/// Order Type: LIMIT, EXCHANGE LIMIT, MARKET, EXCHANGE MARKET,
/// STOP, EXCHANGE STOP, STOP LIMIT, EXCHANGE STOP LIMIT,
/// TRAILING STOP, EXCHANGE TRAILING STOP, FOK,
/// EXCHANGE FOK, IOC, EXCHANGE IOC
kind: OrderKind,
/// Symbol for desired pair
symbol: SymbolPair,
/// Price of order
price: String,
/// Amount of order (positive for buy, negative for sell)
amount: String,
/// Set the leverage for a derivative order, supported by derivative symbol orders only.
/// The value should be between 1 and 100 inclusive.
/// The field is optional, if omitted the default leverage value of 10 will be used.
leverage: Option<u32>,
/// The trailing price for a trailing stop order
price_trailing: Option<String>,
/// Auxiliary Limit price (for STOP LIMIT)
price_aux_limit: Option<String>,
/// Time-In-Force: datetime for automatic order cancellation (ie. 2020-01-01 10:45:23) )
tif: Option<String>,
}
impl OrderForm {
pub fn new(symbol: &SymbolPair, price: f64, amount: f64, kind: OrderKind) -> Self {
OrderForm {
kind,
symbol: symbol.clone(),
price: price.to_string(),
amount: amount.to_string(),
leverage: None,
price_trailing: None,
price_aux_limit: None,
tif: None,
}
}
pub fn with_leverage(mut self, leverage: u32) -> Self {
self.leverage = Some(leverage);
self
}
pub fn with_price_trailing(mut self, trailing: f64) -> Result<Self, BoxError> {
match self.kind {
OrderKind::TrailingStop => {
self.price_trailing = Some(trailing.to_string());
Ok(self)
}
_ => Err("Invalid order type.".into()),
}
}
pub fn price_aux_limit(mut self, limit: f64) -> Result<Self, BoxError> {
match self.kind {
OrderKind::StopLimit | OrderKind::ExchangeStopLimit => {
self.price_aux_limit = Some(limit.to_string());
Ok(self)
}
_ => Err("Invalid order type.".into()),
}
}
pub fn with_tif<T: TimeZone>(mut self, tif: DateTime<T>) -> Self
where
T::Offset: Display,
{
self.tif = Some(tif.format("%Y-%m-%d %H:%M:%S").to_string());
self
}
}
/*************** /***************
* Positions * Positions
***************/ ***************/