diff --git a/rustybot/src/connectors.rs b/rustybot/src/connectors.rs index d550a2f..bc8647d 100644 --- a/rustybot/src/connectors.rs +++ b/rustybot/src/connectors.rs @@ -2,19 +2,31 @@ use std::convert::{TryFrom, TryInto}; use async_trait::async_trait; use bitfinex::api::Bitfinex; +use bitfinex::orders::{OrderForm, OrderMeta}; use bitfinex::ticker::TradingPairTicker; use crate::currency::{Symbol, SymbolPair}; -use crate::orders::Order; +use crate::orders::{Order, OrderKind}; use crate::positions::{Position, PositionState}; use crate::BoxError; +#[derive(Eq, PartialEq, Hash, Clone)] +pub enum ExchangeKind { + Bitfinex, +} + #[async_trait] pub trait Connector { async fn active_positions(&self, pair: &SymbolPair) -> Result, BoxError>; async fn current_prices(&self, pair: &SymbolPair) -> Result; async fn active_orders(&self, pair: &SymbolPair) -> Result, BoxError>; - async fn submit_order(&self) -> Result; + async fn submit_order( + &self, + pair: &SymbolPair, + amount: f64, + price: f64, + kind: &OrderKind, + ) -> Result<(), BoxError>; } pub struct BfxWrapper { @@ -65,6 +77,26 @@ impl TryInto for bitfinex::positions::Position { } } +impl From<&OrderKind> for bitfinex::orders::OrderKind { + fn from(o: &OrderKind) -> Self { + match o { + OrderKind::Limit => Self::Limit, + OrderKind::ExchangeLimit => Self::ExchangeLimit, + OrderKind::Market => Self::Market, + OrderKind::ExchangeMarket => Self::ExchangeMarket, + OrderKind::Stop => Self::Stop, + OrderKind::ExchangeStop => Self::ExchangeStop, + OrderKind::StopLimit => Self::StopLimit, + OrderKind::ExchangeStopLimit => Self::ExchangeStopLimit, + OrderKind::TrailingStop => Self::TrailingStop, + OrderKind::Fok => Self::Fok, + OrderKind::ExchangeFok => Self::ExchangeFok, + OrderKind::Ioc => Self::Ioc, + OrderKind::ExchangeIoc => Self::ExchangeIoc, + } + } +} + #[async_trait] impl Connector for BfxWrapper { async fn active_positions(&self, pair: &SymbolPair) -> Result, BoxError> { @@ -87,7 +119,19 @@ impl Connector for BfxWrapper { unimplemented!() } - async fn submit_order(&self) -> Result { - unimplemented!() + async fn submit_order( + &self, + pair: &SymbolPair, + amount: f64, + price: f64, + kind: &OrderKind, + ) -> Result<(), BoxError> { + let order_form = match &self.affiliate_code { + Some(affiliate_code) => OrderForm::new(pair.trading_repr(), price, amount, kind.into()) + .with_meta(OrderMeta::new(affiliate_code.clone())), + None => OrderForm::new(pair.trading_repr(), price, amount, kind.into()), + }; + + Ok(self.bfx.orders.submit_order(&order_form).await?) } }