use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
use nautilus_core::python::{IntoPyObjectNautilusExt, to_pyvalue_err};
use pyo3::{basic::CompareOp, prelude::*};
use rust_decimal::Decimal;
use crate::{
data::bet::{Bet, BetPosition, calc_bets_pnl, inverse_probability_to_bet, probability_to_bet},
enums::{BetSide, OrderSide},
};
#[pymethods]
#[pyo3_stub_gen::derive::gen_stub_pymethods]
impl Bet {
#[new]
fn py_new(price: Decimal, stake: Decimal, side: BetSide) -> Self {
Self::new(price, stake, side)
}
fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
match op {
CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
_ => py.NotImplemented(),
}
}
fn __hash__(&self) -> isize {
let mut h = DefaultHasher::new();
self.hash(&mut h);
h.finish() as isize
}
fn __repr__(&self) -> String {
format!("{self:?}")
}
fn __str__(&self) -> String {
self.to_string()
}
#[staticmethod]
#[pyo3(name = "from_stake_or_liability")]
fn py_from_stake_or_liability(price: Decimal, volume: Decimal, side: BetSide) -> Self {
Self::from_stake_or_liability(price, volume, side)
}
#[staticmethod]
#[pyo3(name = "from_stake")]
fn py_from_stake(price: Decimal, stake: Decimal, side: BetSide) -> Self {
Self::from_stake(price, stake, side)
}
#[staticmethod]
#[pyo3(name = "from_liability")]
fn py_from_liability(price: Decimal, liability: Decimal, side: BetSide) -> Self {
Self::from_liability(price, liability, side)
}
#[getter]
#[pyo3(name = "price")]
fn py_price(&self) -> Decimal {
self.price()
}
#[getter]
#[pyo3(name = "stake")]
fn py_stake(&self) -> Decimal {
self.stake()
}
#[getter]
#[pyo3(name = "side")]
fn py_side(&self) -> BetSide {
self.side()
}
#[pyo3(name = "exposure")]
fn py_exposure(&self) -> Decimal {
self.exposure()
}
#[pyo3(name = "liability")]
fn py_liability(&self) -> Decimal {
self.liability()
}
#[pyo3(name = "profit")]
fn py_profit(&self) -> Decimal {
self.profit()
}
#[pyo3(name = "outcome_win_payoff")]
fn py_outcome_win_payoff(&self) -> Decimal {
self.outcome_win_payoff()
}
#[pyo3(name = "outcome_lose_payoff")]
fn py_outcome_lose_payoff(&self) -> Decimal {
self.outcome_lose_payoff()
}
#[pyo3(name = "hedging_stake")]
fn py_hedging_stake(&self, price: Decimal) -> Decimal {
self.hedging_stake(price)
}
#[pyo3(name = "hedging_bet")]
fn py_hedging_bet(&self, price: Decimal) -> Self {
self.hedging_bet(price)
}
}
#[pymethods]
#[pyo3_stub_gen::derive::gen_stub_pymethods]
impl BetPosition {
#[new]
fn py_new() -> Self {
Self::default()
}
fn __repr__(&self) -> String {
format!("{self:?}")
}
fn __str__(&self) -> String {
self.to_string()
}
#[getter]
#[pyo3(name = "price")]
fn py_price(&self) -> Decimal {
self.price()
}
#[getter]
#[pyo3(name = "side")]
fn py_side(&self) -> Option<BetSide> {
self.side()
}
#[getter]
#[pyo3(name = "exposure")]
fn py_exposure(&self) -> Decimal {
self.exposure()
}
#[getter]
#[pyo3(name = "realized_pnl")]
fn py_realized_pnl(&self) -> Decimal {
self.realized_pnl()
}
#[pyo3(name = "add_bet")]
fn py_add_bet(&mut self, bet: &Bet) {
self.add_bet(bet.clone());
}
#[pyo3(name = "as_bet")]
fn py_as_bet(&self) -> Option<Bet> {
self.as_bet()
}
#[pyo3(name = "unrealized_pnl")]
fn py_unrealized_pnl(&self, price: Decimal) -> Decimal {
self.unrealized_pnl(price)
}
#[pyo3(name = "total_pnl")]
fn py_total_pnl(&self, price: Decimal) -> Decimal {
self.total_pnl(price)
}
#[pyo3(name = "flattening_bet")]
fn py_flattening_bet(&self, price: Decimal) -> Option<Bet> {
self.flattening_bet(price)
}
#[pyo3(name = "reset")]
fn py_reset(&mut self) {
self.reset();
}
}
#[pyfunction]
#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
#[pyo3(name = "calc_bets_pnl")]
#[allow(clippy::needless_pass_by_value)]
pub fn py_calc_bets_pnl(bets: Vec<Bet>) -> PyResult<Decimal> {
Ok(calc_bets_pnl(&bets))
}
#[pyfunction]
#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
#[pyo3(name = "probability_to_bet")]
pub fn py_probability_to_bet(
probability: Decimal,
volume: Decimal,
side: OrderSide,
) -> PyResult<Bet> {
probability_to_bet(probability, volume, side.as_specified()).map_err(to_pyvalue_err)
}
#[pyfunction]
#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.model")]
#[pyo3(name = "inverse_probability_to_bet")]
pub fn py_inverse_probability_to_bet(
probability: Decimal,
volume: Decimal,
side: OrderSide,
) -> PyResult<Bet> {
inverse_probability_to_bet(probability, volume, side.as_specified()).map_err(to_pyvalue_err)
}