1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ! Tick value conversion post-processor.
//!
//! This module provides a post-processor that converts decimal values into
//! tick values based on a logarithmic scale. Tick values represent price points
//! on a non-linear scale, which can be useful for certain financial applications.
//!
//! The module provides:
//!
//! - The [`TickPostProcessor`] struct which implements conversion to tick values
//! - Constants defining the tick scale parameters
//! - Error handling for out-of-bound conversions
//!
//! # Tick System
//!
//! The tick system uses a logarithmic scale with base 1.0001, where:
//!
//! - Tick 262144 (MID_TICK) corresponds to a value of 1.0
//! - Higher tick values represent larger numbers
//! - Lower tick values represent smaller numbers
//! - The valid range is from tick 1 to tick 524287
//!
//! This provides a compact representation for a wide range of values with
//! consistent relative precision.
use ;
use FromPrimitive;
use ;
use ;
use cratePostProcessError;
/// The base value for the tick system (1.0001).
/// Each tick represents a 0.01% change in value.
const TICK: f64 = 1.0001;
/// The tick value that corresponds to a decimal value of 1.0.
/// This serves as the midpoint of the tick scale.
const MID_TICK: f64 = 262144.0;
/// The maximum allowed tick value in the system.
const MAX_TICK: f64 = 524287.0;
/// The minimum allowed tick value in the system.
const MIN_TICK: f64 = 1.0;
/// Post-processor that converts decimal values to tick values.
///
/// The `TickPostProcessor` converts decimal values to tick values based on a logarithmic
/// scale. This conversion can be useful for representing a wide range of values in a
/// compact form with consistent relative precision.
///
/// # Tick Formula
///
/// The conversion uses the formula:
/// ```text
/// tick = log(value) / log(1.0001) + 262144
/// ```
///
/// # Constraints
///
/// The resulting tick value must be within the range [1, 524287]. If the conversion
/// yields a value outside this range, an error is returned.
///
/// # Examples
///
/// ```
/// use bothan_lib::registry::post_processor::{PostProcessor, tick::TickPostProcessor};
/// use rust_decimal::Decimal;
///
/// // Create a tick convertor post-processor
/// let post_processor = PostProcessor::TickConvertor(TickPostProcessor {});
///
/// // Convert a decimal value to a tick value
/// let value = Decimal::new(20, 0); // The value 20.0
/// let result = post_processor.post_process(value).unwrap();
///
/// // The result will be approximately 292102.8
/// ```