Skip to content

Commit

Permalink
current state
Browse files Browse the repository at this point in the history
  • Loading branch information
LimpidCrypto committed Jul 19, 2024
1 parent 1eed83b commit ad27f06
Show file tree
Hide file tree
Showing 10 changed files with 616 additions and 173 deletions.
56 changes: 41 additions & 15 deletions trading-lib/src/order_books/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,63 @@ pub mod exceptions;
pub mod order;
pub mod order_book;

use std::borrow::Cow;

use anyhow::{Ok, Result};
use order_book::OrderBook;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct OrderBooks<'a> {
pub order_books: Vec<OrderBook<'a>>,
pub order_books: Cow<'a, [OrderBook<'a>]>,
pub liquidity_spread: f64,
}

impl<'a> OrderBooks<'a> {
pub fn sort(&mut self) {
pub fn sort(&'a mut self) -> Result<()> {
self.order_books
.to_mut()
.iter_mut()
.for_each(|order_book| order_book.sort());
.try_for_each(|order_book| order_book.sort())
}

pub fn get_liquid_order_books(&'a self) -> Result<Vec<&OrderBook<'_>>> {
let mut liquid_order_books = Vec::new();
for order_book in self.order_books.iter() {
if order_book.is_liquid(self.liquidity_spread)? {
liquid_order_books.push(order_book);
}
}

Ok(liquid_order_books)
}

pub fn get_illiquid_order_books(&'a self) -> Result<Vec<&OrderBook<'_>>> {
let mut illiquid_order_books = Vec::new();
for order_book in self.order_books.iter() {
if !order_book.is_liquid(self.liquidity_spread)? {
illiquid_order_books.push(order_book);
}
}

Ok(illiquid_order_books)
}
}

pub trait IsLiquid {
pub trait IsLiquid<'a> {
/// Returns true if the order book is liquid determained based on the provided `liquidity_spread`.
fn is_liquid(&self, liquidity_spread: f64) -> bool;
fn is_liquid(&'a self, liquidity_spread: f64) -> Result<bool>;
}

pub trait Flip {
fn flip(&mut self);
fn flip(&mut self) -> Result<()>;

fn get_flipped(&self) -> Self
where
Self: Sized + Clone,
{
let mut flipped = self.clone();
flipped.flip();
// fn get_flipped(&'a self) -> Self
// where
// Self: Sized + Clone,
// {
// let mut flipped = self.clone();
// flipped.flip();

flipped
}
// flipped
// }
}
7 changes: 5 additions & 2 deletions trading-lib/src/order_books/order.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cmp::Ordering, mem::swap};

use anyhow::Result;
use rust_decimal::{prelude::FromPrimitive, Decimal};
#[cfg(feature = "xrpl")]
use xrpl::models::{
Expand All @@ -22,11 +23,13 @@ pub struct Order<'a> {
pub rate: Decimal,
}

impl Flip for Order<'_> {
fn flip(&mut self) {
impl<'a> Flip<'a> for Order<'a> {
fn flip(&'a mut self) -> Result<()> {
self.rate = Decimal::from(1) / self.rate;
self.base_quantity = self.base_quantity * self.rate;
swap(&mut self.base, &mut self.counter);

Ok(())
}
}

Expand Down
Loading

0 comments on commit ad27f06

Please sign in to comment.