Exemplo n.º 1
0
 def get_symbol(self, symbol: str, instrument: str) -> SM:
     if StringUtils.isNullOrWhitespace(symbol):
         return None
     symbol = symbol.strip().upper()
     return BaseService._get_first(
         SM, [SM.symbol == symbol, SM.instrument == instrument])
Exemplo n.º 2
0
 def get_single_etf_price_daily(self, symbol_id: int,
                                price_date: date) -> SPD:
     return BaseService._get_first(
         EPD, [EPD.symbol_id == symbol_id, EPD.price_date == price_date])
Exemplo n.º 3
0
 def get_financial(self, symbol_id: int, year: int, quarter: int) -> FN:
     return BaseService._get_first(FN, [
         FN.symbol_id == symbol_id, FN.year == year, FN.quarter == quarter
     ])
Exemplo n.º 4
0
    def get_suggestions(self,
                        req: TradeSuggestionsRequest) -> List[TradeOrder]:
        if not req or not req.is_valid_model():
            raise BadRequestException()
        response: List[TradeOrder] = []

        # Init Symbols
        symbols: List[SymbolMaster] = self.__get_symbols__(req)

        # Init Prices
        date_to: date = date.today()
        date_from: date = date_to - timedelta(days=100)  # fix this
        prices: DataFrame = self.__get_prices__(symbols, date_from, date_to)

        # Do Base Preparation
        prices[AppConsts.CUSTOM_COL_PV] = prices[
            AppConsts.PRICE_COL_CLOSE] * prices[AppConsts.PRICE_COL_VOLUME]
        for s in symbols:
            prices = self.__calc_service.append_sma(
                prices=prices,
                index=[s.id],
                sma_period=AppConsts.ADV_PERIOD_DFLT,
                sma_column_name=AppConsts.CUSTOM_COL_ADV,
                target_column=AppConsts.PRICE_COL_VOLUME)
            prices = self.__calc_service.append_sma(
                prices=prices,
                index=[s.id],
                sma_period=AppConsts.ADPV_PERIOD_DFLT,
                sma_column_name=AppConsts.CUSTOM_COL_ADPV,
                target_column=AppConsts.CUSTOM_COL_PV)

        LogUtils.debug('Prices shape after base preparation={0}'.format(
            prices.shape))

        # region Init Service
        strategy_service: Any = self.__stock_service.get_strategy_service(
            req.strategy_type, req.strategy_request, symbols, prices)
        if not strategy_service or not strategy_service._is_valid_request():
            raise BadRequestException()
        strategy_service._do_preparations()

        LogUtils.debug('Prices shape after strategy preparation={0}'.format(
            prices.shape))
        # endregion

        LogUtils.debug(prices.info())

        # region Init Dates
        end_date: date = prices.index.get_level_values(
            AppConsts.PRICE_COL_DATE).max()
        LogUtils.debug('End_date={0}'.format(end_date))

        for symbol in symbols:
            LogUtils.debug('Start {0}'.format(symbol.symbol))

            has_price: bool = (symbol.id, end_date) in prices.index
            if not has_price:
                continue
            price: Series = prices.loc[symbol.id, end_date]

            adv: float = price.loc[AppConsts.CUSTOM_COL_ADV] if price.loc[
                AppConsts.CUSTOM_COL_ADV] > 0 else price.loc[
                    AppConsts.PRICE_COL_VOLUME]
            if adv < req.adv_min:
                continue
            adpv: float = price.loc[AppConsts.CUSTOM_COL_ADPV] if price.loc[
                AppConsts.CUSTOM_COL_ADPV] > 0 else price.loc[
                    AppConsts.CUSTOM_COL_PV]
            if adpv < req.adpv_min:
                continue

            has_entry_conditions: bool = strategy_service._has_entry_conditions(
                symbol.id, end_date)
            if not has_entry_conditions:
                continue
            no_of_shares: int = self.__stock_service.get_no_of_shares(
                req.current_capital, req.pct_risk_per_trade, req.volume_limit,
                price)
            if no_of_shares == 0:
                continue
            LogUtils.debug('Has Entry Condition = {0}'.format(symbol.symbol))

            now: datetime = datetime.now()
            order: TradeOrder = TradeOrder()
            order.stock_price_daily_id = price.loc[AppConsts.COL_ID]
            order.strategy = req.strategy_type
            order.status = AppConsts.ORDER_STATUS_INIT

            order.action = strategy_service._get_action()

            order.qty = no_of_shares
            # order.target_price = price.loc[AppConsts.CUSTOM_COL_DOUBLE_BOTTOMS_TARGET_PRICE]
            # order.stop_loss = price.loc[AppConsts.CUSTOM_COL_DOUBLE_BOTTOMS_STOP_LOSS]
            order.created = now
            order.modified = now
            response.append(order)
            if req.is_job:
                org: TradeOrder = BaseService._get_first(
                    TradeOrder, [
                        TradeOrder.stock_price_daily_id
                        == order.stock_price_daily_id, TradeOrder.strategy
                        == order.strategy
                    ])
                if not org:
                    BaseService._insert(order)

        return response