better Signals
This commit is contained in:
@@ -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())),
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
+25
-95
@@ -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;
|
||||
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()));
|
||||
|
||||
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(_) => {}
|
||||
Err(e) => {
|
||||
engine
|
||||
.terminal_server
|
||||
.error("Engine::order", &e.to_string())
|
||||
.await?;
|
||||
}
|
||||
.error("signal", &format!("Failed to execute signal: {e}"))
|
||||
.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 {
|
||||
signal,
|
||||
rejection_confidence,
|
||||
reason,
|
||||
})) => {}
|
||||
Some(RiskMessage::Signal(reject)) => {
|
||||
engine.signals.lock().await.push(Ok(reject));
|
||||
|
||||
engine
|
||||
.terminal_server
|
||||
.broadcast(TerminalServerMessage::SignalsUpdated(
|
||||
engine.signals.lock().await.clone(),
|
||||
))
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user