def get_no_of_shares(self, capital: float, pct_risk_per_trade: float, volume_limit: float, price: Series, slippage: int = None, is_slip_up: bool = True) -> int: if not AppConsts.PRICE_COL_OPEN in price.index \ or not AppConsts.CUSTOM_COL_ADV in price.index: return 0 open_price: float = price.loc[AppConsts.PRICE_COL_OPEN] if slippage: if is_slip_up: open_price = NumberUtils.round(open_price + ( open_price * AppConsts.BASIS_POINT * slippage)) else: open_price = NumberUtils.round(open_price - ( open_price * AppConsts.BASIS_POINT * slippage)) no_of_shares: int = NumberUtils.to_floor(capital * pct_risk_per_trade / 100 / open_price) adv: float = price.loc[AppConsts.CUSTOM_COL_ADV] if price.loc[ AppConsts.CUSTOM_COL_ADV] > 0 else price.loc[ AppConsts.PRICE_COL_VOLUME] max_volume: float = NumberUtils.to_int(adv * volume_limit / 100) if no_of_shares > max_volume: LogUtils.warning( 'Capping max_volume adv={0}, no_of_shares={1}, max_volume={2}'. format(adv, no_of_shares, max_volume)) no_of_shares = max_volume return no_of_shares
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
def import_from_csv_filedates(self) -> str: file: str = FileUtils.get_file(AppConsts.INCOME_STMT_FILE) records: List[Dict] = CsvUtils.parse_as_dict(file) for record in records: if not self.__is_valid_intrinio_record_for_filedates(record): continue symbol: str = record.get(AppConsts.INTRINIO_KEY_INC_STMT_TICKER) fiscal_period: str = record.get( AppConsts.INTRINIO_KEY_INC_STMT_FISC_PD) year: int = NumberUtils.to_int( record.get(AppConsts.INTRINIO_KEY_INC_STMT_FISC_YR)) quarter: int = self.__get_quarter(fiscal_period) file_date: date = DateUtils.get_date( record.get(AppConsts.INTRINIO_KEY_INC_STMT_FILE_DTE), AppConsts.INTRINIO_FILE_DTE_FMT) if not file_date or quarter == 4: continue 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.file_date = file_date BaseService._update() return "1"
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
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"
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"
def set_readonly_props(self) -> None: if not self._transactions: return self._start_capital = self._capital.get( ModelUtils.get_first_key(self._capital)) self._end_capital = self._capital.get( ModelUtils.get_last_key(self._capital)) self._hold_length_days_stats = StatUtils.get_descriptive_stats( [t.hold_length_days for t in self._transactions]) self._change_in_capital_stats = StatUtils.get_descriptive_stats( [t.change_in_capital for t in self._transactions]) self._has_profit_stats = StatUtils.get_descriptive_stats( [NumberUtils.to_int(t.has_profit) for t in self._transactions]) self._pct_return = NumberUtils.get_change(self._end_capital, self._start_capital) self._best_transactions = [ t for t in sorted(self._transactions, key=lambda x: x.change_in_capital, reverse=True) if t.has_profit ][:20] self._worst_transactions = [ t for t in sorted(self._transactions, key=lambda x: x.change_in_capital) if not t.has_profit ][:20] symbol_grouped: Dict = {} for t in self._transactions: if not t.symbol_master.symbol in symbol_grouped: symbol_grouped[t.symbol_master.symbol]: Dict = { 'symbol_master': t.symbol_master, 'change_in_capital': 0, 'no_of_transactions': 0 } symbol_grouped[t.symbol_master. symbol]['change_in_capital'] += t.change_in_capital symbol_grouped[t.symbol_master.symbol]['no_of_transactions'] += 1 symbol_grouped_list: List[BackTestResultItemPerSymbol] = [] for k, v in symbol_grouped.items(): item: BackTestResultItemPerSymbol = BackTestResultItemPerSymbol() item.symbol_master = symbol_grouped[k]['symbol_master'] item.change_in_capital = NumberUtils.round( symbol_grouped[k]['change_in_capital']) item.no_of_transactions = symbol_grouped[k]['no_of_transactions'] symbol_grouped_list.append(item) self._best_symbols = [ i for i in sorted(symbol_grouped_list, key=lambda x: x.change_in_capital, reverse=True) if i.change_in_capital > 0 ][:20] self._worst_symbols = [ i for i in sorted(symbol_grouped_list, key=lambda x: x.change_in_capital) if i.change_in_capital < 0 ][:20]
def __get_quarter(self, fiscal_period: str) -> int: if StringUtils.isNullOrWhitespace(fiscal_period): return None if fiscal_period.endswith(AppConsts.INTRINIO_PERIOD_SUFFIX_FY): return 4 return NumberUtils.to_int( fiscal_period.replace(AppConsts.INTRINIO_PERIOD_PREFIX, '').replace( AppConsts.INTRINIO_PERIOD_SUFFIX_TTM, ''))
def append_abz( self, prices: DataFrame, index: Any, abz_er_period: int, abz_std_distance: float, abz_constant_k: float, target_column: str = AppConsts.PRICE_COL_CLOSE) -> DataFrame: if not isinstance(prices, DataFrame) \ or not target_column in prices.columns: return None if not AppConsts.CUSTOM_COL_ABZ_PERIOD in prices.columns: prices[AppConsts.CUSTOM_COL_ABZ_PERIOD] = 0 if not AppConsts.CUSTOM_COL_ABZ_MIDDLE in prices.columns: prices[AppConsts.CUSTOM_COL_ABZ_MIDDLE] = 0 if not AppConsts.CUSTOM_COL_ABZ_STD in prices.columns: prices[AppConsts.CUSTOM_COL_ABZ_STD] = 0 if not AppConsts.CUSTOM_COL_ABZ_UPPER in prices.columns: prices[AppConsts.CUSTOM_COL_ABZ_UPPER] = 0 if not AppConsts.CUSTOM_COL_ABZ_LOWER in prices.columns: prices[AppConsts.CUSTOM_COL_ABZ_LOWER] = 0 direction: Series = prices.loc[index][target_column].diff( abz_er_period).abs() volatility: Series = prices.loc[index][target_column].diff().abs( ).rolling(abz_er_period).sum() er: Series = direction / volatility periods: Series = er * abz_constant_k prices[AppConsts.CUSTOM_COL_ABZ_PERIOD].update(periods) cursor: int = 0 for i, row in prices.loc[index].iterrows(): symbol_id: int = i[0] curr_date: date = i[1] period: int = NumberUtils.to_int( row[AppConsts.CUSTOM_COL_ABZ_PERIOD]) period = period if period > 2 else 2 sma_series: Series = prices.loc[index][target_column].rolling( period).mean() std_series: Series = prices.loc[index][target_column].rolling( period).std() sma: float = sma_series[symbol_id, curr_date] std: float = std_series[symbol_id, curr_date] upper: float = sma + (std * abz_std_distance) lower: float = sma - (std * abz_std_distance) prices[AppConsts.CUSTOM_COL_ABZ_MIDDLE].loc[symbol_id, curr_date] = sma prices[AppConsts.CUSTOM_COL_ABZ_UPPER].loc[symbol_id, curr_date] = upper prices[AppConsts.CUSTOM_COL_ABZ_LOWER].loc[symbol_id, curr_date] = lower cursor += 1 return prices
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"
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