Ejemplo 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
Ejemplo 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"
Ejemplo n.º 3
0
 def import_from_csv_balancesheets(self) -> str:
     file: str = FileUtils.get_file(AppConsts.BALANCE_SHEET_FILE)
     records: List[Dict] = CsvUtils.parse_as_dict(file)
     for record in records:
         if not self.__is_valid_intrinio_record(record):
             continue
         symbol: str = record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_TICKER)
         fiscal_period: str = record.get(
             AppConsts.INTRINIO_KEY_BLNC_SHEET_FISC_PD)
         year: int = NumberUtils.to_int(
             record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_FISC_YR))
         quarter: int = self.__get_quarter(fiscal_period)
         org_symbol: SM = self.__stock_service.get_symbol(
             symbol, AppConsts.INSTRUMENT_STOCK)
         if not org_symbol:
             continue
         org_fn: FN = self.__stock_service.get_financial(
             org_symbol.id, year, quarter)
         if not org_fn:
             continue
         org_fn.current_assets = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_CURR_ASSETS))
         org_fn.ttl_assets = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_ASSETS))
         org_fn.current_liabilities = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_CURR_LIABS))
         org_fn.ttl_liabilities = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_LIABS))
         org_fn.ttl_equity = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_BLNC_SHEET_EQUITY))
         BaseService._update()
     return "1"
Ejemplo n.º 4
0
 def get_dict(item: Any) -> Dict:
     ret: Dict = {}
     if not item:
         return ret
     for key, val in vars(item).items():
         key = key.lstrip('_')
         if isinstance(val, int):
             ret[key] = NumberUtils.to_int(val)
         elif isinstance(val, (Decimal, float)):
             ret[key] = NumberUtils.to_float(val)
         elif isinstance(val, (bool, np.bool_)):
             ret[key] = bool(val)
         elif isinstance(val, (datetime.date, datetime.datetime)):
             ret[key] = val.isoformat()
         elif isinstance(val, list):
             ret[key] = ModelUtils.get_dicts(val)
         elif isinstance(val, Dict):
             ret[key] = val
         elif isinstance(val, db.Model):
             ret[key] = ModelUtils.get_dict(val)
         elif isinstance(val, (BaseResponse, Entity)):
             ret[key] = ModelUtils.get_dict(val)
         else:
             ret[key] = str(val)
     return ret
Ejemplo n.º 5
0
    def get_all_suggestions(self) -> int:
        error: Exception = None
        try:

            accnt: Account = self.get_account()
            capital: float = NumberUtils.to_floor(
                NumberUtils.to_float(accnt._raw['buying_power']) /
                2)  # 2 to trade everyday

            # Double Bottoms
            req: TradeSuggestionsRequest = TradeSuggestionsRequest()
            req.is_job = True
            req.current_capital = capital
            req.pct_risk_per_trade = 2.5
            req.volume_limit = 0.01
            req.test_limit_symbol = 800
            req.adv_min = AppConsts.ADV_MIN_DFLT
            req.adpv_min = AppConsts.ADPV_MIN_DFLT
            req.strategy_type = AppConsts.STRATEGY_DOUBLE_BOTTOMS
            req.strategy_request = {}
            req.strategy_request['exponential_smoothing_alpha'] = 0.8
            req.strategy_request['exponential_smoothing_max_min_diff'] = 0.7
            req.strategy_request['double_bottoms_diff'] = 1
            LogUtils.debug(StringUtils.to_json(req))
            self.get_suggestions(req)

            # Double Tops
            req: TradeSuggestionsRequest = TradeSuggestionsRequest()
            req.is_job = True
            req.current_capital = capital
            req.pct_risk_per_trade = 2.5
            req.volume_limit = 0.01
            req.test_limit_symbol = 800
            req.adv_min = AppConsts.ADV_MIN_DFLT
            req.adpv_min = AppConsts.ADPV_MIN_DFLT
            req.strategy_type = AppConsts.STRATEGY_DOUBLE_TOPS
            req.strategy_request = {}
            req.strategy_request['exponential_smoothing_alpha'] = 0.8
            req.strategy_request['exponential_smoothing_max_min_diff'] = 0.7
            req.strategy_request['double_tops_diff'] = 1
            LogUtils.debug(StringUtils.to_json(req))
            self.get_suggestions(req)

        except Exception as ex:
            error = ex
        finally:
            self.__email_client.send_html(
                subject=AppConsts.EMAIL_SUBJECT_GET_SUGGESTIONS,
                template_path=AppConsts.TEMPLATE_PATH_GET_SUGGESTIONS,
                model={'errors': [error] if error else []})
            if error:
                LogUtils.error('Get Suggestions Error', error)
            return 1
Ejemplo n.º 6
0
 def get_price_dataframe(self, prices: List[Any]) -> DataFrame:
     if not prices \
             or not isinstance(prices, List) \
             or not isinstance(prices[0], (SPD, EPD)):
         return None
     df: DataFrame = pd.DataFrame(
         data=[
             [
                 p.id,
                 p.symbol_id,  # idx = 0, 0
                 p.price_date,  # idx = 0, 1
                 NumberUtils.to_float(p.open_price),
                 NumberUtils.to_float(p.high_price),
                 NumberUtils.to_float(p.low_price),
                 NumberUtils.to_float(p.close_price),
                 p.volume
             ] for p in prices
         ],
         columns=AppConsts.PRICE_COLS)
     df = df.set_index(
         [AppConsts.PRICE_COL_SYMBOL_ID, AppConsts.PRICE_COL_DATE])
     return df
Ejemplo n.º 7
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"
Ejemplo n.º 8
0
 def _has_entry_conditions(self, symbol_id: int,
                           current_date: date) -> bool:
     current_row: Series = self._prices.loc[symbol_id, current_date]
     if NumberUtils.to_float(
             current_row.loc[AppConsts.CUSTOM_COL_SMA_SLOW]) == 0:
         return False
     is_above: bool = (current_row.loc[AppConsts.CUSTOM_COL_SMA_FAST] >
                       current_row.loc[AppConsts.CUSTOM_COL_SMA_SLOW])
     if not symbol_id in self._target:
         self._target[symbol_id] = {}
         self._target[symbol_id]['is_above'] = is_above
         return False
     was_above: bool = self._target[symbol_id]['is_above']
     self._target[symbol_id]['is_above'] = is_above
     return (is_above and not was_above)
Ejemplo n.º 9
0
 def end_price(self, val: Any) -> None:
     self._end_price = NumberUtils.to_float(val)
Ejemplo n.º 10
0
 def start_price(self, val: Any) -> None:
     self._start_price = NumberUtils.to_float(val)
Ejemplo n.º 11
0
    def sync_orders(self) -> int:
        errors: List[Exception] = []
        try:
            req: GetTradeOrdersRequest = GetTradeOrdersRequest()
            req.status = [
                AppConsts.ORDER_STATUS_SUBMITTED_ENTRY,
                AppConsts.ORDER_STATUS_SUBMITTED_EXIT
            ]
            orders: List[TradeOrderCustom] = self.get_trade_orders(req)

            if not orders:
                LogUtils.debug('No orders submitted')

            for order in orders:
                try:
                    LogUtils.debug('Sync order for = {0}'.format(
                        order.symbol_master.symbol))

                    resp: Order = None
                    if order.trade_order.status == AppConsts.ORDER_STATUS_SUBMITTED_ENTRY:
                        resp = self.__alpaca_client.get_order(
                            order.trade_order.alpaca_id)
                    elif order.trade_order.status == AppConsts.ORDER_STATUS_SUBMITTED_EXIT:
                        resp = self.__alpaca_client.get_order(
                            order.trade_order.exit_alpaca_id)

                    if resp:
                        org: TradeOrder = BaseService._get_by_id(
                            TradeOrder, order.trade_order.id)
                        if not org:
                            raise NotFoundException('TradeOrder', 'id',
                                                    order.trade_order.id)

                        if order.trade_order.status == AppConsts.ORDER_STATUS_SUBMITTED_ENTRY \
                                and resp.status == AppConsts.ALPACA_ORDER_STATUS_FILLED:
                            org.status = AppConsts.ORDER_STATUS_IN_POSITION
                            org.actual_qty = NumberUtils.to_int(
                                resp.filled_qty)
                            org.actual_entry_price = NumberUtils.to_float(
                                resp.filled_avg_price)
                            org.modified = datetime.now()
                            BaseService._update()
                        elif order.trade_order.status == AppConsts.ORDER_STATUS_SUBMITTED_ENTRY \
                                and resp.status == AppConsts.ALPACA_ORDER_STATUS_CANCELLED:
                            org.status = AppConsts.ORDER_STATUS_CANCELLED_ENTRY
                            org.modified = datetime.now()
                            BaseService._update()
                        elif order.trade_order.status == AppConsts.ORDER_STATUS_SUBMITTED_EXIT \
                                and resp.status == AppConsts.ALPACA_ORDER_STATUS_FILLED:
                            exit_price: StockPriceDaily = self.__stock_service.get_single_stock_price_daily(
                                order.symbol_master.id,
                                DateUtils.get_date(
                                    datetime.today().strftime('%Y-%m-%d'),
                                    '%Y-%m-%d'))
                            if exit_price:
                                org.exit_stock_price_daily_id = exit_price.id
                            org.status = AppConsts.ORDER_STATUS_COMPLETED
                            org.actual_exit_price = NumberUtils.to_float(
                                resp.filled_avg_price)
                            org.modified = datetime.now()
                            BaseService._update()
                        elif order.trade_order.status == AppConsts.ORDER_STATUS_SUBMITTED_EXIT \
                                and resp.status == AppConsts.ALPACA_ORDER_STATUS_CANCELLED:
                            org.status = AppConsts.ORDER_STATUS_CANCELLED_EXIT
                            org.modified = datetime.now()
                            BaseService._update()
                            raise Exception('Exit Error = {0}'.format(
                                resp.status))
                        else:
                            raise Exception('Sync Status = {0}'.format(
                                resp.status))
                    else:
                        raise NotFoundException('Alpaca Order', 'id',
                                                order.trade_order.alpaca_id)

                except Exception as ex:
                    LogUtils.error('Sync Orders Error', ex)
                    errors.append(ex)

        except Exception as ex:
            LogUtils.error('Sync Orders Error', ex)
            errors.append(ex)
        finally:
            self.__email_client.send_html(
                subject=AppConsts.EMAIL_SUBJECT_SYNC_ORDERS,
                template_path=AppConsts.TEMPLATE_PATH_SYNC_ORDERS,
                model={'errors': errors})
            return 1
Ejemplo n.º 12
0
    def queue_positions(self) -> int:
        errors: List[Exception] = []
        try:
            is_tmrw_valid: bool = self.__alpaca_client.is_tmrw_valid()
            if not is_tmrw_valid:
                LogUtils.warning('Tmrw is not a valid trade date')
                raise BadRequestException('Date',
                                          DateUtils.to_string(date.today()))

            req: GetTradeOrdersRequest = GetTradeOrdersRequest()

            # If Sunday, check Friday's price.
            today: date = date.today()
            if today.weekday() == AppConsts.WEEKDAY_IDX_SUN:
                today = DateUtils.add_business_days(today, -1)
            req.created = today.strftime('%Y-%m-%d')

            req.exact_status = AppConsts.ORDER_STATUS_INIT
            orders: List[TradeOrderCustom] = self.get_trade_orders(req)

            req_to_ignore: GetTradeOrdersRequest = GetTradeOrdersRequest()
            req_to_ignore.status = [
                AppConsts.ORDER_STATUS_SUBMITTED_ENTRY,
                AppConsts.ORDER_STATUS_IN_POSITION,
                AppConsts.ORDER_STATUS_SUBMITTED_EXIT,
                AppConsts.ORDER_STATUS_CANCELLED_EXIT
            ]
            orders_to_ignore: List[TradeOrderCustom] = self.get_trade_orders(
                req_to_ignore)
            symbols_to_ignore: List[str] = [
                o.symbol_master.symbol for o in orders_to_ignore
            ] if orders_to_ignore else []

            LogUtils.debug('symbols_to_ignore = {0}'.format(symbols_to_ignore))

            if not orders:
                LogUtils.debug('No orders suggested')

            shuffle(orders)
            prioritized_orders: List[TradeOrderCustom] = []
            for order in orders:
                if order.trade_order.strategy == AppConsts.STRATEGY_DOUBLE_BOTTOMS:
                    prioritized_orders.append(order)
            for order in orders:
                if order.trade_order.strategy == AppConsts.STRATEGY_DOUBLE_TOPS:
                    prioritized_orders.append(order)

            accnt: Account = self.__alpaca_client.get_account()
            capital: float = NumberUtils.to_floor(
                NumberUtils.to_float(accnt._raw['buying_power']) /
                2)  # 2 to trade everyday
            for order in prioritized_orders:
                try:
                    LogUtils.debug('Try symbol = {0}'.format(
                        order.symbol_master.symbol))

                    if order.symbol_master.symbol in symbols_to_ignore:
                        LogUtils.debug('Ignore for = {0}'.format(
                            order.symbol_master.symbol))
                        continue

                    cost: float = NumberUtils.to_float(
                        order.stock_price_daily.close_price *
                        order.trade_order.qty)

                    if cost > capital:
                        LogUtils.debug('Too expensive for = {0}'.format(
                            order.symbol_master.symbol))
                        continue
                    capital = capital - cost

                    resp: Order = self.__alpaca_client.submit_order(
                        symbol=order.symbol_master.symbol,
                        qty=order.trade_order.qty,
                        action=order.trade_order.action)
                    if resp:
                        org: TradeOrder = BaseService._get_by_id(
                            TradeOrder, order.trade_order.id)
                        if not org:
                            raise NotFoundException('TradeOrder', 'id',
                                                    order.trade_order.id)
                        org.alpaca_id = resp.id
                        org.status = AppConsts.ORDER_STATUS_SUBMITTED_ENTRY
                        org.order_type = AppConsts.ORDER_TYPE_MARKET
                        org.time_in_force = AppConsts.TIME_IN_FORCE_DAY
                        org.modified = datetime.now()
                        BaseService._update()

                except Exception as ex:
                    LogUtils.error('Queue Position Error', ex)
                    errors.append(ex)

        except Exception as ex:
            LogUtils.error('Queue Position Error', ex)
            errors.append(ex)
        finally:
            self.__email_client.send_html(
                subject=AppConsts.EMAIL_SUBJECT_QUEUE_POSITIONS,
                template_path=AppConsts.TEMPLATE_PATH_QUEUE_POSITIONS,
                model={'errors': errors})
            return 1
Ejemplo n.º 13
0
 def import_from_csv_calculations(self) -> str:
     file: str = FileUtils.get_file(AppConsts.FINANCIAL_CALCS_FILE)
     records: List[Dict] = CsvUtils.parse_as_dict(file)
     for record in records:
         if not self.__is_valid_intrinio_record(record):
             continue
         symbol: str = record.get(AppConsts.INTRINIO_KEY_CALCS_TICKER)
         fiscal_period: str = record.get(
             AppConsts.INTRINIO_KEY_CALCS_FISC_PD)
         year: int = NumberUtils.to_int(
             record.get(AppConsts.INTRINIO_KEY_CALCS_FISC_YR))
         quarter: int = self.__get_quarter(fiscal_period)
         org_symbol: SM = self.__stock_service.get_symbol(
             symbol, AppConsts.INSTRUMENT_STOCK)
         if not org_symbol:
             continue
         org_fn: FN = self.__stock_service.get_financial(
             org_symbol.id, year, quarter)
         if not org_fn:
             continue
         org_fn.market_cap = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_MARK_CAP))
         org_fn.revenue_growth = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_REV_GRTH))
         org_fn.revenue_qq_growth = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_REV_QQ_GRTH))
         org_fn.nopat_growth = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_NOPAT_GRTH))
         org_fn.nopat_qq_growth = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_NOTPAT_QQ_GRTH))
         org_fn.net_income_growth = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_INCM_GRTH))
         org_fn.net_income_qq_growth = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_INCM_QQ_GRTH))
         org_fn.free_cash_flow = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_CSH_FLOW))
         org_fn.current_ratio = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_CURR_RATIO))
         org_fn.debt_to_equity_ratio = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_DE_RATIO))
         org_fn.pe_ratio = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_PE_RATIO))
         org_fn.pb_ratio = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_PB_RATIO))
         org_fn.div_payout_ratio = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_DIV_PAYOUT_RATIO))
         org_fn.roe = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_ROE))
         org_fn.roa = NumberUtils.to_float(
             record.get(AppConsts.INTRINIO_KEY_CALCS_ROA))
         BaseService._update()
     return "1"