changed visibility of ActiveOrder's fields. implemented getters
This commit is contained in:
parent
53fb8781b3
commit
c7c4dd5902
@ -123,7 +123,7 @@ impl Client {
|
|||||||
.active_orders(pair)
|
.active_orders(pair)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| &x.symbol == pair)
|
.filter(|x| &x.pair() == &pair)
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,7 +350,7 @@ impl Connector for BitfinexConnector {
|
|||||||
// adding leverage, if any
|
// adding leverage, if any
|
||||||
match order.leverage() {
|
match order.leverage() {
|
||||||
// TODO: CHANGEME!!!!
|
// TODO: CHANGEME!!!!
|
||||||
Some(leverage) => pre_leverage.with_leverage(15),
|
Some(_leverage) => pre_leverage.with_leverage(15),
|
||||||
// Some(leverage) => pre_leverage.with_leverage(leverage.round() as u32),
|
// Some(leverage) => pre_leverage.with_leverage(leverage.round() as u32),
|
||||||
None => pre_leverage,
|
None => pre_leverage,
|
||||||
}
|
}
|
||||||
@ -468,7 +468,7 @@ impl Connector for BitfinexConnector {
|
|||||||
|
|
||||||
impl From<&ActiveOrder> for CancelOrderForm {
|
impl From<&ActiveOrder> for CancelOrderForm {
|
||||||
fn from(o: &ActiveOrder) -> Self {
|
fn from(o: &ActiveOrder) -> Self {
|
||||||
Self::from_id(o.id)
|
Self::from_id(o.id())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -476,21 +476,18 @@ impl TryFrom<&bitfinex::responses::OrderResponse> for ActiveOrder {
|
|||||||
type Error = BoxError;
|
type Error = BoxError;
|
||||||
|
|
||||||
fn try_from(response: &OrderResponse) -> Result<Self, Self::Error> {
|
fn try_from(response: &OrderResponse) -> Result<Self, Self::Error> {
|
||||||
Ok(Self {
|
let pair = SymbolPair::from_str(response.symbol())?;
|
||||||
exchange: Exchange::Bitfinex,
|
|
||||||
id: response.id(),
|
Ok(ActiveOrder::new(
|
||||||
group_id: response.gid(),
|
Exchange::Bitfinex,
|
||||||
client_id: Some(response.cid()),
|
response.id(),
|
||||||
symbol: SymbolPair::from_str(response.symbol())?,
|
pair.clone(),
|
||||||
details: OrderForm::new(
|
OrderForm::new(pair, response.into(), response.into(), response.amount()),
|
||||||
SymbolPair::from_str(response.symbol())?,
|
response.mts_create(),
|
||||||
response.into(),
|
response.mts_update(),
|
||||||
response.into(),
|
)
|
||||||
response.amount(),
|
.with_group_id(response.gid())
|
||||||
),
|
.with_client_id(Some(response.cid())))
|
||||||
creation_timestamp: 0,
|
|
||||||
update_timestamp: 0,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -670,16 +667,16 @@ impl From<&bitfinex::orders::ActiveOrder> for ActiveOrder {
|
|||||||
fn from(order: &bitfinex::orders::ActiveOrder) -> Self {
|
fn from(order: &bitfinex::orders::ActiveOrder) -> Self {
|
||||||
let pair = SymbolPair::from_str(&order.symbol()).expect("Invalid symbol!");
|
let pair = SymbolPair::from_str(&order.symbol()).expect("Invalid symbol!");
|
||||||
|
|
||||||
Self {
|
ActiveOrder::new(
|
||||||
exchange: Exchange::Bitfinex,
|
Exchange::Bitfinex,
|
||||||
id: order.id(),
|
order.id(),
|
||||||
group_id: order.group_id().map(|x| x as u64),
|
pair.clone(),
|
||||||
client_id: Some(order.client_id()),
|
OrderForm::new(pair, order.into(), order.into(), order.amount()),
|
||||||
symbol: pair.clone(),
|
order.creation_timestamp(),
|
||||||
details: OrderForm::new(pair, order.into(), order.into(), order.amount()),
|
order.update_timestamp(),
|
||||||
creation_timestamp: order.creation_timestamp(),
|
)
|
||||||
update_timestamp: order.update_timestamp(),
|
.with_client_id(Some(order.client_id()))
|
||||||
}
|
.with_group_id(order.group_id())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,13 +434,13 @@ impl OrderManager {
|
|||||||
let open_orders = self.client.active_orders(&self.pair).await?;
|
let open_orders = self.client.active_orders(&self.pair).await?;
|
||||||
let position_orders: Vec<_> = position_orders
|
let position_orders: Vec<_> = position_orders
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|&x| open_orders.iter().find(|y| y.id == x))
|
.filter_map(|&x| open_orders.iter().find(|y| y.id() == x))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
for order in position_orders {
|
for order in position_orders {
|
||||||
match self.client.cancel_order(order).await {
|
match self.client.cancel_order(order).await {
|
||||||
Ok(_) => info!("Order #{} closed successfully.", order.id),
|
Ok(_) => info!("Order #{} closed successfully.", order.id()),
|
||||||
Err(e) => error!("Could not close order #{}: {}", order.id, e),
|
Err(e) => error!("Could not close order #{}: {}", order.id(), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -460,10 +460,10 @@ impl OrderManager {
|
|||||||
match self.tracked_positions.get_mut(&position_id) {
|
match self.tracked_positions.get_mut(&position_id) {
|
||||||
None => {
|
None => {
|
||||||
self.tracked_positions
|
self.tracked_positions
|
||||||
.insert(position_id, vec![active_order.id]);
|
.insert(position_id, vec![active_order.id()]);
|
||||||
}
|
}
|
||||||
Some(position_orders) => {
|
Some(position_orders) => {
|
||||||
position_orders.push(active_order.id);
|
position_orders.push(active_order.id());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -493,7 +493,9 @@ impl OrderManager {
|
|||||||
let opt_position_order = open_orders
|
let opt_position_order = open_orders
|
||||||
.iter()
|
.iter()
|
||||||
// avoid using direct equality, using error margin instead
|
// avoid using direct equality, using error margin instead
|
||||||
.find(|x| (x.details.amount().neg() - position.amount()).abs() < 0.0000001);
|
.find(|x| {
|
||||||
|
(x.order_form().amount().neg() - position.amount()).abs() < 0.0000001
|
||||||
|
});
|
||||||
|
|
||||||
// checking if the position has an open order.
|
// checking if the position has an open order.
|
||||||
// If so, don't do anything since the order is taken care of
|
// If so, don't do anything since the order is taken care of
|
||||||
@ -552,29 +554,29 @@ impl OrderManager {
|
|||||||
for position in positions {
|
for position in positions {
|
||||||
let matching_order = open_orders
|
let matching_order = open_orders
|
||||||
.iter()
|
.iter()
|
||||||
.find(|x| x.details.amount().abs() == position.amount().abs());
|
.find(|x| x.order_form().amount().abs() == position.amount().abs());
|
||||||
|
|
||||||
// if an order is found, we insert the order to our internal mapping, if not already present
|
// if an order is found, we insert the order to our internal mapping, if not already present
|
||||||
if let Some(matching_order) = matching_order {
|
if let Some(matching_order) = matching_order {
|
||||||
match self.tracked_positions.get_mut(&position.id()) {
|
match self.tracked_positions.get_mut(&position.id()) {
|
||||||
Some(position_orders) => {
|
Some(position_orders) => {
|
||||||
if !position_orders.contains(&matching_order.id) {
|
if !position_orders.contains(&matching_order.id()) {
|
||||||
trace!(
|
trace!(
|
||||||
"Mapped order #{} to position #{}",
|
"Mapped order #{} to position #{}",
|
||||||
position.id(),
|
position.id(),
|
||||||
matching_order.id
|
matching_order.id()
|
||||||
);
|
);
|
||||||
position_orders.push(matching_order.id);
|
position_orders.push(matching_order.id());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
trace!(
|
trace!(
|
||||||
"Mapped order #{} to position #{}",
|
"Mapped order #{} to position #{}",
|
||||||
position.id(),
|
position.id(),
|
||||||
matching_order.id
|
matching_order.id()
|
||||||
);
|
);
|
||||||
self.tracked_positions
|
self.tracked_positions
|
||||||
.insert(position.id(), vec![matching_order.id]);
|
.insert(position.id(), vec![matching_order.id()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -595,7 +597,7 @@ impl OrderManager {
|
|||||||
match m {
|
match m {
|
||||||
ActionMessage::SubmitOrder { order: order_form } => {
|
ActionMessage::SubmitOrder { order: order_form } => {
|
||||||
info!("Closing open order...");
|
info!("Closing open order...");
|
||||||
info!("\tCancelling open order #{}", &active_order.id);
|
info!("\tCancelling open order #{}", &active_order.id());
|
||||||
self.client.cancel_order(&active_order).await?;
|
self.client.cancel_order(&active_order).await?;
|
||||||
|
|
||||||
info!("\tSubmitting {}...", order_form.kind());
|
info!("\tSubmitting {}...", order_form.kind());
|
||||||
|
@ -150,14 +150,71 @@ impl OrderDetails {
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct ActiveOrder {
|
pub struct ActiveOrder {
|
||||||
pub(crate) exchange: Exchange,
|
exchange: Exchange,
|
||||||
pub(crate) id: u64,
|
id: u64,
|
||||||
pub(crate) group_id: Option<u64>,
|
group_id: Option<u64>,
|
||||||
pub(crate) client_id: Option<u64>,
|
client_id: Option<u64>,
|
||||||
pub(crate) symbol: SymbolPair,
|
pair: SymbolPair,
|
||||||
pub(crate) details: OrderForm,
|
order_form: OrderForm,
|
||||||
pub(crate) creation_timestamp: u64,
|
creation_timestamp: u64,
|
||||||
pub(crate) update_timestamp: u64,
|
update_timestamp: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveOrder {
|
||||||
|
pub fn new(
|
||||||
|
exchange: Exchange,
|
||||||
|
id: u64,
|
||||||
|
pair: SymbolPair,
|
||||||
|
order_form: OrderForm,
|
||||||
|
creation_timestamp: u64,
|
||||||
|
update_timestamp: u64,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
exchange,
|
||||||
|
id,
|
||||||
|
group_id: None,
|
||||||
|
client_id: None,
|
||||||
|
pair,
|
||||||
|
order_form,
|
||||||
|
creation_timestamp,
|
||||||
|
update_timestamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_group_id(mut self, group_id: Option<u64>) -> Self {
|
||||||
|
self.group_id = group_id;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_client_id(mut self, client_id: Option<u64>) -> Self {
|
||||||
|
self.client_id = client_id;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exchange(&self) -> Exchange {
|
||||||
|
self.exchange
|
||||||
|
}
|
||||||
|
pub fn id(&self) -> u64 {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
pub fn group_id(&self) -> Option<u64> {
|
||||||
|
self.group_id
|
||||||
|
}
|
||||||
|
pub fn client_id(&self) -> Option<u64> {
|
||||||
|
self.client_id
|
||||||
|
}
|
||||||
|
pub fn pair(&self) -> &SymbolPair {
|
||||||
|
&self.pair
|
||||||
|
}
|
||||||
|
pub fn order_form(&self) -> &OrderForm {
|
||||||
|
&self.order_form
|
||||||
|
}
|
||||||
|
pub fn creation_timestamp(&self) -> u64 {
|
||||||
|
self.creation_timestamp
|
||||||
|
}
|
||||||
|
pub fn update_timestamp(&self) -> u64 {
|
||||||
|
self.update_timestamp
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hash for ActiveOrder {
|
impl Hash for ActiveOrder {
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
|
||||||
|
|
||||||
use dyn_clone::DynClone;
|
use dyn_clone::DynClone;
|
||||||
use log::{info};
|
use log::info;
|
||||||
|
|
||||||
use crate::connectors::Connector;
|
use crate::connectors::Connector;
|
||||||
use crate::events::{ActionMessage, Event, EventKind, EventMetadata};
|
use crate::events::{ActionMessage, Event, EventKind, EventMetadata};
|
||||||
use crate::managers::OptionUpdate;
|
use crate::managers::OptionUpdate;
|
||||||
use crate::models::{
|
use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState};
|
||||||
ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState,
|
|
||||||
};
|
|
||||||
use crate::BoxError;
|
use crate::BoxError;
|
||||||
|
|
||||||
/***************
|
/***************
|
||||||
@ -557,7 +554,7 @@ impl OrderStrategy for MarketEnforce {
|
|||||||
|
|
||||||
// long
|
// long
|
||||||
let offer_comparison = {
|
let offer_comparison = {
|
||||||
if order.details.amount() > 0.0 {
|
if order.order_form().amount() > 0.0 {
|
||||||
order_book.highest_bid()
|
order_book.highest_bid()
|
||||||
} else {
|
} else {
|
||||||
order_book.lowest_ask()
|
order_book.lowest_ask()
|
||||||
@ -567,7 +564,7 @@ impl OrderStrategy for MarketEnforce {
|
|||||||
// if the best offer is higher than our threshold,
|
// if the best offer is higher than our threshold,
|
||||||
// ask the manager to close the position with a market order
|
// ask the manager to close the position with a market order
|
||||||
let order_price = order
|
let order_price = order
|
||||||
.details
|
.order_form()
|
||||||
.price()
|
.price()
|
||||||
.ok_or("The active order does not have a price!")?;
|
.ok_or("The active order does not have a price!")?;
|
||||||
let delta = (1.0 - (offer_comparison / order_price)).abs() * 100.0;
|
let delta = (1.0 - (offer_comparison / order_price)).abs() * 100.0;
|
||||||
@ -575,10 +572,10 @@ impl OrderStrategy for MarketEnforce {
|
|||||||
if delta > self.threshold {
|
if delta > self.threshold {
|
||||||
messages.push(ActionMessage::SubmitOrder {
|
messages.push(ActionMessage::SubmitOrder {
|
||||||
order: OrderForm::new(
|
order: OrderForm::new(
|
||||||
order.symbol.clone(),
|
order.pair().clone(),
|
||||||
OrderKind::Market,
|
OrderKind::Market,
|
||||||
*order.details.platform(),
|
*order.order_form().platform(),
|
||||||
order.details.amount(),
|
order.order_form().amount(),
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user