added support for derivative trading in connectors. stub for fees calculation. hardcoded leverage 15 in order submission

This commit is contained in:
Giulio De Pasquale 2021-02-12 14:58:43 +00:00
parent 613b314631
commit e210808983

View File

@ -17,7 +17,7 @@ use tokio::time::Duration;
use crate::currency::{Symbol, SymbolPair}; use crate::currency::{Symbol, SymbolPair};
use crate::models::{ use crate::models::{
ActiveOrder, OrderBook, OrderBookEntry, OrderDetails, OrderFee, OrderForm, OrderKind, Position, ActiveOrder, OrderBook, OrderBookEntry, OrderDetails, OrderFee, OrderForm, OrderKind, Position,
PositionState, PriceTicker, Trade, TradingPlatform, WalletKind, PositionState, PriceTicker, Trade, TradingFees, TradingPlatform, WalletKind,
}; };
use crate::BoxError; use crate::BoxError;
@ -69,9 +69,9 @@ impl Client {
// TODO: change fee with account's taker fee // TODO: change fee with account's taker fee
positions.iter_mut().flatten().for_each(|x| { positions.iter_mut().flatten().for_each(|x| {
if x.is_short() { if x.is_short() {
x.update_profit_loss(best_ask, 0.2); x.update_profit_loss(best_ask, 0.075);
} else { } else {
x.update_profit_loss(best_bid, 0.2); x.update_profit_loss(best_bid, 0.075);
} }
}); });
@ -153,6 +153,7 @@ pub trait Connector: Send + Sync {
&self, &self,
pair: &SymbolPair, pair: &SymbolPair,
) -> Result<Option<Vec<OrderDetails>>, BoxError>; ) -> Result<Option<Vec<OrderDetails>>, BoxError>;
async fn trading_fees(&self) -> Result<Vec<TradingFees>, BoxError>;
} }
impl Debug for dyn Connector { impl Debug for dyn Connector {
@ -189,7 +190,11 @@ impl BitfinexConnector {
if pair.to_string().to_lowercase().contains("test") { if pair.to_string().to_lowercase().contains("test") {
format!("{}:{}", pair.base(), pair.quote()) format!("{}:{}", pair.base(), pair.quote())
} else { } else {
format!("{}{}", pair.base(), pair.quote()) if pair.to_string().to_lowercase().contains("f0") {
format!("{}:{}", pair.base(), pair.quote())
} else {
format!("{}{}", pair.base(), pair.quote())
}
} }
} }
@ -279,9 +284,11 @@ impl Connector for BitfinexConnector {
let order_form = match order.kind() { let order_form = match order.kind() {
OrderKind::Limit { price, amount } => { OrderKind::Limit { price, amount } => {
bitfinex::orders::OrderForm::new(symbol_name, price, amount, order.into()) bitfinex::orders::OrderForm::new(symbol_name, price, amount, order.into())
.with_leverage(15)
} }
OrderKind::Market { amount } => { OrderKind::Market { amount, .. } => {
bitfinex::orders::OrderForm::new(symbol_name, 0.0, amount, order.into()) bitfinex::orders::OrderForm::new(symbol_name, 0.0, amount, order.into())
.with_leverage(15)
} }
OrderKind::Stop { price, amount } => { OrderKind::Stop { price, amount } => {
bitfinex::orders::OrderForm::new(symbol_name, price, amount, order.into()) bitfinex::orders::OrderForm::new(symbol_name, price, amount, order.into())
@ -371,6 +378,10 @@ impl Connector for BitfinexConnector {
Ok((!mapped_vec.is_empty()).then_some(mapped_vec)) Ok((!mapped_vec.is_empty()).then_some(mapped_vec))
} }
async fn trading_fees(&self) -> Result<Vec<TradingFees>, BoxError> {
unimplemented!()
}
} }
impl From<&ActiveOrder> for CancelOrderForm { impl From<&ActiveOrder> for CancelOrderForm {
@ -439,7 +450,7 @@ impl From<&OrderForm> for bitfinex::orders::OrderKind {
OrderKind::FillOrKill { .. } => bitfinex::orders::OrderKind::ExchangeFok, OrderKind::FillOrKill { .. } => bitfinex::orders::OrderKind::ExchangeFok,
OrderKind::ImmediateOrCancel { .. } => bitfinex::orders::OrderKind::ExchangeIoc, OrderKind::ImmediateOrCancel { .. } => bitfinex::orders::OrderKind::ExchangeIoc,
}, },
TradingPlatform::Margin => match o.kind() { TradingPlatform::Margin | TradingPlatform::Derivative => match o.kind() {
OrderKind::Limit { .. } => bitfinex::orders::OrderKind::Limit, OrderKind::Limit { .. } => bitfinex::orders::OrderKind::Limit,
OrderKind::Market { .. } => bitfinex::orders::OrderKind::Market, OrderKind::Market { .. } => bitfinex::orders::OrderKind::Market,
OrderKind::Stop { .. } => bitfinex::orders::OrderKind::Stop, OrderKind::Stop { .. } => bitfinex::orders::OrderKind::Stop,