Skip to content

Commit

Permalink
Colorize gains/losses
Browse files Browse the repository at this point in the history
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 ogham/rust-ansi-term#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.
  • Loading branch information
d-e-s-o committed Jan 18, 2020
1 parent 6eee991 commit 6b06046
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
49 changes: 37 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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, &currency),
cost_basis = format_price(&position.cost_basis, &currency),
unrealized_gain = format_price(&position.unrealized_gain_total, &currency),
unrealized_gain_pct = format_percent(&position.unrealized_gain_total_percent),
unrealized_gain_today = format_price(&position.unrealized_gain_today, &currency),
unrealized_gain_today_pct = format_percent(&position.unrealized_gain_today_percent),
unrealized_gain = format_gain(&position.unrealized_gain_total, &currency),
unrealized_gain_pct = format_percent_gain(&position.unrealized_gain_total_percent),
unrealized_gain_today = format_gain(&position.unrealized_gain_today, &currency),
unrealized_gain_today_pct = format_percent_gain(&position.unrealized_gain_today_percent),
current_price = format_price(&position.current_price, &currency),
last_price = format_price(&position.last_day_price, &currency),
);
Expand Down Expand Up @@ -1040,12 +1042,35 @@ fn format_price(price: &Num, currency: &str) -> String {
format!("{:.2} {}", price, currency)
}

fn format_colored<F>(value: &Num, format: F) -> Paint<String>
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<String> {
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<String> {
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());
Expand Down Expand Up @@ -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),
)
}

Expand All @@ -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),
);
}

Expand Down

0 comments on commit 6b06046

Please sign in to comment.