Removed risk
This commit is contained in:
@@ -69,32 +69,6 @@ impl Engine {
|
||||
.await?;
|
||||
}
|
||||
|
||||
"risk" | "rs" => {
|
||||
set_cfg!(id, {
|
||||
let id: String = id;
|
||||
|
||||
if !crate::store::pulse_plugin(&id)?
|
||||
.join("risk.toml")
|
||||
.exists()
|
||||
{
|
||||
return self
|
||||
.terminal_server
|
||||
.error(
|
||||
"config::set::risk",
|
||||
&format!("Non existent risk `{id}`"),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
self.strategy.reload_risk(id.as_str()).await?;
|
||||
self.config.lock().await.risk = id;
|
||||
});
|
||||
|
||||
self.terminal_server
|
||||
.info("config::set", "risk set successfully, use `config save` to persist changes")
|
||||
.await?;
|
||||
}
|
||||
|
||||
"cooldown" | "cool" | "cd" => {
|
||||
set_cfg!(cooldown, {
|
||||
self.config.lock().await.cooldown = cooldown;
|
||||
@@ -102,7 +76,7 @@ impl Engine {
|
||||
self.terminal_server.broadcast(
|
||||
pulse_sdk::terminal::TerminalServerMessage::StrategyUpdated(Strategy {
|
||||
strategy: self.strategy.strategy.manifest.lock().await.clone(),
|
||||
risk: self.strategy.risk.manifest.lock().await.clone(),
|
||||
|
||||
mode: Mode::Auto,
|
||||
state: ItemState::Running,
|
||||
cooldown,
|
||||
@@ -114,7 +88,7 @@ impl Engine {
|
||||
|
||||
_ => {
|
||||
self.terminal_server
|
||||
.error("config::set", "Invalid usage, available options: watchlist, strategy, risk, cooldown")
|
||||
.error("config::set", "Invalid usage, available options: watchlist, strategy, cooldown")
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ impl Engine {
|
||||
pub async fn new() -> tokio::io::Result<Arc<Self>> {
|
||||
let config = Config::new().await?;
|
||||
|
||||
let strategy = StrategyEngine::new(&config.strategy, &config.risk).await?;
|
||||
let strategy = StrategyEngine::new(&config.strategy).await?;
|
||||
|
||||
let accounts = Arc::new(Mutex::new(AccountList::new().await?));
|
||||
let config = Arc::new(Mutex::new(config));
|
||||
|
||||
+19
-92
@@ -20,7 +20,6 @@ use crate::{
|
||||
|
||||
pub struct StrategyEngine {
|
||||
pub strategy: Arc<Plugin<StrategyEngineMessage, StrategyMessage, StrategyManifest>>,
|
||||
pub risk: Arc<Plugin<RiskEngineMessage, RiskMessage, RiskManifest>>,
|
||||
pub engine: Weak<Engine>,
|
||||
|
||||
pub ws: WebSocket,
|
||||
@@ -28,9 +27,8 @@ pub struct StrategyEngine {
|
||||
}
|
||||
|
||||
impl StrategyEngine {
|
||||
pub async fn new(strategy_id: &str, risk_id: &str) -> tokio::io::Result<Self> {
|
||||
pub async fn new(strategy_id: &str) -> tokio::io::Result<Self> {
|
||||
let strategy = pulse_plugin(strategy_id)?;
|
||||
let risk = pulse_plugin(risk_id)?;
|
||||
|
||||
let (strategy, strategy_manifest) = get_manifest_plugin_pair(
|
||||
&strategy,
|
||||
@@ -38,15 +36,8 @@ impl StrategyEngine {
|
||||
&fs::read(strategy.join("strategy.toml")).await?,
|
||||
)?;
|
||||
|
||||
let (risk, risk_manifest) = get_manifest_plugin_pair(
|
||||
&risk,
|
||||
&risk.join("risk.bash"),
|
||||
&fs::read(risk.join("risk.toml")).await?,
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
strategy: Arc::new(Plugin::new(strategy, strategy_manifest)),
|
||||
risk: Arc::new(Plugin::new(risk, risk_manifest)),
|
||||
engine: Weak::new(),
|
||||
ws: hypercore::mainnet_ws(),
|
||||
subscriptions: Mutex::new(HashSet::new()),
|
||||
@@ -87,7 +78,24 @@ impl StrategyEngine {
|
||||
}
|
||||
|
||||
Some(StrategyMessage::Signal(signal)) => {
|
||||
self.risk.send(&RiskEngineMessage::Signal(signal)).await?;
|
||||
match engine.execute_signal(&signal).await {
|
||||
Ok(_) => engine.signals.lock().await.push(Ok(signal)),
|
||||
Err(e) => {
|
||||
engine.signals.lock().await.push(Err(signal));
|
||||
|
||||
engine
|
||||
.terminal_server
|
||||
.error("signal", &format!("Failed to execute signal: {e}"))
|
||||
.await?
|
||||
}
|
||||
}
|
||||
|
||||
engine
|
||||
.terminal_server
|
||||
.broadcast(TerminalServerMessage::SignalsUpdated(
|
||||
engine.signals.lock().await.clone(),
|
||||
))
|
||||
.await?;
|
||||
}
|
||||
|
||||
Some(StrategyMessage::Subscribe(subscription)) => {
|
||||
@@ -153,74 +161,10 @@ impl StrategyEngine {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_risk(&self) -> tokio::io::Result<()> {
|
||||
let engine = self
|
||||
.engine
|
||||
.upgrade()
|
||||
.expect("Failed to upgrade engine (StrategyEngine)");
|
||||
|
||||
self.risk.send(&RiskEngineMessage::Initialize).await?;
|
||||
|
||||
loop {
|
||||
match self.risk.recv().await? {
|
||||
None => {}
|
||||
|
||||
Some(RiskMessage::GetWatchList) => {
|
||||
self.risk
|
||||
.send(&&RiskEngineMessage::WatchList(
|
||||
engine.watch_list.lock().await.clone().items,
|
||||
))
|
||||
.await?;
|
||||
}
|
||||
|
||||
Some(RiskMessage::Log(mut log)) => {
|
||||
log.name.insert_str(0, "risk::");
|
||||
engine.terminal_server.log_raw(log).await?;
|
||||
}
|
||||
|
||||
Some(RiskMessage::Signal(ref sig @ RiskSignal::Approve(ref signal))) => {
|
||||
match engine.execute_signal(signal).await {
|
||||
Ok(_) => engine.signals.lock().await.push(Ok(sig.clone())),
|
||||
Err(e) => {
|
||||
engine.signals.lock().await.push(Err(sig.clone()));
|
||||
|
||||
engine
|
||||
.terminal_server
|
||||
.error("signal", &format!("Failed to execute signal: {e}"))
|
||||
.await?
|
||||
}
|
||||
}
|
||||
|
||||
engine
|
||||
.terminal_server
|
||||
.broadcast(TerminalServerMessage::SignalsUpdated(
|
||||
engine.signals.lock().await.clone(),
|
||||
))
|
||||
.await?;
|
||||
}
|
||||
|
||||
Some(RiskMessage::Signal(reject)) => {
|
||||
engine.signals.lock().await.push(Ok(reject));
|
||||
|
||||
engine
|
||||
.terminal_server
|
||||
.broadcast(TerminalServerMessage::SignalsUpdated(
|
||||
engine.signals.lock().await.clone(),
|
||||
))
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn spawn(self: &Arc<Self>) {
|
||||
let engine = self.clone();
|
||||
|
||||
tokio::spawn(async move { engine.run_strategy().await });
|
||||
|
||||
let engine = self.clone();
|
||||
|
||||
tokio::spawn(async move { engine.run_risk().await });
|
||||
}
|
||||
|
||||
pub async fn reload_strategy(self: &Arc<Self>, id: &str) -> tokio::io::Result<()> {
|
||||
@@ -239,23 +183,6 @@ impl StrategyEngine {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn reload_risk(self: &Arc<Self>, id: &str) -> tokio::io::Result<()> {
|
||||
let plugin = pulse_plugin(id)?;
|
||||
|
||||
let (child, manifest) = get_manifest_plugin_pair(
|
||||
&plugin,
|
||||
&plugin.join("risk.bash"),
|
||||
&fs::read(plugin.join("risk.toml")).await?,
|
||||
)?;
|
||||
|
||||
self.risk.reload(child, manifest).await?;
|
||||
|
||||
let engine = self.clone();
|
||||
tokio::spawn(async move { engine.run_risk().await });
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_manifest_plugin_pair<'de, M: serde::Deserialize<'de>>(
|
||||
|
||||
@@ -4,7 +4,6 @@ use hypersdk::hypercore::CandleInterval;
|
||||
pub struct Config {
|
||||
pub watchlist: Vec<String>,
|
||||
pub strategy: String,
|
||||
pub risk: String,
|
||||
pub cooldown: CandleInterval,
|
||||
}
|
||||
|
||||
@@ -13,7 +12,6 @@ impl Default for Config {
|
||||
Self {
|
||||
watchlist: vec!["BTC".to_string(), "SOL".to_string(), "ETH".to_string()],
|
||||
strategy: String::new(),
|
||||
risk: String::new(),
|
||||
cooldown: CandleInterval::ThirtyMinutes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@ impl TerminalServer {
|
||||
id,
|
||||
pulse_sdk::terminal::TerminalServerMessage::StrategyUpdated(Strategy {
|
||||
strategy: engine.strategy.strategy.manifest.lock().await.clone(),
|
||||
risk: engine.strategy.risk.manifest.lock().await.clone(),
|
||||
mode: Mode::Auto,
|
||||
state: ItemState::Running,
|
||||
cooldown: engine.config.lock().await.cooldown,
|
||||
|
||||
Reference in New Issue
Block a user