* spark_core is the appropriate project for such 'vocabulary' types. * unit tests in the spark_tests project can now reference these types (this was the immediate impetus for the move).
340 lines
17 KiB
C#
340 lines
17 KiB
C#
using mbts.user.settings;
|
|
using System;
|
|
using System.Globalization;
|
|
using bts;
|
|
using static bts.utils.MathUtils;
|
|
using bts.api;
|
|
using bts.utils;
|
|
|
|
namespace mbts {
|
|
public static class TickExtensions {
|
|
const string _nan_text = "-";
|
|
|
|
#region Public Extensions (is_valid, format, parse, tick_up, tick_down)
|
|
public static bool is_valid(this TickFactors tick) =>
|
|
(tick.tick_unit > 0 && tick.tick_unit <= (tick.tick_base * 0.5)) ||
|
|
tick.tick_unit == tick.tick_base;
|
|
|
|
public static bool is_valid(this RegionalTickFactors regional_tick) =>
|
|
regional_tick.low_tick.is_valid() && regional_tick.high_tick.is_valid();
|
|
|
|
public static double tick_unit_frac(this TickFactors tick) =>
|
|
tick.tick_unit / tick.tick_base;
|
|
|
|
public static double reduced_base(this TickFactors tick) => factor_out(tick.tick_base, 10);
|
|
|
|
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(this TickFactors tick, double value) =>
|
|
format(tick, value, double.NaN, false, _nan_text);
|
|
|
|
public static string format(this TickFactors tick, double value, bool show_commas) =>
|
|
format(tick, value, double.NaN, show_commas, _nan_text);
|
|
|
|
public static string format(this TickFactors tick, double value, double display_multiplier) =>
|
|
format(tick, value, display_multiplier, false, _nan_text);
|
|
|
|
public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas) =>
|
|
format(tick, value, display_multiplier, show_commas, _nan_text);
|
|
|
|
public static string format(this TickFactors tick, double value, double display_multiplier, bool show_commas, string nan_text, bool hide_decimal_part = false) {
|
|
if (double.IsNaN(value)) return nan_text;
|
|
if (double.IsInfinity(value)) return value.ToString();
|
|
if (!tick.is_valid()) return _format_no_tick(value, display_multiplier, show_commas);
|
|
return _format_tick(tick, double.IsNaN(display_multiplier) ? value : value * display_multiplier, show_commas, hide_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) =>
|
|
format(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, double display_multiplier) =>
|
|
format(value <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick : regional_tick.high_tick, value, display_multiplier);
|
|
|
|
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);
|
|
|
|
public static double parse(this TickFactors tick, string text) =>
|
|
parse(tick, text, double.NaN, _nan_text);
|
|
|
|
public static double parse(this TickFactors tick, string text, double display_multiplier) =>
|
|
parse(tick, text, display_multiplier, _nan_text);
|
|
|
|
public static double parse(this TickFactors tick, string text, double display_multiplier, string nan_text) {
|
|
if (!tick.is_valid()) return _parse_no_tick(text, display_multiplier);
|
|
return _parse(tick, text, display_multiplier, nan_text);
|
|
}
|
|
|
|
public static double parse(this RegionalTickFactors regional_tick, string text) =>
|
|
parse(regional_tick, text, double.NaN, _nan_text);
|
|
|
|
public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier) =>
|
|
parse(regional_tick, text, display_multiplier, _nan_text);
|
|
|
|
public static double parse(this RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) {
|
|
if (!regional_tick.is_valid()) return _parse_no_tick(text, display_multiplier);
|
|
return _parse(regional_tick, text, display_multiplier, nan_text);
|
|
}
|
|
|
|
public static double tickup(this TickFactors tick, double input) { return tick.tickup(input, 1); }
|
|
public static double tickup(this TickFactors tick, double input, uint count) {
|
|
if (DoubleHelper.IsNaNOrInfinity(input)) return input;
|
|
double tuf = tick.tick_unit_frac();
|
|
input -= input % tuf;
|
|
double val = input + tuf * count;
|
|
return _round_tick(tuf, val);
|
|
}
|
|
|
|
public static double tickup(this RegionalTickFactors regional_tick, double input) { return regional_tick.tickup(input, 1); }
|
|
public static double tickup(this RegionalTickFactors regional_tick, double input, uint count) {
|
|
if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input;
|
|
double val, tuf;
|
|
if (input < regional_tick.low_tick_inclusive_end) {
|
|
tuf = regional_tick.low_tick.tick_unit_frac();
|
|
double ieeer = Math.IEEERemainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer < -_precision_const) count--;
|
|
val = input + tuf * count;
|
|
if (val > regional_tick.low_tick_inclusive_end) {
|
|
count -= (uint)Math.Ceiling((regional_tick.low_tick_inclusive_end - input) / tuf);
|
|
tuf = regional_tick.high_tick.tick_unit_frac();
|
|
val = regional_tick.low_tick_inclusive_end + tuf * count;
|
|
}
|
|
} else {
|
|
tuf = regional_tick.high_tick.tick_unit_frac();
|
|
double ieeer = Math.IEEERemainder(input, tuf);
|
|
if (Math.Abs(ieeer) <= _precision_const) input -= ieeer;
|
|
val = input + tuf * count;
|
|
}
|
|
return _round_tick(tuf, val);
|
|
}
|
|
|
|
public static ContractSpecification to_spec(this TickFactors tick) {
|
|
if (tick == null)
|
|
return null;
|
|
double tick_frac = tick.tick_unit_frac();
|
|
return new ContractSpecification {
|
|
LowTick = tick_frac,
|
|
HighTick = tick_frac,
|
|
PriceBreak = double.MaxValue,
|
|
MainFraction = (uint)factor_out(tick.tick_base, 10),
|
|
PriceDisplayFormat = (uint)tick.display_digits(),
|
|
PriceMultiplier = 1,
|
|
CashPerPoint = 1
|
|
};
|
|
}
|
|
|
|
public static bool try_get_tickdiff(this RegionalTickFactors regional_tick, double x, double y, out int n_low, out int n_high) {
|
|
if (DoubleHelper.IsNaNOrInfinity(x) || DoubleHelper.IsNaNOrInfinity(y)) {
|
|
n_low = default;
|
|
n_high = default;
|
|
return false;
|
|
}
|
|
(n_low, n_high) = tickdiff_unchecked(regional_tick, x, y);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Callers should be certain that <paramref name="x"/> and <paramref name="y"/> are not nan or infinity.
|
|
/// </summary>
|
|
/// <param name="regional_tick"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
internal static (int n_low, int n_high) tickdiff_unchecked(this RegionalTickFactors regional_tick, double x, double y) {
|
|
double ltie = regional_tick.low_tick_inclusive_end;
|
|
double raw_diff = x - y;
|
|
if (x < ltie) {
|
|
double low_unit_frac = regional_tick.low_tick.tick_unit_frac();
|
|
if (y < ltie) {
|
|
return (RoundFractionalUnits(raw_diff, low_unit_frac), 0);
|
|
} else {
|
|
double high_unit_frac = regional_tick.high_tick.tick_unit_frac();
|
|
double low_diff = ltie - x;
|
|
double high_diff = raw_diff - low_diff;
|
|
int n_low = RoundFractionalUnits(low_diff, low_unit_frac);
|
|
int n_high = RoundFractionalUnits(high_diff, high_unit_frac);
|
|
return (n_low, n_high);
|
|
}
|
|
} else {
|
|
double high_unit_frac = regional_tick.high_tick.tick_unit_frac();
|
|
if (y < ltie) {
|
|
double low_unit_frac = regional_tick.low_tick.tick_unit_frac();
|
|
double high_diff = x - ltie;
|
|
double low_diff = raw_diff - high_diff;
|
|
int n_low = RoundFractionalUnits(low_diff, low_unit_frac);
|
|
int n_high = RoundFractionalUnits(high_diff, high_unit_frac);
|
|
return (n_low, n_high);
|
|
} else {
|
|
return (0, RoundFractionalUnits(raw_diff, high_unit_frac));
|
|
}
|
|
}
|
|
}
|
|
|
|
static int _count_ticks(double tick_unit_frac, double value) =>
|
|
(int)Math.Round(value / tick_unit_frac);
|
|
|
|
private const double _precision_const = 0.000000001;
|
|
|
|
public static double tickdown(this TickFactors tick, double input) { return tick.tickdown(input, 1); }
|
|
public static double tickdown(this TickFactors tick, double input, uint count) {
|
|
if (DoubleHelper.IsNaNOrInfinity(input)) return input;
|
|
double tuf = tick.tick_unit_frac();
|
|
double remainder = input % tuf;
|
|
if (remainder > 0) input += tuf - remainder;
|
|
double val = input - tuf * count;
|
|
return _round_tick(tuf, val);
|
|
}
|
|
|
|
public static double tickdown(this RegionalTickFactors regional_tick, double input) { return regional_tick.tickdown(input, 1); }
|
|
public static double tickdown(this RegionalTickFactors regional_tick, double input, uint count) {
|
|
if (DoubleHelper.IsNaNOrInfinity(input) || count == 0) return input;
|
|
double val, tuf;
|
|
if (input > regional_tick.low_tick_inclusive_end) {
|
|
tuf = regional_tick.high_tick.tick_unit_frac();
|
|
double ieeer = Math.IEEERemainder(input, tuf);
|
|
input -= ieeer;
|
|
if (ieeer > _precision_const) count--;
|
|
val = input - count * tuf;
|
|
if (val < regional_tick.low_tick_inclusive_end) {
|
|
count -= (uint)Math.Ceiling((input - regional_tick.low_tick_inclusive_end) / tuf);
|
|
tuf = regional_tick.low_tick.tick_unit_frac();
|
|
val = regional_tick.low_tick_inclusive_end - tuf * count;
|
|
}
|
|
} else {
|
|
tuf = regional_tick.low_tick.tick_unit_frac();
|
|
double ieeer = Math.IEEERemainder(input, tuf);
|
|
if (Math.Abs(ieeer) <= _precision_const) input -= ieeer;
|
|
val = input - tuf * count;
|
|
}
|
|
return _round_tick(tuf, val);
|
|
}
|
|
|
|
public static double round(this TickFactors tick, double input) { return _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);
|
|
}
|
|
#endregion
|
|
|
|
public static int display_digits(double tick_base) {
|
|
double log_tick_base = Math.Log10(tick_base);
|
|
int floor = (int)Math.Floor(log_tick_base);
|
|
double dec = log_tick_base - floor;
|
|
return Math.Max(1, dec > 0 ? ++floor : floor);
|
|
}
|
|
|
|
#region Private Members
|
|
private static decimal tick_unit_frac_dec(this TickFactors tick) { return new decimal(tick.tick_unit / tick.tick_base); }
|
|
private static char separator(this TickFactors tick) {
|
|
double tb_log10 = Math.Log10(tick.tick_base);
|
|
return tb_log10 >= 1 && tb_log10 % 1 == 0 ? '.' : '-';
|
|
}
|
|
|
|
private static string _format_no_tick(double value, double display_multiplier, bool show_commas) {
|
|
if (!double.IsNaN(display_multiplier)) value *= display_multiplier;
|
|
return value.ToString(show_commas ? "#,0" : "0");
|
|
}
|
|
|
|
private static double _parse_no_tick(string text, double display_multiplier) {
|
|
double result;
|
|
if (!double.TryParse(text, out result)) return double.NaN;
|
|
return double.IsNaN(display_multiplier) || display_multiplier == 0.0 ? result : result / display_multiplier;
|
|
}
|
|
|
|
private static double _parsed_or_nan(string text) { return double.TryParse(text, out double d) ? d : double.NaN; }
|
|
private static double _get_integer_part(string integer_text) {
|
|
return string.IsNullOrEmpty(integer_text) ? 0 : int.TryParse(integer_text, NumberStyles.Any, CultureInfo.CurrentCulture, out int result) ? result : double.NaN;
|
|
}
|
|
|
|
private static double _get_numerator_part(string numerator_text, double tick_base, double tick_unit) {
|
|
if (string.IsNullOrEmpty(numerator_text)) return 0;
|
|
double numerator_part;
|
|
if (!double.TryParse(numerator_text, out numerator_part)) return double.NaN;
|
|
//e.g [tick_base 64] 1989-80 == 1989-8, or 1989-320 == 1989-32
|
|
if (numerator_part >= tick_base) numerator_part /= Math.Pow(10, Math.Ceiling(Math.Log10(numerator_part) - Math.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 *= Math.Pow(10, ndd_diff);
|
|
if (numerator_part >= tick_base) numerator_part /= 10;
|
|
}
|
|
return numerator_part;
|
|
}
|
|
|
|
private static double _parse(RegionalTickFactors regional_tick, string text, double display_multiplier, string nan_text) {
|
|
double result = _parse(regional_tick.low_tick, text, display_multiplier, nan_text);
|
|
double ltie = double.IsNaN(display_multiplier) ? regional_tick.low_tick_inclusive_end : regional_tick.low_tick_inclusive_end * display_multiplier;
|
|
if (result > ltie) result = _parse(regional_tick.high_tick, text, display_multiplier, nan_text);
|
|
return result;
|
|
}
|
|
|
|
private static double _parse(TickFactors tick, string text, double display_multiplier, string nan_text) {
|
|
if (string.IsNullOrWhiteSpace(text) || text == nan_text) return double.NaN;
|
|
text = text.Replace(" ", string.Empty);
|
|
if (text[0] == '-') return text.Length > 1 ? -_parse(tick, text.Substring(1), display_multiplier, nan_text) : double.NaN;
|
|
double d;
|
|
if (double.TryParse(text, out d)) return double.IsNaN(display_multiplier) ? d : d * display_multiplier;
|
|
var parts = text.Split(tick.separator());
|
|
double integer_part = _get_integer_part(parts[0]);
|
|
double result = parts.Length == 1 ? integer_part : _parse(tick, integer_part, parts[1]);
|
|
return double.IsNaN(display_multiplier) ? result : display_multiplier * result;
|
|
}
|
|
|
|
private static double _parse(TickFactors tick, double integer_part, string numerator_part_text) {
|
|
double numerator_part = _get_numerator_part(numerator_part_text, tick.tick_base, tick.tick_unit);
|
|
return integer_part + numerator_part / tick.tick_base;
|
|
}
|
|
|
|
private static string _format_tick(TickFactors tick, double value, bool show_commas, bool hide_decimal_part) {
|
|
if (value < 0) return "-" + _format_tick(tick, Math.Abs(value), show_commas, hide_decimal_part);
|
|
try {
|
|
checked {
|
|
int integer_part = (int)Math.Floor(value);
|
|
double numerator_dec = value - integer_part;
|
|
double unit_frac = tick.tick_unit / tick.tick_base;
|
|
int n_units = (int)Math.Round(numerator_dec / unit_frac);
|
|
if (n_units == tick.tick_base) {
|
|
if (hide_decimal_part) {
|
|
return integer_part.ToString();
|
|
} else {
|
|
return (integer_part + 1).ToString() + tick.separator() + new string('0', display_digits(tick.tick_base));
|
|
}
|
|
}
|
|
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 = show_commas ? integer_part.ToString("#,0") : integer_part.ToString();
|
|
if (hide_decimal_part) {
|
|
return integer_part_text;
|
|
} else {
|
|
return integer_part_text + tick.separator() + decimal_part.ToString(new string('0', display_digits(tick.tick_base)));
|
|
}
|
|
}
|
|
}
|
|
catch (OverflowException ex) {
|
|
Log.Ui.warn($"Arithmetic overflow exception in format_tick (tick:{tick}, value:{value}):{ex}");
|
|
return "Overflow";
|
|
}
|
|
}
|
|
|
|
private static double _round_tick(double tick_unit_frac, double input) {
|
|
return input - Math.IEEERemainder(input, tick_unit_frac);
|
|
}
|
|
#endregion
|
|
}
|
|
} |