stock-bot/apps/stock/engine/src/api_new/orders.rs
2025-07-04 11:24:27 -04:00

87 lines
No EOL
2.6 KiB
Rust

use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
#[napi]
pub struct OrdersAPI {
core: Arc<crate::TradingCore>,
}
impl OrdersAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl OrdersAPI {
#[napi]
pub async fn submit_order(
&self,
symbol: String,
side: String,
quantity: f64,
order_type: String,
limit_price: Option<f64>,
stop_price: Option<f64>,
) -> Result<String> {
let side = match side.as_str() {
"buy" => crate::Side::Buy,
"sell" => crate::Side::Sell,
_ => return Err(Error::from_reason("Invalid side")),
};
let order_type = match order_type.as_str() {
"market" => crate::OrderType::Market,
"limit" => {
let price = limit_price.ok_or_else(|| Error::from_reason("Limit price required"))?;
crate::OrderType::Limit { price }
}
"stop" => {
let price = stop_price.ok_or_else(|| Error::from_reason("Stop price required"))?;
crate::OrderType::Stop { stop_price: price }
}
"stop_limit" => {
let stop = stop_price.ok_or_else(|| Error::from_reason("Stop price required"))?;
let limit = limit_price.ok_or_else(|| Error::from_reason("Limit price required"))?;
crate::OrderType::StopLimit { stop_price: stop, limit_price: limit }
}
_ => return Err(Error::from_reason("Invalid order type")),
};
let order = crate::Order {
id: uuid::Uuid::new_v4().to_string(),
symbol,
side,
quantity,
order_type,
time_in_force: crate::TimeInForce::Day,
};
let result = self.core.execution_handler
.write()
.execute_order(order)
.await
.map_err(|e| Error::from_reason(e))?;
Ok(result.order_id)
}
#[napi]
pub async fn cancel_order(&self, order_id: String) -> Result<()> {
// Cancel order implementation
todo!("Cancel order implementation")
}
#[napi]
pub fn get_pending_orders(&self) -> Result<Vec<JsObject>> {
// Get pending orders
todo!("Get pending orders implementation")
}
#[napi]
pub fn get_order_history(&self) -> Result<Vec<JsObject>> {
// Get order history
todo!("Get order history implementation")
}
}