arbitrage 0.0.3

Fast bi-directional triangular arbitrage calculations in pure rust
Documentation
#[derive(Debug)]
pub struct TradeAction {
    string: String,
    convert: f32,
    fee: f32,
    remaining: f32,
}

#[derive(Debug)]
pub struct ArbitrageResult {
    pub action_i: TradeAction,
    pub action_ii: TradeAction,
    pub action_iii: TradeAction,
}

#[derive(Debug)]
pub struct BiDirectionalArbitrageResult {
    pub is_profitable: bool,
    pub clockwise: ArbitrageResult,
    pub counter_clockwise: ArbitrageResult,
}

pub fn check_triangle_trades_clockwise(
    a: f32,
    b: f32,
    c: f32,
    start_amt: f32,
    default_fee_percentage: f32,
) -> ArbitrageResult {
    // btcs to usdt
    let convert_b = start_amt * a;
    let b_fee = convert_b * default_fee_percentage;
    let remaining_b = convert_b - b_fee;
    // usdt to eths
    let convert_c = remaining_b / b;
    let c_fee = convert_c * default_fee_percentage;
    let remaining_c = convert_c - c_fee;
    // eths to btc
    let convert_a = remaining_c * c;
    let a_fee = convert_a * default_fee_percentage;
    let remaining_a = convert_a - a_fee;

    ArbitrageResult {
        action_i: TradeAction {
            string: "A->B".to_string(),
            convert: convert_b,
            fee: b_fee,
            remaining: remaining_b,
        },
        action_ii: TradeAction {
            string: "B->C".to_string(),
            convert: convert_c,
            fee: c_fee,
            remaining: remaining_c,
        },
        action_iii: TradeAction {
            string: "C->A".to_string(),
            convert: convert_a,
            fee: a_fee,
            remaining: remaining_a,
        },
    }
}

pub fn check_triangle_trades_counter_clockwise(
    a: f32,
    b: f32,
    c: f32,
    start_amt: f32,
    default_fee_percentage: f32,
) -> ArbitrageResult {
    // btc to eth
    let convert_c = start_amt / a;
    let c_fee = convert_c * default_fee_percentage;
    let remaining_c = convert_c - c_fee;
    // eth to usdt
    let convert_b = remaining_c * b;
    let b_fee = convert_b * default_fee_percentage;
    let remaining_b = convert_b - b_fee;
    // usdt to btc
    let convert_a = remaining_b / c;
    let a_fee = convert_a * default_fee_percentage;
    let remaining_a = convert_a - a_fee;

    ArbitrageResult {
        action_i: TradeAction {
            string: "A->C".to_string(),
            convert: convert_c,
            fee: c_fee,
            remaining: remaining_c,
        },
        action_ii: TradeAction {
            string: "C->B".to_string(),
            convert: convert_b,
            fee: b_fee,
            remaining: remaining_b,
        },
        action_iii: TradeAction {
            string: "B->A".to_string(),
            convert: convert_a,
            fee: a_fee,
            remaining: remaining_a,
        },
    }
}

pub fn check(
    a: f32,
    b: f32,
    c: f32,
    start_amt: f32,
    default_fee_percentage: f32,
) -> BiDirectionalArbitrageResult {
    let clockwise = check_triangle_trades_clockwise(a, b, c, start_amt, default_fee_percentage);
    let counter_clockwise =
        check_triangle_trades_counter_clockwise(a, b, c, start_amt, default_fee_percentage);

    let is_profitable =
        clockwise.action_iii.remaining > 1.0 || counter_clockwise.action_iii.remaining > 1.0;

    BiDirectionalArbitrageResult {
        is_profitable,
        clockwise,
        counter_clockwise,
    }
}

// fn main() {
//     // let result = check(1.0 / 2.0, 1.0 / 3.0, 2.08 / 3.0, 1.0, 0.01);
//     // println!("{:#?}", result);

//     // let result = check(1.0 / 2.0, 1.0 / 3.0, 2.08 / 3.0, 1.0, 0.005);
//     // println!("{:#?}", result);

//     // Example from https://www.investopedia.com/terms/t/triangulararbitrage.asp
//     let result = check(0.8631, 1.46, 1.6939, 1_000_000.0, 0.00);
//     println!("{:#?}", result);
// }