Stop loss order management

This commit is contained in:
2026-08-01 19:16:38 +02:00
parent fdb0525ca2
commit f64d12d8c7
2 changed files with 25 additions and 6 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ use hypersdk::hypercore::{
}; };
use pulse_sdk::prelude::*; use pulse_sdk::prelude::*;
use crate::engine::{Engine, risk::OrderIds}; use crate::engine::Engine;
impl Engine { impl Engine {
pub async fn execute_signal(&self, signal: &Signal) -> tokio::io::Result<()> { pub async fn execute_signal(&self, signal: &Signal) -> tokio::io::Result<()> {
@@ -32,7 +32,7 @@ impl Engine {
return Ok(()); return Ok(());
}; };
let order_ids = OrderIds::new(); let order_ids = self.risk_engine.create_order().await?;
let order = BatchOrder { let order = BatchOrder {
orders: vec![ orders: vec![
+23 -4
View File
@@ -18,21 +18,23 @@ pub struct OrderIds {
pub entry: Cloid, pub entry: Cloid,
pub take_profit: Cloid, pub take_profit: Cloid,
pub stop_loss: Cloid, pub stop_loss: Cloid,
pub risk_equity: Decimal,
} }
impl OrderIds { impl OrderIds {
pub fn new() -> Self { pub fn new(risk_equity: Decimal) -> Self {
Self { Self {
entry: Cloid::random(), entry: Cloid::random(),
take_profit: Cloid::random(), take_profit: Cloid::random(),
stop_loss: Cloid::random(), stop_loss: Cloid::random(),
risk_equity,
} }
} }
} }
pub struct RiskState { pub struct RiskState {
pub starting_equity: Decimal, pub starting_equity: Decimal,
pub realized_pnl_today: Decimal, pub pnl: Decimal,
pub open_positions: usize, pub open_positions: usize,
} }
@@ -51,7 +53,7 @@ impl RiskEngine {
handle: Mutex::new(handle), handle: Mutex::new(handle),
state: Mutex::new(RiskState { state: Mutex::new(RiskState {
starting_equity: 0.into(), starting_equity: 0.into(),
realized_pnl_today: 0.into(), pnl: 0.into(),
open_positions: 0, open_positions: 0,
}), }),
engine, engine,
@@ -101,7 +103,9 @@ impl RiskEngine {
} }
for r in rm { for r in rm {
self.orders.lock().await.remove(r); let order = self.orders.lock().await.remove(r);
self.state.lock().await.pnl -= order.risk_equity;
} }
} }
} }
@@ -140,6 +144,21 @@ impl RiskEngine {
true true
} }
pub async fn create_order(&self) -> tokio::io::Result<OrderIds> {
let state = self.state.lock().await;
Ok(OrderIds::new(
self.get_engine()
.config
.lock()
.await
.get_ref()?
.risk
.risk_per_trade
.get(state.starting_equity),
))
}
pub async fn order_placed(&self, order: OrderIds) { pub async fn order_placed(&self, order: OrderIds) {
self.orders.lock().await.push(order); self.orders.lock().await.push(order);
} }