typed Syntax and improved formatting
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
use pulse_macros::pwp;
|
use pulse_macros::pwp;
|
||||||
|
|
||||||
use crate::PulseWire;
|
use crate::{
|
||||||
|
PulseWire,
|
||||||
|
units::{Symbol, USD},
|
||||||
|
};
|
||||||
|
|
||||||
#[pwp]
|
#[pwp]
|
||||||
pub enum MarketTrend {
|
pub enum MarketTrend {
|
||||||
@@ -28,9 +31,9 @@ pub struct Signal {
|
|||||||
pub kind: Direction,
|
pub kind: Direction,
|
||||||
pub confidence: f32,
|
pub confidence: f32,
|
||||||
pub size: f64,
|
pub size: f64,
|
||||||
pub price: f64,
|
pub price: USD,
|
||||||
pub take_profit: f64,
|
pub take_profit: USD,
|
||||||
pub take_stop_loss: f64,
|
pub stop_loss: USD,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pwp]
|
#[pwp]
|
||||||
@@ -50,10 +53,10 @@ pub struct EventLog {
|
|||||||
|
|
||||||
#[pwp]
|
#[pwp]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
pub symbol: String,
|
pub symbol: Symbol,
|
||||||
pub size: f64,
|
pub size: f64,
|
||||||
pub entry_price: f64,
|
pub entry_price: USD,
|
||||||
pub profit: f64,
|
pub profit: USD,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Direction {
|
impl std::fmt::Display for Direction {
|
||||||
@@ -65,7 +68,6 @@ impl std::fmt::Display for Direction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl std::fmt::Display for MarketTrend {
|
impl std::fmt::Display for MarketTrend {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub mod terminal;
|
|
||||||
pub mod general;
|
pub mod general;
|
||||||
|
pub mod terminal;
|
||||||
|
pub mod units;
|
||||||
|
|
||||||
|
pub mod prelude {
|
||||||
|
pub use crate::general::*;
|
||||||
|
pub use crate::terminal::*;
|
||||||
|
pub use crate::units::*;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn server_path() -> PathBuf {
|
pub fn server_path() -> PathBuf {
|
||||||
PathBuf::from("/tmp/pulse-engine.sock")
|
PathBuf::from("/tmp/pulse-engine.sock")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
PulseWire,
|
PulseWire,
|
||||||
general::{EventLog, MarketTrend, Position, Signal, Volatility},
|
general::{EventLog, MarketTrend, Position, Signal, Volatility},
|
||||||
|
units::{Symbol, USD},
|
||||||
};
|
};
|
||||||
use pulse_macros::pwp;
|
use pulse_macros::pwp;
|
||||||
|
|
||||||
@@ -36,10 +37,10 @@ pub enum TerminalClientMessage {
|
|||||||
|
|
||||||
#[pwp]
|
#[pwp]
|
||||||
pub struct MarketItem {
|
pub struct MarketItem {
|
||||||
pub symbol: String,
|
pub symbol: Symbol,
|
||||||
pub price: f64,
|
pub price: USD,
|
||||||
pub trend: f64,
|
pub trend: f64,
|
||||||
pub volume_24h: f64,
|
pub volume_24h: USD,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pwp]
|
#[pwp]
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
use crate::PulseWire;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Symbol(pub String);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct USD(pub f64);
|
||||||
|
|
||||||
|
impl std::fmt::Display for Symbol {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "\x1b[35m{}\x1b[0m", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for USD {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
if self.0 > 0.0 {
|
||||||
|
write!(f, "\x1b[32m${}\x1b[0m", format_f64(self.0))
|
||||||
|
} else {
|
||||||
|
write!(f, "\x1b[31m${}\x1b[0m", format_f64(self.0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PulseWire for Symbol {
|
||||||
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
||||||
|
Self(String::from_com(com))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_com(&self) -> Vec<u8> {
|
||||||
|
self.0.to_com()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PulseWire for USD {
|
||||||
|
fn from_com(com: &mut Vec<u8>) -> Self {
|
||||||
|
Self(f64::from_com(com))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_com(&self) -> Vec<u8> {
|
||||||
|
self.0.to_com()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use pulse_wire::general::Position;
|
use pulse_wire::{
|
||||||
|
general::Position,
|
||||||
|
units::{Symbol, USD},
|
||||||
|
};
|
||||||
use tokio::{sync::Mutex, task::JoinHandle};
|
use tokio::{sync::Mutex, task::JoinHandle};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -91,14 +94,14 @@ impl Engine {
|
|||||||
.asset_positions
|
.asset_positions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|position| Position {
|
.map(|position| Position {
|
||||||
symbol: position.position.coin,
|
symbol: Symbol(position.position.coin),
|
||||||
size: position.position.szi.as_f64(),
|
size: position.position.szi.as_f64(),
|
||||||
entry_price: position
|
entry_price: USD(position
|
||||||
.position
|
.position
|
||||||
.entry_px
|
.entry_px
|
||||||
.map(|px| px.as_f64())
|
.map(|px| px.as_f64())
|
||||||
.unwrap_or(0.0),
|
.unwrap_or(0.0)),
|
||||||
profit: position.position.unrealized_pnl.as_f64(),
|
profit: USD(position.position.unrealized_pnl.as_f64()),
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
|
|||||||
+7
-4
@@ -1,6 +1,9 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use pulse_wire::terminal::MarketItem;
|
use pulse_wire::{
|
||||||
|
terminal::MarketItem,
|
||||||
|
units::{Symbol, USD},
|
||||||
|
};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
fn number(value: &Value, field: &str) -> Result<f64, String> {
|
fn number(value: &Value, field: &str) -> Result<f64, String> {
|
||||||
@@ -64,10 +67,10 @@ pub async fn fetch_watch_list(
|
|||||||
by_symbol.insert(
|
by_symbol.insert(
|
||||||
symbol,
|
symbol,
|
||||||
MarketItem {
|
MarketItem {
|
||||||
symbol: symbol.to_owned(),
|
symbol: Symbol(symbol.to_owned()),
|
||||||
price,
|
price: USD(price),
|
||||||
|
volume_24h: USD(volume_24h),
|
||||||
trend: ((price / previous_day_price) - 1.0) * 100.0,
|
trend: ((price / previous_day_price) - 1.0) * 100.0,
|
||||||
volume_24h,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-66
@@ -3,6 +3,7 @@ use pulse_wire::terminal::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use pulse_wire::general::{Direction, EventLog, LogKind, Position, Signal};
|
use pulse_wire::general::{Direction, EventLog, LogKind, Position, Signal};
|
||||||
|
use pulse_wire::units::format_f64;
|
||||||
|
|
||||||
pub trait Formatted {
|
pub trait Formatted {
|
||||||
fn get_formatted(&self) -> Vec<String>;
|
fn get_formatted(&self) -> Vec<String>;
|
||||||
@@ -71,7 +72,7 @@ impl Formatted for Signal {
|
|||||||
format!("\x1b[31m{}\x1b[0m", self.kind)
|
format!("\x1b[31m{}\x1b[0m", self.kind)
|
||||||
},
|
},
|
||||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
||||||
format_f64(self.price),
|
self.price.to_string(),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,9 +80,9 @@ impl Formatted for Signal {
|
|||||||
impl Formatted for MarketItem {
|
impl Formatted for MarketItem {
|
||||||
fn get_formatted(&self) -> Vec<String> {
|
fn get_formatted(&self) -> Vec<String> {
|
||||||
vec![
|
vec![
|
||||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
self.symbol.to_string(),
|
||||||
format!("${}", format_f64(self.price)),
|
self.price.to_string(),
|
||||||
format!("${}", format_f64(self.volume_24h)),
|
self.volume_24h.to_string(),
|
||||||
format!(
|
format!(
|
||||||
"{} {}",
|
"{} {}",
|
||||||
if self.trend.is_sign_positive() {
|
if self.trend.is_sign_positive() {
|
||||||
@@ -98,17 +99,17 @@ impl Formatted for MarketItem {
|
|||||||
impl Formatted for Position {
|
impl Formatted for Position {
|
||||||
fn get_formatted(&self) -> Vec<String> {
|
fn get_formatted(&self) -> Vec<String> {
|
||||||
vec![
|
vec![
|
||||||
format!("\x1b[35m{}\x1b[0m", self.symbol),
|
self.symbol.to_string(),
|
||||||
format_f64(self.size),
|
format_f64(self.size),
|
||||||
format_f64(self.entry_price),
|
self.entry_price.to_string(),
|
||||||
format!(
|
format!(
|
||||||
"{} {}",
|
"{}{}\x1b[0m",
|
||||||
if self.profit.is_sign_positive() {
|
if self.profit.0.is_sign_positive() {
|
||||||
"\x1b[32m▲\x1b[0m"
|
"\x1b[32m"
|
||||||
} else {
|
} else {
|
||||||
"\x1b[31m▼\x1b[0m"
|
"\x1b[31m"
|
||||||
},
|
},
|
||||||
format_f64(self.profit.abs())
|
self.profit,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -206,58 +207,3 @@ pub fn apply_padding(mut items: Vec<String>) -> Vec<String> {
|
|||||||
|
|
||||||
items
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user