def clean_order(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["state"] = State.to_enum(data["state"]) data["time_in_force"] = TimeInForce.to_enum(data["time_in_force"]) data["side"] = ExecutionType.to_enum(data["side"]) data["type"] = OrderType.to_enum(data["type"]) data["executions"] = [ Execution(**clean_execution(e)) for e in data["executions"] ] consolidate_price_data(data, [ "price", "stop_price", "dollar_based_amount", "total_notional", "executed_notional" ]) data = convert_floats(data, [ "cumulative_quantity", "quantity", "average_price", "fees", "stop_price", "last_trail_price" ]) data = convert_dates(data, [ "last_transaction_at", "stop_triggered_at", "last_trail_price_updated_at" ]) return data
def clean_portfolio(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_floats( data, [ "market_value", "equity", "extended_hours_market_value", "extended_hours_equity", "extended_hours_portfolio_equity", "last_core_market_value", "last_core_equity", "last_core_portfolio_equity", "excess_margin", "excess_maintenance", "excess_margin_with_uncleared_deposits", "excess_maintenance_with_uncleared_deposits", "equity_previous_close", "portfolio_equity_previous_close", "adjusted_equity_previous_close", "adjusted_portfolio_equity_previous_close", "withdrawable_amount", "unwithdrawable_deposits", "unwithdrawable_grants", ], ) data = convert_dates(data, ["start_date"]) return data
def clean_execution(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_floats(data, ["price", "quantity"]) data = convert_dates(data, ["settlement_date", "timestamp"]) return data
def clean_rating(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["type"] = RatingType.to_enum(data["type"]) data = convert_dates(data, ["published_at"]) return data
def clean_historicals(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_floats( data, ["open_price", "close_price", "high_price", "low_price"]) data = convert_dates(data, ["begins_at"]) return data
def clean_mover(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["price_movement"] = PriceMovement( **clean_price_movement(data["price_movement"])) data = convert_dates(data) return data
def clean_offers(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["asks"] = [Offer(**clean_offer(o)) for o in data["asks"]] data["bids"] = [Offer(**clean_offer(o)) for o in data["bids"]] data = convert_dates(data) return data
def clean_transfer(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["direction"] = TransferDirection.to_enum(data["direction"]) data["state"] = State.to_enum(data["state"]) data["rhs_state"] = State.to_enum(data["rhs_state"]) data = convert_floats(data, ["amount", "fees", "early_access_amount"]) data = convert_dates( data, ["expected_landing_date", "expected_landing_datetime"]) return data
def clean_dividend(input_data: Dict[str, str]) -> Dict[str, Any]: data: Dict[str, Any] = deepcopy(input_data) data["drip_order_execution_price"] = ( Price(**clean_price(data["drip_order_execution_price"])) if "drip_order_execution_price" in data else None) data["state"] = DividendState.to_enum(data["state"]) data = convert_floats( data, ["amount", "rate", "position", "withholding", "drip_order_quantity"]) data = convert_dates(data, ["record_date", "payable_date", "paid_at"]) return data
def clean_option(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["min_ticks"] = Tick(**clean_tick(data["min_ticks"])) data["rhs_tradability"] = Tradability.to_enum(data["rhs_tradability"]) data["state"] = State.to_enum(data["state"]) data["tradability"] = Tradability.to_enum(data["tradability"]) data["type"] = OptionType.to_enum(data["type"]) data = convert_floats( data, [ "strike_price", "adjusted_mark_price", "ask_price", "ask_size", "bid_price", "bid_size", "break_even_price", "high_price", "last_trade_price", "last_trade_size", "low_price", "mark_price", "open_interest", "previous_close_price", "volume", "chance_of_profit_long", "chance_of_profit_short", "delta", "gamma", "implied_volatility", "rho", "theta", "vega", "high_fill_rate_buy_price", "high_fill_rate_sell_price", "low_fill_rate_buy_price", "low_fill_rate_sell_price", ], ) data = convert_dates(data, ["expiration_date", "issue_date", "sellout_datetime", "previous_close_date"]) return data
def clean_quote(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_floats( data, [ "ask_price", "bid_price", "last_trade_price", "last_extended_hours_trade_price", "previous_close", "adjusted_previous_close", ], ) data = convert_dates(data, ["previous_close_date"]) return data
def clean_ratings(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data["ratings"] = [Rating(**clean_rating(r)) for r in data["ratings"]] # Map summary keys to RatingType enum mapping = { "num_buy_ratings": RatingType.BUY, "num_hold_ratings": RatingType.HOLD, "num_sell_ratings": RatingType.SELL, } summary: Dict[RatingType, int] = {} for summary_key, value in data["summary"].items(): if summary_key in mapping.keys(): summary[mapping[summary_key]] = value else: print(f"WARNING: Rating Summary Type: {summary_key} not found.") data["summary"] = summary data = convert_dates(data, ["ratings_published_at"]) return data
def clean_position(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_floats( data, [ "quantity", "intraday_quantity", "average_buy_price", "pending_average_buy_price", "intraday_average_buy_price", "shares_available_for_exercise", "shares_held_for_buys", "shares_held_for_options_collateral", "shares_held_for_options_events", "shares_held_for_sells", "shares_held_for_stock_grants", "shares_pending_from_options_events", ], ) data = convert_dates(data) return data
def clean_fundamentals(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_floats( data, [ "open", "high", "low", "volume", "average_volume_2_weeks", "average_volume", "high_52_weeks", "low_52_weeks", "market_cap", "shares_outstanding", ], ) data = convert_floats(data, ["dividend_yield", "float", "pb_ratio", "pe_ratio"], 0.00) data = convert_dates(data, ["datetime"]) return data
def clean_earnings_call(input_data: Dict[str, Any]) -> Dict[str, Any]: return convert_dates(input_data, ["datetime"])
def clean_earnings_report(input_data: Dict[str, Any]) -> Dict[str, Any]: return convert_dates(input_data, ["date"])
def clean_news(input_data: Dict[str, Any]) -> Dict[str, Any]: data = deepcopy(input_data) data = convert_dates(data, ["published_at"]) return data