Refactor some tick code

This commit is contained in:
John Colvin committed 2017-10-11 15:33:21 -04:00
1 parent 361b15b7fd
commit 3d82f28d33
2 files changed
+88 -53

No files matched your search

@@ -7,7 +7,6 @@ namespace mbts
public static class TickExtensions
{
#region Public Extensions (is_valid, format, parse, tick_up, tick_down)
public static bool is_valid(this TickFactors tick) { return tick.tick_unit > 0 && tick.tick_unit <= (tick.tick_base * 0.5); }
public static bool is_valid(this RegionalTickFactors regional_tick) { return regional_tick.low_tick.is_valid() && regional_tick.high_tick.is_valid(); /*&& regional_tick.low_tick.tick_base == regional_tick.high_tick.tick_base;*/ }
@@ -34,25 +33,74 @@ namespace mbts
return _parse(regional_tick, text);
}
public static double tick_up(this TickFactors tick, double input) {
if (DoubleHelper.IsNaNOrInfinity(input) || !tick.is_valid()) return input;
return _tick(tick, input, true);
}
public static double tick_up(this RegionalTickFactors regional_tick, double input) {
if (DoubleHelper.IsNaNOrInfinity(input) || !regional_tick.is_valid()) return input;
return input <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick.tick_up(input) : regional_tick.high_tick.tick_up(input);
}
public static double tick_down(this TickFactors tick, double input) {
if (DoubleHelper.IsNaNOrInfinity(input) || !tick.is_valid()) return input;
return _tick(tick, input, false);
}
public static double tick_down(this RegionalTickFactors regional_tick, double input) {
if (DoubleHelper.IsNaNOrInfinity(input) || !regional_tick.is_valid()) return input;
return input <= regional_tick.low_tick_inclusive_end ? regional_tick.low_tick.tick_down(input) : regional_tick.high_tick.tick_down(input);
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)) return input;
double val, tuf;
if (input < regional_tick.low_tick_inclusive_end) {
tuf = regional_tick.low_tick.tick_unit_frac();
input -= input % tuf;
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();
input -= input % tuf;
val = input + tuf * count;
}
return _round_tick(tuf, val);
}
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();
input += tuf - (input % tuf);
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)) return input;
double val, tuf;
if (input > regional_tick.low_tick_inclusive_end) {
tuf = regional_tick.high_tick.tick_unit_frac();
input += tuf - (input % tuf);
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();
return regional_tick.low_tick_inclusive_end - tuf * count;
}
} else {
tuf = regional_tick.low_tick.tick_unit_frac();
val = input - tuf * count;
}
return _round_tick(tuf, val);
}
#endregion
#region Private Members
private static decimal tick_unit_frac_dec(this TickFactors tick) { return new decimal(tick.tick_unit / tick.tick_base); }
private static double tick_unit_frac(this TickFactors tick) { return 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");
@@ -64,13 +112,6 @@ namespace mbts
return double.IsNaN(display_multiplier) || display_multiplier == 0.0 ? result : result / display_multiplier;
}
private static decimal tick_unit_frac_dec(this TickFactors tick) { return new decimal(tick.tick_unit / tick.tick_base); }
private static double tick_unit_frac(this TickFactors tick) { return 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 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;
@@ -115,21 +156,6 @@ namespace mbts
return integer_part + numerator_part / tick.tick_base;
}
private const double _precision_const = 0.000000001;
private static double _tick(TickFactors tick, double input, bool tick_up) {
if (double.IsNaN(input)) return double.NaN;
if (input < 0) return -(_tick(tick, -input, !tick_up));
else {
double unit = tick.tick_unit / tick.tick_base;
// IEEERemainder represents the shortest distance to a multiple of the unit, can be negative or positive
// makes it easy to check if the input is ~= unit multiple (within the precision const)
double ieeer = Math.IEEERemainder(input, unit);
if (Math.Abs(ieeer) <= _precision_const) return tick_up ? (input - ieeer) + unit : (input - ieeer) - unit;
else return ieeer < 0 ? (tick_up ? input - ieeer : input + ieeer) : (tick_up ? input - ieeer + unit : input + ieeer - unit);
}
}
private static string _format_tick(TickFactors tick, double value, bool show_commas) {
if (value < 0) return "-" + _format_tick(tick, Math.Abs(value), show_commas);
int integer_part = (int)Math.Floor(value);
@@ -146,11 +172,16 @@ namespace mbts
return integer_part_text + tick.separator() + decimal_part.ToString(new string('0', _n_display_digits(tick.tick_base)));
}
private static double _round_tick(double tick_unit_frac, double input) {
return input + Math.IEEERemainder(input, tick_unit_frac);
}
private static int _n_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);
}
#endregion
}
}
+19 -15
View File
@@ -1,7 +1,7 @@
using bts.api;
using mbts.collections.extensions;
using mbts.user.settings;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
@@ -34,18 +34,11 @@ namespace mbts.spark {
return mid;
}
public static double tickup(this ContractSpecification contract, double value) { return tick(contract, value, true, 1); }
public static double tickup(this ContractSpecification contract, double value, int count) { return tick(contract, value, true, count); }
public static double tickdown(this ContractSpecification contract, double value) { return tick(contract, value, false, 1); }
public static double tickdown(this ContractSpecification contract, double value, int count) { return tick(contract, value, false, count); }
static double tick(ContractSpecification spec, double value, bool up, int count) {
double factor = up ? 1 : -1;
for (int x = 0; x < count; ++x) {
value += spec.ticksize(value) * factor;
}
return value;
}
public static double tickup(this ContractSpecification contract, double value) { return _get_regional_ticks(contract).tickup(value); }
public static double tickup(this ContractSpecification contract, double value, int count) { return _get_regional_ticks(contract).tickup(value, (uint)count); }
public static double tickdown(this ContractSpecification contract, double value) { return _get_regional_ticks(contract).tickdown(value); }
public static double tickdown(this ContractSpecification contract, double value, int count) { return _get_regional_ticks(contract).tickdown(value, (uint)count); }
public static double round_to_tick(this ContractSpecification spec, double value) {
double intpart = (long)value;
@@ -66,9 +59,20 @@ namespace mbts.spark {
return count;
}
public static int[] get_tick_jump_quantities(ContractSpecification spec) { return _ticks_cache.TryGetValue(spec.LowTick, out (TickFactors tf, int[] jumps) result) ? result.jumps : new[] { 1, 5, 10, 25, 50, 100 }; }
private static TickFactors _get_tick_factors(double tick) { return _ticks_cache.TryGetValue(tick, out (TickFactors tick_factors, int[] preset_price_ticks) result) ? result.tick_factors : _default_tick; }
public static int[] get_tick_jump_quantities(ContractSpecification spec) {
return _ticks_cache.TryGetValue(spec.LowTick, out (TickFactors tf, int[] jumps) result) ? result.jumps : new[] { 1, 5, 10, 25, 50, 100 };
}
public static RegionalTickFactors _get_regional_ticks(ContractSpecification spec) {
return _regional_ticks_cache.GetOrAdd((spec.LowTick, spec.HighTick, spec.PriceBreak), _create_regional_ticks);
}
private static RegionalTickFactors _create_regional_ticks((double low_tick, double high_tick, double price_break) def) {
return new RegionalTickFactors { low_tick = _get_tick_factors(def.low_tick), high_tick = _get_tick_factors(def.high_tick), low_tick_inclusive_end = def.price_break };
}
private static TickFactors _get_tick_factors(double tick) {
return _ticks_cache.TryGetValue(tick, out (TickFactors tick_factors, int[] preset_price_ticks) result) ? result.tick_factors : _default_tick;
}
private static readonly TickFactors _default_tick = new TickFactors(100, 1);
private static readonly ConcurrentDictionary<(double low_tick, double high_tick, double price_break), RegionalTickFactors> _regional_ticks_cache = new ConcurrentDictionary<(double low_tick, double high_tick, double price_break), RegionalTickFactors>();
private static readonly IReadOnlyDictionary<double, (TickFactors tick_factors, int[] preset_price_ticks)> _ticks_cache = new ReadOnlyDictionary<double, (TickFactors tick_factors, int[] preset_price_ticks)>(new[] {
new { tick_unit = 0.05, tick_factors = new TickFactors(100, 5), jump_quantities = new[] { 1, 5, 10, 20, 100, 200 } },
new { tick_unit = 0.10, tick_factors = new TickFactors(100, 10), jump_quantities = new[] { 1, 5, 10, 20, 100, 200 } },