order submission

This commit is contained in:
Giulio De Pasquale 2021-01-11 17:16:44 +00:00
parent 293ea60919
commit 8a7b3d4e22

View File

@ -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<Vec<Position>, BoxError>;
async fn current_prices(&self, pair: &SymbolPair) -> Result<TradingPairTicker, BoxError>;
async fn active_orders(&self, pair: &SymbolPair) -> Result<Vec<Order>, BoxError>;
async fn submit_order(&self) -> Result<u64, BoxError>;
async fn submit_order(
&self,
pair: &SymbolPair,
amount: f64,
price: f64,
kind: &OrderKind,
) -> Result<(), BoxError>;
}
pub struct BfxWrapper {
@ -65,6 +77,26 @@ impl TryInto<Position> 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<Vec<Position>, BoxError> {
@ -87,7 +119,19 @@ impl Connector for BfxWrapper {
unimplemented!()
}
async fn submit_order(&self) -> Result<u64, BoxError> {
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?)
}
}