Thank you Clippy!
This commit is contained in:
parent
597dc57bd5
commit
e133092831
@ -208,7 +208,7 @@ impl BitfinexConnector {
|
||||
if e.to_string().contains("nonce: small") {
|
||||
return RetryPolicy::WaitRetry(Duration::from_millis(1));
|
||||
}
|
||||
return RetryPolicy::ForwardError(e);
|
||||
RetryPolicy::ForwardError(e)
|
||||
}
|
||||
|
||||
pub fn new(api_key: &str, api_secret: &str) -> Self {
|
||||
@ -218,14 +218,12 @@ impl BitfinexConnector {
|
||||
}
|
||||
|
||||
fn format_trading_pair(pair: &SymbolPair) -> String {
|
||||
if pair.to_string().to_lowercase().contains("test") {
|
||||
if pair.to_string().to_lowercase().contains("test")
|
||||
|| pair.to_string().to_lowercase().contains("f0")
|
||||
{
|
||||
format!("{}:{}", pair.base(), pair.quote())
|
||||
} else {
|
||||
if pair.to_string().to_lowercase().contains("f0") {
|
||||
format!("{}:{}", pair.base(), pair.quote())
|
||||
} else {
|
||||
format!("{}{}", pair.base(), pair.quote())
|
||||
}
|
||||
format!("{}{}", pair.base(), pair.quote())
|
||||
}
|
||||
}
|
||||
|
||||
@ -736,7 +734,7 @@ impl From<&bitfinex::responses::TradeResponse> for Trade {
|
||||
price: response.execution_price(),
|
||||
amount: response.execution_amount(),
|
||||
fee,
|
||||
fee_currency: Symbol::new(response.symbol().to_owned().clone()),
|
||||
fee_currency: Symbol::new(response.symbol().to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,12 +44,9 @@ impl PriceManager {
|
||||
}
|
||||
|
||||
pub async fn handle_message(&mut self, message: ActorMessage) -> Result<(), BoxError> {
|
||||
match message.message {
|
||||
Message::Update { tick } => {
|
||||
let a = self.update(tick).await?;
|
||||
self.add_entry(a);
|
||||
}
|
||||
_ => {}
|
||||
if let Message::Update { tick } = message.message {
|
||||
let a = self.update(tick).await?;
|
||||
self.add_entry(a);
|
||||
}
|
||||
|
||||
Ok(message
|
||||
@ -247,11 +244,7 @@ impl PositionManager {
|
||||
|
||||
Some(positions) => {
|
||||
// checking if there are positions open for our pair
|
||||
match positions
|
||||
.into_iter()
|
||||
.filter(|x| x.pair() == &self.pair)
|
||||
.next()
|
||||
{
|
||||
match positions.into_iter().find(|x| x.pair() == &self.pair) {
|
||||
// no open positions for our pair, setting active position to none
|
||||
None => {
|
||||
self.active_position = None;
|
||||
@ -300,10 +293,7 @@ impl PositionManager {
|
||||
None => self.current_tick() - 1,
|
||||
};
|
||||
|
||||
self.positions_history
|
||||
.get(&tick)
|
||||
.filter(|x| x.id() == id)
|
||||
.and_then(|x| Some(x))
|
||||
self.positions_history.get(&tick).filter(|x| x.id() == id)
|
||||
}
|
||||
}
|
||||
|
||||
@ -421,13 +411,14 @@ impl OrderManager {
|
||||
if let Some(position) = open_positions.into_iter().find(|x| x.id() == position_id) {
|
||||
let opt_position_order = open_orders
|
||||
.iter()
|
||||
.find(|x| x.details.amount().neg() == position.amount());
|
||||
// avoid using direct equality, using error margin instead
|
||||
.find(|x| (x.details.amount().neg() - position.amount()).abs() < 0.0000001);
|
||||
|
||||
// checking if the position has an open order.
|
||||
// If so, don't do anything since the order is taken care of
|
||||
// in the update phase.
|
||||
// If no order is open, send an undercut limit order at the best current price.
|
||||
if let None = opt_position_order {
|
||||
if opt_position_order.is_none() {
|
||||
// No open order, undercutting best price with limit order
|
||||
let closing_price = self.best_closing_price(&position, &order_book);
|
||||
|
||||
@ -509,26 +500,22 @@ impl OrderManager {
|
||||
let delta = (ask - bid) / 10.0;
|
||||
|
||||
let closing_price = {
|
||||
let closing_price = {
|
||||
if position.is_short() {
|
||||
bid - delta
|
||||
} else {
|
||||
ask + delta
|
||||
}
|
||||
};
|
||||
|
||||
if avg > 9999.0 {
|
||||
if position.is_short() {
|
||||
closing_price.ceil()
|
||||
} else {
|
||||
closing_price.floor()
|
||||
}
|
||||
if position.is_short() {
|
||||
bid - delta
|
||||
} else {
|
||||
closing_price
|
||||
ask + delta
|
||||
}
|
||||
};
|
||||
|
||||
closing_price
|
||||
if avg > 9999.0 {
|
||||
if position.is_short() {
|
||||
closing_price.ceil()
|
||||
} else {
|
||||
closing_price.floor()
|
||||
}
|
||||
} else {
|
||||
closing_price
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -550,8 +537,8 @@ impl PairManager {
|
||||
Box::new(FastOrderStrategy::default()),
|
||||
),
|
||||
position_manager: PositionManagerHandle::new(
|
||||
pair.clone(),
|
||||
client.clone(),
|
||||
pair,
|
||||
client,
|
||||
Box::new(TrailingStop::new()),
|
||||
),
|
||||
}
|
||||
@ -578,11 +565,8 @@ impl PairManager {
|
||||
// TODO: to move into Handler?
|
||||
if let Some(messages) = messages {
|
||||
for m in messages {
|
||||
match m {
|
||||
Message::ClosePosition { position_id } => {
|
||||
self.order_manager.close_position(position_id).await?;
|
||||
}
|
||||
_ => {}
|
||||
if let Message::ClosePosition { position_id } = m {
|
||||
self.order_manager.close_position(position_id).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -594,21 +578,19 @@ impl PairManager {
|
||||
pub struct ExchangeManager {
|
||||
kind: ExchangeDetails,
|
||||
pair_managers: Vec<PairManager>,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl ExchangeManager {
|
||||
pub fn new(kind: &ExchangeDetails, pairs: &Vec<SymbolPair>) -> Self {
|
||||
pub fn new(kind: &ExchangeDetails, pairs: &[SymbolPair]) -> Self {
|
||||
let client = Client::new(kind);
|
||||
let pair_managers = pairs
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|x| PairManager::new(x.clone(), client.clone()))
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
kind: kind.clone(),
|
||||
pair_managers,
|
||||
client,
|
||||
}
|
||||
}
|
||||
|
||||
@ -620,7 +602,7 @@ impl ExchangeManager {
|
||||
.collect();
|
||||
|
||||
// execute the futures
|
||||
while let Some(_) = futures.next().await {}
|
||||
while futures.next().await.is_some() {}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -139,18 +139,6 @@ impl OrderDetails {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exchange(&self) -> Exchange {
|
||||
self.exchange
|
||||
}
|
||||
pub fn platform(&self) -> TradingPlatform {
|
||||
self.platform
|
||||
}
|
||||
pub fn kind(&self) -> OrderKind {
|
||||
self.kind
|
||||
}
|
||||
pub fn execution_timestamp(&self) -> u64 {
|
||||
self.execution_timestamp
|
||||
}
|
||||
pub fn id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
@ -496,17 +484,6 @@ pub enum PositionProfitState {
|
||||
Profit,
|
||||
}
|
||||
|
||||
impl PositionProfitState {
|
||||
fn color(self) -> String {
|
||||
match self {
|
||||
PositionProfitState::Critical | PositionProfitState::Loss => "red",
|
||||
PositionProfitState::BreakEven => "yellow",
|
||||
PositionProfitState::MinimumProfit | PositionProfitState::Profit => "green",
|
||||
}
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
||||
pub enum PositionState {
|
||||
Closed,
|
||||
|
@ -7,11 +7,7 @@ use log::info;
|
||||
use crate::connectors::Connector;
|
||||
use crate::events::{Event, EventKind, EventMetadata, Message};
|
||||
use crate::managers::OptionUpdate;
|
||||
|
||||
use crate::models::{
|
||||
ActiveOrder, OrderBook, OrderBookEntry, OrderForm, OrderKind, Position, PositionProfitState,
|
||||
PositionState, TradingFees, TradingPlatform,
|
||||
};
|
||||
use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState};
|
||||
use crate::BoxError;
|
||||
|
||||
/***************
|
||||
@ -159,9 +155,9 @@ impl PositionStrategy for TrailingStop {
|
||||
&& pl_perc < TrailingStop::GOOD_PROFIT_PERC
|
||||
{
|
||||
PositionProfitState::MinimumProfit
|
||||
} else if 0.0 <= pl_perc && pl_perc < TrailingStop::MIN_PROFIT_PERC {
|
||||
} else if (0.0..TrailingStop::MIN_PROFIT_PERC).contains(&pl_perc) {
|
||||
PositionProfitState::BreakEven
|
||||
} else if TrailingStop::MAX_LOSS_PERC < pl_perc && pl_perc < 0.0 {
|
||||
} else if (TrailingStop::MAX_LOSS_PERC..0.0).contains(&pl_perc) {
|
||||
PositionProfitState::Loss
|
||||
} else {
|
||||
PositionProfitState::Critical
|
||||
@ -170,7 +166,7 @@ impl PositionStrategy for TrailingStop {
|
||||
|
||||
let opt_prev_position = positions_history.get(&(current_tick - 1));
|
||||
let event_metadata = EventMetadata::new(Some(position.id()), None);
|
||||
let new_position = position.clone().with_profit_state(Some(state));
|
||||
let new_position = position.with_profit_state(Some(state));
|
||||
|
||||
match opt_prev_position {
|
||||
Some(prev) => {
|
||||
@ -219,7 +215,7 @@ impl PositionStrategy for TrailingStop {
|
||||
events
|
||||
};
|
||||
|
||||
return (new_position, Some(events), None);
|
||||
(new_position, Some(events), None)
|
||||
}
|
||||
|
||||
fn post_tick(
|
||||
@ -233,14 +229,9 @@ impl PositionStrategy for TrailingStop {
|
||||
};
|
||||
|
||||
// if critical, early return with close position
|
||||
if let Some(profit_state) = position.profit_state() {
|
||||
match profit_state {
|
||||
PositionProfitState::Critical => {
|
||||
info!("Maximum loss reached. Closing position.");
|
||||
return (position, None, Some(vec![close_message]));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Some(PositionProfitState::Critical) = position.profit_state() {
|
||||
info!("Maximum loss reached. Closing position.");
|
||||
return (position, None, Some(vec![close_message]));
|
||||
};
|
||||
|
||||
// let's check if we surpassed an existing stop percentage
|
||||
@ -270,12 +261,6 @@ impl Default for FastOrderStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
impl FastOrderStrategy {
|
||||
pub fn new(threshold: f64) -> Self {
|
||||
Self { threshold }
|
||||
}
|
||||
}
|
||||
|
||||
impl OrderStrategy for FastOrderStrategy {
|
||||
fn name(&self) -> String {
|
||||
"Fast order strategy".into()
|
||||
@ -310,7 +295,7 @@ impl OrderStrategy for FastOrderStrategy {
|
||||
order: OrderForm::new(
|
||||
order.symbol.clone(),
|
||||
OrderKind::Market {},
|
||||
order.details.platform().clone(),
|
||||
*order.details.platform(),
|
||||
order.details.amount(),
|
||||
),
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user