Too many functions were performing essentially the same service: rounding ticks. It was confusing.
767 lines
32 KiB
C#
767 lines
32 KiB
C#
namespace mbts {
|
|
using bts;
|
|
using bts.api;
|
|
using bts.utils;
|
|
|
|
using mbts.spark;
|
|
using mbts.user.settings;
|
|
|
|
using Spark.Core;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Numerics;
|
|
|
|
public static class TickExtensions {
|
|
static string _nan_text => TickDisplaySettings.Default.nan_text;
|
|
|
|
public static TickFactors to_tick_factors(
|
|
this bts.api.domain.TickFactorsImmutable api_tick_factors) =>
|
|
new(api_tick_factors.TickBase, api_tick_factors.TickUnit);
|
|
|
|
public static bts.api.domain.TickFactors to_api_tick_factors(
|
|
this TickFactors tick_factors) =>
|
|
new() {
|
|
TickBase = tick_factors.tick_base,
|
|
TickUnit = tick_factors.tick_unit
|
|
};
|
|
|
|
public static bts.api.domain.TickFactorsImmutable to_api_tick_factorsi(
|
|
this TickFactors tick_factors) =>
|
|
new(tick_factors.to_api_tick_factors());
|
|
|
|
public static TickDisplaySettings to_tick_display_settings(
|
|
this bts.api.domain.TickDisplaySettingsImmutable api_tick_display_settings) =>
|
|
new() {
|
|
pad_with_leading_zeros = api_tick_display_settings.PadWithLeadingZeros,
|
|
display_multiplier = api_tick_display_settings.DisplayMultiplier,
|
|
show_commas = api_tick_display_settings.ShowCommas,
|
|
nan_text = api_tick_display_settings.NanText,
|
|
show_decimal_part = api_tick_display_settings.ShowDecimalPart,
|
|
show_zero_integer = api_tick_display_settings.ShowZeroInteger,
|
|
tick_factors = api_tick_display_settings.HasTickFactors ?
|
|
api_tick_display_settings.TickFactors.to_tick_factors() : null
|
|
};
|
|
|
|
public static bts.api.domain.TickDisplaySettings to_api_tick_display_settings(
|
|
this TickFactors tick_factors) {
|
|
var api_tds = Security.Defaults.price_display_settings;
|
|
api_tds.TickFactors = tick_factors.to_api_tick_factors();
|
|
return api_tds;
|
|
}
|
|
|
|
public static bts.api.domain.TickDisplaySettingsImmutable to_api_tick_display_settingsi(
|
|
this TickFactors tick_factors) =>
|
|
new(tick_factors.to_api_tick_display_settings());
|
|
|
|
public static bool is_valid(this TickFactors tick) {
|
|
if (!tick.tick_unit.is_real_positive() && !tick.tick_base.is_real_positive()) {
|
|
return false;
|
|
}
|
|
var ticks_per_base = tick.tick_base / tick.tick_unit;
|
|
if (ticks_per_base.is_int()) {
|
|
return true;
|
|
}
|
|
return tick.tick_unit > tick.tick_base && tick.tick_unit_frac().is_int();
|
|
}
|
|
|
|
public static bool is_valid(this RegionalTickFactors regional_tick) =>
|
|
regional_tick.low_tick.is_valid() && regional_tick.high_tick.is_valid();
|
|
|
|
public static bool is_integer(this TickFactors tick) {
|
|
if (tick.tick_unit == tick.tick_base) {
|
|
return true;
|
|
}
|
|
if (tick.tick_unit > tick.tick_base && tick.tick_base != 0) {
|
|
var units_per_base = (int)(tick.tick_unit / tick.tick_base);
|
|
return units_per_base * tick.tick_base == tick.tick_unit;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static TickFactors tick_factors(this RegionalTickFactors regional_tick, double value) =>
|
|
value > regional_tick.low_tick_inclusive_end ?
|
|
regional_tick.high_tick : regional_tick.low_tick;
|
|
|
|
public static double tick_unit_frac(this TickFactors tick) =>
|
|
tick.tick_unit / tick.tick_base;
|
|
|
|
public static double tick_unit_frac(this bts.api.domain.TickFactorsImmutable tick) =>
|
|
tick.TickUnit / tick.TickBase;
|
|
|
|
public static T tick_unit_frac<T>(this TickFactors tick) where T : IFloatingPoint<T> =>
|
|
NumericCastOutConverter<T>.convert(tick.tick_unit / tick.tick_base);
|
|
|
|
public static double reduced_base(this TickFactors tick) => MathUtils.factor_out(tick.tick_base, 10);
|
|
|
|
public static bool tick_equals(this RegionalTickFactors regional_tick, double x, double y) {
|
|
if (x.is_nan()) {
|
|
return y.is_nan();
|
|
}
|
|
if (y.is_nan()) {
|
|
return false;
|
|
}
|
|
var xtick = regional_tick.tick_factors(x);
|
|
var ytick = regional_tick.tick_factors(y);
|
|
return xtick is not null
|
|
&& ytick is not null
|
|
&& xtick.Equals(ytick)
|
|
&& xtick.tick_equals(x, y);
|
|
}
|
|
|
|
public static bool tick_equals(this TickFactors tick, double x, double y) {
|
|
if (x.is_nan()) {
|
|
return y.is_nan();
|
|
}
|
|
var x_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac());
|
|
var y_units = MathUtils.round_fractional_units(y, tick.tick_unit_frac());
|
|
return x_units == y_units;
|
|
}
|
|
|
|
public static bool tick_equals<T>(this TickFactors tick, T x, T y)
|
|
where T : IFloatingPoint<T> {
|
|
if (x.is_nan()) {
|
|
return y.is_nan();
|
|
}
|
|
var x_units = MathUtils.round_fractional_units(x, tick.tick_unit_frac<T>());
|
|
var y_units = MathUtils.round_fractional_units(y, tick.tick_unit_frac<T>());
|
|
return NumericCastConverter<T, long>.convert(x_units) ==
|
|
NumericCastConverter<T, long>.convert(y_units);
|
|
}
|
|
|
|
public static IEqualityComparer<T> tick_equality_comparer<T>(this TickFactors tick)
|
|
where T : IFloatingPoint<T> =>
|
|
EqualityComparer.Create<T>(
|
|
(a, b) => tick_equals(tick, a, b),
|
|
x => NumericCastConverter<T, int>.convert(
|
|
MathUtils.round_fractional_units(x, tick.tick_unit_frac<T>())));
|
|
|
|
public static IEqualityComparer<double> tick_equality_comparer(this TickFactors tick) =>
|
|
EqualityComparer.Create<double>(
|
|
(a, b) => tick_equals(tick, a, b),
|
|
x => (int)MathUtils.round_fractional_units(x, tick.tick_unit_frac()));
|
|
|
|
public static TickFactors get_tick_for(this RegionalTickFactors rtick, double value) =>
|
|
value > rtick.low_tick_inclusive_end ? rtick.high_tick : rtick.low_tick;
|
|
|
|
public static string format_without_decimal_part(this TickFactors tick, double value) =>
|
|
format(tick, value, double.NaN, false, _nan_text, true);
|
|
|
|
public static string format<T>(this TickFactors tick, T value) where T : IFloatingPointIeee754<T> =>
|
|
format(tick, value, T.NaN, false, _nan_text);
|
|
|
|
public static string format<T>(this RegionalTickFactors regional_tick, T value, bool show_commas)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
format(regional_tick, value, T.NaN, show_commas, _nan_text);
|
|
|
|
public static string format<T>(this TickFactors tick, T value, bool show_commas)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
format(tick, value, T.NaN, show_commas, _nan_text);
|
|
|
|
public static string format<T>(this TickDisplaySettings settings, T value)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
format(settings.tick_factors, value, settings);
|
|
|
|
public static string format<T>(
|
|
this bts.api.domain.TickDisplaySettingsImmutable settings, T value)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
settings.to_tick_display_settings().format(value);
|
|
|
|
public static string format<T>(this TickFactors tick, T value, TickDisplaySettings settings)
|
|
where T : IFloatingPoint<T> {
|
|
if (settings is null) {
|
|
settings = TickDisplaySettings.Default;
|
|
}
|
|
if (T.IsNaN(value)) {
|
|
return settings.nan_text;
|
|
}
|
|
if (T.IsInfinity(value)) {
|
|
return value.ToString();
|
|
}
|
|
return _format_tick(tick, value, settings);
|
|
}
|
|
|
|
public static string format<T>(
|
|
this RegionalTickFactors regional_tick,
|
|
T value,
|
|
T display_multiplier,
|
|
bool show_commas,
|
|
string nan_text,
|
|
bool show_decimal_part = true
|
|
) where T : IFloatingPointIeee754<T> {
|
|
var value_d = NumericCastConverter<T, double>.convert(value);
|
|
return regional_tick.tick_factors(value_d)
|
|
.format(value, display_multiplier, show_commas, nan_text, show_decimal_part);
|
|
}
|
|
|
|
public static string format<T>(
|
|
this TickFactors tick,
|
|
T value,
|
|
T display_multiplier,
|
|
bool show_commas,
|
|
string nan_text,
|
|
bool show_decimal_part = true)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tick.format(
|
|
value,
|
|
TickDisplaySettings.Default.with(
|
|
display_multiplier: display_multiplier.as_type<T, double>(),
|
|
show_commas: show_commas,
|
|
nan_text: nan_text,
|
|
show_decimal_part: show_decimal_part));
|
|
|
|
public static string format_without_decimal_part(this RegionalTickFactors regional_tick, double value) =>
|
|
format_without_decimal_part(
|
|
value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick,
|
|
value);
|
|
|
|
public static string format(this RegionalTickFactors regional_tick, double value, TickDisplaySettings settings = null) =>
|
|
format(
|
|
value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick,
|
|
value,
|
|
settings);
|
|
|
|
public static string format(this RegionalTickFactors regional_tick, double value, double display_multiplier, bool show_commas, string nan_text) =>
|
|
format(
|
|
value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick,
|
|
value,
|
|
display_multiplier,
|
|
show_commas, nan_text);
|
|
|
|
#nullable enable
|
|
public static string format(this TickDisplaySettingsTuple tick_disp_tup, double value) {
|
|
if (tick_disp_tup.factors is null) {
|
|
throw new ArgumentException($"{nameof(tick_disp_tup.factors)} is null", nameof(tick_disp_tup));
|
|
}
|
|
return tick_disp_tup.to_display_settings().format(value);
|
|
}
|
|
|
|
public static string format(this TickDisplaySettings display_settings, double value) {
|
|
if (display_settings.tick_factors is null) {
|
|
throw new ArgumentException(
|
|
$"{nameof(display_settings.tick_factors)} is null", nameof(display_settings));
|
|
}
|
|
return format(display_settings.tick_factors, value, display_settings);
|
|
}
|
|
|
|
public static double from_display_value(
|
|
this bts.api.domain.TickDisplaySettingsImmutable display_settings,
|
|
double display_value,
|
|
bool round = true) {
|
|
//
|
|
var rv = display_value;
|
|
if (round && display_settings.HasTickFactors) {
|
|
rv = display_settings.TickFactors.round(rv);
|
|
}
|
|
var display_mult = display_settings.DisplayMultiplier;
|
|
if (display_mult.is_real_non_zero()) {
|
|
rv /= display_mult;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
public static double to_display_value(
|
|
this bts.api.domain.TickDisplaySettingsImmutable display_settings,
|
|
double actual_value,
|
|
bool round = true) {
|
|
//
|
|
var rv = actual_value;
|
|
var display_mult = display_settings.DisplayMultiplier;
|
|
if (display_mult.is_real_non_zero()) {
|
|
rv *= display_mult;
|
|
}
|
|
if (round && display_settings.HasTickFactors) {
|
|
rv = display_settings.TickFactors.round(rv);
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
public static double parse(this TickDisplaySettings display_settings, string text) =>
|
|
parse(display_settings.tick_factors, text, display_settings);
|
|
|
|
public static T parse<T>(this TickDisplaySettings display_settings, string text)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
parse<T>(display_settings.tick_factors, text, display_settings);
|
|
#nullable disable
|
|
|
|
public static double parse(this TickFactors tick, string text, TickDisplaySettings display_settings) =>
|
|
parse<double>(tick, text, display_settings);
|
|
|
|
public static T parse<T>(this TickFactors tick, string text, TickDisplaySettings display_settings)
|
|
where T : IFloatingPointIeee754<T> {
|
|
if (display_settings is null) {
|
|
return parse(tick, text, T.NaN, _nan_text);
|
|
}
|
|
var display_multiplier = display_settings.display_multiplier.as_type<T>();
|
|
var nan_text = display_settings.nan_text ?? string.Empty;
|
|
return parse(tick, text, display_multiplier, nan_text);
|
|
}
|
|
|
|
public static T parse<T>(
|
|
this TickFactors tick, string text, T display_multiplier, string nan_text)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tick?.is_valid() ?? false ?
|
|
_parse(tick, text, display_multiplier, nan_text) :
|
|
_parse_no_tick(text, display_multiplier, nan_text);
|
|
|
|
public static T parse<T>(
|
|
this RegionalTickFactors regional_tick,
|
|
string text,
|
|
T display_multiplier,
|
|
string nan_text
|
|
) where T : IFloatingPointIeee754<T> =>
|
|
regional_tick.is_valid() ?
|
|
_parse(regional_tick, text, display_multiplier, nan_text) :
|
|
_parse_no_tick(text, display_multiplier, nan_text);
|
|
|
|
public static T tick<T>(this TickFactors tick, T value, long count)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
count < 0 ? tick.tickdown(value, (uint)-count) : tick.tickup(value, (ulong)count);
|
|
|
|
public static T tick<T>(this bts.api.domain.TickFactorsImmutable tick, T value, long count)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tick.to_tick_factors().tick(value, count);
|
|
|
|
public static T tick<T>(
|
|
this bts.api.domain.TickDisplaySettingsImmutable settings, T value, long count)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
settings.HasTickFactors ?
|
|
settings.TickFactors.tick(value, count) :
|
|
throw new ArgumentException(
|
|
"Can't tick because 'settings.HasTickFactors' is false", nameof(settings));
|
|
|
|
public static double tick(
|
|
this RegionalTickFactors regional_tick, double input, long count) =>
|
|
tick<double>(regional_tick, input, count);
|
|
|
|
public static T tick<T>(
|
|
this RegionalTickFactors regional_tick, T input, long count)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
count > 0 ?
|
|
tickup(regional_tick, input, (ulong)count) :
|
|
tickdown(regional_tick, input, (ulong)-count);
|
|
|
|
public static T tickup<T>(this TickFactors tick, T input, ulong count = 1)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tickup(tick, input, count, TickUtils.precision_const<T>());
|
|
|
|
public static T tickup<T>(this TickFactors tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tickup<T, NumericCastOutConverter<T>>(tick, input, count, precision);
|
|
|
|
public static T tickup<T, TFromUInt64>(this TickFactors tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T>
|
|
where TFromUInt64 : IConverter<ulong, T> {
|
|
if (!precision.is_real_positive()) {
|
|
throw new ArgumentException("Must be a real positive number.", nameof(precision));
|
|
}
|
|
if (!input.is_real() || count == 0) {
|
|
return input;
|
|
}
|
|
var tuf = tick.tick_unit_frac<T>();
|
|
var ieeer = T.Ieee754Remainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer < -precision) {
|
|
count--;
|
|
}
|
|
var result = input + tuf * TFromUInt64.convert(count);
|
|
return _round_tick(tuf, result);
|
|
}
|
|
|
|
public static double tickup(
|
|
this RegionalTickFactors regional_tick, double input, long count = 1) =>
|
|
count switch {
|
|
> 0 => tickup<double>(regional_tick, input, (ulong)count),
|
|
< 0 => tickdown<double>(regional_tick, input, (ulong)-count),
|
|
0 => input,
|
|
};
|
|
|
|
public static double tickup(
|
|
this RegionalTickFactors regional_tick, double input, ulong count) =>
|
|
tickup<double>(regional_tick, input, count);
|
|
|
|
public static T tickup<T>(
|
|
this RegionalTickFactors regional_tick, T input) where T : IFloatingPointIeee754<T> =>
|
|
regional_tick.tickup(input, 1);
|
|
|
|
public static T tickup<T>(
|
|
this RegionalTickFactors regional_tick, T input, ulong count)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
regional_tick.tickup(input, count, TickUtils.precision_const<T>());
|
|
|
|
public static T tickup<T>(
|
|
this RegionalTickFactors regional_tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T> {
|
|
if (!precision.is_real_positive()) {
|
|
throw new ArgumentException("Must be a real positive number.", nameof(precision));
|
|
}
|
|
if (!input.is_real() || count == 0) {
|
|
return input;
|
|
}
|
|
|
|
T val;
|
|
T tuf;
|
|
T ltie = regional_tick.low_tick_inclusive_end.as_type<double, T>();
|
|
if (input < ltie) {
|
|
tuf = regional_tick.low_tick.tick_unit_frac<T>();
|
|
var ieeer = T.Ieee754Remainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer < -precision) count--;
|
|
val = input + tuf * count.as_type<ulong, T>();
|
|
if (val > ltie) {
|
|
count -= MathUtils.round_up((ltie - input) / tuf, precision).as_type<T, ulong>();
|
|
tuf = regional_tick.high_tick.tick_unit_frac<T>();
|
|
val = ltie + tuf * count.as_type<ulong, T>();
|
|
}
|
|
} else {
|
|
tuf = regional_tick.high_tick.tick_unit_frac<T>();
|
|
var ieeer = T.Ieee754Remainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer < -precision) count--;
|
|
val = input + tuf * count.as_type<ulong, T>();
|
|
}
|
|
return _round_tick(tuf, val);
|
|
}
|
|
|
|
public static T tickdown<T>(this TickFactors tick, T input, ulong count = 1)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tickdown(tick, input, count, TickUtils.precision_const<T>());
|
|
|
|
public static T tickdown<T>(this TickFactors tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tickdown<T, NumericCastOutConverter<T>>(tick, input, count, precision);
|
|
|
|
public static T tickdown<T, TFromUInt64>(this TickFactors tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T>
|
|
where TFromUInt64 : IConverter<ulong, T> {
|
|
if (!input.is_real()) {
|
|
return input;
|
|
}
|
|
var tuf = tick.tick_unit_frac<T>();
|
|
var ieeer = T.Ieee754Remainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer > precision) {
|
|
count--;
|
|
}
|
|
var result = input - tuf * TFromUInt64.convert(count);
|
|
return _round_tick(tuf, result);
|
|
}
|
|
|
|
public static double tickdown(
|
|
this RegionalTickFactors regional_tick, double input, long count = 1) =>
|
|
count switch {
|
|
> 0 => tickdown<double>(regional_tick, input, (ulong)count),
|
|
< 0 => tickup<double>(regional_tick, input, (ulong)count),
|
|
0 => input,
|
|
};
|
|
|
|
public static double tickdown(
|
|
this RegionalTickFactors regional_tick, double input, ulong count) =>
|
|
tickdown<double>(regional_tick, input, count);
|
|
|
|
public static T tickdown<T>(this RegionalTickFactors regional_tick, T input)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
regional_tick.tickdown(input, 1);
|
|
|
|
public static T tickdown<T>(this RegionalTickFactors regional_tick, T input, ulong count)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tickdown(regional_tick, input, count, TickUtils.precision_const<T>());
|
|
|
|
public static T tickdown<T>(
|
|
this RegionalTickFactors regional_tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T> =>
|
|
tickdown<T, NumericCastOutConverter<T>, NumericCastOutConverter<T>, NumericCastInConverter<T>>(
|
|
regional_tick, input, count, precision);
|
|
|
|
public static T tickdown<T, TFromUInt32, TFromDouble, TToUInt16>(
|
|
this RegionalTickFactors regional_tick, T input, ulong count, T precision)
|
|
where T : IFloatingPointIeee754<T>
|
|
where TFromDouble : IConverter<double, T>
|
|
where TFromUInt32 : IConverter<ulong, T>
|
|
where TToUInt16 : IConverter<T, ulong> {
|
|
if (!precision.is_real_positive()) {
|
|
throw new ArgumentException("Must be a real positive number.", nameof(precision));
|
|
}
|
|
if (!input.is_real() || count == 0) {
|
|
return input;
|
|
}
|
|
T val;
|
|
T tuf;
|
|
T low_tick_inclusive_end = TFromDouble.convert(regional_tick.low_tick_inclusive_end);
|
|
if (input > low_tick_inclusive_end) {
|
|
tuf = regional_tick.high_tick.tick_unit_frac<T>();
|
|
var ieeer = T.Ieee754Remainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer > precision) count--;
|
|
val = input - tuf * TFromDouble.convert(count);
|
|
if (val < low_tick_inclusive_end) {
|
|
count -= TToUInt16.convert(MathUtils.round_up(
|
|
(input - low_tick_inclusive_end) / tuf, precision));
|
|
tuf = regional_tick.low_tick.tick_unit_frac<T>();
|
|
val = low_tick_inclusive_end - tuf * TFromDouble.convert(count);
|
|
}
|
|
} else {
|
|
tuf = regional_tick.low_tick.tick_unit_frac<T>();
|
|
var ieeer = T.Ieee754Remainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer > precision) count--;
|
|
val = input - tuf * TFromDouble.convert(count);
|
|
}
|
|
return _round_tick(tuf, val);
|
|
}
|
|
|
|
#nullable enable
|
|
public static TickDisplaySettings to_display_settings(
|
|
this TickDisplaySettingsTuple tick_disp_tup) {
|
|
var rv = new TickDisplaySettings();
|
|
var s = tick_disp_tup.settings;
|
|
if (s is not null) {
|
|
rv.pad_with_leading_zeros = s.pad_with_leading_zeros;
|
|
rv.display_multiplier = s.display_multiplier;
|
|
rv.show_commas = s.show_commas;
|
|
rv.nan_text = s.nan_text;
|
|
rv.show_decimal_part = s.show_decimal_part;
|
|
rv.show_zero_integer = s.show_zero_integer;
|
|
rv.tick_factors = s.tick_factors;
|
|
}
|
|
rv.tick_factors ??= tick_disp_tup.factors;
|
|
return rv;
|
|
}
|
|
#nullable disable
|
|
|
|
public static ContractSpecification to_spec(this TickFactors tick) =>
|
|
tick is null ? null :
|
|
new ContractSpecification {
|
|
LowTick = tick.tick_unit_frac(),
|
|
HighTick = tick.tick_unit_frac(),
|
|
PriceBreak = double.MaxValue,
|
|
MainFraction = (uint)MathUtils.factor_out(tick.tick_base, 10),
|
|
PriceDisplayFormat = (uint)tick.display_digits(),
|
|
PriceMultiplier = 1,
|
|
CashPerPoint = 1
|
|
};
|
|
|
|
/// <summary>
|
|
/// Computes the difference, in tick units, between <paramref name="minuend"/> and <paramref name="subtrahend"/>
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Asserts that <paramref name="minuend"/> and <paramref name="subtrahend"/> are real numbers (i.e., not nan or infinity)
|
|
/// </remarks>
|
|
/// <param name="minuend">The minuend</param>
|
|
/// <param name="subtrahend">The subtrahend</param>
|
|
public static long? tickdiff(this RegionalTickFactors regional_tick, double minuend, double subtrahend) {
|
|
if (!minuend.is_real() || !subtrahend.is_real()) {
|
|
return null;
|
|
}
|
|
var minuend_n = TickUtils.count_ticks(regional_tick, minuend);
|
|
var subtrahend_n = TickUtils.count_ticks(regional_tick, subtrahend);
|
|
var n = minuend_n - subtrahend_n;
|
|
return n;
|
|
}
|
|
|
|
public static double round(this TickFactors tick, double input) =>
|
|
_round_tick(tick.tick_unit_frac(), input);
|
|
|
|
public static double round(this bts.api.domain.TickFactorsImmutable tick, double input) =>
|
|
_round_tick(tick.tick_unit_frac(), input);
|
|
|
|
public static double round(this RegionalTickFactors regional_tick, double input) {
|
|
if (DoubleHelper.IsNaNOrInfinity(input)) {
|
|
return input;
|
|
}
|
|
return input <= regional_tick.low_tick_inclusive_end ?
|
|
regional_tick.low_tick.round(input) :
|
|
regional_tick.high_tick.round(input);
|
|
}
|
|
|
|
public static int display_digits(this TickFactors tick) {
|
|
if (tick.tick_unit == tick.tick_base) {
|
|
return (int)Math.Floor(Math.Log10(tick.tick_base));
|
|
}
|
|
return display_digits(tick.tick_base);
|
|
}
|
|
|
|
public static int display_digits<T>(T tick_base) where T : IFloatingPoint<T>, ILogarithmicFunctions<T> {
|
|
var log_tick_base = T.Log10(tick_base);
|
|
var floor = T.Floor(log_tick_base);
|
|
var dec = log_tick_base - floor;
|
|
var floor_int = NumericCastInConverter<T>.to_int32(floor);
|
|
return Math.Max(1, dec > T.Zero ? floor_int + 1 : floor_int);
|
|
}
|
|
|
|
public static TickFactors with(this TickFactors tick_factors, double? tick_base = null, double? tick_unit = null) =>
|
|
new(tick_base ?? tick_factors.tick_base, tick_unit ?? tick_factors.tick_unit);
|
|
|
|
static char _separator(TickFactors tick) {
|
|
if (tick.tick_base == 1 && tick.tick_unit > tick.tick_base &&
|
|
MathUtils.is_int(tick.tick_unit / tick.tick_base)) {
|
|
return '.';
|
|
}
|
|
double tb_log10 = Math.Log10(tick.tick_base);
|
|
return tb_log10 >= 1 && tb_log10 % 1 == 0 ? '.' : '-';
|
|
}
|
|
|
|
static string _format_no_tick<T>(T value, T display_multiplier, bool show_commas)
|
|
where T : INumberBase<T> {
|
|
if (!display_multiplier.is_nan()) {
|
|
value *= display_multiplier;
|
|
}
|
|
return value.ToString(show_commas ? "#,0" : "0", null);
|
|
}
|
|
|
|
static T _parse_no_tick<T>(string text, T display_multiplier, string nan_text) where T : IFloatingPointIeee754<T> {
|
|
if (text == nan_text) {
|
|
return T.NaN;
|
|
}
|
|
return T.TryParse(text, null, out var result) ?
|
|
result.safe_divide(display_multiplier) : T.NaN;
|
|
}
|
|
|
|
static T _get_integer_part<T>(string integer_text) where T : IFloatingPointIeee754<T> {
|
|
if (integer_text.is_null_or_white_space()) {
|
|
return T.Zero;
|
|
}
|
|
return T.TryParse(
|
|
integer_text, NumberStyles.Any, CultureInfo.CurrentCulture, out var result) ?
|
|
result : T.NaN;
|
|
}
|
|
|
|
static T _get_numerator_part<T>(string numerator_text, T tick_base) where T : IFloatingPointIeee754<T> {
|
|
if (string.IsNullOrEmpty(numerator_text)) {
|
|
return T.Zero;
|
|
}
|
|
if (!T.TryParse(numerator_text, null, out var numerator_part)) {
|
|
return T.NaN;
|
|
}
|
|
|
|
//e.g [tick_base 64] 1989-80 == 1989-8, or 1989-320 == 1989-32
|
|
if (numerator_part >= tick_base) {
|
|
numerator_part /= T.Pow(
|
|
NumericCastOutConverter<T>.convert(10),
|
|
T.Ceiling(T.Log10(numerator_part) - T.Log10(tick_base)));
|
|
} else {
|
|
//e.g [tick_base 640] 1989-8 == 1989-80, or 1989-32 == 1989-320
|
|
int ndd_diff = display_digits(tick_base) - numerator_text.Length;
|
|
if (ndd_diff > 0) {
|
|
numerator_part *= T.Pow(
|
|
NumericCastOutConverter<T>.convert(10),
|
|
NumericCastOutConverter<T>.convert(ndd_diff));
|
|
}
|
|
if (numerator_part >= tick_base) {
|
|
numerator_part /= NumericCastOutConverter<T>.convert(10);
|
|
}
|
|
}
|
|
return numerator_part;
|
|
}
|
|
|
|
static T _parse<T>(
|
|
RegionalTickFactors regional_tick, string text, T display_multiplier, string nan_text)
|
|
where T : IFloatingPointIeee754<T> {
|
|
T result = _parse(regional_tick.low_tick, text, display_multiplier, nan_text);
|
|
|
|
T ltie = regional_tick.low_tick_inclusive_end.as_type<double, T>()
|
|
.safe_multiply(display_multiplier);
|
|
if (result > ltie) {
|
|
result = _parse(regional_tick.high_tick, text, display_multiplier, nan_text);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static T _parse<T>(TickFactors tick, string text, T display_multiplier, string nan_text)
|
|
where T : IFloatingPointIeee754<T> {
|
|
if (text.is_null_or_white_space() || text == nan_text) {
|
|
return T.NaN;
|
|
}
|
|
text = text.Replace(" ", string.Empty);
|
|
if (text[0] == '-') {
|
|
return text.Length > 1 ?
|
|
-_parse(tick, text[1..], display_multiplier, nan_text) : T.NaN;
|
|
}
|
|
if (!T.TryParse(text, null, out var result)) {
|
|
var parts = text.Split(_separator(tick));
|
|
var integer_part = _get_integer_part<T>(parts[0]);
|
|
result = parts.Length == 1 ? integer_part : _parse(tick, integer_part, parts[1]);
|
|
}
|
|
return result.safe_divide(display_multiplier);
|
|
}
|
|
|
|
static T _parse<T>(TickFactors tick, T integer_part, string numerator_part_text)
|
|
where T : IFloatingPointIeee754<T> {
|
|
T tick_base = tick.tick_base.as_type<double, T>();
|
|
T numerator_part = _get_numerator_part(numerator_part_text, tick_base);
|
|
return integer_part + numerator_part / tick_base;
|
|
}
|
|
|
|
static string _format_tick<T>(TickFactors tick, T value, TickDisplaySettings settings)
|
|
where T : IFloatingPoint<T> {
|
|
if (!settings.display_multiplier.is_nan()) {
|
|
value *= settings.display_multiplier.as_type<double, T>();
|
|
}
|
|
// Hide the sign from the 'core_impl.' If the value is negative,
|
|
// flip it before calling core_impl, then prepend a '-' to the resultant string.
|
|
bool negate = value < T.Zero;
|
|
if (negate) {
|
|
value = -value;
|
|
}
|
|
var text = core_impl();
|
|
if (!settings.show_zero_integer) {
|
|
// Hide the integer if its zero. This could be better optimized,
|
|
// but speed shouldn't be an issue, so it doesn't seem worth the time/effort.
|
|
var parts = text.Split(_separator(tick));
|
|
if (parts.Length > 1 && parts[0] == "0")
|
|
text = parts[1];
|
|
}
|
|
if (negate) {
|
|
text = "-" + text;
|
|
}
|
|
return text;
|
|
|
|
// Formats a 'normalized' value. This method expects 'value' to be positive,
|
|
// and [already] multiplied by the display multiplier (if non-nan).
|
|
string core_impl() {
|
|
try {
|
|
checked {
|
|
var integer_part = T.Floor(value);
|
|
var numerator_dec = value - integer_part;
|
|
var unit_frac = tick.tick_unit_frac<T>();
|
|
int n_units = T.Round(numerator_dec / unit_frac).as_type<T, int>();
|
|
if (n_units == tick.tick_base) {
|
|
if (settings.show_decimal_part) {
|
|
return (integer_part + T.One).ToString() + _separator(tick) + new string('0',
|
|
settings.pad_with_leading_zeros ? display_digits(tick.tick_base) : 1);
|
|
} else {
|
|
return integer_part.ToString();
|
|
}
|
|
}
|
|
|
|
double decimal_part = n_units * tick.tick_unit;
|
|
if (decimal_part == tick.tick_base) {
|
|
++integer_part;
|
|
decimal_part = 0;
|
|
} else {
|
|
// some ticks are not formatted with 100% precision, using less than the required display_digits
|
|
// for such cases, we need to truncate the decimal part rather than round it.
|
|
decimal_part = Math.Floor(decimal_part);
|
|
}
|
|
|
|
string integer_part_text = settings.show_commas ?
|
|
integer_part.ToString("#,0", null) : integer_part.ToString();
|
|
if (settings.show_decimal_part) {
|
|
return integer_part_text + _separator(tick) + decimal_part.ToString(
|
|
settings.pad_with_leading_zeros ? new string('0', display_digits(tick.tick_base)) : "0");
|
|
}
|
|
return integer_part_text;
|
|
}
|
|
} catch (OverflowException ex) {
|
|
Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}");
|
|
return "Overflow";
|
|
}
|
|
}
|
|
}
|
|
|
|
static T _round_tick<T>(T tick_unit_frac, T input) where T : IFloatingPointIeee754<T> =>
|
|
input - T.Ieee754Remainder(input, tick_unit_frac);
|
|
}
|
|
}
|