Validate signal
This commit is contained in:
@@ -30,7 +30,7 @@ pub enum Mode {
|
|||||||
Manual,
|
Manual,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum Allocation {
|
pub enum Allocation {
|
||||||
Fixed(Decimal),
|
Fixed(Decimal),
|
||||||
Percent(Decimal),
|
Percent(Decimal),
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use hypersdk::{
|
|||||||
use pulse_sdk::general::Signal;
|
use pulse_sdk::general::Signal;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::engine::Engine;
|
use crate::{engine::Engine, store::config::RiskConfig};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct OrderIds {
|
pub struct OrderIds {
|
||||||
@@ -74,6 +74,8 @@ impl RiskEngine {
|
|||||||
.sum();
|
.sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.state.lock().await.pnl = 0.into();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,20 +142,25 @@ impl RiskEngine {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn validate_signal(&self, _signal: &mut Signal) -> bool {
|
pub async fn validate_signal(&self, _signal: &mut Signal) -> tokio::io::Result<bool> {
|
||||||
true
|
let state = self.state.lock().await;
|
||||||
|
|
||||||
|
let under_max_losses = state.pnl
|
||||||
|
< self
|
||||||
|
.get_risk_config()
|
||||||
|
.await?
|
||||||
|
.max_daily_loss
|
||||||
|
.get(state.starting_equity);
|
||||||
|
|
||||||
|
Ok(under_max_losses)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_order(&self) -> tokio::io::Result<OrderIds> {
|
pub async fn create_order(&self) -> tokio::io::Result<OrderIds> {
|
||||||
let state = self.state.lock().await;
|
let state = self.state.lock().await;
|
||||||
|
|
||||||
Ok(OrderIds::new(
|
Ok(OrderIds::new(
|
||||||
self.get_engine()
|
self.get_risk_config()
|
||||||
.config
|
.await?
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.get_ref()?
|
|
||||||
.risk
|
|
||||||
.risk_per_trade
|
.risk_per_trade
|
||||||
.get(state.starting_equity),
|
.get(state.starting_equity),
|
||||||
))
|
))
|
||||||
@@ -163,6 +170,10 @@ impl RiskEngine {
|
|||||||
self.orders.lock().await.push(order);
|
self.orders.lock().await.push(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_risk_config(&self) -> tokio::io::Result<RiskConfig> {
|
||||||
|
Ok(self.get_engine().config.lock().await.get_ref()?.risk)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_engine(&self) -> Arc<Engine> {
|
pub fn get_engine(&self) -> Arc<Engine> {
|
||||||
self.engine
|
self.engine
|
||||||
.upgrade()
|
.upgrade()
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ impl StrategyEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Some(StrategyMessage::Signal(mut signal)) => {
|
Some(StrategyMessage::Signal(mut signal)) => {
|
||||||
if !engine.risk_engine.validate_signal(&mut signal).await {
|
if !engine.risk_engine.validate_signal(&mut signal).await? {
|
||||||
engine.signals.lock().await.push(Err(signal));
|
engine.signals.lock().await.push(Err(signal));
|
||||||
|
|
||||||
engine
|
engine
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use pulse_sdk::general::Allocation;
|
use pulse_sdk::general::Allocation;
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct RiskConfig {
|
pub struct RiskConfig {
|
||||||
pub risk_per_trade: Allocation,
|
pub risk_per_trade: Allocation,
|
||||||
pub max_open_positions: u32,
|
pub max_open_positions: u32,
|
||||||
|
|||||||
Reference in New Issue
Block a user