Daily scheduler
This commit is contained in:
@@ -63,6 +63,7 @@ pub struct Position {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct EngineStatus {
|
pub struct EngineStatus {
|
||||||
|
pub equity: Decimal,
|
||||||
pub strategy_mode: Mode,
|
pub strategy_mode: Mode,
|
||||||
pub strategy_state: ItemState,
|
pub strategy_state: ItemState,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use hypersdk::hypercore::ws::ConnectionStream;
|
use hypersdk::hypercore::ws::ConnectionStream;
|
||||||
use pulse_sdk::prelude::*;
|
use pulse_sdk::prelude::*;
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc, time::Duration};
|
||||||
use tokio::sync::Mutex;
|
use tokio::{sync::Mutex, time::Instant};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct WatchList {
|
pub struct WatchList {
|
||||||
@@ -66,6 +66,7 @@ impl Engine {
|
|||||||
|
|
||||||
// live data / status
|
// live data / status
|
||||||
status: Arc::new(Mutex::new(EngineStatus {
|
status: Arc::new(Mutex::new(EngineStatus {
|
||||||
|
equity: 0.into(),
|
||||||
strategy_mode: Mode::Auto,
|
strategy_mode: Mode::Auto,
|
||||||
strategy_state: ItemState::Stopped,
|
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> {
|
pub async fn get_config_status(&self) -> tokio::io::Result<EngineConfig> {
|
||||||
let manager = self.config.lock().await;
|
let manager = self.config.lock().await;
|
||||||
let config = manager.get_ref()?;
|
let config = manager.get_ref()?;
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ impl RiskEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn day_tick(&self) -> anyhow::Result<()> {
|
pub async fn day_tick(&self) -> anyhow::Result<()> {
|
||||||
|
println!("TICK");
|
||||||
|
|
||||||
let engine = self.get_engine();
|
let engine = self.get_engine();
|
||||||
|
|
||||||
let client = hypercore::mainnet();
|
let client = hypercore::mainnet();
|
||||||
|
|||||||
+8
-4
@@ -23,13 +23,17 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.run_event_stream(sides.strategy_stream),
|
.run_event_stream(sides.strategy_stream),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let daily_scheduler = tokio::spawn(engine.clone().run_daily_scheduler());
|
||||||
|
|
||||||
engine.run().await?;
|
engine.run().await?;
|
||||||
|
|
||||||
server.await??;
|
server.abort();
|
||||||
broadcaster.await??;
|
broadcaster.abort();
|
||||||
|
|
||||||
risk.await??;
|
risk.abort();
|
||||||
strategy.await??;
|
strategy.abort();
|
||||||
|
|
||||||
|
daily_scheduler.abort();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user