This commit is contained in:
Giulio De Pasquale 2021-01-02 14:10:16 +00:00
parent 370a57dbb9
commit d4e388154d
7 changed files with 112 additions and 82 deletions

99
rustybot/Cargo.lock generated
View File

@ -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",

View File

@ -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"] }

View File

@ -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<EventMetadata>,
@ -49,21 +50,27 @@ impl Event {
}
}
pub struct Dispatcher {
event_handlers: HashMap<EventKind, Box<dyn FnMut(String) -> JoinHandle<()>>>
pub struct EventDispatcher {
event_handlers: HashMap<EventKind, Box<dyn Fn(String) -> JoinHandle<()>>>
}
impl Dispatcher {
impl EventDispatcher {
pub fn new() -> Self {
Dispatcher {
EventDispatcher {
event_handlers: HashMap::new()
}
}
pub fn register_event_handler<F: 'static, Fut: 'static>(&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<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
where
F: FnMut(String) -> Fut,
Fut: Future<Output = ()> + Send,
F: Fn(String) -> Fut,
Fut: Future<Output=()> + Send,
{
self.event_handlers.insert(event, Box::new(move |args| tokio::spawn(f(args))));
}

View File

@ -1,12 +1,21 @@
use bitfinex::api::Bitfinex;
mod ticker;
mod positions;
mod events;
mod pairs;
mod positions;
mod strategy;
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
#[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(())
}

View File

@ -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<u64, f64>,
events: Vec<Event>,
// orders: HashMap<u64, Vec<Order>>,
positions: HashMap<u64, Vec<PositionWrapper>>,
current_tick: u64,
strategy: Option<Box<dyn Strategy>>,
}
impl PairStatus {
pub fn new(pair: String, current_tick: u64, strategy: Option<Box<dyn Strategy>>) -> Self {
PairStatus {
pair,
dispatcher: EventDispatcher::new(),
prices: HashMap::new(),
events: Vec::new(),
positions: HashMap::new(),
current_tick,
strategy,
}
}
}

View File

@ -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,
}
}
}

8
rustybot/src/strategy.rs Normal file
View File

@ -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<Event>);
}