other stuff

This commit is contained in:
Giulio De Pasquale 2021-01-04 10:45:54 +00:00
parent 0470578739
commit f211b2cded
5 changed files with 128 additions and 44 deletions

View File

@ -1,30 +1,73 @@
use core::time::Duration;
use std::collections::HashMap; use std::collections::HashMap;
use bitfinex::api::Bitfinex; 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::pairs::PairStatus;
use crate::ticker::Ticker; use crate::ticker::Ticker;
use core::time::Duration; use bitfinex::ticker::TradingPairTicker;
struct BfxBot { pub struct BfxWrapper {
bfx: Bitfinex, bfx: Bitfinex
ticker: Ticker,
pair_status: HashMap<String, PairStatus>,
quote: String,
account_info: String,
ledger: String,
} }
impl BfxBot { impl BfxWrapper {
pub fn new<S: Into<String>>(api_key: S, api_secret: S, pairs: Vec<String>, quote: String, tick_duration: Duration) -> Self { pub fn new(api_key: &str, api_secret: &str) -> Self {
BfxBot { BfxWrapper {
bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())), 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(),
} }
} }
pub async fn current_prices(&self, pair: &SymbolPair) -> Result<TradingPairTicker, BoxError> {
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<PairStatus<'a>>,
quote: Symbol<'a>,
trading_symbols: Vec<Symbol<'a>>,
// account_info: String,
// ledger: String,
}
impl BfxBot {
pub fn new<S: Into<String>>(api_key: S, api_secret: S, trading_symbols: Vec<Symbol>, 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 {}
// }
} }

View File

@ -1,21 +1,32 @@
use core::fmt; use core::fmt;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
const XMR: Symbol = Symbol { name: "XMR" }; pub const XMR: Symbol = Symbol { name: "XMR" };
const BTC: Symbol = Symbol { name: "BTC" }; pub const BTC: Symbol = Symbol { name: "BTC" };
const ETH: Symbol = Symbol { name: "ETH" }; pub const ETH: Symbol = Symbol { name: "ETH" };
const LTC: Symbol = Symbol { name: "LTC" }; pub const LTC: Symbol = Symbol { name: "LTC" };
const USD: Symbol = Symbol { name: "USD" }; pub const USD: Symbol = Symbol { name: "USD" };
const GBP: Symbol = Symbol { name: "GBP" }; pub const GBP: Symbol = Symbol { name: "GBP" };
const EUR: Symbol = Symbol { name: "EUR" }; pub const EUR: Symbol = Symbol { name: "EUR" };
#[derive(Clone)] #[derive(Clone)]
struct Symbol { pub struct Symbol<'a> {
name: &'static str name: &'a str
} }
impl<S> From<S> for Symbol where S:Into<String> {
fn from(item: S) -> Self {
Symbol { name: &item.into() }
}
}
impl Symbol { impl Symbol {
pub fn new(name: &str) -> Self {
Symbol { name }
}
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
&self.name &self.name
} }
@ -28,17 +39,21 @@ impl Display for Symbol {
} }
#[derive(Clone)] #[derive(Clone)]
struct SymbolPair { pub struct SymbolPair<'a> {
quote: Symbol, quote: Symbol<'a>,
base: Symbol, base: Symbol<'a>,
} }
impl SymbolPair { 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) format!("t{}{}", self.quote, self.base)
} }
fn funding_repr(&self) -> String { pub fn funding_repr(&self) -> String {
format!("f{}{}", self.quote, self.base) format!("f{}{}", self.quote, self.base)
} }
@ -58,8 +73,8 @@ enum WalletKind {
} }
#[derive(Clone)] #[derive(Clone)]
struct Balance { struct Balance<'a> {
pair: SymbolPair, pair: SymbolPair<'a>,
base_price: f64, base_price: f64,
base_amount: f64, base_amount: f64,
quote_equivalent: f64, quote_equivalent: f64,
@ -88,9 +103,9 @@ impl Balance {
} }
} }
struct BalanceGroup { struct BalanceGroup<'a> {
quote_equivalent: f64, quote_equivalent: f64,
balances: Vec<Balance>, balances: Vec<Balance<'a>>,
} }
impl BalanceGroup { impl BalanceGroup {

View File

@ -1,4 +1,9 @@
use bitfinex::api::Bitfinex; 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 ticker;
mod events; mod events;
@ -13,11 +18,21 @@ pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), BoxError> { 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 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(()) Ok(())
} }

View File

@ -5,9 +5,10 @@ use bitfinex::positions::Position;
use crate::events::{Event, EventDispatcher}; use crate::events::{Event, EventDispatcher};
use crate::positions::PositionWrapper; use crate::positions::PositionWrapper;
use crate::strategy::Strategy; use crate::strategy::Strategy;
use crate::currency::SymbolPair;
pub struct PairStatus { pub struct PairStatus<'a> {
pair: String, pair: SymbolPair<'a>,
dispatcher: EventDispatcher, dispatcher: EventDispatcher,
prices: HashMap<u64, f64>, prices: HashMap<u64, f64>,
events: Vec<Event>, events: Vec<Event>,
@ -18,7 +19,7 @@ pub struct PairStatus {
} }
impl PairStatus { impl PairStatus {
pub fn new(pair: String, current_tick: u64, strategy: Option<Box<dyn Strategy>>) -> Self { pub fn new(pair: SymbolPair, current_tick: u64, strategy: Option<Box<dyn Strategy>>) -> Self {
PairStatus { PairStatus {
pair, pair,
dispatcher: EventDispatcher::new(), dispatcher: EventDispatcher::new(),

View File

@ -7,7 +7,7 @@ pub struct Ticker {
} }
impl Ticker { impl Ticker {
pub(crate) fn new(duration: Duration) -> Self { pub fn new(duration: Duration) -> Self {
Ticker { Ticker {
duration, duration,
start_time: Instant::now(), start_time: Instant::now(),
@ -15,7 +15,17 @@ impl Ticker {
} }
} }
fn inc(&mut self) { pub fn inc(&mut self) {
self.current_tick += 1 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
}
} }