122 lines
5.9 KiB
C#
122 lines
5.9 KiB
C#
using bts.api;
|
|
using bts.resources;
|
|
using bts.test;
|
|
using bts.utils;
|
|
using bts.utils.proto;
|
|
using mbts.spark;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace bts.tests.security {
|
|
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.Concat(
|
|
adjustments.SelectMany(a => price_base_set.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 int? full_ticks_from_zero(ContractSpecificationImmutable tick, double value) {
|
|
if (!value.is_real())
|
|
return null;
|
|
if (MathUtils.marginal_equals(value, 0, TickUtils.precision_const))
|
|
return 0;
|
|
bool low_tick_valid = tick.LowTick.is_real_positive();
|
|
bool high_tick_valid = tick.HighTick.is_real_positive();
|
|
if (low_tick_valid) {
|
|
if (high_tick_valid) {
|
|
if (value < 0) {
|
|
if (tick.PriceBreak > value) {
|
|
if (tick.PriceBreak >= 0) {
|
|
return (int)MathUtils.round_up(value / tick.LowTick, TickUtils.precision_const);
|
|
} else {
|
|
var high_ticks = (int)MathUtils.round_up(tick.PriceBreak / tick.HighTick, TickUtils.precision_const);
|
|
var low_ticks = (int)MathUtils.round_up((value - tick.PriceBreak) / tick.LowTick, TickUtils.precision_const);
|
|
return high_ticks + low_ticks;
|
|
}
|
|
} else {
|
|
return (int)MathUtils.round_up(value / (tick.PriceBreak.is_real() ? tick.HighTick : tick.LowTick), TickUtils.precision_const);
|
|
}
|
|
} else {
|
|
if (tick.PriceBreak < value) {
|
|
if (tick.PriceBreak <= 0) {
|
|
return (int)MathUtils.round_down(value / tick.HighTick, TickUtils.precision_const);
|
|
} else {
|
|
var low_ticks = (int)MathUtils.round_down(tick.PriceBreak / tick.LowTick, TickUtils.precision_const);
|
|
var high_ticks = (int)MathUtils.round_down((value - tick.PriceBreak) / tick.HighTick, TickUtils.precision_const);
|
|
return low_ticks + high_ticks;
|
|
}
|
|
} else {
|
|
return (int)MathUtils.round_down(value / (tick.PriceBreak.is_real() ? tick.LowTick : tick.HighTick), TickUtils.precision_const);
|
|
}
|
|
}
|
|
} else {
|
|
return (int)MathUtils.round_down_abs(value / tick.LowTick, TickUtils.precision_const);
|
|
}
|
|
} else if (high_tick_valid) {
|
|
return (int)MathUtils.round_down_abs(value / tick.HighTick, TickUtils.precision_const);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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)) {
|
|
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));
|
|
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 ?
|
|
1 : 0;
|
|
}
|
|
}
|