Skip to content

Commit

Permalink
chore: Fetch best price directly from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Apr 5, 2024
1 parent cc80eb8 commit e245c9c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
19 changes: 4 additions & 15 deletions coordinator/src/node/liquidated_positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use commons::MatchState;
use commons::NewMarketOrder;
use commons::OrderReason;
use commons::OrderState;
use commons::OrderType;
use commons::Price;
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::prelude::ToPrimitive;
Expand Down Expand Up @@ -43,18 +42,8 @@ async fn check_if_positions_need_to_get_liquidated(
) -> Result<()> {
let mut conn = node.pool.get()?;
let open_positions = db::positions::Position::get_all_open_positions(&mut conn)?;
let open_orders =
orderbook::db::orders::get_all_orders(&mut conn, OrderType::Limit, OrderState::Open, true)?;

if open_orders.is_empty() {
tracing::warn!("No open orders found.");
return Ok(());
}

let best_current_price = commons::best_current_price(&open_orders);
let best_current_price = best_current_price
.get(&ContractSymbol::BtcUsd)
.expect("btc usd prices");
let best_current_price =
orderbook::db::orders::get_best_price(&mut conn, ContractSymbol::BtcUsd)?;

for position in open_positions {
let coordinator_liquidation_price =
Expand All @@ -64,12 +53,12 @@ async fn check_if_positions_need_to_get_liquidated(

let trader_liquidation = check_if_position_needs_to_get_liquidated(
position.trader_direction,
best_current_price,
&best_current_price,
trader_liquidation_price,
);
let coordinator_liquidation = check_if_position_needs_to_get_liquidated(
position.trader_direction.opposite(),
best_current_price,
&best_current_price,
coordinator_liquidation_price,
);

Expand Down
47 changes: 47 additions & 0 deletions coordinator/src/orderbook/db/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use commons::Order as OrderbookOrder;
use commons::OrderReason as OrderBookOrderReason;
use commons::OrderState as OrderBookOrderState;
use commons::OrderType as OrderBookOrderType;
use commons::Price;
use diesel::dsl::max;
use diesel::dsl::min;
use diesel::prelude::*;
use diesel::result::QueryResult;
use diesel::PgConnection;
Expand Down Expand Up @@ -250,6 +253,50 @@ pub fn all_by_direction_and_type(
Ok(orders.into_iter().map(OrderbookOrder::from).collect())
}

pub fn get_best_price(
conn: &mut PgConnection,
contract_symbol: trade::ContractSymbol,
) -> QueryResult<Price> {
let best_price = Price {
bid: get_best_bid_price(conn, contract_symbol)?,
ask: get_best_ask_price(conn, contract_symbol)?,
};

Ok(best_price)
}

pub fn get_best_bid_price(
conn: &mut PgConnection,
contract_symbol: trade::ContractSymbol,
) -> QueryResult<Option<Decimal>> {
let price: Option<f32> = orders::table
.select(max(orders::price))
.filter(orders::order_state.eq(OrderState::Open))
.filter(orders::order_type.eq(OrderType::Limit))
.filter(orders::direction.eq(Direction::Long))
.filter(orders::contract_symbol.eq(ContractSymbol::from(contract_symbol)))
.filter(orders::expiry.gt(OffsetDateTime::now_utc()))
.first::<Option<f32>>(conn)?;

Ok(price.map(|bid| Decimal::try_from(bid).expect("to fit into decimal")))
}

pub fn get_best_ask_price(
conn: &mut PgConnection,
contract_symbol: trade::ContractSymbol,
) -> QueryResult<Option<Decimal>> {
let price: Option<f32> = orders::table
.select(min(orders::price))
.filter(orders::order_state.eq(OrderState::Open))
.filter(orders::order_type.eq(OrderType::Limit))
.filter(orders::direction.eq(Direction::Short))
.filter(orders::contract_symbol.eq(ContractSymbol::from(contract_symbol)))
.filter(orders::expiry.gt(OffsetDateTime::now_utc()))
.first::<Option<f32>>(conn)?;

Ok(price.map(|ask| Decimal::try_from(ask).expect("to fit into decimal")))
}

pub fn get_all_orders(
conn: &mut PgConnection,
order_type: OrderBookOrderType,
Expand Down

0 comments on commit e245c9c

Please sign in to comment.