added tick unit test (namely for 'full_ticks_from_zero' method)
This commit is contained in:
1 parent
fa882d5472
commit
b2b8c083a6
2 files changed
+120
-4
No files matched your search
@@ -1,12 +1,15 @@
|
||||
using bts.api;
|
||||
using bts.collections;
|
||||
using bts.mcore;
|
||||
using bts.resources;
|
||||
using bts.test;
|
||||
using bts.utils;
|
||||
using bts.utils.proto;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace bts.mcore {
|
||||
namespace bts.tests.security {
|
||||
[TestGroup]
|
||||
class SecurityTests {
|
||||
[Test]
|
||||
@@ -30,7 +33,46 @@ namespace bts.mcore {
|
||||
[Test]
|
||||
public void test_security_compare() {
|
||||
foreach (var file_path in SecurityFiles.all())
|
||||
_test_security_compare(_read_securities(file_path));
|
||||
_test_security_compare(read_securities(file_path));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void test_ticks() {
|
||||
foreach (var file_path in SecurityFiles.all()) {
|
||||
var ticks = read_securities(file_path).Select(s => s.ContractSpec).Distinct().ToList();
|
||||
foreach (var tick in ticks) {
|
||||
_test_ticks(tick);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _test_ticks(ContractSpecificationImmutable tick) {
|
||||
// the prices straddle a particular number (the radius determines how far above/below the straddle the price range spans)
|
||||
const int min_tick_radius = 200;
|
||||
// some ticks can have more than 200 prices between integers (n and n+1)
|
||||
const int min_int_radius = 2;
|
||||
// test price range about zero
|
||||
var prices_about_zero = TickTestHelper.generate_test_prices(tick.LowTick, radius: get_radius(tick.LowTick), origin_price: 0);
|
||||
_test_ticks(tick, prices_about_zero);
|
||||
// test price range about the price break
|
||||
if (tick.PriceBreak.is_real()) {
|
||||
var prices_about_break = TickTestHelper.generate_test_prices(tick.HighTick, radius: get_radius(tick.HighTick), origin_price: tick.PriceBreak);
|
||||
_test_ticks(tick, prices_about_break);
|
||||
}
|
||||
int get_radius(double tick_size) {
|
||||
Assert.That(tick_size.is_real_positive());
|
||||
return Math.Max(min_tick_radius, (int)(min_int_radius / tick_size));
|
||||
}
|
||||
}
|
||||
|
||||
void _test_ticks(ContractSpecificationImmutable tick, IEnumerable<double> prices) {
|
||||
foreach (var p in prices) {
|
||||
var expected_ticks_from_zero = TickTestHelper.full_ticks_from_zero(tick, p);
|
||||
var actual_ticks_from_zero = TickTestHelper.full_ticks_from_zero(tick, p);
|
||||
Assert.That(expected_ticks_from_zero == actual_ticks_from_zero,
|
||||
$"Expected ticks from zero to be '{expected_ticks_from_zero}', actually '{actual_ticks_from_zero}' " +
|
||||
$"(low/high/pricebreak: {tick.LowTick}/{tick.HighTick}/{tick.PriceBreak}, price: {p}).");
|
||||
}
|
||||
}
|
||||
|
||||
void _test_security_compare(IEnumerable<SecurityImmutable> securities) {
|
||||
@@ -42,10 +84,10 @@ namespace bts.mcore {
|
||||
ComparisonTests.identity_comparison(sec_list, SecurityHelper.Comparer.InstanceImmutable);
|
||||
}
|
||||
|
||||
IEnumerable<SecurityImmutable> _read_securities(string file_path) =>
|
||||
public static IEnumerable<SecurityImmutable> read_securities(string file_path) =>
|
||||
StreamHelper.ReadProtos<Security>(ResourceHelper.LoadResource(file_path)).Select(s => new SecurityImmutable(s));
|
||||
|
||||
static class SecurityFiles {
|
||||
public static class SecurityFiles {
|
||||
public const string
|
||||
KrandallOrderBookCrash = "KrandallOrderBookCrash.security_proto",
|
||||
NonDeterministicICEFutures = "NonDeterministicICEFutures.security_proto";
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
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(tick_size, 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(double tick_size, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user