Add naive digits count routine

bar_select
Tyler Goodlet 2020-10-22 14:05:35 -04:00
parent f2c4a46c94
commit 18dc809acb
1 changed files with 13 additions and 2 deletions

View File

@ -1,7 +1,7 @@
""" """
Numpy data source machinery. Numpy data source machinery.
""" """
import math import decimal
from dataclasses import dataclass from dataclasses import dataclass
import numpy as np import numpy as np
@ -33,11 +33,18 @@ tf_in_1m = {
} }
def float_digits(
value: float,
) -> int:
return int(-decimal.Decimal(str(value)).as_tuple().exponent)
def ohlc_zeros(length: int) -> np.ndarray: def ohlc_zeros(length: int) -> np.ndarray:
"""Construct an OHLC field formatted structarray. """Construct an OHLC field formatted structarray.
For "why a structarray" see here: https://stackoverflow.com/a/52443038 For "why a structarray" see here: https://stackoverflow.com/a/52443038
Bottom line, they're faster then ``np.recarray``. Bottom line, they're faster then ``np.recarray``.
""" """
return np.zeros(length, dtype=base_ohlc_dtype) return np.zeros(length, dtype=base_ohlc_dtype)
@ -46,6 +53,7 @@ def ohlc_zeros(length: int) -> np.ndarray:
class Symbol: class Symbol:
"""I guess this is some kinda container thing for dealing with """I guess this is some kinda container thing for dealing with
all the different meta-data formats from brokers? all the different meta-data formats from brokers?
""" """
key: str = '' key: str = ''
min_tick: float = 0.01 min_tick: float = 0.01
@ -54,8 +62,9 @@ class Symbol:
def digits(self) -> int: def digits(self) -> int:
"""Return the trailing number of digits specified by the """Return the trailing number of digits specified by the
min tick size for the instrument. min tick size for the instrument.
""" """
return int(math.log(self.min_tick, 0.1)) return float_digits(self.min_tick)
def from_df( def from_df(
@ -64,6 +73,7 @@ def from_df(
default_tf=None default_tf=None
) -> np.recarray: ) -> np.recarray:
"""Convert OHLC formatted ``pandas.DataFrame`` to ``numpy.recarray``. """Convert OHLC formatted ``pandas.DataFrame`` to ``numpy.recarray``.
""" """
df.reset_index(inplace=True) df.reset_index(inplace=True)
@ -103,6 +113,7 @@ def from_df(
def _nan_to_closest_num(array: np.ndarray): def _nan_to_closest_num(array: np.ndarray):
"""Return interpolated values instead of NaN. """Return interpolated values instead of NaN.
""" """
for col in ['open', 'high', 'low', 'close']: for col in ['open', 'high', 'low', 'close']:
mask = np.isnan(array[col]) mask = np.isnan(array[col])