186 lines
7.7 KiB
C#
186 lines
7.7 KiB
C#
using bts.api;
|
|
using bts.resources;
|
|
using bts.test;
|
|
using bts.utils;
|
|
using bts.utils.proto;
|
|
|
|
using mbts;
|
|
using mbts.spark;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace bts.tests.security {
|
|
using RegionalTickFactors = mbts.user.settings.RegionalTickFactors;
|
|
|
|
static class TickTestHelper {
|
|
public static IEnumerable<double> generate_test_prices(double tick_size, int radius = 200, double origin_price = 0) {
|
|
Assert.That(radius > 0);
|
|
var price_base_set = Enumerable.Range(-radius, radius * 2).Select(
|
|
n => origin_price + tick_size * n);
|
|
return generate_test_prices(price_base_set, new[] {
|
|
0,
|
|
MathUtils.EPSILON,
|
|
-MathUtils.EPSILON,
|
|
MathUtils.EPSILON * 100,
|
|
-MathUtils.EPSILON * 100,
|
|
tick_size * 0.02,
|
|
-tick_size * 0.02,
|
|
tick_size * 0.49,
|
|
-tick_size * 0.49,
|
|
tick_size * 0.99,
|
|
-tick_size * 0.99,
|
|
});
|
|
}
|
|
|
|
public static IEnumerable<double> generate_test_prices(IEnumerable<double> price_base_set, IEnumerable<double> adjustments) {
|
|
price_base_set = price_base_set.Where(p => p < long.MaxValue && p > long.MinValue);
|
|
return price_base_set.Concat(
|
|
adjustments.SelectMany(
|
|
a => price_base_set
|
|
.Where(p => a > 0 ?
|
|
(long.MaxValue - p > a) :
|
|
(long.MinValue + a) < a)
|
|
.Select(p => p + a)));
|
|
}
|
|
|
|
public static IEnumerable<ContractSpecificationImmutable> read_contract_specs(string file_path) =>
|
|
StreamHelper.ReadProtos<ContractSpecification>(ResourceHelper.LoadResource(file_path)).Select(s => new ContractSpecificationImmutable(s));
|
|
|
|
public static long? full_ticks_from_zero(ContractSpecificationImmutable tick, double value) =>
|
|
count_ticks(tick.LowTick, tick.HighTick, tick.PriceBreak, value);
|
|
|
|
public static long? count_ticks(RegionalTickFactors tick, double value) =>
|
|
count_ticks(
|
|
tick.low_tick.tick_unit / tick.low_tick.tick_base,
|
|
tick.high_tick.tick_unit / tick.high_tick.tick_base,
|
|
tick.low_tick_inclusive_end,
|
|
value);
|
|
|
|
public static long? count_ticks(double low_tick_sz, double high_tick_sz, double price_break, double value) {
|
|
if (!value.is_real())
|
|
return null;
|
|
if (MathUtils.marginal_equals(value, 0, TickUtils.precision_const<double>()))
|
|
return 0;
|
|
bool low_tick_valid = low_tick_sz.is_real_positive();
|
|
bool high_tick_valid = high_tick_sz.is_real_positive();
|
|
if (!low_tick_valid || !high_tick_valid) {
|
|
if (high_tick_valid) {
|
|
return count_ticks(value, high_tick_sz);
|
|
}
|
|
return low_tick_valid ? count_ticks(value, low_tick_sz) : null;
|
|
}
|
|
|
|
// Both low and high ticks are valid
|
|
if (!price_break.is_real()) {
|
|
// Default to low_tick_sz if price_break isn't a factor
|
|
return count_ticks(value, low_tick_sz);
|
|
}
|
|
if (value < 0) {
|
|
if (price_break <= value) {
|
|
// price_break <= value < 0
|
|
return count_ticks(value, high_tick_sz);
|
|
}
|
|
if (price_break >= 0) {
|
|
// value < 0 <= price_break
|
|
return count_ticks(value, low_tick_sz);
|
|
}
|
|
// value < price_break < 0
|
|
return count_ticks(price_break, high_tick_sz)
|
|
+ count_ticks(value - price_break, low_tick_sz);
|
|
}
|
|
|
|
if (price_break < value) {
|
|
if (price_break <= 0) {
|
|
// price_break <= 0 < value
|
|
return count_ticks(value, high_tick_sz);
|
|
}
|
|
// 0 < price_break < value
|
|
return count_ticks(price_break, low_tick_sz)
|
|
+ count_ticks(value - price_break, high_tick_sz);
|
|
}
|
|
// 0 < value <= price_break
|
|
return count_ticks(value, low_tick_sz);
|
|
}
|
|
|
|
public static long count_ticks(double value, double tick_sz) {
|
|
Log.assert(tick_sz.is_real_positive());
|
|
Log.assert(value.is_real());
|
|
return value < 0 ?
|
|
-count_positive_ticks(-value, tick_sz) :
|
|
count_positive_ticks(value, tick_sz);
|
|
|
|
static long count_positive_ticks(double value, double tick_sz) {
|
|
Log.assert(tick_sz.is_real_positive());
|
|
Log.assert(value.is_real_positive());
|
|
if (value == 0) {
|
|
return 0;
|
|
}
|
|
var n = (long)Math.Floor(value / tick_sz);
|
|
Log.assert(n >= 0);
|
|
var rounded_val = n * tick_sz;
|
|
var diff = value - rounded_val;
|
|
Log.assert(diff >= 0);
|
|
if (diff >= tick_sz / 2) {
|
|
n++;
|
|
}
|
|
return n;
|
|
}
|
|
}
|
|
|
|
public static bool round_ticks(double tick_size, double value, out double rounded_value, out int rounded_ticks) {
|
|
if (!value.is_real() || !tick_size.is_real_positive()) {
|
|
rounded_value = double.NaN;
|
|
rounded_ticks = 0;
|
|
return false;
|
|
}
|
|
var ticks_to_val = value / tick_size;
|
|
ticks_to_val = ticks_to_val > 0 ? Math.Floor(ticks_to_val) : Math.Ceiling(ticks_to_val);
|
|
var rounded_down = ticks_to_val * tick_size;
|
|
var one_tick_up_abs = value > 0 ? rounded_down + tick_size : rounded_down - tick_size;
|
|
if (MathUtils.marginal_equals(one_tick_up_abs, value, TickUtils.precision_const<double>())) {
|
|
rounded_value = one_tick_up_abs;
|
|
rounded_ticks = (int)(value > 0 ? ticks_to_val + 1 : ticks_to_val - 1);
|
|
} else {
|
|
rounded_value = rounded_down;
|
|
rounded_ticks = (int)ticks_to_val;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines if the first tick in the specified direction should be a partial tick (to correct a rounding discrepency).
|
|
/// If this is so, it asserts that ticking up/down once in that direction corrects the rounding discrepency as expected.
|
|
/// </summary>
|
|
public static int check_partial_tick(ContractSpecificationImmutable spec, double value, double rounded_value, bool ticking_up) {
|
|
var partial_tick = count_partial_tick(value, rounded_value, ticking_up);
|
|
if (partial_tick != 0)
|
|
Assert.That(MathUtils.marginal_equals(spec.tickup(value, ticking_up ? partial_tick : -partial_tick), rounded_value, TickUtils.precision_const<double>()));
|
|
return partial_tick;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines if the first tick in the specified direction should be a partial tick (to correct a rounding discrepency).
|
|
/// If this is so, it asserts that ticking up/down once in that direction corrects the rounding discrepency as expected.
|
|
/// </summary>
|
|
public static int check_partial_tick(
|
|
RegionalTickFactors tick, double value, double rounded_value, bool ticking_up) {
|
|
var partial_tick = count_partial_tick(value, rounded_value, ticking_up);
|
|
if (partial_tick != 0)
|
|
Assert.That(MathUtils.marginal_equals(
|
|
tick.tickup(value, ticking_up ? partial_tick : -partial_tick),
|
|
rounded_value,
|
|
TickUtils.precision_const<double>()));
|
|
return partial_tick;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines if the first tick in the specified direction should be a partial tick (to correct a rounding discrepency).
|
|
/// </summary>
|
|
public static int count_partial_tick(double value, double rounded_value, bool ticking_up) =>
|
|
(ticking_up ? (rounded_value - value) : (value - rounded_value)) > TickUtils.precision_const<double>() ?
|
|
1 : 0;
|
|
}
|
|
}
|