Exemplo n.º 1
0
def recursive_inputs_shape(
        base_calc_type: DataType.Enum, incr_calc_type: DataType.Enum,
        t: time.Time, time_spec: calc.CalcTimeSpecs) -> calc.RecursiveInputs:
    # TODO: same here, think about how to get it work with period other than
    # days. e.g week-based moving averages.
    the_day_before = trading_days.get_last_n(time.ToCivil(t),
                                             1,
                                             include_input_date=False)[0]
    return (DataEntry(
        data_type=base_calc_type,
        timestamp=time_util.from_time(time.FromCivil(the_day_before)),
    ), DataEntry(
        data_type=incr_calc_type,
        timestamp=time_util.from_time(t),
    ))
Exemplo n.º 2
0
def _make_close_price(value: float, t: time.Time) -> _DataEntry:
    return _DataEntry(
        symbol='TEST',
        data_space=_DataEntry.STOCK_DATA,
        data_type=data_type_pb2.DataType.CLOSE_PRICE,
        value=value,
        timestamp=time_util.from_time(t),
    )
Exemplo n.º 3
0
 def _make_data_base(self, symbol: str, data_date: time.CivilTime,
                     now: time.time) -> _DataEntry:
     data_entry = _DataEntry()
     data_entry.symbol = symbol
     data_entry.data_space = _DataEntry.STOCK_DATA
     data_entry.timestamp.CopyFrom(time_util.from_civil(data_date))
     data_entry.updated_at.CopyFrom(time_util.from_time(now))
     return data_entry
Exemplo n.º 4
0
def get_recursive_inputs_for_ema20(t: time.Time,
                                   symbol: str) -> List[DataEntry]:
    return [
        DataEntry(
            symbol=symbol,
            data_space=DataEntry.STOCK_DATA,
            data_type=DataType.EMA_20D,
            value=58.31,
            timestamp=time_util.from_time(t - time.Hours(24)),
        ),
        _make_close_price(symbol, 59.85, t)
    ]
Exemplo n.º 5
0
def series_source_inputs_shape(
        source_calc_type: DataType.Enum, t: time.Time,
        time_spec: calc.CalcTimeSpecs) -> calc.SourceInputs:
    # TODO: right now this is still no properly generalized. For time span
    # longer than a day, they should all work. However for day and shorter span,
    # they need to be checked against trading days and trading hours.
    # NOTE: both FromCivil and ToCivil assumes UTC, which later might
    # be updated to take into account the exchange timezone of a
    # security.
    timestamps = [
        time.FromCivil(day) for day in trading_days.get_last_n(
            time.ToCivil(t), time_spec.num_periods)
    ]
    return [
        DataEntry(
            data_type=source_calc_type,
            timestamp=time_util.from_time(timestmap),
        ) for timestmap in timestamps
    ]
Exemplo n.º 6
0
Arquivo: ema.py Projeto: dayfine/xlab
    def calculate(self, inputs: calc.CalcInputs) -> DataEntry:
        if not inputs:
            raise errors.InvalidArgumentError('Inputs cannot be empty')

        symbol = input_util.are_for_stock(inputs)
        t = time_util.to_time(inputs[-1].timestamp)
        try:
            input_util.validate_inputs(inputs, self.recursive_inputs_shape(t))
            ema = self._recur(inputs)
        except errors.InvalidArgumentError:
            input_util.validate_inputs(inputs, self.source_inputs_shape(t))
            ema = self._initial(inputs)
        except errors.InvalidArgumentError as e:
            raise e

        return DataEntry(
            symbol=symbol,
            data_space=data_entry_pb2.DataEntry.STOCK_DATA,
            data_type=self._calc_type,
            value=ema,
            timestamp=time_util.from_time(t),
        )
Exemplo n.º 7
0
    def test_ema20_recursive_calculation(self):
        date = time.FromCivil(time.CivilTime(2020, 1, 1))
        close_now = _make_close_price(60.84, date)

        ema_last = _DataEntry(
            symbol='TEST',
            data_space=_DataEntry.STOCK_DATA,
            data_type=data_type_pb2.DataType.EMA_20D,
            value=58.46,
            timestamp=time_util.from_time(date - time.Hours(24)),
        )

        assert_that(
            ema.make_ema_20d_producer().calculate((ema_last, close_now)),
            equals_proto(f"""
                symbol: "TEST"
                data_space: STOCK_DATA
                data_type: EMA_20D
                value: 58.68666666666667
                timestamp {{
                    seconds: {time.as_seconds(2020, 1, 1)}
                }}"""))