75 lines
3.2 KiB
C#
75 lines
3.2 KiB
C#
using bts.api;
|
|
using bts.test;
|
|
using bts.utils;
|
|
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 int? full_ticks_from_zero(ContractSpecificationImmutable tick, double value) {
|
|
if (!value.is_real() || MathUtils.epsilon_equals(value, 0))
|
|
return null;
|
|
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);
|
|
} else {
|
|
var low_ticks = (int)MathUtils.round_up(value / tick.LowTick);
|
|
var high_ticks = (int)MathUtils.round_up((value - tick.PriceBreak) / tick.HighTick);
|
|
return low_ticks + high_ticks;
|
|
}
|
|
} else {
|
|
return (int)MathUtils.round_up(value / (tick.PriceBreak.is_real() ? tick.HighTick : tick.LowTick));
|
|
}
|
|
} else {
|
|
if (tick.PriceBreak < value) {
|
|
if (tick.PriceBreak < 0) {
|
|
return (int)MathUtils.round_down(value / tick.HighTick);
|
|
} else {
|
|
var low_ticks = (int)MathUtils.round_down(value / tick.HighTick);
|
|
var high_ticks = (int)MathUtils.round_down((value - tick.PriceBreak) / tick.LowTick);
|
|
return low_ticks + high_ticks;
|
|
}
|
|
} else {
|
|
return (int)MathUtils.round_down(value / (tick.PriceBreak.is_real() ? tick.LowTick : tick.HighTick));
|
|
}
|
|
}
|
|
} else {
|
|
return (int)MathUtils.round_down_abs(value / tick.LowTick);
|
|
}
|
|
} else if (high_tick_valid) {
|
|
return (int)MathUtils.round_down_abs(value / tick.HighTick);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|