This commit is contained in:
Giulio De Pasquale 2021-01-04 12:07:03 +00:00
parent f211b2cded
commit 168f324d6b
5 changed files with 62 additions and 42 deletions

View File

@ -10,4 +10,4 @@ edition = "2018"
bitfinex = { path= "/home/giulio/dev/bitfinex-rs" } bitfinex = { path= "/home/giulio/dev/bitfinex-rs" }
tokio = { version = "0.2", features=["full"]} tokio = { version = "0.2", features=["full"]}
tokio-tungstenite = "*" tokio-tungstenite = "*"
futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] } futures-util = { version = "0.3", default-features = false, features = ["async-await", "sink", "std"] }

View File

@ -23,18 +23,18 @@ impl BfxWrapper {
} }
pub async fn current_prices(&self, pair: &SymbolPair) -> Result<TradingPairTicker, BoxError> { pub async fn current_prices(&self, pair: &SymbolPair) -> Result<TradingPairTicker, BoxError> {
let ticker: TradingPairTicker = self.bfx.ticker.trading_pair(pair.trading_repr()).await?; let ticker: TradingPairTicker = self.bfx.ticker.trading_pair(pair.clone()).await?;
Ok(ticker) Ok(ticker)
} }
} }
pub struct BfxBot<'a> { pub struct BfxBot {
pub bfx: BfxWrapper, pub bfx: BfxWrapper,
ticker: Ticker, ticker: Ticker,
pair_status: Vec<PairStatus<'a>>, pair_status: Vec<PairStatus>,
quote: Symbol<'a>, quote: Symbol,
trading_symbols: Vec<Symbol<'a>>, trading_symbols: Vec<Symbol>,
// account_info: String, // account_info: String,
// ledger: String, // ledger: String,
} }
@ -56,6 +56,16 @@ impl BfxBot {
} }
} }
pub async fn current_prices(&self, symbol: Symbol) -> Result<TradingPairTicker, BoxError> {
let trading_pair = SymbolPair::new(self.quote.clone(), symbol.clone());
if !self.trading_symbols.contains(&symbol){
return Err("Symbol not supported.".into());
}
self.bfx.current_prices(&trading_pair).await
}
pub async fn update(&mut self) { pub async fn update(&mut self) {
println!("Updating..."); println!("Updating...");
delay_for(self.ticker.duration()).await; delay_for(self.ticker.duration()).await;

View File

@ -1,30 +1,34 @@
use core::fmt; use core::fmt;
use std::borrow::Cow;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
pub const XMR: Symbol = Symbol { name: "XMR" }; #[derive(Clone, PartialEq, Hash)]
pub const BTC: Symbol = Symbol { name: "BTC" }; pub struct Symbol {
pub const ETH: Symbol = Symbol { name: "ETH" }; name: Cow<'static, str>
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)]
pub struct Symbol<'a> {
name: &'a str
} }
impl<S> From<S> for Symbol where S:Into<String> { impl<S> From<S> for Symbol where S: Into<String> {
fn from(item: S) -> Self { fn from(item: S) -> Self {
Symbol { name: &item.into() } Symbol::new(item.into())
} }
} }
impl Symbol { impl Symbol {
pub fn new(name: &str) -> Self { pub const XMR: Symbol = Symbol::new_static("XMR");
Symbol { name } pub const BTC: Symbol = Symbol::new_static("BTC");
pub const ETH: Symbol = Symbol::new_static("ETH");
pub const LTC: Symbol = Symbol::new_static("LTC");
pub const USD: Symbol = Symbol::new_static("USD");
pub const GBP: Symbol = Symbol::new_static("GBP");
pub const EUR: Symbol = Symbol::new_static("EUR");
pub fn new(name: String) -> Self {
Symbol { name: Cow::from(name) }
}
pub const fn new_static(name: &'static str) -> Self {
Symbol { name: Cow::Borrowed(name) }
} }
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
@ -39,24 +43,21 @@ impl Display for Symbol {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct SymbolPair<'a> { pub struct SymbolPair {
quote: Symbol<'a>, quote: Symbol,
base: Symbol<'a>, base: Symbol,
} }
impl SymbolPair { impl SymbolPair {
pub fn new(quote: Symbol, base: Symbol) -> Self { pub fn new(quote: Symbol, base: Symbol) -> Self {
SymbolPair { quote, base } SymbolPair { quote, base }
} }
pub fn trading_repr(&self) -> String { pub fn trading_repr(&self) -> String {
format!("t{}{}", self.quote, self.base) format!("t{}{}", self.quote, self.base)
} }
pub fn funding_repr(&self) -> String { pub fn funding_repr(&self) -> String {
format!("f{}{}", self.quote, self.base) format!("f{}{}", self.quote, self.base)
} }
pub fn quote(&self) -> &Symbol { pub fn quote(&self) -> &Symbol {
&self.quote &self.quote
} }
@ -65,6 +66,18 @@ impl SymbolPair {
} }
} }
impl Into<String> for SymbolPair {
fn into(self) -> String {
format!("{}{}", self.base, self.quote)
}
}
impl Display for SymbolPair {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.base, self.quote)
}
}
#[derive(Clone)] #[derive(Clone)]
enum WalletKind { enum WalletKind {
Margin, Margin,
@ -73,8 +86,8 @@ enum WalletKind {
} }
#[derive(Clone)] #[derive(Clone)]
struct Balance<'a> { struct Balance {
pair: SymbolPair<'a>, pair: SymbolPair,
base_price: f64, base_price: f64,
base_amount: f64, base_amount: f64,
quote_equivalent: f64, quote_equivalent: f64,
@ -103,9 +116,9 @@ impl Balance {
} }
} }
struct BalanceGroup<'a> { struct BalanceGroup {
quote_equivalent: f64, quote_equivalent: f64,
balances: Vec<Balance<'a>>, balances: Vec<Balance>,
} }
impl BalanceGroup { impl BalanceGroup {

View File

@ -1,9 +1,9 @@
use bitfinex::api::Bitfinex; use bitfinex::api::Bitfinex;
use bitfinex::ticker::TradingPairTicker; use bitfinex::ticker::TradingPairTicker;
use tokio::time::Duration; use tokio::time::{Duration, delay_for};
use crate::bot::BfxBot; use crate::bot::BfxBot;
use crate::currency::{BTC, ETH, SymbolPair, USD, XMR}; use crate::currency::{Symbol, SymbolPair};
mod ticker; mod ticker;
mod events; mod events;
@ -21,17 +21,14 @@ async fn main() -> Result<(), BoxError> {
let test_api_key = "P1EVE68DJByDAkGQvpIkTwfrbYXd2Vo2ZaIhTYb9vx2"; let test_api_key = "P1EVE68DJByDAkGQvpIkTwfrbYXd2Vo2ZaIhTYb9vx2";
let test_api_secret = "1nicg8z0zKVEt5Rb7ZDpIYjVYVTgvCaCPMZqB0niFli"; let test_api_secret = "1nicg8z0zKVEt5Rb7ZDpIYjVYVTgvCaCPMZqB0niFli";
let mut bot = BfxBot::new(test_api_key, test_api_secret, vec![BTC, XMR, ETH], USD, Duration::new(20, 0)); let mut bot = BfxBot::new(test_api_key, test_api_secret, vec![Symbol::BTC, Symbol::ETH, Symbol::XMR], Symbol::USD, Duration::new(20, 0));
let btcusd = SymbolPair::new("USD".into(), "BTC".into());
loop { loop {
let ticker = bot.current_prices("ETH".into()).await?;
bot.update().await; bot.update().await;
let ticker: TradingPairTicker = bot.bfx.current_prices(&btcusd).await?; // let ticker = bot.current_prices("ETH".into()).await?;
println!("{:?}", ticker); println!("{:?}", ticker);
if (2 < 1) {
break;
}
} }
Ok(()) Ok(())

View File

@ -7,8 +7,8 @@ use crate::positions::PositionWrapper;
use crate::strategy::Strategy; use crate::strategy::Strategy;
use crate::currency::SymbolPair; use crate::currency::SymbolPair;
pub struct PairStatus<'a> { pub struct PairStatus {
pair: SymbolPair<'a>, pair: SymbolPair,
dispatcher: EventDispatcher, dispatcher: EventDispatcher,
prices: HashMap<u64, f64>, prices: HashMap<u64, f64>,
events: Vec<Event>, events: Vec<Event>,