From 6b0604683528bc7a7e30f0a0abfecb9f7f486167 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Fri, 17 Jan 2020 16:28:14 -0800 Subject: [PATCH] Colorize gains/losses Colors can help a lot in decreasing the time it takes to understand a listing or at least infer where to look at more closely. To that end, this change enables the coloring of gains/losses when printing positions. There are various crates that allow us to colorize text in a terminal using ANSI escape sequences, all with different trade-offs. We currently rely on the yansi crate for doing this job. The downside is that it is a new dependency. Ideally we would want to use ansi_term instead (which is already pulled in by other dependencies), but it turns out that the color enabled strings it produces do not handle advanced formatting options as we use them properly. See https://github.com/ogham/rust-ansi-term/issues/59 for details. We also have used the colored crate successfully, but it does not support RGB colors (only predefined ones) and the default green is commonly too bright. Also, it comes with more dependencies than seemingly necessary. --- Cargo.toml | 1 + src/main.rs | 49 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c4d435a..e524070 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,4 @@ tokio = {version = "0.2", default-features = false, features = ["rt-core"]} tracing = {version = "0.1", default-features = false, features = ["std"]} tracing-subscriber = {version = "0.1.6", default-features = false, features = ["ansi", "chrono", "env-filter", "fmt"]} uuid = {version = "0.8", features = ["v4"]} +yansi = {version = "0.5", default-features = false} diff --git a/src/main.rs b/src/main.rs index 519e9b6..167dae2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,8 @@ use tracing_subscriber::FmtSubscriber; use uuid::Error as UuidError; use uuid::Uuid; +use yansi::Paint; + /// A command line client for automated trading with Alpaca. #[derive(Debug, StructOpt)] @@ -979,10 +981,10 @@ async fn position_get(client: Client, symbol: Symbol) -> Result<(), Error> { side = format_position_side(position.side), value = format_price(&position.market_value, ¤cy), cost_basis = format_price(&position.cost_basis, ¤cy), - unrealized_gain = format_price(&position.unrealized_gain_total, ¤cy), - unrealized_gain_pct = format_percent(&position.unrealized_gain_total_percent), - unrealized_gain_today = format_price(&position.unrealized_gain_today, ¤cy), - unrealized_gain_today_pct = format_percent(&position.unrealized_gain_today_percent), + unrealized_gain = format_gain(&position.unrealized_gain_total, ¤cy), + unrealized_gain_pct = format_percent_gain(&position.unrealized_gain_total_percent), + unrealized_gain_today = format_gain(&position.unrealized_gain_today, ¤cy), + unrealized_gain_today_pct = format_percent_gain(&position.unrealized_gain_today_percent), current_price = format_price(&position.current_price, ¤cy), last_price = format_price(&position.last_day_price, ¤cy), ); @@ -1040,12 +1042,35 @@ fn format_price(price: &Num, currency: &str) -> String { format!("{:.2} {}", price, currency) } +fn format_colored(value: &Num, format: F) -> Paint +where + F: Fn(&Num) -> String, +{ + if value.is_positive() { + Paint::rgb(0x00, 0x70, 0x00, format(value)) + } else if value.is_negative() { + Paint::red(format(value)) + } else { + Paint::black(format(value)) + } +} + +/// Format gain. +fn format_gain(price: &Num, currency: &str) -> Paint { + format_colored(price, |price| format_price(price, currency)) +} + /// Format a percentage value. fn format_percent(percent: &Num) -> String { format!("{:.2}%", percent * 100) } +/// Format percent gain. +fn format_percent_gain(percent: &Num) -> Paint { + format_colored(percent, format_percent) +} + /// Print a table with the given positions. fn position_print(positions: &[position::Position], currency: &str) { let qty_max = max_width(&positions, |p| format_quantity(&p.quantity).len()); @@ -1140,13 +1165,13 @@ fn position_print(positions: &[position::Position], currency: &str) { entry_width = entry_max, entry = format_price(&position.average_entry_price, currency), today_width = today_max, - today = format_price(&position.unrealized_gain_today, currency), + today = format_gain(&position.unrealized_gain_today, currency), today_pct_width = today_pct_max, - today_pct = format_percent(&position.unrealized_gain_today_percent), + today_pct = format_percent_gain(&position.unrealized_gain_today_percent), total_width = total_max, - total = format_price(&position.unrealized_gain_total, currency), + total = format_gain(&position.unrealized_gain_total, currency), total_pct_width = total_pct_max, - total_pct = format_percent(&position.unrealized_gain_total_percent), + total_pct = format_percent_gain(&position.unrealized_gain_total_percent), ) } @@ -1168,14 +1193,14 @@ fn position_print(positions: &[position::Position], currency: &str) { value_width = position_col, base = format_price(&base_value, currency), base_width = entry_max, - today = format_price(&today_gain, currency), - today_pct = format_percent(&today_gain_pct), + today = format_gain(&today_gain, currency), + today_pct = format_percent_gain(&today_gain_pct), today_pct_width = today_pct_max, today_width = today_max, total_width = total_max, - total = format_price(&total_gain, currency), + total = format_gain(&total_gain, currency), total_pct_width = total_pct_max, - total_pct = format_percent(&total_gain_pct), + total_pct = format_percent_gain(&total_gain_pct), ); }