positions are now visible in python. can be check by if not sel.position

This commit is contained in:
KhizarImran
2026-07-02 22:12:20 +01:00
parent 53fe8e0e9d
commit 2af19afafa
4 changed files with 17 additions and 0 deletions
+4
View File
@@ -10,6 +10,10 @@ class Strategy:
self._bar: Any = None self._bar: Any = None
self._broker: Any = None self._broker: Any = None
@property
def positions(self):
return self._broker.positions() if self._broker else []
def init(self): def init(self):
pass pass
+4
View File
@@ -103,6 +103,10 @@ impl Broker {
} }
} }
pub fn positions(&self) -> Vec<Position> {
self.positions.clone()
}
pub fn buy( pub fn buy(
&mut self, &mut self,
price: f64, price: f64,
+1
View File
@@ -14,5 +14,6 @@ fn backtestingfx(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<stats::Stats>()?; m.add_class::<stats::Stats>()?;
m.add_class::<broker::Broker>()?; m.add_class::<broker::Broker>()?;
m.add_class::<engine::Engine>()?; m.add_class::<engine::Engine>()?;
m.add_class::<types::Position>()?;
Ok(()) Ok(())
} }
+8
View File
@@ -33,15 +33,23 @@ impl Bar {
} }
} }
#[pyclass]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Position { pub struct Position {
// this is for the trading position // this is for the trading position
#[pyo3(get)]
pub id: u64, pub id: u64,
#[pyo3(get)]
pub entry_price: f64, pub entry_price: f64,
#[pyo3(get)]
pub lot_size: f64, pub lot_size: f64,
#[pyo3(get)]
pub is_long: bool, pub is_long: bool,
#[pyo3(get)]
pub entry_timestamp: i64, pub entry_timestamp: i64,
#[pyo3(get)]
pub stop_loss: Option<f64>, pub stop_loss: Option<f64>,
#[pyo3(get)]
pub take_profit: Option<f64>, pub take_profit: Option<f64>,
} }