From 945f5f63c1637c986d7e07900b72ea8fc128970c Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Tue, 19 Jan 2021 21:29:02 +0000 Subject: [PATCH] implemented order book call for bitfinex connector. submit order stub working --- rustybot/src/connectors.rs | 66 +++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/rustybot/src/connectors.rs b/rustybot/src/connectors.rs index c80a17c..d3ea78d 100644 --- a/rustybot/src/connectors.rs +++ b/rustybot/src/connectors.rs @@ -1,14 +1,17 @@ -use std::convert::TryInto; -use std::fmt::{Debug, Formatter}; -use std::sync::Arc; - use async_trait::async_trait; use bitfinex::api::Bitfinex; use bitfinex::orders::{ActiveOrder, OrderMeta}; use bitfinex::ticker::TradingPairTicker; +use log::debug; +use std::convert::TryInto; +use std::fmt::{Debug, Formatter}; +use std::sync::Arc; use crate::currency::SymbolPair; -use crate::models::{ExecutedOrder, OrderForm, OrderKind, Position, PositionState, PriceTicker}; +use crate::models::{ + ExecutedOrder, OrderBook, OrderBookEntry, OrderForm, OrderKind, Position, PositionState, + PriceTicker, +}; use crate::BoxError; #[derive(Eq, PartialEq, Hash, Clone, Debug)] @@ -57,6 +60,10 @@ impl Client { pub async fn submit_order(&self, order: OrderForm) -> Result { self.inner.submit_order(order).await } + + pub async fn order_book(&self, pair: &SymbolPair) -> Result { + self.inner.order_book(pair).await + } } #[async_trait] @@ -64,6 +71,7 @@ pub trait Connector: Send + Sync { fn name(&self) -> String; async fn active_positions(&self, pair: &SymbolPair) -> Result>, BoxError>; async fn current_prices(&self, pair: &SymbolPair) -> Result; + async fn order_book(&self, pair: &SymbolPair) -> Result; async fn active_orders(&self, pair: &SymbolPair) -> Result, BoxError>; async fn submit_order(&self, order: OrderForm) -> Result; } @@ -141,23 +149,65 @@ impl Connector for BitfinexConnector { } async fn submit_order(&self, order: OrderForm) -> Result { + // TODO: change trading pair formatting. awful. let order_form = match &self.affiliate_code { Some(affiliate_code) => bitfinex::orders::OrderForm::new( - order.pair().trading_repr(), + format!("t{}", self.format_trading_pair(order.pair())), *order.price(), *order.amount(), order.kind().into(), ) .with_meta(OrderMeta::new(affiliate_code.clone())), None => bitfinex::orders::OrderForm::new( - order.pair().trading_repr(), + format!("t{}", self.format_trading_pair(order.pair())), *order.price(), *order.amount(), order.kind().into(), ), }; - Ok(self.bfx.orders.submit_order(&order_form).await?.into()) + let response = self.bfx.orders.submit_order(&order_form).await?; + + Ok(ExecutedOrder { + id: 1, + group_id: None, + client_id: 0, + symbol: "".to_string(), + creation_timestamp: 0, + update_timestamp: 0, + amount: 0.0, + amount_original: 0.0, + order_type: "".to_string(), + previous_order_type: None, + flags: None, + order_status: None, + price: 0.0, + price_avg: 0.0, + price_trailing: None, + price_aux_limit: None, + notify: 0, + hidden: 0, + placed_id: None, + }) + } + + async fn order_book(&self, pair: &SymbolPair) -> Result { + let x = self + .bfx + .book + .trading_pair(self.format_trading_pair(&pair), "P0".into()) + .await?; + + let entries = x + .into_iter() + .map(|x| OrderBookEntry::Trading { + price: x.price, + count: x.count as u64, + amount: x.amount, + }) + .collect(); + + Ok(OrderBook::new(pair.clone()).with_entries(entries)) } }