From f211b2cded245872d7e3f99f5271e2c7353d32e1 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 4 Jan 2021 10:45:54 +0000 Subject: [PATCH] other stuff --- rustybot/src/bot.rs | 77 +++++++++++++++++++++++++++++++--------- rustybot/src/currency.rs | 51 ++++++++++++++++---------- rustybot/src/main.rs | 23 +++++++++--- rustybot/src/pairs.rs | 7 ++-- rustybot/src/ticker.rs | 14 ++++++-- 5 files changed, 128 insertions(+), 44 deletions(-) diff --git a/rustybot/src/bot.rs b/rustybot/src/bot.rs index 055cd9d..be4a9e3 100644 --- a/rustybot/src/bot.rs +++ b/rustybot/src/bot.rs @@ -1,30 +1,73 @@ +use core::time::Duration; use std::collections::HashMap; use bitfinex::api::Bitfinex; +use bitfinex::positions::Position; +use tokio::time::delay_for; +use crate::BoxError; +use crate::currency::{Symbol, SymbolPair}; use crate::pairs::PairStatus; use crate::ticker::Ticker; -use core::time::Duration; +use bitfinex::ticker::TradingPairTicker; -struct BfxBot { - bfx: Bitfinex, - ticker: Ticker, - pair_status: HashMap, - quote: String, - account_info: String, - ledger: String, +pub struct BfxWrapper { + bfx: Bitfinex } -impl BfxBot { - pub fn new>(api_key: S, api_secret: S, pairs: Vec, quote: String, tick_duration: Duration) -> Self { - BfxBot { - bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())), - ticker: Ticker::new(tick_duration), - pair_status: HashMap::new(), - quote, - account_info: String::new(), - ledger: String::new(), +impl BfxWrapper { + pub fn new(api_key: &str, api_secret: &str) -> Self { + BfxWrapper { + bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())) } } + + pub async fn current_prices(&self, pair: &SymbolPair) -> Result { + let ticker: TradingPairTicker = self.bfx.ticker.trading_pair(pair.trading_repr()).await?; + + Ok(ticker) + } +} + +pub struct BfxBot<'a> { + pub bfx: BfxWrapper, + ticker: Ticker, + pair_status: Vec>, + quote: Symbol<'a>, + trading_symbols: Vec>, + // account_info: String, + // ledger: String, +} + +impl BfxBot { + pub fn new>(api_key: S, api_secret: S, trading_symbols: Vec, quote: Symbol, tick_duration: Duration) -> Self { + BfxBot { + bfx: BfxWrapper::new(&api_key.into(), &api_secret.into()), + ticker: Ticker::new(tick_duration), + pair_status: trading_symbols + .iter() + .map(|x| SymbolPair::new(quote.clone(), x.clone())) + .map(|x| PairStatus::new(x, 1, None)) + .collect(), + quote, + // account_info: String::new(), + // ledger: String::new(), + trading_symbols, + } + } + + pub async fn update(&mut self) { + println!("Updating..."); + delay_for(self.ticker.duration()).await; + self.ticker.inc(); + // self.update_pairs().await; + println!("Done!"); + } + + // async fn update_pairs(&mut self) { + // let active_positions = self.bfx.positions.active_positions().await?; + // + // for p in active_positions {} + // } } diff --git a/rustybot/src/currency.rs b/rustybot/src/currency.rs index a9a67af..d31dc05 100644 --- a/rustybot/src/currency.rs +++ b/rustybot/src/currency.rs @@ -1,21 +1,32 @@ use core::fmt; use std::fmt::{Display, Formatter}; -const XMR: Symbol = Symbol { name: "XMR" }; -const BTC: Symbol = Symbol { name: "BTC" }; -const ETH: Symbol = Symbol { name: "ETH" }; -const LTC: Symbol = Symbol { name: "LTC" }; -const USD: Symbol = Symbol { name: "USD" }; -const GBP: Symbol = Symbol { name: "GBP" }; -const EUR: Symbol = Symbol { name: "EUR" }; +pub const XMR: Symbol = Symbol { name: "XMR" }; +pub const BTC: Symbol = Symbol { name: "BTC" }; +pub const ETH: Symbol = Symbol { name: "ETH" }; +pub const LTC: Symbol = Symbol { name: "LTC" }; +pub const USD: Symbol = Symbol { name: "USD" }; +pub const GBP: Symbol = Symbol { name: "GBP" }; +pub const EUR: Symbol = Symbol { name: "EUR" }; #[derive(Clone)] -struct Symbol { - name: &'static str +pub struct Symbol<'a> { + name: &'a str } +impl From for Symbol where S:Into { + fn from(item: S) -> Self { + Symbol { name: &item.into() } + } +} + + impl Symbol { + pub fn new(name: &str) -> Self { + Symbol { name } + } + pub fn name(&self) -> &str { &self.name } @@ -28,17 +39,21 @@ impl Display for Symbol { } #[derive(Clone)] -struct SymbolPair { - quote: Symbol, - base: Symbol, +pub struct SymbolPair<'a> { + quote: Symbol<'a>, + base: Symbol<'a>, } impl SymbolPair { - fn trading_repr(&self) -> String { + pub fn new(quote: Symbol, base: Symbol) -> Self { + SymbolPair { quote, base } + } + + pub fn trading_repr(&self) -> String { format!("t{}{}", self.quote, self.base) } - fn funding_repr(&self) -> String { + pub fn funding_repr(&self) -> String { format!("f{}{}", self.quote, self.base) } @@ -58,8 +73,8 @@ enum WalletKind { } #[derive(Clone)] -struct Balance { - pair: SymbolPair, +struct Balance<'a> { + pair: SymbolPair<'a>, base_price: f64, base_amount: f64, quote_equivalent: f64, @@ -88,9 +103,9 @@ impl Balance { } } -struct BalanceGroup { +struct BalanceGroup<'a> { quote_equivalent: f64, - balances: Vec, + balances: Vec>, } impl BalanceGroup { diff --git a/rustybot/src/main.rs b/rustybot/src/main.rs index d77b4e1..bf3d1d9 100644 --- a/rustybot/src/main.rs +++ b/rustybot/src/main.rs @@ -1,4 +1,9 @@ use bitfinex::api::Bitfinex; +use bitfinex::ticker::TradingPairTicker; +use tokio::time::Duration; + +use crate::bot::BfxBot; +use crate::currency::{BTC, ETH, SymbolPair, USD, XMR}; mod ticker; mod events; @@ -13,11 +18,21 @@ pub type BoxError = Box; #[tokio::main] async fn main() -> Result<(), BoxError> { - let TEST_API_KEY="P1EVE68DJByDAkGQvpIkTwfrbYXd2Vo2ZaIhTYb9vx2"; - let TEST_API_SECRET="1nicg8z0zKVEt5Rb7ZDpIYjVYVTgvCaCPMZqB0niFli"; + let test_api_key = "P1EVE68DJByDAkGQvpIkTwfrbYXd2Vo2ZaIhTYb9vx2"; + let test_api_secret = "1nicg8z0zKVEt5Rb7ZDpIYjVYVTgvCaCPMZqB0niFli"; - let bfx = Bitfinex::new(Some(TEST_API_KEY.into()), Some(TEST_API_SECRET.into())); + let mut bot = BfxBot::new(test_api_key, test_api_secret, vec![BTC, XMR, ETH], USD, Duration::new(20, 0)); + let btcusd = SymbolPair::new("USD".into(), "BTC".into()); + + loop { + bot.update().await; + + let ticker: TradingPairTicker = bot.bfx.current_prices(&btcusd).await?; + println!("{:?}", ticker); + if (2 < 1) { + break; + } + } - println!("{:?}", bfx.positions.active_positions().await); Ok(()) } diff --git a/rustybot/src/pairs.rs b/rustybot/src/pairs.rs index 506bf51..06d391a 100644 --- a/rustybot/src/pairs.rs +++ b/rustybot/src/pairs.rs @@ -5,9 +5,10 @@ use bitfinex::positions::Position; use crate::events::{Event, EventDispatcher}; use crate::positions::PositionWrapper; use crate::strategy::Strategy; +use crate::currency::SymbolPair; -pub struct PairStatus { - pair: String, +pub struct PairStatus<'a> { + pair: SymbolPair<'a>, dispatcher: EventDispatcher, prices: HashMap, events: Vec, @@ -18,7 +19,7 @@ pub struct PairStatus { } impl PairStatus { - pub fn new(pair: String, current_tick: u64, strategy: Option>) -> Self { + pub fn new(pair: SymbolPair, current_tick: u64, strategy: Option>) -> Self { PairStatus { pair, dispatcher: EventDispatcher::new(), diff --git a/rustybot/src/ticker.rs b/rustybot/src/ticker.rs index 87226bb..6a4d4c0 100644 --- a/rustybot/src/ticker.rs +++ b/rustybot/src/ticker.rs @@ -7,7 +7,7 @@ pub struct Ticker { } impl Ticker { - pub(crate) fn new(duration: Duration) -> Self { + pub fn new(duration: Duration) -> Self { Ticker { duration, start_time: Instant::now(), @@ -15,7 +15,17 @@ impl Ticker { } } - fn inc(&mut self) { + pub fn inc(&mut self) { self.current_tick += 1 } + + pub fn duration(&self) -> Duration { + self.duration + } + pub fn start_time(&self) -> Instant { + self.start_time + } + pub fn current_tick(&self) -> u64 { + self.current_tick + } }