This commit is contained in:
2026-07-30 04:44:21 +02:00
parent 8dcf6b2a41
commit 87e3c2b964
8 changed files with 95 additions and 236 deletions
+12 -12
View File
@@ -1,4 +1,4 @@
use hypersdk::hypercore::{self, BatchOrder, OrderRequest, OrderTypePlacement, TimeInForce};
use hypersdk::hypercore::{self, BatchOrder, OrderRequest, OrderTypePlacement, Side, TimeInForce};
use pulse_sdk::prelude::*;
use crate::engine::Engine;
@@ -34,9 +34,9 @@ impl Engine {
orders: vec![
OrderRequest {
asset: asset_id,
is_buy: matches!(signal.kind, Direction::Buy),
limit_px: signal.price.0,
sz: signal.size,
is_buy: matches!(signal.side, Side::Bid),
limit_px: signal.price,
sz: 0.into(),
reduce_only: false,
order_type: OrderTypePlacement::Limit {
tif: TimeInForce::Gtc,
@@ -45,26 +45,26 @@ impl Engine {
},
OrderRequest {
asset: asset_id,
is_buy: matches!(signal.kind, Direction::Buy),
limit_px: signal.price.0,
sz: signal.size,
is_buy: matches!(signal.side, Side::Bid),
limit_px: signal.price,
sz: 0.into(),
reduce_only: true,
order_type: OrderTypePlacement::Trigger {
is_market: true,
trigger_px: signal.take_profit.0,
trigger_px: signal.take_profit,
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,
is_buy: matches!(signal.side, Side::Bid),
limit_px: signal.price,
sz: 0.into(),
reduce_only: true,
order_type: OrderTypePlacement::Trigger {
is_market: true,
trigger_px: signal.stop_loss.0,
trigger_px: signal.stop_loss,
tpsl: hypercore::TpSl::Sl,
},
cloid: Default::default(),
+4 -7
View File
@@ -104,13 +104,10 @@ impl Engine {
.asset_positions
.into_iter()
.map(|position| Position {
symbol: Symbol(position.position.coin),
size: position.position.szi.as_f64(),
entry_price: USD(position
.position
.entry_px
.unwrap_or_default()),
profit: USD(position.position.unrealized_pnl),
symbol: position.position.coin,
size: position.position.szi,
entry_price: position.position.entry_px.unwrap_or_default(),
pnl: position.position.unrealized_pnl,
})
.collect(),
))
+4 -4
View File
@@ -1,5 +1,5 @@
use rust_decimal::Decimal;
use pulse_sdk::prelude::*;
use rust_decimal::Decimal;
use serde_json::Value;
use std::collections::HashMap;
@@ -80,9 +80,9 @@ pub async fn fetch_watch_list(
}
items.push(MarketItem {
symbol: Symbol(symbol.clone()),
price: USD(price),
volume_24h: USD(volume_24h),
symbol: symbol.clone(),
price: price,
volume_24h: volume_24h,
trend: ((price / previous_day_price) - <Decimal as From<i32>>::from(1))
* <Decimal as From<i32>>::from(100),
});
+61 -47
View File
@@ -1,3 +1,4 @@
use hypersdk::hypercore::Side;
use pulse_sdk::prelude::*;
pub trait Formatted {
@@ -22,23 +23,6 @@ impl Formatted for InspectTarget {
}
}
impl Formatted for Alert {
fn get_formatted(&self) -> Vec<String> {
vec![
format!(
"{}{}\x1b[0m",
match &self.level {
AlertLevel::Low => "\x1b[34m",
AlertLevel::Medium => "\x1b[33m",
AlertLevel::High => "\x1b[31m",
},
self.level
),
self.message.clone(),
]
}
}
impl Formatted for EventLog {
fn get_formatted(&self) -> Vec<String> {
vec![
@@ -64,7 +48,7 @@ impl Formatted for SignalStatus {
Ok(signal) => {
vec![
format!("\x1b[33mOK\x1b[0m"),
if matches!(signal.kind, Direction::Buy) {
if matches!(signal.side, Side::Ask) {
format!("\x1b[32mBUY\x1b[0m")
} else {
format!("\x1b[31mSELL\x1b[0m")
@@ -78,7 +62,7 @@ impl Formatted for SignalStatus {
Err(signal) => {
vec![
format!("\x1b[31mERR\x1b[0m"),
if matches!(signal.kind, Direction::Buy) {
if matches!(signal.side, Side::Ask) {
format!("\x1b[32mBUY\x1b[0m")
} else {
format!("\x1b[31mSELL\x1b[0m")
@@ -115,16 +99,16 @@ impl Formatted for Position {
fn get_formatted(&self) -> Vec<String> {
vec![
self.symbol.to_string(),
format_f64(self.size),
format_f64(self.size.as_f64()),
self.entry_price.to_string(),
format!(
"{}{}\x1b[0m",
if self.profit.0.is_sign_positive() {
if self.pnl.is_sign_positive() {
"\x1b[32m"
} else {
"\x1b[31m"
},
self.profit,
self.pnl,
),
]
}
@@ -134,31 +118,6 @@ struct Pair<'a>(&'a str, &'a str);
struct Triple<'a>(&'a str, &'a str, &'a str);
impl Formatted for MarketOverview {
fn get_formatted(&self) -> Vec<String> {
let mut o = vec![
Pair("TREND", &format!("{}", self.trend)),
Pair("VOLATILITY", &format!("{}", self.volatility)),
Pair(
"PRESSURE",
&if self.pressure.is_sign_positive() {
format!("\x1b[32m{:.3}\x1b[0m", self.pressure)
} else {
format!("\x1b[31m{:.3}\x1b[0m", self.pressure)
},
),
]
.get_formatted();
o.push(format!(
"\n{}",
apply_padding(self.alerts.get_formatted()).join("\n")
));
o
}
}
impl Formatted for Status {
fn get_formatted(&self) -> Vec<String> {
vec![
@@ -272,3 +231,58 @@ pub fn apply_padding(mut items: Vec<String>) -> Vec<String> {
items
}
pub fn format_f64(value: f64) -> String {
let abs = value.abs();
let (divisor, suffix) = if abs >= 1_000_000_000.0 {
(1_000_000_000.0, "B")
} else if abs >= 1_000_000.0 {
(1_000_000.0, "M")
} else if abs >= 1_000.0 {
(1_000.0, "K")
} else {
(1.0, "")
};
if divisor != 1.0 {
let formatted = value / divisor;
// Remove unnecessary trailing zeros
let s = format!("{:.2}", formatted)
.trim_end_matches('0')
.trim_end_matches('.')
.to_string();
return format!("{}{}", s, suffix);
}
let val = format!("{:.3}", value);
let parts: Vec<&str> = val.split('.').collect();
let int = parts[0].to_string();
let negative = int.starts_with('-');
let start = if negative { 1 } else { 0 };
let mut result = String::new();
for (i, c) in int[start..].chars().rev().enumerate() {
if i > 0 && i % 3 == 0 {
result.push(',');
}
result.push(c);
}
let mut formatted: String = result.chars().rev().collect();
if negative {
formatted.insert(0, '-');
}
if parts.len() > 1 {
formatted.push('.');
formatted.push_str(parts[1]);
}
formatted
}