rust #10

Merged
peperunas merged 127 commits from rust into master 2021-02-18 09:42:16 +00:00
5 changed files with 62 additions and 42 deletions
Showing only changes of commit 168f324d6b - Show all commits

View File

@ -10,4 +10,4 @@ edition = "2018"
bitfinex = { path= "/home/giulio/dev/bitfinex-rs" }
tokio = { version = "0.2", features=["full"]}
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> {
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)
}
}
pub struct BfxBot<'a> {
pub struct BfxBot {
pub bfx: BfxWrapper,
ticker: Ticker,
pair_status: Vec<PairStatus<'a>>,
quote: Symbol<'a>,
trading_symbols: Vec<Symbol<'a>>,
pair_status: Vec<PairStatus>,
quote: Symbol,
trading_symbols: Vec<Symbol>,
// account_info: 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) {
println!("Updating...");
delay_for(self.ticker.duration()).await;

View File

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

View File

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

View File

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