rust #10

Merged
peperunas merged 127 commits from rust into master 2021-02-18 09:42:16 +00:00
Showing only changes of commit 945f5f63c1 - Show all commits

View File

@ -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<ExecutedOrder, BoxError> {
self.inner.submit_order(order).await
}
pub async fn order_book(&self, pair: &SymbolPair) -> Result<OrderBook, BoxError> {
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<Option<Vec<Position>>, BoxError>;
async fn current_prices(&self, pair: &SymbolPair) -> Result<TradingPairTicker, BoxError>;
async fn order_book(&self, pair: &SymbolPair) -> Result<OrderBook, BoxError>;
async fn active_orders(&self, pair: &SymbolPair) -> Result<Vec<ExecutedOrder>, BoxError>;
async fn submit_order(&self, order: OrderForm) -> Result<ExecutedOrder, BoxError>;
}
@ -141,23 +149,65 @@ impl Connector for BitfinexConnector {
}
async fn submit_order(&self, order: OrderForm) -> Result<ExecutedOrder, BoxError> {
// 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<OrderBook, BoxError> {
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))
}
}