From 370a57dbb9c6a1955f4c0bdf9438646020ffa24e Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sat, 2 Jan 2021 12:16:48 +0000 Subject: [PATCH] stuff --- rustybot/src/events.rs | 70 +++++++++++++++++++++++++++++++++++++++ rustybot/src/pairs.rs | 3 ++ rustybot/src/positions.rs | 29 ++++++++++++++++ rustybot/src/ticker.rs | 21 ++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 rustybot/src/events.rs create mode 100644 rustybot/src/pairs.rs create mode 100644 rustybot/src/positions.rs create mode 100644 rustybot/src/ticker.rs diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs new file mode 100644 index 0000000..52fae85 --- /dev/null +++ b/rustybot/src/events.rs @@ -0,0 +1,70 @@ +use std::collections::HashMap; +use std::future::Future; + +use tokio::task::JoinHandle; +use crate::BoxError; + +enum SignalKind { + ClosePosition, + OpenPosition, +} + +struct EventMetadata { + position_id: Option, + order_id: Option, +} + +#[derive(PartialEq, Eq, Hash)] +enum EventKind { + NewMinimum, + NewMaximum, + ReachedLoss, + ReachedBreakEven, + ReachedMinProfit, + ReachedGoodProfit, + ReachedMaxLoss, + TrailingStopSet, + TrailingStopMoved, + OrderSubmitted, + NewTick, +} + +struct Event { + kind: EventKind, + tick: u64, + metadata: Option, +} + +impl Event { + pub fn new(kind: EventKind, tick: u64, metadata: Option) -> Self { + Event { + kind, + tick, + metadata, + } + } + + fn has_metadata(&self) -> bool { + self.metadata.is_some() + } +} + +pub struct Dispatcher { + event_handlers: HashMap JoinHandle<()>>> +} + +impl Dispatcher { + pub fn new() -> Self { + Dispatcher { + event_handlers: HashMap::new() + } + } + + pub fn register_event_handler(&mut self, event: EventKind, mut f: F) + where + F: FnMut(String) -> Fut, + Fut: Future + Send, + { + self.event_handlers.insert(event, Box::new(move |args| tokio::spawn(f(args)))); + } +} \ No newline at end of file diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs new file mode 100644 index 0000000..302c0bd --- /dev/null +++ b/rustybot/src/pairs.rs @@ -0,0 +1,3 @@ +struct PairStatus { + +} \ No newline at end of file diff --git a/rustybot/src/positions.rs b/rustybot/src/positions.rs new file mode 100644 index 0000000..d259cb3 --- /dev/null +++ b/rustybot/src/positions.rs @@ -0,0 +1,29 @@ +pub enum PositionState { + Critical, + Loss, + BreakEven, + MinimumProfit, + Profit, +} + +impl PositionState { + fn color(self) -> String { + match self { + PositionState::Critical | PositionState::Loss => { "red" } + PositionState::BreakEven => { "yellow" } + PositionState::MinimumProfit | PositionState::Profit => { "green" } + }.into() + } +} + +// TODO: implement position in bitfinex API before completing this struct +pub struct PositionWrapper { + position: String, + net_profit_loss: f64, + net_profit_loss_percentage: f64, + state: PositionState +} + + + + diff --git a/rustybot/src/ticker.rs b/rustybot/src/ticker.rs new file mode 100644 index 0000000..7bdf318 --- /dev/null +++ b/rustybot/src/ticker.rs @@ -0,0 +1,21 @@ +use tokio::time::{Duration, Instant}; + +pub struct Ticker { + duration: Duration, + start_time: Instant, + current_tick: u64, +} + +impl Ticker { + fn new(duration: Duration) -> Self { + Ticker { + duration, + start_time: Instant::now(), + current_tick: 1, + } + } + + fn inc(&mut self) { + self.current_tick += 1 + } +}