wyn-pm


Namewyn-pm JSON
Version 0.2.8 PyPI version JSON
download
home_pageNone
SummaryThe library helps analysts to investigate portfolio and stock market.
upload_time2024-07-03 20:39:34
maintainerNone
docs_urlNone
authorYiqiao Yin
requires_python<4.0,>=3.9
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # WYN-PM ๐Ÿ“ˆ๐Ÿ’ผ

Welcome to the `wyn-pm` library, an official library from [W.Y.N. Associates, LLC](https://wyn-associates.com/) FinTech branch. This library provides tools for stock analysis, efficient portfolio generation, and training sequential neural networks for financial data.

[![YouTube Video](https://img.youtube.com/vi/85Rv7jg8gc8/0.jpg)](https://www.youtube.com/embed/85Rv7jg8gc8?si=L4zlKGmdJOu6bO82)

## Links

- [W.Y.N. Associates Fintech Library](https://wyn-associates.com/fintech/)
- [WYN PM on PyPI](https://pypi.org/project/wyn-pm/)
- [Web App](https://huggingface.co/spaces/eagle0504/Momentum-Strategy-Screener)
- [Github](https://github.com/yiqiao-yin/wyn-pm)

## Installation ๐Ÿš€

To install the library, use the following command:

```bash
! pip install wyn-pm
```

Please feel free to use [this jupyter notebook](https://github.com/yiqiao-yin/WYNAssociates/blob/main/docs/ref-deeplearning/ex_%20-%20wyn-pm%20tutorial.ipynb) as reference.

## Stock Analyzer: Plot Buy/Sell Signal ๐Ÿ“Š

Analyze stocks and plot buy/sell signals using the MACD indicator.

### Example Usage:

```python
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:

```python
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:

```python
# 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:

```python
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](https://onlinelibrary.wiley.com/doi/10.1111/j.1540-6261.1997.tb03808.x)

### 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 = \alpha^J + \beta_{mkt} * EXMKT_t + \epsilon_t
$$
The intercept in this model is referred to as "Jensen's alpha".

- The Valuation of Risk Assets and the Selection of Risky Investments in Stock Portfolios and Capital Budgets, [link](https://www.sciencedirect.com/science/article/abs/pii/B9780127808505500186)
- Capital Asset Prices: A Theory of Market Equilibrium under Conditions of Risk, [link](https://onlinelibrary.wiley.com/doi/10.1111/j.1540-6261.1964.tb02865.x)

### 2. Famaโ€“French Three-Factor Model:

$$
EXR_t = \alpha^{FF} + \beta_{mkt} * EXMKT_t + \beta_{HML} * HML_t + \beta_{SMB} * SMB_t + \epsilon_t
$$
The intercept in this model is referred to as the "three-factor alpha".

- Common risk factors in the returns on stocks and bonds, [link](https://www.sciencedirect.com/science/article/abs/pii/0304405X93900235?via%3Dihub)
- The Capital Asset Pricing Model: Theory and Evidence, [link](https://mba.tuck.dartmouth.edu/bespeneckbo/default/AFA611-Eckbo%20web%20site/AFA611-S6B-FamaFrench-CAPM-JEP04.pdf)
- The other side of value: The gross profitability premium, [link](https://www.sciencedirect.com/science/article/abs/pii/S0304405X13000044)
- A five-factor asset pricing model, [link](https://www.sciencedirect.com/science/article/abs/pii/S0304405X14002323?via%3Dihub)

### 3. Carhart Four-Factor Model:

$$
EXR_t = \alpha^c + \beta_{mkt} * EXMKT_t + \beta_{HML} * HML_t + \beta_{SMB} * SMB_t + \beta_{UMD} * UMD_t + \epsilon_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! ๐Ÿ–ฅ๏ธโœจ
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "wyn-pm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Yiqiao Yin",
    "author_email": "yiqiao.yin@wyn-associates.com",
    "download_url": "https://files.pythonhosted.org/packages/e8/96/6beac25881632daf9473eb01507cafbd881a78a82c93a30892e645be3e96/wyn_pm-0.2.8.tar.gz",
    "platform": null,
    "description": "# WYN-PM \ud83d\udcc8\ud83d\udcbc\n\nWelcome to the `wyn-pm` library, an official library from [W.Y.N. Associates, LLC](https://wyn-associates.com/) FinTech branch. This library provides tools for stock analysis, efficient portfolio generation, and training sequential neural networks for financial data.\n\n[![YouTube Video](https://img.youtube.com/vi/85Rv7jg8gc8/0.jpg)](https://www.youtube.com/embed/85Rv7jg8gc8?si=L4zlKGmdJOu6bO82)\n\n## Links\n\n- [W.Y.N. Associates Fintech Library](https://wyn-associates.com/fintech/)\n- [WYN PM on PyPI](https://pypi.org/project/wyn-pm/)\n- [Web App](https://huggingface.co/spaces/eagle0504/Momentum-Strategy-Screener)\n- [Github](https://github.com/yiqiao-yin/wyn-pm)\n\n## Installation \ud83d\ude80\n\nTo install the library, use the following command:\n\n```bash\n! pip install wyn-pm\n```\n\nPlease feel free to use [this jupyter notebook](https://github.com/yiqiao-yin/WYNAssociates/blob/main/docs/ref-deeplearning/ex_%20-%20wyn-pm%20tutorial.ipynb) as reference.\n\n## Stock Analyzer: Plot Buy/Sell Signal \ud83d\udcca\n\nAnalyze stocks and plot buy/sell signals using the MACD indicator.\n\n### Example Usage:\n\n```python\nfrom wyn_pm.stock_analyzer import *\n\n# Initialize stock analysis for a given ticker\nstock_analysis = StockAnalysis(ticker=\"AAPL\")\n\n# Fetch stock data\nstock_analysis.fetch_data()\n\n# Calculate MACD\nstock_analysis.calculate_macd()\n\n# Find crossovers to generate buy/sell signals\nstock_analysis.find_crossovers(bullish_threshold=-2, bearish_threshold=2)\n\n# Create and show the plot\nfig = stock_analysis.create_fig()\nfig.show()\n```\n\n## Efficient Portfolio: Generate Optimal Weights \ud83d\udcb9\n\nCreate an optimal portfolio by generating efficient weights for a list of stock tickers.\n\n### Example Usage:\n\n```python\nfrom wyn_pm.efficient_portfolio import *\n\n# Initialize portfolio with given tickers and date range\nportfolio = EfficientPortfolio(tickers=[\"AAPL\", \"MSFT\", \"GOOGL\"], start_date=\"2020-01-01\", end_date=\"2022-01-01\", interval=\"1d\")\n\n# Download stock data\nstock_data = portfolio.download_stock_data()\n\n# Calculate portfolio returns\nportfolio_returns = portfolio.create_portfolio_and_calculate_returns(top_n=5)\n\n# Calculate mean returns and covariance matrix\nmean_returns = stock_data.pct_change().mean()\ncov_matrix = stock_data.pct_change().cov()\n\n# Define the number of portfolios to simulate and the risk-free rate\nnum_portfolios = 10000\nrisk_free_rate = 0.01\n\n# Display the efficient frontier with randomly generated portfolios\nfig, details = portfolio.display_simulated_ef_with_random(mean_returns.values, cov_matrix.values, num_portfolios, risk_free_rate)\nfig.show()\n\n# Print details of the max Sharpe and min volatility portfolios\nprint(details)\n```\n\n## Momentum Strategy: Generate Portfolio Arbitrage\n\nCreate a portfolio based on the famous **momentum strateg** in asset pricing given a list of stock tickers.\n\n### Example Usage:\n\n```python\n# Acquire data for the \"Momentum Strategy\":\nportfolio = EfficientPortfolio(tickers=[\"AAPL\", \"MSFT\", \"GOOGL\", \"NFLX\", \"IBM\"], start_date=\"2017-01-01\", end_date=\"2024-07-01\", interval=\"1mo\")\nstock_data = portfolio.download_stock_data()\nportfolio_returns = portfolio.create_portfolio_and_calculate_returns(top_n=3)\n\n# Plot\nfig = portfolio.plot_portfolio_performance(portfolio_returns, height_of_graph=600)\nfig.show()\n```\n\n## Training Sequential Neural Networks: Stock Prediction \ud83e\udd16\ud83d\udcc8\n\nTrain various neural network models on stock data and perform Monte Carlo simulations.\n\n### Example Usage:\n\n```python\nfrom wyn_pm.trainer import *\n\n# Example usage:\nstock_modeling = StockModeling()\n\n# Training: ~ 9 min on CPU\nforecast_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)\n\n# Results\nprint(forecast_results)\nmc_figure.show()\n```\n\n## Technical Discussion of the Momentum Strategy\n\n### Monthly Momentum Factor (MOM)\n\nThe 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.\n\n- Carhart, M. M. (1997). On persistence in mutual fund performance. The Journal of finance, 52(1), 57-82. [link](https://onlinelibrary.wiley.com/doi/10.1111/j.1540-6261.1997.tb03808.x)\n\n### Four-Factor Model\n\nThe 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:\n\n#### 1. Market Model:\n\n$$\nEXR_t = \\alpha^J + \\beta_{mkt} * EXMKT_t + \\epsilon_t\n$$\nThe intercept in this model is referred to as \"Jensen's alpha\".\n\n- The Valuation of Risk Assets and the Selection of Risky Investments in Stock Portfolios and Capital Budgets, [link](https://www.sciencedirect.com/science/article/abs/pii/B9780127808505500186)\n- Capital Asset Prices: A Theory of Market Equilibrium under Conditions of Risk, [link](https://onlinelibrary.wiley.com/doi/10.1111/j.1540-6261.1964.tb02865.x)\n\n### 2. Fama\u2013French Three-Factor Model:\n\n$$\nEXR_t = \\alpha^{FF} + \\beta_{mkt} * EXMKT_t + \\beta_{HML} * HML_t + \\beta_{SMB} * SMB_t + \\epsilon_t\n$$\nThe intercept in this model is referred to as the \"three-factor alpha\".\n\n- Common risk factors in the returns on stocks and bonds, [link](https://www.sciencedirect.com/science/article/abs/pii/0304405X93900235?via%3Dihub)\n- The Capital Asset Pricing Model: Theory and Evidence, [link](https://mba.tuck.dartmouth.edu/bespeneckbo/default/AFA611-Eckbo%20web%20site/AFA611-S6B-FamaFrench-CAPM-JEP04.pdf)\n- The other side of value: The gross profitability premium, [link](https://www.sciencedirect.com/science/article/abs/pii/S0304405X13000044)\n- A five-factor asset pricing model, [link](https://www.sciencedirect.com/science/article/abs/pii/S0304405X14002323?via%3Dihub)\n\n### 3. Carhart Four-Factor Model:\n\n$$\nEXR_t = \\alpha^c + \\beta_{mkt} * EXMKT_t + \\beta_{HML} * HML_t + \\beta_{SMB} * SMB_t + \\beta_{UMD} * UMD_t + \\epsilon_t\n$$\n\nThe 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).\n\nA fund manager demonstrates forecasting ability when their fund has a positive and statistically significant alpha.\n\nSMB 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.\n\n---\n\nEnjoy analyzing stocks, creating efficient portfolios, and training neural networks with `wyn-pm`! If you have any questions, feel free to reach out.\n\nHappy coding! \ud83d\udda5\ufe0f\u2728",
    "bugtrack_url": null,
    "license": null,
    "summary": "The library helps analysts to investigate portfolio and stock market.",
    "version": "0.2.8",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc26d48c07c35753aac44f813ceb58c9ae7429105ae60561b8d9548fa3f13cb9",
                "md5": "59eae3e00952decb28055e9ae72fb003",
                "sha256": "1351972c7af6ba5d03b6da64e81bca2e64dee7290cd83bc09e5138f167180d9d"
            },
            "downloads": -1,
            "filename": "wyn_pm-0.2.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "59eae3e00952decb28055e9ae72fb003",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 14226,
            "upload_time": "2024-07-03T20:39:33",
            "upload_time_iso_8601": "2024-07-03T20:39:33.682338Z",
            "url": "https://files.pythonhosted.org/packages/bc/26/d48c07c35753aac44f813ceb58c9ae7429105ae60561b8d9548fa3f13cb9/wyn_pm-0.2.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e8966beac25881632daf9473eb01507cafbd881a78a82c93a30892e645be3e96",
                "md5": "2da9dc485a39032259b851153ddfce29",
                "sha256": "0adf3d90021d03872a415616c313c7914379712840dcf36233c4c5589fa6d53a"
            },
            "downloads": -1,
            "filename": "wyn_pm-0.2.8.tar.gz",
            "has_sig": false,
            "md5_digest": "2da9dc485a39032259b851153ddfce29",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 15549,
            "upload_time": "2024-07-03T20:39:34",
            "upload_time_iso_8601": "2024-07-03T20:39:34.772980Z",
            "url": "https://files.pythonhosted.org/packages/e8/96/6beac25881632daf9473eb01507cafbd881a78a82c93a30892e645be3e96/wyn_pm-0.2.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-03 20:39:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "wyn-pm"
}
        
Elapsed time: 0.26491s