From dcc12934550b6e6ee68ba27c7c3c646395ee749c Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Thu, 14 Jan 2021 12:53:54 +0000 Subject: [PATCH] moved price manager from pairs into managers --- rustybot/src/bot.rs | 5 +- rustybot/src/events.rs | 2 +- rustybot/src/managers.rs | 141 ++++++++++++++++++++++++++++++++++++- rustybot/src/pairs.rs | 145 --------------------------------------- rustybot/src/strategy.rs | 2 +- 5 files changed, 143 insertions(+), 152 deletions(-) delete mode 100644 rustybot/src/pairs.rs diff --git a/rustybot/src/bot.rs b/rustybot/src/bot.rs index 6743af7..8e92842 100644 --- a/rustybot/src/bot.rs +++ b/rustybot/src/bot.rs @@ -5,17 +5,16 @@ use bitfinex::api::Bitfinex; use bitfinex::positions::Position; use bitfinex::ticker::TradingPairTicker; use futures_util::stream::FuturesUnordered; +use tokio::stream::StreamExt; use tokio::time::delay_for; use crate::connectors::{Client, Connector, ExchangeKind}; use crate::currency::{Symbol, SymbolPair}; use crate::events::{Event, EventKind}; -use crate::managers::{OrderManager, PositionManager}; -use crate::pairs::PriceManager; +use crate::managers::{OrderManager, PositionManager, PriceManager}; use crate::strategy::PositionStrategy; use crate::ticker::Ticker; use crate::BoxError; -use tokio::stream::StreamExt; pub struct BfxBot { ticker: Ticker, diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 1fdfa14..6bff8cc 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -5,8 +5,8 @@ use tokio::stream::StreamExt; use tokio::task::JoinHandle; use crate::bot::BfxBot; +use crate::managers::PriceManager; use crate::models::{Position, PositionProfitState}; -use crate::pairs::PriceManager; use crate::BoxError; #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] diff --git a/rustybot/src/managers.rs b/rustybot/src/managers.rs index 8aa16f2..e9a2721 100644 --- a/rustybot/src/managers.rs +++ b/rustybot/src/managers.rs @@ -1,15 +1,152 @@ use std::collections::{HashMap, VecDeque}; use crate::connectors::{Client, Connector}; -use crate::events::Event; -use crate::models::{Order, Position}; +use crate::currency::SymbolPair; +use crate::events::{Event, EventDispatcher, SignalKind}; +use crate::models::{Order, Position, PriceTicker}; use crate::strategy::PositionStrategy; use crate::ticker::Ticker; +use crate::BoxError; pub struct EventManager { events: Vec, } +#[derive(Clone, Debug)] +pub struct PriceManager { + pair: SymbolPair, + prices: Vec, + client: Client, +} + +impl PriceManager { + pub fn new(pair: SymbolPair, client: Client) -> Self { + PriceManager { + pair, + prices: Vec::new(), + client, + } + } + + pub fn add_entry(&mut self, entry: PriceEntry) { + self.prices.push(entry); + } + + pub async fn update(&mut self, tick: u64) -> Result { + let current_prices = self.client.current_prices(&self.pair).await?.into(); + + Ok(PriceEntry::new( + tick, + current_prices, + self.pair.clone(), + None, + None, + )) + } + + // pub fn add_position(&mut self, position: Position) { + // let (new_position, events, signals) = { + // match &self.strategy { + // Some(strategy) => strategy.on_new_tick(&position, &self), + // None => (position, vec![], vec![]), + // } + // }; + // + // self.positions + // .entry(self.current_tick) + // .or_default() + // .push(new_position.clone()); + // + // // calling position state callbacks + // self.dispatcher + // .call_position_state_handlers(&new_position, &self); + // + // // adding events and calling callbacks + // for e in events { + // self.add_event(e); + // } + // + // // adding signals to current tick vector + // for s in signals { + // self.add_signal(s); + // } + // } + + // fn add_event(&mut self, event: Event) { + // self.events.push(event); + // + // self.dispatcher.call_event_handlers(&event, &self); + // } + // + // fn add_signal(&mut self, signal: SignalKind) { + // self.signals.insert(self.current_tick(), signal); + // } + + pub fn pair(&self) -> &SymbolPair { + &self.pair + } + + // pub fn position_previous_tick(&self, id: u64, tick: Option) -> Option<&Position> { + // let tick = match tick { + // Some(tick) => { + // if tick < 1 { + // 1 + // } else { + // tick + // } + // } + // None => self.current_tick() - 1, + // }; + // + // self.positions + // .get(&tick) + // .and_then(|x| x.iter().find(|x| x.position_id() == id)) + // } +} + +#[derive(Clone, Debug)] +pub struct PriceEntry { + tick: u64, + pair: SymbolPair, + price: PriceTicker, + events: Option>, + signals: Option>, +} + +impl PriceEntry { + pub fn new( + tick: u64, + price: PriceTicker, + pair: SymbolPair, + events: Option>, + signals: Option>, + ) -> Self { + PriceEntry { + tick, + pair, + price, + events, + signals, + } + } + + pub fn tick(&self) -> u64 { + self.tick + } + pub fn pair(&self) -> &SymbolPair { + &self.pair + } + pub fn price(&self) -> PriceTicker { + self.price + } + pub fn events(&self) -> &Option> { + &self.events + } + pub fn signals(&self) -> &Option> { + &self.signals + } +} + pub struct PositionManager { queue: VecDeque, open_positions: Vec, diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs deleted file mode 100644 index 5ca22d6..0000000 --- a/rustybot/src/pairs.rs +++ /dev/null @@ -1,145 +0,0 @@ -use std::collections::HashMap; - -// use crate::strategy::PositionStrategy; -use crate::connectors::Client; -use crate::currency::SymbolPair; -use crate::events::{Event, EventDispatcher, SignalKind}; -use crate::models::{Order, Position, PriceTicker}; -use crate::BoxError; - -#[derive(Clone, Debug)] -pub struct PriceManager { - pair: SymbolPair, - prices: Vec, - client: Client, -} - -#[derive(Clone, Debug)] -pub struct PriceEntry { - tick: u64, - pair: SymbolPair, - price: PriceTicker, - events: Option>, - signals: Option>, -} - -impl PriceEntry { - pub fn tick(&self) -> u64 { - self.tick - } - pub fn pair(&self) -> &SymbolPair { - &self.pair - } - pub fn price(&self) -> PriceTicker { - self.price - } - pub fn events(&self) -> &Option> { - &self.events - } - pub fn signals(&self) -> &Option> { - &self.signals - } -} - -impl PriceEntry { - pub fn new( - tick: u64, - price: PriceTicker, - pair: SymbolPair, - events: Option>, - signals: Option>, - ) -> Self { - PriceEntry { - tick, - pair, - price, - events, - signals, - } - } -} - -impl PriceManager { - pub fn new(pair: SymbolPair, client: Client) -> Self { - PriceManager { - pair, - prices: Vec::new(), - client, - } - } - - pub fn add_entry(&mut self, entry: PriceEntry) { - self.prices.push(entry); - } - - pub async fn update(&mut self, tick: u64) -> Result { - let current_prices = self.client.current_prices(&self.pair).await?.into(); - - Ok(PriceEntry::new( - tick, - current_prices, - self.pair.clone(), - None, - None, - )) - } - - // pub fn add_position(&mut self, position: Position) { - // let (new_position, events, signals) = { - // match &self.strategy { - // Some(strategy) => strategy.on_new_tick(&position, &self), - // None => (position, vec![], vec![]), - // } - // }; - // - // self.positions - // .entry(self.current_tick) - // .or_default() - // .push(new_position.clone()); - // - // // calling position state callbacks - // self.dispatcher - // .call_position_state_handlers(&new_position, &self); - // - // // adding events and calling callbacks - // for e in events { - // self.add_event(e); - // } - // - // // adding signals to current tick vector - // for s in signals { - // self.add_signal(s); - // } - // } - - // fn add_event(&mut self, event: Event) { - // self.events.push(event); - // - // self.dispatcher.call_event_handlers(&event, &self); - // } - // - // fn add_signal(&mut self, signal: SignalKind) { - // self.signals.insert(self.current_tick(), signal); - // } - - pub fn pair(&self) -> &SymbolPair { - &self.pair - } - - // pub fn position_previous_tick(&self, id: u64, tick: Option) -> Option<&Position> { - // let tick = match tick { - // Some(tick) => { - // if tick < 1 { - // 1 - // } else { - // tick - // } - // } - // None => self.current_tick() - 1, - // }; - // - // self.positions - // .get(&tick) - // .and_then(|x| x.iter().find(|x| x.position_id() == id)) - // } -} diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index ed9589c..23ed3cf 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use crate::events::{Event, EventKind, EventMetadata, SignalKind}; +use crate::managers::PriceManager; use crate::models::{Position, PositionProfitState}; -use crate::pairs::PriceManager; use dyn_clone::DynClone; pub trait PositionStrategy: DynClone {