Daily scheduler
This commit is contained in:
@@ -10,8 +10,8 @@ use crate::{
|
||||
};
|
||||
use hypersdk::hypercore::ws::ConnectionStream;
|
||||
use pulse_sdk::prelude::*;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use tokio::sync::Mutex;
|
||||
use std::{collections::HashMap, sync::Arc, time::Duration};
|
||||
use tokio::{sync::Mutex, time::Instant};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WatchList {
|
||||
@@ -66,6 +66,7 @@ impl Engine {
|
||||
|
||||
// live data / status
|
||||
status: Arc::new(Mutex::new(EngineStatus {
|
||||
equity: 0.into(),
|
||||
strategy_mode: Mode::Auto,
|
||||
strategy_state: ItemState::Stopped,
|
||||
})),
|
||||
@@ -102,6 +103,39 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_daily_scheduler(self: Arc<Self>) -> anyhow::Result<()> {
|
||||
loop {
|
||||
self.risk_engine.day_tick().await?;
|
||||
|
||||
let now_local = chrono::Local::now();
|
||||
|
||||
let midnight = chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap();
|
||||
|
||||
let tomorrow_local = now_local
|
||||
.date_naive()
|
||||
.succ_opt()
|
||||
.unwrap()
|
||||
.and_time(midnight)
|
||||
.and_local_timezone(chrono::Local)
|
||||
.unwrap();
|
||||
|
||||
let duration_until_midnight = tomorrow_local.signed_duration_since(now_local);
|
||||
|
||||
let std_duration = Duration::from_secs(duration_until_midnight.num_seconds() as u64);
|
||||
|
||||
let deadline = Instant::now() + std_duration;
|
||||
|
||||
self.terminal_server
|
||||
.info(
|
||||
"engine::schedule",
|
||||
&format!("next daily reset: {tomorrow_local}"),
|
||||
)
|
||||
.await?;
|
||||
|
||||
tokio::time::sleep_until(deadline).await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_config_status(&self) -> tokio::io::Result<EngineConfig> {
|
||||
let manager = self.config.lock().await;
|
||||
let config = manager.get_ref()?;
|
||||
|
||||
@@ -61,6 +61,8 @@ impl RiskEngine {
|
||||
}
|
||||
|
||||
pub async fn day_tick(&self) -> anyhow::Result<()> {
|
||||
println!("TICK");
|
||||
|
||||
let engine = self.get_engine();
|
||||
|
||||
let client = hypercore::mainnet();
|
||||
|
||||
+8
-4
@@ -23,13 +23,17 @@ async fn main() -> anyhow::Result<()> {
|
||||
.run_event_stream(sides.strategy_stream),
|
||||
);
|
||||
|
||||
let daily_scheduler = tokio::spawn(engine.clone().run_daily_scheduler());
|
||||
|
||||
engine.run().await?;
|
||||
|
||||
server.await??;
|
||||
broadcaster.await??;
|
||||
server.abort();
|
||||
broadcaster.abort();
|
||||
|
||||
risk.await??;
|
||||
strategy.await??;
|
||||
risk.abort();
|
||||
strategy.abort();
|
||||
|
||||
daily_scheduler.abort();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user