예제 #1
0
    def _populate_recent_register(self, stocks: List[Stock]):

        start_epoch_s = IntuitiveDateConverter.to_epoch_s(
            IntuitiveDateConverter.to_datetime(self.today_epoch_s) -
            timedelta(days=20))
        end_epoch_s = IntuitiveDateConverter.to_epoch_s(
            IntuitiveDateConverter.to_datetime(self.today_epoch_s) -
            timedelta(days=1))

        self.data_ingest_manager.populate_warehouse(stocks,
                                                    start_epoch_s,
                                                    end_epoch_s,
                                                    batch_size=1)
        register = self.data_ingest_manager.create_register_from_warehouse(
            stocks, start_epoch_s, end_epoch_s)

        return register
예제 #2
0
    def _run_and_evaluate_to_date(self,
                                  conversion_input: object,
                                  expected_output_type: type,
                                  expected_output_value: object) -> None:

        idc = IntuitiveDateConverter()
        actual_output = idc.to_datetime(conversion_input)
        actual_output_type = type(actual_output)

        self.assertEqual(expected_output_type, actual_output_type)
        self.assertEqual(expected_output_value, actual_output)
    def _build_url(self, exchange: str, symbols: List[str],
                   start_epoch_s: float, end_epoch_s: float) -> str:
        """
        Build url from parameters and api key
        """

        start_str = IntuitiveDateConverter.to_day_str(start_epoch_s)
        end_datetime = IntuitiveDateConverter.to_datetime(end_epoch_s)
        end_datetime += timedelta(days=1)
        end_str = IntuitiveDateConverter.to_day_str(end_datetime)
        symbols_str = ','.join(symbols)
        url = f'https://api.twelvedata.com/time_series?exchange={exchange}&symbol={symbols_str}&interval=1day&' \
              f'start_date={start_str}&end_date={end_str}&apikey={self.api_key}'

        return url
    def get_technical_indicator(self, stock: Stock, indicator_name: str,
                                start_epoch_s: float,
                                end_epoch_s: float) -> List[Moment]:

        start_date = IntuitiveDateConverter.to_day_str(start_epoch_s)
        end_datetime = IntuitiveDateConverter.to_datetime(end_epoch_s)
        end_datetime += timedelta(days=1)
        end_date = IntuitiveDateConverter.to_day_str(end_datetime)

        request_url = f'https://api.twelvedata.com/{indicator_name.lower()}?symbol=' \
                      f'{stock.symbol}&exchange={stock.exchange}&interval=1day&start_date=' \
                      f'{start_date}&end_date={end_date}&apikey={self.api_key}'

        for _ in range(MAX_RETRIES):
            try:
                print(
                    f'API call for technical indicator {indicator_name.lower()} for {stock.to_str()}'
                )
                response = requests.get(request_url)
            except ChunkedEncodingError:
                print(
                    f'intermittent ChunkedEncodingError. Retrying in {RETRY_PAUSE_CHUNK_ERROR}s'
                )
                time.sleep(RETRY_PAUSE_CHUNK_ERROR)
                print('retrying')
                continue
            except Exception as e:
                print("Stopping retry loop because of un-handled error")
                raise e
            else:
                content = response.content
                content = json.loads(content)
                if 'code' in content.keys():
                    if content['code'] == ERROR_CODE_TOO_MANY_REQUESTS:
                        print(
                            f'Too many requests. Pausing for {RETRY_PAUSE_S} seconds before retrying.'
                        )
                        time.sleep(RETRY_PAUSE_S)
                        continue

                if 'values' not in content:
                    print('unexpected response:')
                    print(content)

                break

        moments = list()

        if 'values' not in content:
            return moments

        for item in content['values']:

            epoch_s = IntuitiveDateConverter.to_epoch_s(item['datetime'])
            for k, v in item.items():

                if k == 'datetime':
                    continue

                if k == indicator_name:
                    moment_data = {
                        'epoch_s': epoch_s,
                        'exchange': stock.exchange,
                        'symbol': stock.symbol,
                        indicator_name: item[k]
                    }
                    moment = Moment(**moment_data)
                    moments.append(moment)
                else:
                    moment_data = {
                        'epoch_s': epoch_s,
                        'exchange': stock.exchange,
                        'symbol': stock.symbol,
                        f'{indicator_name}_{k}': item[k]
                    }
                    moment = Moment(**moment_data)
                    moments.append(moment)

        return moments