better Signals
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
use crate::{
|
||||
general::{EventLog, MarketTrend, Position, Signal},
|
||||
plugin::{RiskManifest, StrategyManifest},
|
||||
general::{EventLog, MarketTrend, Position},
|
||||
plugin::{RiskManifest, RiskSignal, StrategyManifest},
|
||||
units::{Symbol, USD, Volatility},
|
||||
};
|
||||
use hypersdk::{Decimal, hypercore::CandleInterval};
|
||||
|
||||
pub type SignalStatus = Result<RiskSignal, RiskSignal>;
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub enum TerminalServerMessage {
|
||||
// WatchList
|
||||
@@ -17,7 +19,7 @@ pub enum TerminalServerMessage {
|
||||
StrategyUpdated(Strategy),
|
||||
|
||||
// Signals
|
||||
SignalsUpdated(Vec<Signal>),
|
||||
SignalsUpdated(Vec<SignalStatus>),
|
||||
|
||||
// Inspector
|
||||
Inspect(InspectTarget),
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
use hypersdk::hypercore::{self, BatchOrder, OrderRequest, OrderTypePlacement, TimeInForce};
|
||||
use pulse_sdk::prelude::*;
|
||||
|
||||
use crate::engine::Engine;
|
||||
|
||||
impl Engine {
|
||||
pub async fn execute_signal(&self, signal: &Signal) -> tokio::io::Result<()> {
|
||||
let client = hypercore::mainnet();
|
||||
let accounts = self.accounts.lock().await;
|
||||
|
||||
if let Some(acc) = accounts.get_active() {
|
||||
let Some(asset_id) = self
|
||||
.watch_list
|
||||
.lock()
|
||||
.await
|
||||
.name_to_index
|
||||
.get(&signal.symbol)
|
||||
.cloned()
|
||||
else {
|
||||
self.terminal_server
|
||||
.error(
|
||||
"self::order",
|
||||
&format!(
|
||||
"Invalid Symbol: {:?}, Unable to get asset id",
|
||||
signal.symbol
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let order = BatchOrder {
|
||||
orders: vec![
|
||||
OrderRequest {
|
||||
asset: asset_id,
|
||||
is_buy: matches!(signal.kind, Direction::Buy),
|
||||
limit_px: signal.price.0,
|
||||
sz: signal.size,
|
||||
reduce_only: false,
|
||||
order_type: OrderTypePlacement::Limit {
|
||||
tif: TimeInForce::Gtc,
|
||||
},
|
||||
cloid: Default::default(),
|
||||
},
|
||||
OrderRequest {
|
||||
asset: asset_id,
|
||||
is_buy: matches!(signal.kind, Direction::Buy),
|
||||
limit_px: signal.price.0,
|
||||
sz: signal.size,
|
||||
reduce_only: true,
|
||||
order_type: OrderTypePlacement::Trigger {
|
||||
is_market: true,
|
||||
trigger_px: signal.take_profit.0,
|
||||
tpsl: hypercore::TpSl::Tp,
|
||||
},
|
||||
cloid: Default::default(),
|
||||
},
|
||||
OrderRequest {
|
||||
asset: asset_id,
|
||||
is_buy: matches!(signal.kind, Direction::Buy),
|
||||
limit_px: signal.price.0,
|
||||
sz: signal.size,
|
||||
reduce_only: true,
|
||||
order_type: OrderTypePlacement::Trigger {
|
||||
is_market: true,
|
||||
trigger_px: signal.stop_loss.0,
|
||||
tpsl: hypercore::TpSl::Sl,
|
||||
},
|
||||
cloid: Default::default(),
|
||||
},
|
||||
],
|
||||
grouping: hypercore::OrderGrouping::Na,
|
||||
builder: None,
|
||||
};
|
||||
|
||||
let nonce = chrono::Utc::now().timestamp_millis() as u64;
|
||||
|
||||
match client
|
||||
.place(&acc.private_key.0, order, nonce, None, None)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
self.terminal_server
|
||||
.error("self::order", &e.to_string())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.terminal_server
|
||||
.error("Engine::order", "Unable to get active account")
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod command;
|
||||
pub mod plugin;
|
||||
pub mod execution;
|
||||
|
||||
use crate::{
|
||||
engine::plugin::StrategyEngine,
|
||||
@@ -23,6 +24,7 @@ pub struct Engine {
|
||||
pub config: Arc<Mutex<Config>>,
|
||||
pub accounts: Arc<Mutex<AccountList>>,
|
||||
pub watch_list: Arc<Mutex<WatchList>>,
|
||||
pub signals: Arc<Mutex<Vec<SignalStatus>>>,
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
@@ -43,6 +45,7 @@ impl Engine {
|
||||
name_to_index: HashMap::new(),
|
||||
items: Vec::new(),
|
||||
})),
|
||||
signals: Arc::new(Mutex::new(Vec::new())),
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
+26
-96
@@ -1,7 +1,4 @@
|
||||
use hypersdk::hypercore::{
|
||||
self, BatchOrder, CandleInterval, OrderRequest, OrderTypePlacement, Subscription, TimeInForce,
|
||||
WebSocket,
|
||||
};
|
||||
use hypersdk::hypercore::{self, CandleInterval, Subscription, WebSocket};
|
||||
use pulse_sdk::prelude::*;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
@@ -181,104 +178,37 @@ impl StrategyEngine {
|
||||
engine.terminal_server.log_raw(log).await?;
|
||||
}
|
||||
|
||||
Some(RiskMessage::Signal(RiskSignal::Approve(signal))) => {
|
||||
let client = hypercore::mainnet();
|
||||
let accounts = engine.accounts.lock().await;
|
||||
|
||||
if let Some(acc) = accounts.get_active() {
|
||||
let Some(asset_id) = engine
|
||||
.watch_list
|
||||
.lock()
|
||||
.await
|
||||
.name_to_index
|
||||
.get(&signal.symbol)
|
||||
.cloned()
|
||||
else {
|
||||
engine
|
||||
.terminal_server
|
||||
.error(
|
||||
"Engine::order",
|
||||
&format!(
|
||||
"Invalid Symbol: {:?}, Unable to get asset id",
|
||||
signal.symbol
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
|
||||
continue;
|
||||
};
|
||||
|
||||
let order = BatchOrder {
|
||||
orders: vec![
|
||||
OrderRequest {
|
||||
asset: asset_id,
|
||||
is_buy: matches!(signal.kind, Direction::Buy),
|
||||
limit_px: signal.price.0,
|
||||
sz: signal.size,
|
||||
reduce_only: false,
|
||||
order_type: OrderTypePlacement::Limit {
|
||||
tif: TimeInForce::Gtc,
|
||||
},
|
||||
cloid: Default::default(),
|
||||
},
|
||||
OrderRequest {
|
||||
asset: asset_id,
|
||||
is_buy: matches!(signal.kind, Direction::Buy),
|
||||
limit_px: signal.price.0,
|
||||
sz: signal.size,
|
||||
reduce_only: true,
|
||||
order_type: OrderTypePlacement::Trigger {
|
||||
is_market: true,
|
||||
trigger_px: signal.take_profit.0,
|
||||
tpsl: hypercore::TpSl::Tp,
|
||||
},
|
||||
cloid: Default::default(),
|
||||
},
|
||||
OrderRequest {
|
||||
asset: asset_id,
|
||||
is_buy: matches!(signal.kind, Direction::Buy),
|
||||
limit_px: signal.price.0,
|
||||
sz: signal.size,
|
||||
reduce_only: true,
|
||||
order_type: OrderTypePlacement::Trigger {
|
||||
is_market: true,
|
||||
trigger_px: signal.stop_loss.0,
|
||||
tpsl: hypercore::TpSl::Sl,
|
||||
},
|
||||
cloid: Default::default(),
|
||||
},
|
||||
],
|
||||
grouping: hypercore::OrderGrouping::Na,
|
||||
builder: None,
|
||||
};
|
||||
|
||||
let nonce = chrono::Utc::now().timestamp_millis() as u64;
|
||||
|
||||
match client
|
||||
.place(&acc.private_key.0, order, nonce, None, None)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
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("Engine::order", &e.to_string())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
engine
|
||||
.terminal_server
|
||||
.error("Engine::order", "Unable to get active account")
|
||||
.await?;
|
||||
.error("signal", &format!("Failed to execute signal: {e}"))
|
||||
.await?
|
||||
}
|
||||
}
|
||||
|
||||
Some(RiskMessage::Signal(RiskSignal::Reject {
|
||||
signal,
|
||||
rejection_confidence,
|
||||
reason,
|
||||
})) => {}
|
||||
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?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,18 +58,72 @@ impl Formatted for EventLog {
|
||||
}
|
||||
}
|
||||
|
||||
impl Formatted for Signal {
|
||||
impl Formatted for SignalStatus {
|
||||
fn get_formatted(&self) -> Vec<String> {
|
||||
match self {
|
||||
Ok(RiskSignal::Approve(signal)) => {
|
||||
vec![
|
||||
if matches!(self.kind, Direction::Buy) {
|
||||
format!("\x1b[32m{}\x1b[0m", self.kind)
|
||||
format!("\x1b[33mOK\x1b[0m"),
|
||||
if matches!(signal.kind, Direction::Buy) {
|
||||
format!("\x1b[32mBUY\x1b[0m")
|
||||
} else {
|
||||
format!("\x1b[31m{}\x1b[0m", self.kind)
|
||||
format!("\x1b[31mSELL\x1b[0m")
|
||||
},
|
||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
||||
self.price.to_string(),
|
||||
format!("\x1b[35m{}\x1b[0m", signal.symbol),
|
||||
signal.price.to_string(),
|
||||
format!("\x1b[33mAPR {}\x1b[0m", signal.confidence),
|
||||
]
|
||||
}
|
||||
Ok(RiskSignal::Reject {
|
||||
signal,
|
||||
rejection_confidence,
|
||||
reason,
|
||||
}) => {
|
||||
vec![
|
||||
format!("\x1b[33mOK\x1b[0m"),
|
||||
if matches!(signal.side, Direction::Buy) {
|
||||
format!("\x1b[32mBUY\x1b[0m")
|
||||
} else {
|
||||
format!("\x1b[31mSELL\x1b[0m")
|
||||
},
|
||||
format!("\x1b[35m{}\x1b[0m", signal.symbol),
|
||||
format!("\x1b[31mREJ {}\x1b[0m", rejection_confidence),
|
||||
reason.to_owned(),
|
||||
]
|
||||
}
|
||||
|
||||
Err(RiskSignal::Approve(signal)) => {
|
||||
vec![
|
||||
format!("\x1b[31mERR\x1b[0m"),
|
||||
if matches!(signal.kind, Direction::Buy) {
|
||||
format!("\x1b[32mBUY\x1b[0m")
|
||||
} else {
|
||||
format!("\x1b[31mSELL\x1b[0m")
|
||||
},
|
||||
format!("\x1b[35m{}\x1b[0m", signal.symbol),
|
||||
signal.price.to_string(),
|
||||
format!("\x1b[33mAPR {}\x1b[0m", signal.confidence),
|
||||
]
|
||||
}
|
||||
Err(RiskSignal::Reject {
|
||||
signal,
|
||||
rejection_confidence,
|
||||
reason,
|
||||
}) => {
|
||||
vec![
|
||||
format!("\x1b[31mERR\x1b[0m"),
|
||||
if matches!(signal.side, Direction::Buy) {
|
||||
format!("\x1b[32mBUY\x1b[0m")
|
||||
} else {
|
||||
format!("\x1b[31mSELL\x1b[0m")
|
||||
},
|
||||
format!("\x1b[35m{}\x1b[0m", signal.symbol),
|
||||
format!("\x1b[31mREJ {}\x1b[0m", rejection_confidence),
|
||||
reason.to_owned(),
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Formatted for MarketItem {
|
||||
|
||||
@@ -35,7 +35,7 @@ pub struct PulseTradeApp {
|
||||
watch_list: State<Vec<MarketItem>>,
|
||||
active_positions: State<Vec<Position>>,
|
||||
logs: State<Vec<EventLog>>,
|
||||
signals: State<Vec<Signal>>,
|
||||
signals: State<Vec<SignalStatus>>,
|
||||
inspect: State<InspectTarget>,
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ impl TerminalClient {
|
||||
watch_list: State<Vec<MarketItem>>,
|
||||
active_positions: State<Vec<Position>>,
|
||||
logs: State<Vec<EventLog>>,
|
||||
signals: State<Vec<Signal>>,
|
||||
signals: State<Vec<SignalStatus>>,
|
||||
market_overview: State<Option<Strategy>>,
|
||||
status: State<Option<Status>>,
|
||||
inspect: State<InspectTarget>,
|
||||
|
||||
Reference in New Issue
Block a user