alpaca_daily_losers.close_positions
Classes
Functions
|
Retrieve historical data for given tickers and compute technical indicators. |
|
Send a message to Slack. |
|
Sends position messages based on the type of position. |
Module Contents
- alpaca_daily_losers.close_positions.get_ticker_data(tickers, stock_client: py_alpaca_api.Stock, py_logger=None) pandas.DataFrame
Retrieve historical data for given tickers and compute technical indicators.
- Parameters:
tickers (list) – List of stock ticker symbols.
stock_client (Stock) – Stock client for retrieving historical data.
py_logger (logging.Logger, optional) – Logger for logging warnings and errors.
- Returns:
DataFrame with the latest technical indicators for each ticker.
- Return type:
df_tech (pd.DataFrame)
- alpaca_daily_losers.close_positions.send_message(message: str)
Send a message to Slack.
- Parameters:
message (str) – Message to send.
- alpaca_daily_losers.close_positions.send_position_messages(positions: list, pos_type: str)
Sends position messages based on the type of position.
- Parameters:
positions (list) – List of position dictionaries.
pos_type (str) – Type of position (“buy”, “sell”, or “liquidate”).
- Returns:
True if message was sent successfully, False otherwise.
- Return type:
bool
- class alpaca_daily_losers.close_positions.ClosePositions(trading_client: py_alpaca_api.Trading, stock_client: py_alpaca_api.Stock, py_logger: logging.Logger)
- sell_positions_from_criteria(stop_loss_percentage: float = 10.0, take_profit_percentage: float = 10.0) None
Sells positions based on the defined sell criteria, including RSI and Bollinger Band High Index (BBHI) thresholds, as well as take profit and stop loss conditions.
This method first retrieves the current positions, filters out the cash position, and then uses the get_stocks_to_sell() method to determine which positions should be sold. It then calls the _sell_positions() method to execute the sell orders and sends messages to notify of the sold positions. If no positions meet the sell criteria, a message is sent indicating that no sell opportunities were found. :raises Exception: If an error occurs while selling the positions.
- _sell_positions(stocks_to_sell: List[str], current_positions: pandas.DataFrame) List[dict]
Sell positions for the given stocks.
- Parameters:
stocks_to_sell (list) – List of symbols for the stocks to sell.
current_positions (pandas.DataFrame) – DataFrame containing the current positions.
- Returns:
List of dictionaries representing the sold positions, each containing the symbol and quantity.
- Return type:
list
- get_stocks_to_sell(stop_loss_percentage: float = 10.0, take_profit_percentage: float = 10.0) List[str]
Retrieves a list of stocks to sell based on specific criteria.
- Parameters:
stop_loss_percentage (float) – The stop loss percentage criteria.
take_profit_percentage (float) – The take profit percentage criteria.
- Returns:
A list of stocks to sell.
- Return type:
list
- _add_stocks_from_criteria(non_cash_positions: pandas.DataFrame, stocks_to_sell: List[str], stop_loss_percentage: float, take_profit_percentage: float) List[str]
Adds stocks to the sell list based on stop loss and take profit criteria.
- Parameters:
non_cash_positions (pd.DataFrame) – DataFrame of non-cash positions.
stocks_to_sell (list) – Current list of stocks to sell.
stop_loss_percentage (float) – Stop loss percentage criteria.
take_profit_percentage (float) – Take profit percentage criteria.
- Returns:
Updated list of stocks to sell.
- Return type:
list