-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
price-scale-conversions.ts
100 lines (77 loc) · 2.67 KB
/
price-scale-conversions.ts
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
import { log10 } from '../helpers/mathex';
import { PriceRangeImpl } from './price-range-impl';
const enum Constants {
LogicalOffset = 4,
CoordOffset = 0.0001,
}
export function fromPercent(value: number, baseValue: number): number {
if (baseValue < 0) {
value = -value;
}
return (value / 100) * baseValue + baseValue;
}
export function toPercent(value: number, baseValue: number): number {
const result = 100 * (value - baseValue) / baseValue;
return (baseValue < 0 ? -result : result);
}
export function toPercentRange(priceRange: PriceRangeImpl, baseValue: number): PriceRangeImpl {
const minPercent = toPercent(priceRange.minValue(), baseValue);
const maxPercent = toPercent(priceRange.maxValue(), baseValue);
return new PriceRangeImpl(minPercent, maxPercent);
}
export function fromIndexedTo100(value: number, baseValue: number): number {
value -= 100;
if (baseValue < 0) {
value = -value;
}
return (value / 100) * baseValue + baseValue;
}
export function toIndexedTo100(value: number, baseValue: number): number {
const result = 100 * (value - baseValue) / baseValue + 100;
return (baseValue < 0 ? -result : result);
}
export function toIndexedTo100Range(priceRange: PriceRangeImpl, baseValue: number): PriceRangeImpl {
const minPercent = toIndexedTo100(priceRange.minValue(), baseValue);
const maxPercent = toIndexedTo100(priceRange.maxValue(), baseValue);
return new PriceRangeImpl(minPercent, maxPercent);
}
export function toLog(price: number): number {
const m = Math.abs(price);
if (m < 1e-8) {
return 0;
}
const res = log10(m + Constants.CoordOffset) + Constants.LogicalOffset;
return ((price < 0) ? -res : res);
}
export function fromLog(logical: number): number {
const m = Math.abs(logical);
if (m < 1e-8) {
return 0;
}
const res = Math.pow(10, m - Constants.LogicalOffset) - Constants.CoordOffset;
return (logical < 0) ? -res : res;
}
export function convertPriceRangeToLog(priceRange: PriceRangeImpl | null): PriceRangeImpl | null {
if (priceRange === null) {
return null;
}
const min = toLog(priceRange.minValue());
const max = toLog(priceRange.maxValue());
return new PriceRangeImpl(min, max);
}
export function canConvertPriceRangeFromLog(priceRange: PriceRangeImpl | null): boolean {
if (priceRange === null) {
return false;
}
const min = fromLog(priceRange.minValue());
const max = fromLog(priceRange.maxValue());
return isFinite(min) && isFinite(max);
}
export function convertPriceRangeFromLog(priceRange: PriceRangeImpl | null): PriceRangeImpl | null {
if (priceRange === null) {
return null;
}
const min = fromLog(priceRange.minValue());
const max = fromLog(priceRange.maxValue());
return new PriceRangeImpl(min, max);
}