Exemplo n.º 1
0
    def test_format_civil_time(self):
        ct = time.CivilTime(1970, 1, 1)
        self.assertEqual("1970-01-01T00:00:00", time.FormatCivilTime(ct))
        ct = time.CivilTime(2015, 1, 2, 3, 4, 5)
        self.assertEqual("2015-01-02T03:04:05", time.FormatCivilTime(ct))

        ct = time.ToCivil(time.Time(1234567890 * 1e9))
        self.assertEqual("2009-02-13T23:31:30", time.FormatCivilTime(ct))
        est: time.Timezone = time.timezone('US/Eastern')
        ct = time.ToCivil(time.Time(1234567890 * 1e9), est)
        self.assertEqual("2009-02-13T18:31:30", time.FormatCivilTime(ct))
Exemplo n.º 2
0
 def _get_filter(self, lookup_key: store.DataStore.LookupKey) -> Dict:
     res = {}
     if lookup_key.data_space:
         res['dataSpace'] = lookup_key.data_space
     if lookup_key.symbol:
         res['symbol'] = lookup_key.symbol
     if lookup_key.data_type:
         res['dataType'] = lookup_key.data_type
     if lookup_key.timestamp:
         res['timestamp'] = time.ToCivil(lookup_key.timestamp)
     return res
Exemplo n.º 3
0
def get_close_prices_for_ema20(t: time.Time, symbol: str) -> List[DataEntry]:
    # INTC 2019-12-03 to 2019-12-31
    price_data = [
        56.07, 56.02, 56.08, 56.81, 56.53, 56.59, 57.07, 57.55, 57.79, 57.70,
        57.23, 57.17, 57.96, 58.95, 59.23, 59.41, 59.82, 60.08, 59.62, 59.85,
    ]  # yapf: disable

    close_prices = [
        _make_close_price(symbol, value, time.FromCivil(d))
        for value, d in zip(price_data,
                            trading_days.get_last_n(time.ToCivil(t), 20))
    ]
    random.shuffle(close_prices)
    return close_prices
Exemplo n.º 4
0
    def get_data(
        self,
        symbol: str,
        start_date: Optional[time.CivilTime] = None,
        end_date: Optional[time.CivilTime] = None,
    ) -> Dict[int, List[_DataEntry]]:
        if not symbol:
            raise errors.InvalidArgumentError('Symbol not specified')

        if not end_date:
            end_date = time.ToCivil(time.Now())
        if not start_date:
            start_date = end_date
        if start_date > end_date:
            raise errors.InvalidArgumentError(
                f'start_date {start_date} is later than end_date {end_date}')
        if end_date > time.ToCivil(time.Now()):
            raise errors.InvalidArgumentError(
                f'end_date {end_date} must be in the past')

        data = self._batch_api.get_batch_quotes(symbol, start_date, end_date)
        chart_series = data[symbol]['chart']

        now = time.Now()
        data_types_to_import = _DATA_TYPE_TO_CHART_FIELD_MAP.keys()
        results = {data_type: [] for data_type in data_types_to_import}
        for chart_data in chart_series:
            data_date = time.ParseCivilTime(chart_data['date'])

            for data_type in data_types_to_import:
                data = self._make_data_base(symbol, data_date, now)
                data.data_type = data_type
                data.value = chart_data[
                    _DATA_TYPE_TO_CHART_FIELD_MAP[data_type]]
                results[data_type].append(data)

        return results
Exemplo n.º 5
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.º 6
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.º 7
0
def _get_shortest_historical_ranges(start_date: time.CivilTime) -> str:
    """Get the shortest API range string that covers the requested number of
       days for the time period ending today.
       See: https://iexcloud.io/docs/api/#historical-prices.
    """
    num_days = 1 + time.ToCivil(time.Now()).diff(start_date).in_days()
    if num_days <= 5:
        return '5d'
    if num_days <= 28:  # Shortest month has 28 days
        return '1m'
    if num_days <= 90:
        return '3m'
    if num_days <= 182:
        return '6m'
    if num_days <= 365:
        return '1y'
    if num_days <= 730:
        return '2y'
    if num_days <= 365 * 5 + 1:
        return '5y'
    if num_days <= 365 * 15 + 3:
        return 'max'
    raise errors.InvalidArgumentError(
        'IEX does not support historical data older than 15 years')
Exemplo n.º 8
0
def to_civil(pb: timestamp_pb2.Timestamp) -> time.CivilTime:
    return time.ToCivil(to_time(pb))