other stuff
This commit is contained in:
parent
0470578739
commit
f211b2cded
@ -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,
|
||||
pub struct BfxWrapper {
|
||||
bfx: Bitfinex
|
||||
}
|
||||
|
||||
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<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: HashMap<String, PairStatus>,
|
||||
quote: String,
|
||||
account_info: String,
|
||||
ledger: String,
|
||||
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, pairs: Vec<String>, quote: String, tick_duration: Duration) -> Self {
|
||||
pub fn new<S: Into<String>>(api_key: S, api_secret: S, trading_symbols: Vec<Symbol>, quote: Symbol, tick_duration: Duration) -> Self {
|
||||
BfxBot {
|
||||
bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())),
|
||||
bfx: BfxWrapper::new(&api_key.into(), &api_secret.into()),
|
||||
ticker: Ticker::new(tick_duration),
|
||||
pair_status: HashMap::new(),
|
||||
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(),
|
||||
}
|
||||
// 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 {}
|
||||
// }
|
||||
}
|
||||
|
||||
|
@ -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<S> From<S> for Symbol where S:Into<String> {
|
||||
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<Balance>,
|
||||
balances: Vec<Balance<'a>>,
|
||||
}
|
||||
|
||||
impl BalanceGroup {
|
||||
|
@ -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<dyn std::error::Error + Send + Sync>;
|
||||
|
||||
#[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(())
|
||||
}
|
||||
|
@ -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<u64, f64>,
|
||||
events: Vec<Event>,
|
||||
@ -18,7 +19,7 @@ pub struct 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 {
|
||||
pair,
|
||||
dispatcher: EventDispatcher::new(),
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user