general cleanup
This commit is contained in:
parent
7de2a6ad77
commit
bb5d1328d6
@ -11,8 +11,6 @@ use crate::BoxError;
|
|||||||
|
|
||||||
pub struct BfxBot {
|
pub struct BfxBot {
|
||||||
ticker: Ticker,
|
ticker: Ticker,
|
||||||
quote: Symbol,
|
|
||||||
trading_symbols: Vec<Symbol>,
|
|
||||||
exchange_managers: Vec<ExchangeManager>,
|
exchange_managers: Vec<ExchangeManager>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,8 +33,6 @@ impl BfxBot {
|
|||||||
|
|
||||||
BfxBot {
|
BfxBot {
|
||||||
ticker: Ticker::new(tick_duration),
|
ticker: Ticker::new(tick_duration),
|
||||||
quote,
|
|
||||||
trading_symbols,
|
|
||||||
exchange_managers,
|
exchange_managers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,7 @@ impl Client {
|
|||||||
let (best_ask, best_bid) = (order_book.lowest_ask(), order_book.highest_bid());
|
let (best_ask, best_bid) = (order_book.lowest_ask(), order_book.highest_bid());
|
||||||
|
|
||||||
// updating positions with effective profit/loss
|
// updating positions with effective profit/loss
|
||||||
|
// TODO: change fee with account's taker fee
|
||||||
positions.iter_mut().flatten().for_each(|x| {
|
positions.iter_mut().flatten().for_each(|x| {
|
||||||
if x.is_short() {
|
if x.is_short() {
|
||||||
x.update_profit_loss(best_ask, 0.2);
|
x.update_profit_loss(best_ask, 0.2);
|
||||||
@ -117,9 +118,6 @@ impl Debug for dyn Connector {
|
|||||||
|
|
||||||
pub struct BitfinexConnector {
|
pub struct BitfinexConnector {
|
||||||
bfx: Bitfinex,
|
bfx: Bitfinex,
|
||||||
affiliate_code: Option<String>,
|
|
||||||
// account_info: String,
|
|
||||||
// ledger: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BitfinexConnector {
|
impl BitfinexConnector {
|
||||||
@ -128,7 +126,6 @@ impl BitfinexConnector {
|
|||||||
pub fn new(api_key: &str, api_secret: &str) -> Self {
|
pub fn new(api_key: &str, api_secret: &str) -> Self {
|
||||||
BitfinexConnector {
|
BitfinexConnector {
|
||||||
bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())),
|
bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())),
|
||||||
affiliate_code: Some(BitfinexConnector::AFFILIATE_CODE.into()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,6 @@ pub enum EventKind {
|
|||||||
TrailingStopMoved,
|
TrailingStopMoved,
|
||||||
OrderSubmitted,
|
OrderSubmitted,
|
||||||
NewTick,
|
NewTick,
|
||||||
Any,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
@ -66,124 +65,4 @@ impl Event {
|
|||||||
fn has_metadata(&self) -> bool {
|
fn has_metadata(&self) -> bool {
|
||||||
self.metadata.is_some()
|
self.metadata.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn kind(&self) -> EventKind {
|
|
||||||
self.kind
|
|
||||||
}
|
|
||||||
pub fn tick(&self) -> u64 {
|
|
||||||
self.tick
|
|
||||||
}
|
|
||||||
pub fn metadata(&self) -> Option<EventMetadata> {
|
|
||||||
self.metadata
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// pub struct Dispatcher {
|
|
||||||
// event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PriceManager) -> JoinHandle<()>>>>,
|
|
||||||
// profit_state_handlers:
|
|
||||||
// HashMap<PositionProfitState, Vec<Box<dyn Fn(&Position, &PriceManager) -> JoinHandle<()>>>>,
|
|
||||||
// signal_handlers: HashMap<SignalKind, Vec<Box<dyn Fn(&SignalKind) -> JoinHandle<()>>>>,
|
|
||||||
//
|
|
||||||
// on_any_event_handlers: Vec<Box<dyn Fn(&Event, &PriceManager) -> JoinHandle<()>>>,
|
|
||||||
// on_any_profit_state_handlers: Vec<Box<dyn Fn(&Position, &PriceManager) -> JoinHandle<()>>>,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// impl Dispatcher {
|
|
||||||
// pub fn new() -> Self {
|
|
||||||
// Dispatcher {
|
|
||||||
// event_handlers: HashMap::new(),
|
|
||||||
// profit_state_handlers: HashMap::new(),
|
|
||||||
// signal_handlers: HashMap::new(),
|
|
||||||
// on_any_event_handlers: Vec::new(),
|
|
||||||
// on_any_profit_state_handlers: Vec::new(),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn call_signal_handlers(&self, signal: &SignalKind) {
|
|
||||||
// if let Some(functions) = self.signal_handlers.get(&signal) {
|
|
||||||
// for f in functions {
|
|
||||||
// f(signal);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn call_event_handlers(&self, event: &Event, status: &PriceManager) {
|
|
||||||
// if let Some(functions) = self.event_handlers.get(&event.kind()) {
|
|
||||||
// for f in functions {
|
|
||||||
// f(event, status);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// for f in &self.on_any_event_handlers {
|
|
||||||
// f(event, status);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn call_position_state_handlers(&self, position: &Position, status: &PriceManager) {
|
|
||||||
// if let Some(profit_state) = &position.profit_state() {
|
|
||||||
// if let Some(functions) = self.profit_state_handlers.get(profit_state) {
|
|
||||||
// for f in functions {
|
|
||||||
// f(position, status);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// for f in &self.on_any_profit_state_handlers {
|
|
||||||
// f(position, status);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
|
|
||||||
// where
|
|
||||||
// F: Fn(&Event, &PriceManager) -> Fut,
|
|
||||||
// Fut: Future<Output = ()> + Send,
|
|
||||||
// {
|
|
||||||
// match event {
|
|
||||||
// EventKind::Any => self
|
|
||||||
// .on_any_event_handlers
|
|
||||||
// .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))),
|
|
||||||
// _ => self
|
|
||||||
// .event_handlers
|
|
||||||
// .entry(event)
|
|
||||||
// .or_default()
|
|
||||||
// .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn register_positionstate_handler<F: 'static, Fut: 'static>(
|
|
||||||
// &mut self,
|
|
||||||
// state: PositionProfitState,
|
|
||||||
// f: F,
|
|
||||||
// ) where
|
|
||||||
// F: Fn(&Position, &PriceManager) -> Fut,
|
|
||||||
// Fut: Future<Output = ()> + Send,
|
|
||||||
// {
|
|
||||||
// match state {
|
|
||||||
// // PositionProfitState::Any => self
|
|
||||||
// // .on_any_position_state_handlers
|
|
||||||
// // .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
|
|
||||||
// _ => self
|
|
||||||
// .profit_state_handlers
|
|
||||||
// .entry(state)
|
|
||||||
// .or_default()
|
|
||||||
// .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub fn register_signal_handler<F: 'static, Fut: 'static>(&mut self, signal: SignalKind, f: F)
|
|
||||||
// where
|
|
||||||
// F: Fn(&SignalKind) -> Fut,
|
|
||||||
// Fut: Future<Output = ()> + Send,
|
|
||||||
// {
|
|
||||||
// match signal {
|
|
||||||
// // PositionProfitState::Any => self
|
|
||||||
// // .on_any_position_state_handlers
|
|
||||||
// // .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))),
|
|
||||||
// _ => self
|
|
||||||
// .signal_handlers
|
|
||||||
// .entry(signal)
|
|
||||||
// .or_default()
|
|
||||||
// .push(Box::new(move |s| tokio::spawn(f(s)))),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
@ -85,9 +85,10 @@ impl PriceManager {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
message.respond_to.send((None, None));
|
Ok(message
|
||||||
|
.respond_to
|
||||||
Ok(())
|
.send((None, None))
|
||||||
|
.map_err(|_| BoxError::from("Could not send message."))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_entry(&mut self, entry: PriceEntry) {
|
pub fn add_entry(&mut self, entry: PriceEntry) {
|
||||||
@ -224,9 +225,11 @@ impl PositionManager {
|
|||||||
pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> {
|
pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> {
|
||||||
match msg.message {
|
match msg.message {
|
||||||
Message::Update { tick } => {
|
Message::Update { tick } => {
|
||||||
let result = self.update(tick).await?;
|
self.update(tick).await?;
|
||||||
|
|
||||||
msg.respond_to.send(result);
|
msg.respond_to
|
||||||
|
.send((None, None))
|
||||||
|
.map_err(|_| BoxError::from("Could not send message."))?;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
@ -397,9 +400,10 @@ impl OrderManager {
|
|||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
msg.respond_to.send((None, None));
|
Ok(msg
|
||||||
|
.respond_to
|
||||||
Ok(())
|
.send((None, None))
|
||||||
|
.map_err(|_| BoxError::from("Could not send message."))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn close_position(&mut self, position_id: u64) -> Result<(), BoxError> {
|
pub async fn close_position(&mut self, position_id: u64) -> Result<(), BoxError> {
|
||||||
|
@ -73,8 +73,6 @@ impl TrailingStop {
|
|||||||
const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5;
|
const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5;
|
||||||
const MAX_LOSS_PERC: f64 = -0.5;
|
const MAX_LOSS_PERC: f64 = -0.5;
|
||||||
|
|
||||||
const TAKER_FEE: f64 = 0.2;
|
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TrailingStop {
|
TrailingStop {
|
||||||
stop_percentages: HashMap::new(),
|
stop_percentages: HashMap::new(),
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
use tokio::time::{Duration, Instant};
|
use tokio::time::{Duration};
|
||||||
|
|
||||||
pub struct Ticker {
|
pub struct Ticker {
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
start_time: Instant,
|
|
||||||
current_tick: u64,
|
current_tick: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -10,7 +9,6 @@ impl Ticker {
|
|||||||
pub fn new(duration: Duration) -> Self {
|
pub fn new(duration: Duration) -> Self {
|
||||||
Ticker {
|
Ticker {
|
||||||
duration,
|
duration,
|
||||||
start_time: Instant::now(),
|
|
||||||
current_tick: 1,
|
current_tick: 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -18,13 +16,9 @@ impl Ticker {
|
|||||||
pub fn inc(&mut self) {
|
pub fn inc(&mut self) {
|
||||||
self.current_tick += 1
|
self.current_tick += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn duration(&self) -> Duration {
|
pub fn duration(&self) -> Duration {
|
||||||
self.duration
|
self.duration
|
||||||
}
|
}
|
||||||
pub fn start_time(&self) -> Instant {
|
|
||||||
self.start_time
|
|
||||||
}
|
|
||||||
pub fn current_tick(&self) -> u64 {
|
pub fn current_tick(&self) -> u64 {
|
||||||
self.current_tick
|
self.current_tick
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user