From d4e388154d672b86fa4e4efe0b4114c85a068cbb Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sat, 2 Jan 2021 14:10:16 +0000 Subject: [PATCH] stuff --- rustybot/Cargo.lock | 99 +++++++++++++-------------------------- rustybot/Cargo.toml | 2 +- rustybot/src/events.rs | 23 +++++---- rustybot/src/main.rs | 11 ++++- rustybot/src/pairs.rs | 29 +++++++++++- rustybot/src/positions.rs | 22 +++++++-- rustybot/src/strategy.rs | 8 ++++ 7 files changed, 112 insertions(+), 82 deletions(-) create mode 100644 rustybot/src/strategy.rs diff --git a/rustybot/Cargo.lock b/rustybot/Cargo.lock index a10ac48..a320dd2 100644 --- a/rustybot/Cargo.lock +++ b/rustybot/Cargo.lock @@ -516,15 +516,6 @@ dependencies = [ "bytes 0.5.6", ] -[[package]] -name = "instant" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" -dependencies = [ - "cfg-if 1.0.0", -] - [[package]] name = "iovec" version = "0.1.4" @@ -577,15 +568,6 @@ version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" -[[package]] -name = "lock_api" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" -dependencies = [ - "scopeguard", -] - [[package]] name = "log" version = "0.3.9" @@ -674,6 +656,29 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "mio-named-pipes" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" +dependencies = [ + "log 0.4.11", + "mio 0.6.23", + "miow 0.3.6", + "winapi 0.3.9", +] + +[[package]] +name = "mio-uds" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" +dependencies = [ + "iovec", + "libc", + "mio 0.6.23", +] + [[package]] name = "miow" version = "0.2.2" @@ -801,31 +806,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "parking_lot" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" -dependencies = [ - "instant", - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ccb628cad4f84851442432c60ad8e1f607e29752d0bf072cbd0baf28aa34272" -dependencies = [ - "cfg-if 1.0.0", - "instant", - "libc", - "redox_syscall", - "smallvec", - "winapi 0.3.9", -] - [[package]] name = "percent-encoding" version = "2.1.0" @@ -1038,7 +1018,7 @@ version = "0.1.0" dependencies = [ "bitfinex", "futures-util", - "tokio 0.3.6", + "tokio 0.2.24", "tokio-tungstenite", ] @@ -1058,12 +1038,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "scopeguard" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" - [[package]] name = "security-framework" version = "2.0.0" @@ -1167,12 +1141,6 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" -[[package]] -name = "smallvec" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae524f056d7d770e174287294f562e95044c68e88dec909a00d2094805db9d75" - [[package]] name = "socket2" version = "0.3.19" @@ -1241,10 +1209,17 @@ dependencies = [ "futures-core", "iovec", "lazy_static", + "libc", "memchr", "mio 0.6.23", + "mio-named-pipes", + "mio-uds", + "num_cpus", "pin-project-lite 0.1.11", + "signal-hook-registry", "slab", + "tokio-macros", + "winapi 0.3.9", ] [[package]] @@ -1255,25 +1230,17 @@ checksum = "720ba21c25078711bf456d607987d95bce90f7c3bea5abe1db587862e7a1e87c" dependencies = [ "autocfg", "bytes 0.6.0", - "futures-core", "libc", "memchr", "mio 0.7.7", - "num_cpus", - "once_cell", - "parking_lot", "pin-project-lite 0.2.0", - "signal-hook-registry", - "slab", - "tokio-macros", - "winapi 0.3.9", ] [[package]] name = "tokio-macros" -version = "0.3.2" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46dfffa59fc3c8aad216ed61bdc2c263d2b9d87a9c8ac9de0c11a813e51b6db7" +checksum = "e44da00bfc73a25f814cd8d7e57a68a5c31b74b3152a0a1d1f590c97ed06265a" dependencies = [ "proc-macro2", "quote", diff --git a/rustybot/Cargo.toml b/rustybot/Cargo.toml index c4e7e80..06577b5 100644 --- a/rustybot/Cargo.toml +++ b/rustybot/Cargo.toml @@ -8,6 +8,6 @@ edition = "2018" [dependencies] bitfinex = { path= "/home/giulio/dev/bitfinex-rs" } -tokio = { version = "0.3", features=["full"]} +tokio = { version = "0.2", features=["full"]} tokio-tungstenite = "*" futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] } \ No newline at end of file diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 52fae85..0cd7eed 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::future::Future; use tokio::task::JoinHandle; + use crate::BoxError; enum SignalKind { @@ -29,7 +30,7 @@ enum EventKind { NewTick, } -struct Event { +pub struct Event { kind: EventKind, tick: u64, metadata: Option, @@ -49,21 +50,27 @@ impl Event { } } -pub struct Dispatcher { - event_handlers: HashMap JoinHandle<()>>> +pub struct EventDispatcher { + event_handlers: HashMap JoinHandle<()>>> } -impl Dispatcher { +impl EventDispatcher { pub fn new() -> Self { - Dispatcher { + EventDispatcher { event_handlers: HashMap::new() } } - pub fn register_event_handler(&mut self, event: EventKind, mut f: F) + pub fn call_handler(&self, event: &EventKind) { + self.event_handlers.iter() + .filter(|(e, _)| e == event) + .map(|(_, f)| f("test".into())); + } + + pub fn register_event_handler(&mut self, event: EventKind, f: F) where - F: FnMut(String) -> Fut, - Fut: Future + Send, + F: Fn(String) -> Fut, + Fut: Future + Send, { self.event_handlers.insert(event, Box::new(move |args| tokio::spawn(f(args)))); } diff --git a/rustybot/src/main.rs b/rustybot/src/main.rs index bfc351e..914da6e 100644 --- a/rustybot/src/main.rs +++ b/rustybot/src/main.rs @@ -1,12 +1,21 @@ +use bitfinex::api::Bitfinex; + mod ticker; -mod positions; mod events; mod pairs; +mod positions; +mod strategy; pub type BoxError = Box; #[tokio::main] async fn main() -> Result<(), BoxError> { + let TEST_API_KEY="P1EVE68DJByDAkGQvpIkTwfrbYXd2Vo2ZaIhTYb9vx2"; + let TEST_API_SECRET="1nicg8z0zKVEt5Rb7ZDpIYjVYVTgvCaCPMZqB0niFli"; + + let bfx = Bitfinex::new(Some(TEST_API_KEY.into()), Some(TEST_API_SECRET.into())); + + println!("{:?}", bfx.positions.active_positions().await); Ok(()) } diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs index 302c0bd..a3e834c 100644 --- a/rustybot/src/pairs.rs +++ b/rustybot/src/pairs.rs @@ -1,3 +1,30 @@ -struct PairStatus { +use std::collections::HashMap; +use crate::events::{EventDispatcher, Event}; +use crate::positions::PositionWrapper; +use crate::strategy::Strategy; + +pub struct PairStatus { + pair: String, + dispatcher: EventDispatcher, + prices: HashMap, + events: Vec, + // orders: HashMap>, + positions: HashMap>, + current_tick: u64, + strategy: Option>, +} + +impl PairStatus { + pub fn new(pair: String, current_tick: u64, strategy: Option>) -> Self { + PairStatus { + pair, + dispatcher: EventDispatcher::new(), + prices: HashMap::new(), + events: Vec::new(), + positions: HashMap::new(), + current_tick, + strategy, + } + } } \ No newline at end of file diff --git a/rustybot/src/positions.rs b/rustybot/src/positions.rs index d259cb3..4b228cc 100644 --- a/rustybot/src/positions.rs +++ b/rustybot/src/positions.rs @@ -1,3 +1,5 @@ +use bitfinex::positions::Position; + pub enum PositionState { Critical, Loss, @@ -16,12 +18,22 @@ impl PositionState { } } -// 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 + position: Position, + net_pl: f64, + net_pl_perc: f64, + state: PositionState, +} + +impl PositionWrapper { + pub fn new(position: Position, net_pl: f64, net_pl_perc: f64, state: PositionState) -> Self { + PositionWrapper { + position, + net_pl, + net_pl_perc, + state, + } + } } diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs new file mode 100644 index 0000000..b8acf7d --- /dev/null +++ b/rustybot/src/strategy.rs @@ -0,0 +1,8 @@ +use bitfinex::positions::Position; + +use crate::events::Event; +use crate::positions::PositionWrapper; + +pub trait Strategy { + fn position_on_new_tick(&self, position: Position) -> (PositionWrapper, Vec); +} \ No newline at end of file