374 lines
20 KiB
C#
374 lines
20 KiB
C#
using bts;
|
|
using bts.api;
|
|
using bts.collections;
|
|
using bts.api.md.auxiliary;
|
|
using mbts.co2;
|
|
using mbts.ui;
|
|
using mbts.ui.controls;
|
|
using mbts.ui.utility.text;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
|
|
namespace mbts.spark {
|
|
static class BadgeRender {
|
|
// IMPORTANT: the return values for this method MUST BE IN SYNC with the "format" method, else "measure_height" and "LadderRender.measure_row_height" will be broken.
|
|
static int _line_count_for_style(LadderSettings.BadgeStyle badge_style) => badge_style == LadderSettings.BadgeStyle.Alternate ? 2 : 1;
|
|
// IMPORTANT: at the risk of being redundant, see above comment.
|
|
public static IReadOnlyList<string> format(Side side, uint quantity, uint filled, LadderSettings.BadgeStyle style) {
|
|
switch (style) {
|
|
case LadderSettings.BadgeStyle.Alternate:
|
|
return new[] {
|
|
$"{side.abbreviate()}:{filled}",
|
|
$"W:{quantity - filled}"
|
|
};
|
|
default: return new[] { $"{filled}/{quantity}" };
|
|
}
|
|
}
|
|
|
|
public static double measure_height(LadderSettings.BadgeStyle badge_style, Thickness border_thickness, Thickness padding, double font_size) =>
|
|
measure_height(_line_count_for_style(badge_style), border_thickness, padding, font_size);
|
|
|
|
public static double measure_height(int line_count, Thickness border_thickness, Thickness padding, double font_size) =>
|
|
WPFHelper.control_base_height(border_thickness, padding) + font_size * line_count + Badge.LINE_SPACING * (line_count - 1);
|
|
}
|
|
|
|
static class LadderRender {
|
|
// Note: the orders-column has a separate "row_padding" value: Badge.DefaultMargin
|
|
// (see how it is incorporated in measure_row_height() below)
|
|
public static readonly Thickness row_padding = new Thickness(6);
|
|
const string tick_separator_period = ".";
|
|
const string tick_separator_dash = "-";
|
|
|
|
// IMPORTANT: this method ONLY WORKS if Badges use the BadgeRender.margin/border_thickness/padding values.
|
|
// TODO: elminate the dependency on BadgeRender.margin/border_thickness/padding by integrating them into settings.
|
|
public static double measure_row_height(LadderSettings settings) =>
|
|
Math.Max(settings.visible_columns().Max(c => c.font_size, 0) + row_padding.Top + row_padding.Bottom,
|
|
BadgeRender.measure_height(
|
|
settings.badge_style, Badge.DefaultBorderThickness, Badge.DefaultPadding, settings.orders_column.font_size
|
|
) + Badge.DefaultMargin.Top + Badge.DefaultMargin.Bottom);
|
|
|
|
public static int index_of(double[] prices, double price, IComparer<double> comparer) =>
|
|
Array.BinarySearch(prices, price, comparer);
|
|
|
|
public static GlyphRun format_text(string text, int row, double row_height, LadderColumnSettings settings, GlyphContext glyph_context, LadderSettings.PriceDisplayFormat price_display_format = LadderSettings.PriceDisplayFormat.Default) =>
|
|
glyph_context.get_glyph_run(
|
|
// long term there is probably a better way to get rid of the separator, such as in the TickExtensions._format_tick
|
|
price_display_format == LadderSettings.PriceDisplayFormat.Integer ? text.Replace(tick_separator_period, string.Empty).Replace(tick_separator_dash, string.Empty) : text,
|
|
settings.font_size, settings.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, row * row_height, true);
|
|
|
|
public static double[] gen_prices(double mid, int row_count, ContractSpecificationImmutable spec) {
|
|
// require odd number
|
|
Log.assert(row_count % 2 == 1);
|
|
var result = new double[row_count];
|
|
int range = row_count / 2;
|
|
int middle = row_count / 2;
|
|
result[middle] = mid;
|
|
double up = mid, down = mid;
|
|
// Starting from the middle iterate out to the edges
|
|
for (int x = 1; x <= range; ++x) {
|
|
up = spec.tickup(up);
|
|
down = spec.tickdown(down);
|
|
result[middle - x] = up;
|
|
result[middle + x] = down;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static GlyphRun[] format_prices(double[] prices, double reference_price, bool format_in_tick_terms, double row_height, LadderColumnSettings settings, GlyphContext glyph_context, ContractSpecificationImmutable spec) {
|
|
var result = new GlyphRun[prices.Length];
|
|
Parallel.For(0, prices.Length, i =>
|
|
result[i] = format_text(spec.format(prices[i], reference_price, format_in_tick_terms), i, row_height, settings, glyph_context));
|
|
return result;
|
|
}
|
|
public static GlyphRun[] format_prices(double[] prices, double row_height, LadderColumnSettings settings, GlyphContext glyph_context, ContractSpecificationImmutable spec, LadderSettings.PriceDisplayFormat price_display_format) {
|
|
var result = new GlyphRun[prices.Length];
|
|
Parallel.For(0, prices.Length, i =>
|
|
result[i] = format_text(spec.format(prices[i]), i, row_height, settings, glyph_context, price_display_format));
|
|
return result;
|
|
}
|
|
|
|
public static void clear_prices(DrawingVisual price, int row_count, double row_height, LadderColumnSettings settings, GlyphContext glyph_context) {
|
|
var foreground = settings.foreground.rgb_brush();
|
|
var fprices = _format_empty_prices(row_count, row_height, settings, glyph_context);
|
|
using (var dc = price.RenderOpen())
|
|
using (_draw_with_clip(dc, settings, row_height, row_count)) {
|
|
for (int x = 0; x < row_count; ++x)
|
|
dc.DrawGlyphRun(foreground, fprices[x]);
|
|
}
|
|
}
|
|
|
|
public static double[] render_prices(DrawingVisual price, GlyphContext glyph_context, double mid, double wrt_price, bool format_in_tick_terms, int row_count, double row_height,
|
|
LadderColumnSettings settings, ContractSpecificationImmutable spec) {
|
|
var dprices = gen_prices(mid, row_count, spec);
|
|
render_prices(price, glyph_context, row_count, row_height, dprices, wrt_price, format_in_tick_terms, settings, spec);
|
|
return dprices;
|
|
}
|
|
|
|
public static double[] render_prices(DrawingVisual price, GlyphContext glyph_context, double mid, int row_count, double row_height,
|
|
LadderColumnSettings settings, ContractSpecificationImmutable spec, LadderSettings.PriceDisplayFormat price_display_format) {
|
|
var dprices = gen_prices(mid, row_count, spec);
|
|
render_prices(price, glyph_context, row_count, row_height, dprices, settings, spec, price_display_format);
|
|
return dprices;
|
|
}
|
|
|
|
public static void render_prices(DrawingVisual price, GlyphContext glyph_context, int row_count, double row_height, double[] dprices, double wrt_price, bool format_in_tick_terms, LadderColumnSettings settings, ContractSpecificationImmutable spec) {
|
|
var foreground = settings.foreground.rgb_brush();
|
|
//var background = new SolidColorBrush(settings.background.rgb_color());
|
|
var fprices = format_prices(dprices, wrt_price, format_in_tick_terms, row_height, settings, glyph_context, spec);
|
|
using (var dc = price.RenderOpen())
|
|
using (_draw_with_clip(dc, settings, row_height, row_count)) {
|
|
//dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, row_count * row_height));
|
|
for (int x = 0; x < row_count; ++x)
|
|
dc.DrawGlyphRun(foreground, fprices[x]);
|
|
}
|
|
}
|
|
|
|
public static void render_prices(DrawingVisual price, GlyphContext glyph_context, int row_count, double row_height, double[] dprices, LadderColumnSettings settings, ContractSpecificationImmutable spec, LadderSettings.PriceDisplayFormat price_display_format) {
|
|
var foreground = settings.foreground.rgb_brush();
|
|
//var background = new SolidColorBrush(settings.background.rgb_color());
|
|
var fprices = format_prices(dprices, row_height, settings, glyph_context, spec, price_display_format);
|
|
using (var dc = price.RenderOpen())
|
|
using (_draw_with_clip(dc, settings, row_height, row_count)) {
|
|
//dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, row_count * row_height));
|
|
for (int x = 0; x < row_count; ++x)
|
|
dc.DrawGlyphRun(foreground, fprices[x]);
|
|
}
|
|
}
|
|
|
|
public static void render_grid(DrawingVisual dv, IEnumerable<LadderGridHost.IColumn> columns, int rowcount, double row_height, UInt32 line_color) {
|
|
var height = rowcount * row_height;
|
|
var brush = line_color.rgb_brush();
|
|
using (var dc = dv.RenderOpen()) {
|
|
// draw column backgrounds
|
|
foreach (var col in columns) {
|
|
var background = col.settings.background.rgb_brush();
|
|
background.Freeze();
|
|
dc.DrawRectangle(background, null, new Rect(col.x_left, 0, col.settings.width, rowcount * row_height));
|
|
}
|
|
// draw grid lines
|
|
for (int row = 1; row < rowcount; ++row) {
|
|
double y = row * row_height;
|
|
dc.DrawRectangle(brush, null, new Rect(0, y, 500, 1));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void render_sizes(
|
|
DrawingVisual dv,
|
|
GlyphContext glyph_context,
|
|
int origin_index,
|
|
double[] prices,
|
|
NativePriceSize[] rungs,
|
|
double row_height,
|
|
ContractSpecificationImmutable spec,
|
|
LadderColumnSettings settings,
|
|
Func<NativePriceSize, string> fmt
|
|
) {
|
|
//var background = new SolidColorBrush(settings.background.rgb_color());
|
|
using (var dc = dv.RenderOpen())
|
|
using (_draw_with_clip(dc, settings, row_height, prices.Length)) {
|
|
//dc.DrawRectangle(background, null, new Rect(0, 0, settings.width, prices.Length * row_height));
|
|
if (!prices.within_range(origin_index) || rungs.is_null_or_empty())
|
|
return;
|
|
var foreground = settings.foreground.rgb_brush();
|
|
int curr_index = origin_index;
|
|
double curr_price = prices[curr_index];
|
|
for (int i = 0; i < rungs.Length; i++) {
|
|
if (rungs[i].is_nan) break;
|
|
double price = rungs[i].price;
|
|
// prices are in descending order, so get diff from curr_price - price
|
|
int tick_diff = spec.tickdiff_unchecked(curr_price, price);
|
|
curr_index += tick_diff;
|
|
curr_price = price;
|
|
if (curr_index >= 0 && curr_index < prices.Length) {
|
|
var gr = format_text(fmt(rungs[i]), curr_index, row_height, settings, glyph_context);
|
|
dc.DrawGlyphRun(foreground, gr);
|
|
} else break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void render_volumes(
|
|
DrawingVisual dv,
|
|
GlyphContext glyph_context,
|
|
double[] prices,
|
|
IEnumerable<VolumeAtPriceLevelImmutable> data,
|
|
double row_height,
|
|
ContractSpecificationImmutable spec,
|
|
VolumeColumnSettings settings,
|
|
int last_index,
|
|
string last_size_and_direction
|
|
) {
|
|
if (!data.Any()) {
|
|
_clear_visual(dv);
|
|
return;
|
|
}
|
|
/// create a list of 'render components' (buy/sell/passive). This does not include 'total volume,' which is treated a little differently
|
|
/// (<see cref="render_cell(DrawingContext, int, VolumeAtPriceLevelImmutable)"/> for details)
|
|
var components = new List<(Func<VolumeAtPriceLevelImmutable, uint> get_volue, Brush bars, bool show_numbers)>();
|
|
if (settings.buy.active)
|
|
components.Add((v => v.BuyVolume, settings.buy.bars.result?.to_brush(), settings.buy.show_numbers));
|
|
if (settings.sell.active)
|
|
components.Add((v => v.SellVolume, settings.sell.bars.result?.to_brush(), settings.sell.show_numbers));
|
|
if (settings.passive.active)
|
|
components.Add((v => v.PassiveVolume, settings.passive.bars.result?.to_brush(), settings.passive.show_numbers));
|
|
///
|
|
Brush total_brush = null;
|
|
if (settings.total.bars.result.HasValue) {
|
|
total_brush = new SolidColorBrush(settings.total.bars.result.Value);
|
|
} else if (components.Count == 0 && !settings.show_ltq) {
|
|
_clear_visual(dv);
|
|
return;
|
|
}
|
|
// inflate the max so that the graph never fills the available space
|
|
double max = data.Max(v => v.Volume) * 1.1;
|
|
_render_cells(dv, row_height, data.Select(d => (d.Price, d)), settings.general, prices, spec, render_cell);
|
|
/// local methods
|
|
void render_cell(DrawingContext dc, int r, VolumeAtPriceLevelImmutable vap) {
|
|
Brush text_brush = settings.general.foreground.rgb_brush();
|
|
double y = row_height * r;
|
|
var last_trade_here = r == last_index;
|
|
|
|
// render the total first; each component (buy/sell/passive) will be rendered on top of the total
|
|
if (total_brush != null) {
|
|
// if we have to change the brush to indicate this is the last, save brush, draw, then restore brush
|
|
var temp = total_brush;
|
|
if (settings.show_ltq && last_trade_here) {
|
|
total_brush = BTSColors.yellow_brush;
|
|
}
|
|
render_bar(dc, 0, y, vap.Volume, total_brush, out double _);
|
|
total_brush = temp;
|
|
}
|
|
List<string> numbers_text = new List<string>();
|
|
double x_bar = 0;
|
|
foreach ((var get_volume, Brush bars, bool show_numbers) in components) {
|
|
uint v = get_volume(vap);
|
|
double width;
|
|
if (settings.show_ltq && last_trade_here) {
|
|
render_bar(dc, x_bar, y, v, BTSColors.yellow_brush, out width);
|
|
} else {
|
|
render_bar(dc, x_bar, y, v, bars, out width);
|
|
}
|
|
x_bar += width;
|
|
if (show_numbers)
|
|
numbers_text.Add(v.ToString());
|
|
}
|
|
|
|
if (settings.total.show_numbers) {
|
|
numbers_text.Add(vap.Volume.ToString());
|
|
}
|
|
if (settings.show_ltq && last_trade_here) {
|
|
numbers_text.Clear();
|
|
numbers_text.Add(last_size_and_direction);
|
|
text_brush = BTSCoreColors.white;
|
|
}
|
|
if (numbers_text.Count > 0) {
|
|
string str = string.Join("/", numbers_text);
|
|
GlyphRun gr = glyph_context.get_glyph_run(str, settings.general.font_size, settings.general.width, row_height, HorizontalAlignment.Center, 0, VerticalAlignment.Center, y, true);
|
|
dc.DrawGlyphRun(text_brush, gr);
|
|
}
|
|
}
|
|
|
|
void render_bar(DrawingContext dc, double x, double y, double v, Brush brush, out double width) {
|
|
if (v == 0) {
|
|
width = 0;
|
|
return;
|
|
}
|
|
double pct = v / max;
|
|
width = pct * settings.general.width;
|
|
// 4 seems like a good number to move the y pos down by and similarly
|
|
// to shorten the total y coponent by (-4 for the shift of +4, and another -4 to give it some space);
|
|
dc.DrawRectangle(brush, null, new Rect(x, y + 4, width, row_height - 2 * 4));
|
|
}
|
|
}
|
|
|
|
public delegate void RenderCell<T>(DrawingContext dc, int row, T data_item);
|
|
|
|
static void _render_cells<T>(DrawingVisual dv, double row_height, IEnumerable<(double price, T value)> data, LadderColumnSettings settings, double[] prices, ContractSpecificationImmutable spec, RenderCell<T> render_cell) {
|
|
using (var dc = dv.RenderOpen())
|
|
using (_draw_with_clip(dc, settings, row_height, prices.Length)) {
|
|
if (data.is_null_or_empty())
|
|
return;
|
|
int curr_index = 0;
|
|
double curr_price = prices[curr_index];
|
|
foreach (var (price, value) in data) {
|
|
if (double.IsNaN(price))
|
|
continue;
|
|
// prices are in descending order, so get diff from curr_price - price
|
|
int tick_diff = spec.tickdiff(curr_price, price) ?? 0;
|
|
curr_index += tick_diff;
|
|
curr_price = price;
|
|
if (curr_index >= 0 && curr_index < prices.Length) {
|
|
render_cell(dc, curr_index, value);
|
|
} else break;
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static void render_last(DrawingVisual dv, GlyphContext glyph_context, NativePriceSize last, int index, double row_height, LadderColumnSettings settings) {
|
|
using (var dc = dv.RenderOpen()) {
|
|
var gr = format_text(last.size.ToString(), index, row_height, settings, glyph_context);
|
|
dc.DrawGlyphRun(settings.foreground.rgb_brush(), gr);
|
|
}
|
|
}
|
|
|
|
// this is basically a copy of redner_sizes up above
|
|
internal static void render_piqs(DrawingVisual dv, GlyphContext glyph_context, int origin_index, double[] prices, IEnumerable<(double price, uint piq)> pvp, double row_height, ContractSpecificationImmutable spec, LadderColumnSettings settings) {
|
|
using (var dc = dv.RenderOpen()) {
|
|
using (_draw_with_clip(dc, settings, row_height, prices.Length)) {
|
|
if (!prices.within_range(origin_index) || pvp.is_null_or_empty())
|
|
return;
|
|
var foreground = settings.foreground.rgb_brush();
|
|
int curr_index = origin_index;
|
|
double curr_price = prices[curr_index];
|
|
foreach (var entry in pvp) {
|
|
double price = entry.price;
|
|
// prices are in descending order, so get diff from curr_price - price
|
|
int tick_diff = spec.tickdiff_unchecked(curr_price, price);
|
|
curr_index += tick_diff;
|
|
curr_price = price;
|
|
if (curr_index >= 0 && curr_index < prices.Length) {
|
|
var gr = format_text(entry.piq.ToString(), curr_index, row_height, settings, glyph_context);
|
|
dc.DrawGlyphRun(foreground, gr);
|
|
} else break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void render_mkt_break(DrawingVisual dv, int index, double row_height, uint color) =>
|
|
render_single_grid_line(dv, index, row_height, color, 0, SystemParameters.FullPrimaryScreenWidth);
|
|
|
|
public static void render_single_grid_line(DrawingVisual dv, int index, double row_height, uint color, double x_left, double width) {
|
|
using (var dc = dv.RenderOpen()) {
|
|
if (index >= 0)
|
|
dc.DrawRectangle(color.rgb_brush(), null, new Rect(x_left, index * row_height - 1, width, 3));
|
|
}
|
|
}
|
|
|
|
public static void render_session_range(DrawingVisual dv, uint color, LadderColumnSettings settings, int low_index, int high_index, double[] prices, double row_height) {
|
|
using (var dc = dv.RenderOpen())
|
|
using (_draw_with_clip(dc, settings, row_height, prices.Length))
|
|
dc.DrawRectangle(new SolidColorBrush(color.rgb_color()) { Opacity = 0.2 }, null, new Rect(0, high_index * row_height, settings.width, (low_index - high_index) * row_height));
|
|
}
|
|
|
|
static GlyphRun[] _format_empty_prices(int row_count, double row_height, LadderColumnSettings settings, GlyphContext glyph_context) {
|
|
var result = new GlyphRun[row_count];
|
|
Parallel.For(0, row_count, i => result[i] = format_text("-", i, row_height, settings, glyph_context));
|
|
return result;
|
|
}
|
|
|
|
static IDisposable _draw_with_clip(DrawingContext dc, LadderColumnSettings settings, double row_height, int row_count) {
|
|
dc.PushClip(new RectangleGeometry(new Rect(0, 0, settings.width, row_height * row_count)));
|
|
return Disposable.Create(dc.Pop);
|
|
}
|
|
|
|
static void _clear_visual(DrawingVisual dv) {
|
|
using (dv.RenderOpen()) { }
|
|
}
|
|
}
|
|
} |