alpaca_daily_losers.daily_losers
Attributes
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
- class alpaca_daily_losers.daily_losers.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
- alpaca_daily_losers.daily_losers.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.daily_losers.send_message(message: str)
Send a message to Slack.
- Parameters:
message (str) – Message to send.
- alpaca_daily_losers.daily_losers.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.daily_losers.Liquidate(trading_client: py_alpaca_api.trading.Trading, py_logger: logging.Logger)
- static calculate_cash_needed(total_holdings: float, cash_row: pandas.DataFrame) float
Calculate the amount of cash needed to liquidate a portion of holdings.
- Parameters:
total_holdings (float) – The total value of the holdings to be liquidated.
cash_row (pd.DataFrame) – A DataFrame containing the cash information.
- Returns:
The amount of cash needed for liquidation, including a fixed fee of $5.00.
- Return type:
float
- static get_top_performers(current_positions: pandas.DataFrame) pandas.DataFrame
Returns the top performers from the given current positions DataFrame.
- Parameters:
current_positions (pd.DataFrame) – DataFrame containing the current positions.
- Returns:
DataFrame containing the top performers.
- Return type:
pd.DataFrame
- liquidate_positions() None
Liquidates positions to make cash 10% of the portfolio.
This method sells positions in order to meet the requirement of having cash equal to 10% of the portfolio’s total value. It identifies the top performers in the current positions and calculates the amount of cash needed to meet the requirement. It then sells the necessary amount of shares for each top performer.
- Returns:
None
- _sell_top_performers(top_performers: pandas.DataFrame, top_performers_market_value: float, cash_needed: float) list
Sells positions of top performers to liquidate the required cash.
- Parameters:
top_performers (pd.DataFrame) – DataFrame containing top performer positions.
top_performers_market_value (float) – The total market value of top performers.
cash_needed (float) – The amount of cash needed to be liquidated.
- Returns:
List of sold positions with their details.
- Return type:
list
- static _send_liquidation_message(message: str)
Send a liquidation message using the global send_message function.
- Parameters:
message (str) – The message to be sent.
- class alpaca_daily_losers.daily_losers.OpenAIAPI
- completion_with_backoff(**kwargs)
Makes a completion call to the OpenAI API with exponential backoff in case of failures.
- Returns:
The response object from the OpenAI API.
- Return type:
response (openai.Completion)
- chat(msgs)
Chat with the OpenAI API.
- Parameters:
msgs (list) – List of messages.
- Returns:
The response from OpenAI.
- Return type:
response (openai.ChatCompletion)
- get_sentiment_analysis(title, symbol, article)
Get the sentiment analysis for financial news.
- Parameters:
title (str) – The title of the news.
symbol (str) – The stock symbol associated with the news.
article (str) – The content of the news article.
- Returns:
The sentiment analysis result - “BEARISH”, “BULLISH”, or “NEUTRAL”.
- Return type:
signal (str)
- class alpaca_daily_losers.daily_losers.Statistics(account: py_alpaca_api.trading.account.Account, py_logger: logging.Logger)
- alpaca_daily_losers.daily_losers.WATCHLIST_NAME = 'DailyLosers'
- alpaca_daily_losers.daily_losers.DEFAULT_BUY_LIMIT = 4
- alpaca_daily_losers.daily_losers.DEFAULT_ARTICLE_LIMIT = 4
- alpaca_daily_losers.daily_losers.DEFAULT_STOP_LOSS_PERCENTAGE = 10.0
- alpaca_daily_losers.daily_losers.DEFAULT_TAKE_PROFIT_PERCENTAGE = 10.0
- alpaca_daily_losers.daily_losers.DEFAULT_FUTURE_DAYS = 4
- alpaca_daily_losers.daily_losers.PRODUCTION
- alpaca_daily_losers.daily_losers.API_KEY
- alpaca_daily_losers.daily_losers.API_SECRET
- alpaca_daily_losers.daily_losers.API_PAPER
- alpaca_daily_losers.daily_losers.logger
- class alpaca_daily_losers.daily_losers.DailyLosers
- run(buy_limit=DEFAULT_BUY_LIMIT, article_limit=DEFAULT_ARTICLE_LIMIT, stop_loss_percentage=DEFAULT_STOP_LOSS_PERCENTAGE, take_profit_percentage=DEFAULT_TAKE_PROFIT_PERCENTAGE, future_days=DEFAULT_FUTURE_DAYS)
Executes the main logic of the program, orchestrating the various components.
- check_for_buy_opportunities(buy_limit=DEFAULT_BUY_LIMIT, article_limit=DEFAULT_ARTICLE_LIMIT, future_days=DEFAULT_FUTURE_DAYS)
Checks for buy opportunities based on daily losers and news sentiment.
- open_positions(tickers: List[str], ticker_limit=DEFAULT_BUY_LIMIT)
Opens buying orders based on buy opportunities and OpenAI sentiment.
- update_or_create_watchlist(name: str, symbols: List[str])
Updates an existing watchlist or creates a new one.
- filter_tickers_with_news(tickers: List[str], article_limit=DEFAULT_ARTICLE_LIMIT, filter_ticker_limit=DEFAULT_BUY_LIMIT)
Filters tickers based on news sentiment.
- get_daily_losers(future_days=DEFAULT_FUTURE_DAYS)
Get daily losers based on the criteria and predictions.
- buy_criteria(data: pandas.DataFrame)
Apply buy criteria to the given DataFrame and return symbols that meet the criteria.