The library helps analysts to investigate portfolio and stock market.
Project description
WYN-PM 📈💼
Welcome to the wyn-pm
library, an official library from W.Y.N. Associates, LLC FinTech branch. This library provides tools for stock analysis, efficient portfolio generation, and training sequential neural networks for financial data.
Links
Installation 🚀
To install the library, use the following command:
! pip install wyn-pm
Please feel free to use this jupyter notebook as reference.
Stock Analyzer: Plot Buy/Sell Signal 📊
Analyze stocks and plot buy/sell signals using the MACD indicator.
Example Usage:
from wyn_pm.stock_analyzer import *
# Initialize stock analysis for a given ticker
stock_analysis = StockAnalysis(ticker="AAPL")
# Fetch stock data
stock_analysis.fetch_data()
# Calculate MACD
stock_analysis.calculate_macd()
# Find crossovers to generate buy/sell signals
stock_analysis.find_crossovers(bullish_threshold=-2, bearish_threshold=2)
# Create and show the plot
fig = stock_analysis.create_fig()
fig.show()
Efficient Portfolio: Generate Optimal Weights 💹
Create an optimal portfolio by generating efficient weights for a list of stock tickers.
Example Usage:
from wyn_pm.efficient_portfolio import *
# Initialize portfolio with given tickers and date range
portfolio = EfficientPortfolio(tickers=["AAPL", "MSFT", "GOOGL"], start_date="2020-01-01", end_date="2022-01-01", interval="1d")
# Download stock data
stock_data = portfolio.download_stock_data()
# Calculate portfolio returns
portfolio_returns = portfolio.create_portfolio_and_calculate_returns(top_n=5)
# Calculate mean returns and covariance matrix
mean_returns = stock_data.pct_change().mean()
cov_matrix = stock_data.pct_change().cov()
# Define the number of portfolios to simulate and the risk-free rate
num_portfolios = 10000
risk_free_rate = 0.01
# Display the efficient frontier with randomly generated portfolios
fig, details = portfolio.display_simulated_ef_with_random(mean_returns.values, cov_matrix.values, num_portfolios, risk_free_rate)
fig.show()
# Print details of the max Sharpe and min volatility portfolios
print(details)
Momentum Strategy: Generate Portfolio Arbitrage
Create a portfolio based on the famous momentum strateg in asset pricing given a list of stock tickers.
Example Usage:
# Acquire data for the "Momentum Strategy":
portfolio = EfficientPortfolio(tickers=["AAPL", "MSFT", "GOOGL", "NFLX", "IBM"], start_date="2017-01-01", end_date="2024-07-01", interval="1mo")
stock_data = portfolio.download_stock_data()
portfolio_returns = portfolio.create_portfolio_and_calculate_returns(top_n=3)
# Plot
fig = portfolio.plot_portfolio_performance(portfolio_returns, height_of_graph=600)
fig.show()
Training Sequential Neural Networks: Stock Prediction 🤖📈
Train various neural network models on stock data and perform Monte Carlo simulations.
Example Usage:
from wyn_pm.trainer import *
# Example usage:
stock_modeling = StockModeling()
# Training: ~ 9 min on CPU
forecast_results, mc_figure = stock_modeling.forecast_and_plot(stock="AAPL", start_date="2020-01-01", end_date="2023-01-01", look_back=50, num_of_epochs=10, n_futures=365, n_samples=1000, verbose_style=1)
# Results
print(forecast_results)
mc_figure.show()
Technical Discussion of the Momentum Strategy
Monthly Momentum Factor (MOM)
The Monthly Momentum Factor (MOM) can be calculated by subtracting the equal-weighted average of the lowest performing firms from the equal-weighted average of the highest performing firms, lagged one month (Carhart, 1997). A stock exhibits momentum if its prior 12-month average of returns is positive. Similar to the three-factor model, the momentum factor is defined by a self-financing portfolio of (long positive momentum) + (short negative momentum). Momentum strategies remain popular in financial markets, and financial analysts often incorporate the 52-week price high/low in their Buy/Sell recommendations.
- Carhart, M. M. (1997). On persistence in mutual fund performance. The Journal of finance, 52(1), 57-82. link
Four-Factor Model
The four-factor model is commonly used for active management and mutual fund evaluation. Three commonly used methods to adjust a mutual fund's returns for risk are:
1. Market Model:
$$ EXR_t = α^J + β_mkt * EXMKT_t + ε_t $$ The intercept in this model is referred to as "Jensen's alpha".
2. Fama–French Three-Factor Model:
$$ EXR_t = α^FF + β_mkt * EXMKT_t + β_HML * HML_t + β_SMB * SMB_t + ε_t $$ The intercept in this model is referred to as the "three-factor alpha".
3. Carhart Four-Factor Model:
$$ EXR_t = α^c + β_mkt * EXMKT_t + β_HML * HML_t + β_SMB * SMB_t + β_UMD * UMD_t + ε_t $$
The intercept in this model is referred to as the "four-factor alpha".
EXR_t
is the monthly return to the asset of concern in excess of the monthly t-bill rate. These models are used to adjust for risk by regressing the excess returns of the asset on an intercept (the alpha) and some factors on the right-hand side of the equation that attempt to control for market-wide risk factors. The right-hand side risk factors include the monthly return of the CRSP value-weighted index less the risk-free rate (EXMKT_t
), monthly premium of the book-to-market factor (HML_t
), monthly premium of the size factor (SMB_t
), and the monthly premium on winners minus losers (UMD_t
) from Fama-French (1993) and Carhart (1997).
A fund manager demonstrates forecasting ability when their fund has a positive and statistically significant alpha.
SMB is a zero-investment portfolio that is long on small capitalization (cap) stocks and short on big-cap stocks. Similarly, HML is a zero-investment portfolio that is long on high book-to-market (B/M) stocks and short on low B/M stocks, and UMD is a zero-cost portfolio that is long previous 12-month return winners and short previous 12-month loser stocks.
Enjoy analyzing stocks, creating efficient portfolios, and training neural networks with wyn-pm
! If you have any questions, feel free to reach out.
Happy coding! 🖥️✨
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.