better Signals
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
general::{EventLog, MarketTrend, Position, Signal},
|
general::{EventLog, MarketTrend, Position},
|
||||||
plugin::{RiskManifest, StrategyManifest},
|
plugin::{RiskManifest, RiskSignal, StrategyManifest},
|
||||||
units::{Symbol, USD, Volatility},
|
units::{Symbol, USD, Volatility},
|
||||||
};
|
};
|
||||||
use hypersdk::{Decimal, hypercore::CandleInterval};
|
use hypersdk::{Decimal, hypercore::CandleInterval};
|
||||||
|
|
||||||
|
pub type SignalStatus = Result<RiskSignal, RiskSignal>;
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum TerminalServerMessage {
|
pub enum TerminalServerMessage {
|
||||||
// WatchList
|
// WatchList
|
||||||
@@ -17,7 +19,7 @@ pub enum TerminalServerMessage {
|
|||||||
StrategyUpdated(Strategy),
|
StrategyUpdated(Strategy),
|
||||||
|
|
||||||
// Signals
|
// Signals
|
||||||
SignalsUpdated(Vec<Signal>),
|
SignalsUpdated(Vec<SignalStatus>),
|
||||||
|
|
||||||
// Inspector
|
// Inspector
|
||||||
Inspect(InspectTarget),
|
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 command;
|
||||||
pub mod plugin;
|
pub mod plugin;
|
||||||
|
pub mod execution;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
engine::plugin::StrategyEngine,
|
engine::plugin::StrategyEngine,
|
||||||
@@ -23,6 +24,7 @@ pub struct Engine {
|
|||||||
pub config: Arc<Mutex<Config>>,
|
pub config: Arc<Mutex<Config>>,
|
||||||
pub accounts: Arc<Mutex<AccountList>>,
|
pub accounts: Arc<Mutex<AccountList>>,
|
||||||
pub watch_list: Arc<Mutex<WatchList>>,
|
pub watch_list: Arc<Mutex<WatchList>>,
|
||||||
|
pub signals: Arc<Mutex<Vec<SignalStatus>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Engine {
|
impl Engine {
|
||||||
@@ -43,6 +45,7 @@ impl Engine {
|
|||||||
name_to_index: HashMap::new(),
|
name_to_index: HashMap::new(),
|
||||||
items: Vec::new(),
|
items: Vec::new(),
|
||||||
})),
|
})),
|
||||||
|
signals: Arc::new(Mutex::new(Vec::new())),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
-95
@@ -1,7 +1,4 @@
|
|||||||
use hypersdk::hypercore::{
|
use hypersdk::hypercore::{self, CandleInterval, Subscription, WebSocket};
|
||||||
self, BatchOrder, CandleInterval, OrderRequest, OrderTypePlacement, Subscription, TimeInForce,
|
|
||||||
WebSocket,
|
|
||||||
};
|
|
||||||
use pulse_sdk::prelude::*;
|
use pulse_sdk::prelude::*;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::HashSet,
|
||||||
@@ -181,104 +178,37 @@ impl StrategyEngine {
|
|||||||
engine.terminal_server.log_raw(log).await?;
|
engine.terminal_server.log_raw(log).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(RiskMessage::Signal(RiskSignal::Approve(signal))) => {
|
Some(RiskMessage::Signal(ref sig @ RiskSignal::Approve(ref signal))) => {
|
||||||
let client = hypercore::mainnet();
|
match engine.execute_signal(signal).await {
|
||||||
let accounts = engine.accounts.lock().await;
|
Ok(_) => engine.signals.lock().await.push(Ok(sig.clone())),
|
||||||
|
Err(e) => {
|
||||||
|
engine.signals.lock().await.push(Err(sig.clone()));
|
||||||
|
|
||||||
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
|
engine
|
||||||
.terminal_server
|
.terminal_server
|
||||||
.error(
|
.error("signal", &format!("Failed to execute signal: {e}"))
|
||||||
"Engine::order",
|
.await?
|
||||||
&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(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
engine
|
|
||||||
.terminal_server
|
|
||||||
.error("Engine::order", &e.to_string())
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
engine
|
|
||||||
.terminal_server
|
|
||||||
.error("Engine::order", "Unable to get active account")
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
engine
|
||||||
|
.terminal_server
|
||||||
|
.broadcast(TerminalServerMessage::SignalsUpdated(
|
||||||
|
engine.signals.lock().await.clone(),
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(RiskMessage::Signal(RiskSignal::Reject {
|
Some(RiskMessage::Signal(reject)) => {
|
||||||
signal,
|
engine.signals.lock().await.push(Ok(reject));
|
||||||
rejection_confidence,
|
|
||||||
reason,
|
engine
|
||||||
})) => {}
|
.terminal_server
|
||||||
|
.broadcast(TerminalServerMessage::SignalsUpdated(
|
||||||
|
engine.signals.lock().await.clone(),
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+64
-10
@@ -58,17 +58,71 @@ impl Formatted for EventLog {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Formatted for Signal {
|
impl Formatted for SignalStatus {
|
||||||
fn get_formatted(&self) -> Vec<String> {
|
fn get_formatted(&self) -> Vec<String> {
|
||||||
vec![
|
match self {
|
||||||
if matches!(self.kind, Direction::Buy) {
|
Ok(RiskSignal::Approve(signal)) => {
|
||||||
format!("\x1b[32m{}\x1b[0m", self.kind)
|
vec![
|
||||||
} else {
|
format!("\x1b[33mOK\x1b[0m"),
|
||||||
format!("\x1b[31m{}\x1b[0m", self.kind)
|
if matches!(signal.kind, Direction::Buy) {
|
||||||
},
|
format!("\x1b[32mBUY\x1b[0m")
|
||||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
} else {
|
||||||
self.price.to_string(),
|
format!("\x1b[31mSELL\x1b[0m")
|
||||||
]
|
},
|
||||||
|
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(),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ pub struct PulseTradeApp {
|
|||||||
watch_list: State<Vec<MarketItem>>,
|
watch_list: State<Vec<MarketItem>>,
|
||||||
active_positions: State<Vec<Position>>,
|
active_positions: State<Vec<Position>>,
|
||||||
logs: State<Vec<EventLog>>,
|
logs: State<Vec<EventLog>>,
|
||||||
signals: State<Vec<Signal>>,
|
signals: State<Vec<SignalStatus>>,
|
||||||
inspect: State<InspectTarget>,
|
inspect: State<InspectTarget>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ impl TerminalClient {
|
|||||||
watch_list: State<Vec<MarketItem>>,
|
watch_list: State<Vec<MarketItem>>,
|
||||||
active_positions: State<Vec<Position>>,
|
active_positions: State<Vec<Position>>,
|
||||||
logs: State<Vec<EventLog>>,
|
logs: State<Vec<EventLog>>,
|
||||||
signals: State<Vec<Signal>>,
|
signals: State<Vec<SignalStatus>>,
|
||||||
market_overview: State<Option<Strategy>>,
|
market_overview: State<Option<Strategy>>,
|
||||||
status: State<Option<Status>>,
|
status: State<Option<Status>>,
|
||||||
inspect: State<InspectTarget>,
|
inspect: State<InspectTarget>,
|
||||||
|
|||||||
Reference in New Issue
Block a user