Exemplo n.º 1
0
  def import_prices(self, limit: int = 100) -> int:
    results: Dict = {
        'stock_prices': [],
        'etf_prices': [],
        'missing_symbols': [],
        'errors': []
    }
    try:
      symbol_masters: List[SymbolMaster] = self.__stock_service.get_symbols(instrument='', exclude_status=[AppConsts.SYMBOL_STATUS_ARCHIVED])
      symbols: List[str] = [s.symbol for s in symbol_masters]
      price_set: BarSet = self.__alpaca_client.get_prices(symbols, limit)

      if not price_set:
        raise NotFoundException('BarSet', 'symbols', '')

      for symbol in symbol_masters:
        LogUtils.debug('start {0}'.format(symbol.symbol))

        prices: Bars = price_set[symbol.symbol]
        if not prices:
          LogUtils.warning('{0} price not found.'.format(symbol))
          results['missing_symbols'].append(symbol.symbol)
          continue

        for price in prices:
          price_date: datetime = price.t.to_pydatetime()
          data: tuple = (
              symbol.id,
              price_date,
              NumberUtils.to_float(price.o),
              NumberUtils.to_float(price.h),
              NumberUtils.to_float(price.l),
              NumberUtils.to_float(price.c),
              NumberUtils.to_int(price.v),
          )
          if symbol.instrument == AppConsts.INSTRUMENT_STOCK:
            org: StockPriceDaily = self.__stock_service.get_single_stock_price_daily(symbol.id, price_date)
            if not org:
              results['stock_prices'].append(data)
          else:
            org: EtfPriceDaily = self.__stock_service.get_single_etf_price_daily(symbol.id, price_date)
            if not org:
              results['etf_prices'].append(data)
      if results['stock_prices']:
        BaseService._insert_bulk(StockPriceDaily, results['stock_prices'])
      if results['etf_prices']:
        BaseService._insert_bulk(EtfPriceDaily, results['etf_prices'])
    except NotFoundException as ex:
      results['errors'].append(ex)
    except Exception as ex:
      results['errors'].append(ex)
    finally:
      self.__email_client.send_html(
          subject=AppConsts.EMAIL_SUBJECT_IMPORT_PRICES,
          template_path=AppConsts.TEMPLATE_PATH_IMPORT_PRICES,
          model=results)
      if results['errors']:
        for error in results['errors']:
          LogUtils.error('Import Price Error', error)
      return 1
Exemplo n.º 2
0
    def import_from_csv_yahoo(self) -> str:
        BaseService._truncate(EPD)

        files: List[str] = FileUtils.get_files(AppConsts.ETF_PRICE_FOLDER,
                                               is_full=True)
        for file in files:
            symbol: str = FileUtils.get_wo_ext(FileUtils.get_base_name(file))
            org_symbol: SM = self.__stock_service.get_symbol(
                symbol, AppConsts.INSTRUMENT_ETF)
            if not org_symbol:
                continue
            records: List[Dict] = CsvUtils.parse_as_dict(file)
            models: List[Any] = []
            for record in records:
                models.append((
                    org_symbol.id,
                    DateUtils.get_date(record.get(AppConsts.YAHOO_KEY_DATE),
                                       AppConsts.YAHOO_DATE_FORMAT),
                    NumberUtils.to_float(record.get(AppConsts.YAHOO_KEY_OPEN)),
                    NumberUtils.to_float(record.get(AppConsts.YAHOO_KEY_HIGH)),
                    NumberUtils.to_float(record.get(AppConsts.YAHOO_KEY_LOW)),
                    NumberUtils.to_float(record.get(
                        AppConsts.YAHOO_KEY_CLOSE)),
                    NumberUtils.to_int(record.get(AppConsts.YAHOO_KEY_VOLUME)),
                ))
            BaseService._insert_bulk(EPD, models)
        return "1"
Exemplo n.º 3
0
    def import_from_csv_prices(self) -> str:
        BaseService._truncate(SPD)

        files: List[str] = FileUtils.get_files(AppConsts.STOCK_PRICE_FOLDER,
                                               is_full=True)
        for file in files:
            symbol: str = FileUtils.get_wo_ext(FileUtils.get_base_name(file))
            org_symbol: SM = self.__stock_service.get_symbol(
                symbol, AppConsts.INSTRUMENT_STOCK)
            if not org_symbol:
                continue
            records: List[List[str]] = CsvUtils.parse_as_list(file)
            if not records:
                continue
            for record in records:
                record[AppConsts.KIBOT_IDX_DATE] = DateUtils.get_date(
                    record[AppConsts.KIBOT_IDX_DATE],
                    AppConsts.KIBOT_DATE_FORMAT)
                record.insert(0, org_symbol.id)
            BaseService._insert_bulk(SPD, records)
        return "1"
Exemplo n.º 4
0
    def import_from_csv_incomestatements(self) -> str:
        BaseService._truncate(FN)

        file: str = FileUtils.get_file(AppConsts.INCOME_STMT_FILE)
        records: List[Dict] = CsvUtils.parse_as_dict(file)
        models: List[Any] = []
        for record in records:
            if not self.__is_valid_intrinio_record(record):
                continue
            symbol: str = record.get(AppConsts.INTRINIO_KEY_INC_STMT_TICKER)
            fiscal_period: str = record.get(
                AppConsts.INTRINIO_KEY_INC_STMT_FISC_PD)
            org_symbol: SM = self.__stock_service.get_symbol(
                symbol, AppConsts.INSTRUMENT_STOCK)
            if not org_symbol:
                continue
            quarter_end_dte: date = DateUtils.get_date(
                record.get(AppConsts.INTRINIO_KEY_INC_STMT_END_DTE),
                AppConsts.INTRINIO_END_DTE_FMT)
            file_date: date = DateUtils.get_date(
                record.get(AppConsts.INTRINIO_KEY_INC_STMT_FILE_DTE),
                AppConsts.INTRINIO_FILE_DTE_FMT)
            models.append(
                (org_symbol.id,
                 record.get(AppConsts.INTRINIO_KEY_INC_STMT_FISC_YR),
                 self.__get_quarter(fiscal_period), quarter_end_dte, file_date,
                 None,
                 NumberUtils.to_float(
                     record.get(AppConsts.INTRINIO_KEY_INC_STMT_TTLREV)),
                 NumberUtils.to_float(
                     record.get(AppConsts.INTRINIO_KEY_INC_STMT_TTLPROF)),
                 NumberUtils.to_float(
                     record.get(AppConsts.INTRINIO_KEY_INC_STMT_TTLOPINC)),
                 NumberUtils.to_float(
                     record.get(AppConsts.INTRINIO_KEY_INC_STMT_NETINC)), None,
                 None, None, None, None, None, None, None, None, None, None,
                 None, None, None, None, None, None, None, None))
        BaseService._insert_bulk(FN, models)
        return "1"