Subscriptions
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
use hypersdk::hypercore::{self, WebSocket};
|
||||
use hypersdk::hypercore::{self, CandleInterval, Subscription, WebSocket};
|
||||
use pulse_wire::prelude::*;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
path::PathBuf,
|
||||
process::Stdio,
|
||||
sync::{Arc, Weak},
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use tokio::{
|
||||
fs,
|
||||
process::{Child, Command},
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@@ -19,7 +22,9 @@ pub struct StrategyEngine {
|
||||
pub strategy: Arc<Plugin<StrategyEngineMessage, StrategyMessage, StrategyManifest>>,
|
||||
pub risk: Arc<Plugin<RiskEngineMessage, RiskMessage, RiskManifest>>,
|
||||
pub engine: Weak<Engine>,
|
||||
|
||||
pub ws: WebSocket,
|
||||
pub subscriptions: Mutex<HashSet<Subscription>>,
|
||||
}
|
||||
|
||||
impl StrategyEngine {
|
||||
@@ -44,6 +49,7 @@ impl StrategyEngine {
|
||||
risk: Arc::new(Plugin::new(risk, risk_manifest)),
|
||||
engine: Weak::new(),
|
||||
ws: hypercore::mainnet_ws(),
|
||||
subscriptions: Mutex::new(HashSet::new()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -75,13 +81,57 @@ impl StrategyEngine {
|
||||
risk.send(&RiskEngineMessage::Signal(signal)).await?;
|
||||
}
|
||||
|
||||
Some(StrategyMessage::Subscribe(val)) => {}
|
||||
Some(StrategyMessage::Subscribe(subscription)) => {
|
||||
self.ws.subscribe(subscription.clone());
|
||||
self.subscriptions.lock().await.insert(subscription);
|
||||
}
|
||||
|
||||
Some(StrategyMessage::Unsubscribe(subscription)) => {
|
||||
self.subscriptions.lock().await.remove(&subscription);
|
||||
self.ws.unsubscribe(subscription);
|
||||
}
|
||||
|
||||
Some(StrategyMessage::UnsubscribeAll) => {
|
||||
for sub in self.subscriptions.lock().await.drain() {
|
||||
self.ws.unsubscribe(sub);
|
||||
}
|
||||
}
|
||||
|
||||
Some(StrategyMessage::RequestOHLC {
|
||||
symbol,
|
||||
timeframe,
|
||||
interval,
|
||||
count,
|
||||
}) => {}
|
||||
}) => {
|
||||
let client = hypercore::mainnet();
|
||||
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as u64;
|
||||
|
||||
let interval_ms = match interval {
|
||||
CandleInterval::OneMinute => 60_000,
|
||||
CandleInterval::ThreeMinutes => 3 * 60_000,
|
||||
CandleInterval::FiveMinutes => 5 * 60_000,
|
||||
CandleInterval::FifteenMinutes => 15 * 60_000,
|
||||
CandleInterval::ThirtyMinutes => 30 * 60_000,
|
||||
CandleInterval::OneHour => 60 * 60_000,
|
||||
CandleInterval::TwoHours => 2 * 60 * 60_000,
|
||||
CandleInterval::FourHours => 4 * 60 * 60_000,
|
||||
CandleInterval::EightHours => 8 * 60 * 60_000,
|
||||
CandleInterval::TwelveHours => 12 * 60 * 60_000,
|
||||
CandleInterval::OneDay => 24 * 60 * 60_000,
|
||||
CandleInterval::ThreeDays => 3 * 24 * 60 * 60_000,
|
||||
CandleInterval::OneWeek => 7 * 24 * 60 * 60_000,
|
||||
CandleInterval::OneMonth => 30 * 24 * 60 * 60_000,
|
||||
};
|
||||
|
||||
let start_time = now.saturating_sub(interval_ms * count as u64);
|
||||
|
||||
client
|
||||
.candle_snapshot(symbol, interval, start_time, now)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ impl TerminalServer {
|
||||
risk: engine.strategy.risk.manifest.lock().await.clone(),
|
||||
mode: Mode::Auto,
|
||||
state: ItemState::Running,
|
||||
cooldown: TimeFrame::M15,
|
||||
cooldown: hypersdk::hypercore::CandleInterval::ThirtyMinutes,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -254,7 +254,7 @@ impl Formatted for Strategy {
|
||||
Triple("", "", ""),
|
||||
Triple(
|
||||
"\x1b[2mCooldown\x1b[0m",
|
||||
"\x1b[2mTimeframes\x1b[0m",
|
||||
"\x1b[2mStrat Author\x1b[0m",
|
||||
"\x1b[2mMax loss\x1b[0m",
|
||||
),
|
||||
Triple(
|
||||
@@ -262,7 +262,7 @@ impl Formatted for Strategy {
|
||||
"\x1b[96m{:?}\x1b[0m (\x1b[90m{:?} rec\x1b[0m)",
|
||||
self.cooldown, self.risk.cooldown
|
||||
),
|
||||
&format!("\x1b[96m{:?}\x1b[0m", self.strategy.timeframes),
|
||||
&format!("\x1b[96m{:?}\x1b[0m", self.strategy.author),
|
||||
&format!("\x1b[93m{}%\x1b[0m", self.risk.max_loss),
|
||||
),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user