Running strategy engine
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::{
|
||||
marker::PhantomData,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
use pulse_wire::{
|
||||
PulseWire,
|
||||
@@ -16,7 +19,7 @@ use tokio::{
|
||||
task::JoinHandle,
|
||||
};
|
||||
|
||||
use crate::store::pulse_plugin;
|
||||
use crate::{engine::Engine, store::pulse_plugin};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Plugin<S: PulseWire, R: PulseWire> {
|
||||
@@ -108,8 +111,10 @@ impl StrategyPair {
|
||||
pub struct StrategyEngine {
|
||||
pub pair: Mutex<StrategyPair>,
|
||||
|
||||
pub strategy_handle: Mutex<Option<JoinHandle<()>>>,
|
||||
pub risk_handle: Mutex<Option<JoinHandle<()>>>,
|
||||
pub strategy_handle: Mutex<Option<JoinHandle<tokio::io::Result<()>>>>,
|
||||
pub risk_handle: Mutex<Option<JoinHandle<tokio::io::Result<()>>>>,
|
||||
|
||||
pub engine: Weak<Engine>,
|
||||
}
|
||||
|
||||
impl StrategyEngine {
|
||||
@@ -118,9 +123,49 @@ impl StrategyEngine {
|
||||
pair: Mutex::new(StrategyPair::new(strategy_id, risk_id).await?),
|
||||
strategy_handle: Mutex::new(None),
|
||||
risk_handle: Mutex::new(None),
|
||||
engine: Weak::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn initialize(mut self, engine: Weak<Engine>) -> Arc<Self> {
|
||||
self.engine = engine;
|
||||
|
||||
Arc::new(self)
|
||||
}
|
||||
|
||||
pub async fn run_strategy(&self) -> tokio::io::Result<()> {
|
||||
let engine = self
|
||||
.engine
|
||||
.upgrade()
|
||||
.expect("Failed to upgrade engine (StrategyEngine)");
|
||||
|
||||
loop {}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_risk(&self) -> tokio::io::Result<()> {
|
||||
let engine = self
|
||||
.engine
|
||||
.upgrade()
|
||||
.expect("Failed to upgrade engine (StrategyEngine)");
|
||||
|
||||
loop {}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn spawn(self: &Arc<Self>) {
|
||||
let engine = self.clone();
|
||||
|
||||
*self.strategy_handle.lock().await =
|
||||
Some(tokio::spawn(async move { engine.run_strategy().await }));
|
||||
|
||||
let engine = self.clone();
|
||||
|
||||
*self.risk_handle.lock().await = Some(tokio::spawn(async move { engine.run_risk().await }));
|
||||
}
|
||||
|
||||
pub async fn reload(&self, strategy_id: &str, risk_id: &str) -> tokio::io::Result<()> {
|
||||
if let Some(handle) = &*self.strategy_handle.lock().await {
|
||||
handle.abort();
|
||||
|
||||
Reference in New Issue
Block a user