diff --git a/src/account.rs b/src/account.rs index 370f7a0b..cdd71a98 100644 --- a/src/account.rs +++ b/src/account.rs @@ -1,6 +1,6 @@ use error_chain::bail; -use crate::util::build_signed_request; +use crate::util::{build_signed_request, is_start_time_valid}; use crate::model::{ AccountInformation, Balance, Empty, Order, OrderCanceled, TradeHistory, Transaction, }; @@ -754,6 +754,49 @@ impl Account { .get_signed(API::Spot(Spot::MyTrades), Some(request)) } + // Trade history starting from selected date + pub fn trade_history_from(&self, symbol: S, start_time: u64) -> Result> + where + S: Into, + { + if !is_start_time_valid(&start_time) { + return bail!("Start time should be less than the current time"); + } + + let mut parameters: BTreeMap = BTreeMap::new(); + parameters.insert("symbol".into(), symbol.into()); + parameters.insert("startTime".into(), start_time.to_string()); + let request = build_signed_request(parameters, self.recv_window)?; + self.client + .get_signed(API::Spot(Spot::MyTrades), Some(request)) + } + + // Trade history starting from selected time to some time + pub fn trade_history_from_to(&self, symbol: S, start_time: u64, end_time: u64) -> Result> + where + S: Into, + { + if end_time <= start_time { + return bail!("End time should be greater than start time"); + } + if !is_start_time_valid(&start_time) { + return bail!("Start time should be less than the current time"); + } + self.get_trades(symbol, start_time, end_time) + } + + fn get_trades(&self, symbol: S, start_time: u64, end_time: u64) -> Result> + where + S: Into, + { + let mut trades = match self.trade_history_from(symbol, start_time) { + Ok(trades) => trades, + Err(e) => return Err(e), + }; + trades.retain(|trade| trade.time <= end_time); + Ok(trades) + } + fn build_order(&self, order: OrderRequest) -> BTreeMap { let mut order_parameters: BTreeMap = BTreeMap::new(); diff --git a/src/util.rs b/src/util.rs index 5761bf4c..cd1ea1ba 100644 --- a/src/util.rs +++ b/src/util.rs @@ -45,3 +45,13 @@ fn get_timestamp(start: SystemTime) -> Result { let since_epoch = start.duration_since(UNIX_EPOCH)?; Ok(since_epoch.as_secs() * 1000 + u64::from(since_epoch.subsec_nanos()) / 1_000_000) } + +pub fn is_start_time_valid(start_time: &u64) -> bool { + let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); + + if start_time > ¤t_time { + false + } else { + true + } +} \ No newline at end of file