def update(portfolio: Tuple[str, ...]) -> None: StockDAO.create_if_not_exists(portfolio) rows: List[IntradayEntity] = IntradayEntity.query.with_entities( IntradayEntity.ticker).filter( IntradayEntity.ticker.in_(portfolio)).distinct( IntradayEntity.ticker).all() if len(rows) < len(portfolio): tickers: List[str] = list(map(lambda r: r.ticker, rows)) difference: str = Utils.first( sorted(list(set(portfolio) - set(tickers)))) if difference is not None: IntradayDAO.create_ticker(difference) else: rows: List[IntradayEntity] = db.session.query( IntradayEntity.ticker, db.func.max(IntradayEntity.date)).group_by( IntradayEntity.ticker).all() if rows is not None: latest_date: datetime = max(list(map(lambda r: r[1], rows))) ticker: str = Utils.first( sorted( list( map( lambda r: r.ticker, list( filter( lambda f: (f[1].date() != latest_date.date()), rows)))))) if ticker is not None: IntradayDAO.create_ticker(ticker)
def start() -> None: latest_date: datetime = ForwardDAO.read_latest_date() evaluation: EvaluationEntity = EvaluationDAO.read_order_by_sum() if evaluation is None or Utils.is_today(latest_date) or not Utils.is_working_day_ny(): return read_latest_date: List[IntradayEntity] = IntradayDAO.read_latest_date() latest_date_dict: Dict[str, str] = {r.ticker: r[0] for r in read_latest_date} rows: List[IntradayEntity] = IntradayDAO.read_order_by_date_asc() frame: DataFrame = IntradayDAO.dataframe(rows) inventory, cash, fee = ForwardBO.init() broker: BrokerBO = ForwardBrokerBO(cash, fee, inventory) statistic: StatisticBO = StatisticBO('forward') attempt: AttemptDTO = AttemptDTOConverter.from_evaluation(evaluation) AnalyserBO.analyse(frame, StrategyBO.counter_cyclical, broker, statistic, attempt, latest_date_dict)
def update(inventory: Dict[str, InventoryBO], cash: Decimal) -> Tuple[Dict[str, InventoryBO], Decimal, Decimal]: total: Decimal = ZERO total_value: Decimal = ZERO for ticker, entry in inventory.items(): intraday: IntradayEntity = IntradayDAO.read_filter_by_ticker_first(ticker) if intraday is None: continue entry.price = Decimal(intraday.close) total_value += entry.value() total += cash + total_value return inventory, total_value, total
def to_file() -> List[Dict[str, str]]: rows: List[IntradayEntity] = IntradayDAO.read_order_by_date_asc() rows_dict: List[Dict[str, str]] = list( map( lambda row: dict( list( map( lambda i: (str(i[0]), str(float(i[1])) if isinstance(i[1], Decimal) else str(i[1])), filter(lambda e: not e[0].startswith('_'), row.__dict__.items())))), rows)) return rows_dict
def intraday_view() -> str: return render_template('intraday.html', tables=[IntradayDAO.dataframe_ticker().to_html(classes='data', header='true')])
def stock_intraday_view(ticker: str) -> str: return render_template('stock-intraday.html', intradays=IntradayDAO.read_filter_by_ticker(ticker))
def start(portfolio: List[str], number: int, group_number: int) -> None: group_size: int = int(number / group_number) groups: Tuple[Tuple[str]] = Utils.group(group_size, portfolio[:number]) OptimizationBO.optimise(IntradayDAO.dataframe_group(groups))
def from_file(request: Request) -> None: IntradayDAO.create_from_file(request.files['file'].read())