rust #10
30
rustybot/src/bot.rs
Normal file
30
rustybot/src/bot.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use bitfinex::api::Bitfinex;
|
||||
|
||||
use crate::pairs::PairStatus;
|
||||
use crate::ticker::Ticker;
|
||||
use core::time::Duration;
|
||||
|
||||
struct BfxBot {
|
||||
bfx: Bitfinex,
|
||||
ticker: Ticker,
|
||||
pair_status: HashMap<String, PairStatus>,
|
||||
quote: String,
|
||||
account_info: String,
|
||||
ledger: String,
|
||||
}
|
||||
|
||||
impl BfxBot {
|
||||
pub fn new<S: Into<String>>(api_key: S, api_secret: S, pairs: Vec<String>, 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
111
rustybot/src/currency.rs
Normal file
111
rustybot/src/currency.rs
Normal file
@ -0,0 +1,111 @@
|
||||
#[derive(Clone)]
|
||||
struct Symbol;
|
||||
|
||||
impl Symbol {
|
||||
const XMR: &'static str = "XMR";
|
||||
const BTC: &'static str = "BTC";
|
||||
const ETH: &'static str = "ETH";
|
||||
const USD: &'static str = "USD";
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TradingPair {
|
||||
quote: Symbol
|
||||
}
|
||||
|
||||
impl TradingPair {}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Currency {
|
||||
name: String,
|
||||
amount: f64,
|
||||
price: Option<f64>,
|
||||
}
|
||||
|
||||
impl Currency {
|
||||
pub fn new<S: Into<String>, F: Into<f64>>(name: S, amount: F, price: Option<F>) -> Self {
|
||||
Currency {
|
||||
name: name.into(),
|
||||
amount: amount.into(),
|
||||
price: price.map(|x| x.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
pub fn amount(&self) -> f64 {
|
||||
self.amount
|
||||
}
|
||||
pub fn price(&self) -> Option<f64> {
|
||||
self.price
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum WalletKind {
|
||||
Margin,
|
||||
Exchange,
|
||||
Funding,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Balance {
|
||||
currency: Currency,
|
||||
quote: Symbol,
|
||||
quote_equivalent: f64,
|
||||
wallet: WalletKind,
|
||||
}
|
||||
|
||||
impl Balance {
|
||||
pub fn new(currency: Currency, quote: Symbol, quote_equivalent: f64, wallet: WalletKind) -> Self {
|
||||
Balance { currency, quote, quote_equivalent, wallet }
|
||||
}
|
||||
|
||||
pub fn currency(&self) -> &Currency {
|
||||
&self.currency
|
||||
}
|
||||
pub fn quote(&self) -> &Symbol {
|
||||
&self.quote
|
||||
}
|
||||
pub fn quote_equivalent(&self) -> f64 {
|
||||
self.quote_equivalent
|
||||
}
|
||||
pub fn wallet(&self) -> &WalletKind {
|
||||
&self.wallet
|
||||
}
|
||||
}
|
||||
|
||||
struct BalanceGroup {
|
||||
quote: Symbol,
|
||||
quote_equivalent: f64,
|
||||
balances: Vec<Balance>,
|
||||
}
|
||||
|
||||
impl BalanceGroup {
|
||||
pub fn new(quote: Symbol) -> Self {
|
||||
BalanceGroup { quote, balances: Vec::new(), quote_equivalent: 0f64 }
|
||||
}
|
||||
|
||||
pub fn add_balance(&mut self, balance: &Balance) {
|
||||
self.balances.push(balance.clone());
|
||||
|
||||
self.quote_equivalent += balance.quote_equivalent()
|
||||
}
|
||||
|
||||
pub fn currency_names(&self) -> Vec<String> {
|
||||
self.balances.iter()
|
||||
.map(|x| x.currency().name().into())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn quote(&self) -> &Symbol {
|
||||
&self.quote
|
||||
}
|
||||
pub fn balances(&self) -> &Vec<Balance> {
|
||||
&self.balances
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,6 +5,8 @@ mod events;
|
||||
mod pairs;
|
||||
mod positions;
|
||||
mod strategy;
|
||||
mod bot;
|
||||
mod currency;
|
||||
|
||||
|
||||
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
@ -7,7 +7,7 @@ pub struct Ticker {
|
||||
}
|
||||
|
||||
impl Ticker {
|
||||
fn new(duration: Duration) -> Self {
|
||||
pub(crate) fn new(duration: Duration) -> Self {
|
||||
Ticker {
|
||||
duration,
|
||||
start_time: Instant::now(),
|
||||
|
Loading…
Reference in New Issue
Block a user