stock-analyser


Namestock-analyser JSON
Version 0.3.7 PyPI version JSON
download
home_pagehttps://github.com/themagicalmammal/stock-analyser
SummaryClasses for technical analysis of stocks.
upload_time2023-01-03 16:03:27
maintainer
docs_urlNone
authorStefanie Molin
requires_python
licenseMIT
keywords python stock analysis
VCS
bugtrack_url
requirements matplotlib numpy pandas pandas-datareader seaborn statsmodels mplfinance
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Stock Analysis

Package for making elements of technical analysis of a stock easier. This package is meant to be a starting point for you to develop your own. As such, all the instructions for installing/setup will be assuming you will continue to develop on your end.

## Usage

This section will show some of the functionality of each class; however, it is by no means exhaustive.

### Getting data

```python
from stock_analysis import StockReader

reader = StockReader("2017-01-01", "2018-12-31")

# get bitcoin data in USD
bitcoin = reader.get_bitcoin_data("USD")

# get faang data
fb, aapl, amzn, nflx, goog = (
    reader.get_ticker_data(ticker) for ticker in ["FB", "AAPL", "AMZN", "NFLX", "GOOG"]
)

# get S&P 500 data
sp = reader.get_index_data("S&P 500")
```

### Grouping data

```python
from stock_analysis import group_stocks, describe_group

faang = group_stocks(
    {"Facebook": fb, "Apple": aapl, "Amazon": amzn, "Netflix": nflx, "Google": goog}
)

# describe the group
describe_group(faang)
```

### Building a portfolio

Groups assets by date and sums columns to build a portfolio.

```python
from stock_analysis import make_portfolio

faang_portfolio = make_portfolio(faang)
```

### Visualizing data

Be sure to check out the other methods here for different plot types, reference lines, shaded regions, and more!

#### Single asset

Evolution over time:

```python
import matplotlib.pyplot as plt
from stock_analysis import StockVisualizer

netflix_viz = StockVisualizer(nflx)

ax = netflix_viz.evolution_over_time(
    "close", figsize=(10, 4), legend=False, title="Netflix closing price over time"
)
netflix_viz.add_reference_line(
    ax,
    x=nflx.high.idxmax(),
    color="k",
    linestyle=":",
    label=f"highest value ({nflx.high.idxmax():%b %d})",
    alpha=0.5,
)
ax.set_ylabel("price ($)")
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/netflix_line_plot.png" align="center" width="600" alt="line plot with reference line">

After hours trades:

```python
netflix_viz.after_hours_trades()
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/netflix_after_hours_trades.png" align="center" width="800" alt="after hours trades plot">

Differential in closing price versus another asset:

```python
netflix_viz.fill_between_other(fb)
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/nflx_vs_fb_closing_price.png" align="center" width="600" alt="differential between NFLX and FB">

Candlestick plots with resampling (uses `mplfinance`):

```python
netflix_viz.candlestick(
    resample="2W", volume=True, xrotation=90, datetime_format="%Y-%b -"
)
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/candlestick.png" align="center" width="600" alt="resampled candlestick plot">

*Note: run `help()` on `StockVisualizer` for more visualizations*

#### Asset groups

Correlation heatmap:

```python
from stock_analysis import AssetGroupVisualizer

faang_viz = AssetGroupVisualizer(faang)
faang_viz.heatmap(True)
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/faang_heatmap.png" align="center" width="450" alt="correlation heatmap">

*Note: run `help()` on `AssetGroupVisualizer` for more visualizations. This object has many of the visualizations of the `StockVisualizer` class.*

### Analyzing data

Below are a few of the metrics you can calculate.

#### Single asset

```python
from stock_analysis import StockAnalyzer

nflx_analyzer = stock_analysis.StockAnalyzer(nflx)
nflx_analyzer.annualized_volatility()
```

#### Asset group

Methods of the `StockAnalyzer` class can be accessed by name with the `AssetGroupAnalyzer` class's `analyze()` method.

```python
from stock_analysis import AssetGroupAnalyzer

faang_analyzer = AssetGroupAnalyzer(faang)
faang_analyzer.analyze("annualized_volatility")

faang_analyzer.analyze("beta")
```

### Modeling

```python
from stock_analysis import StockModeler
```

#### Time series decomposition

```python
decomposition = StockModeler.decompose(nflx, 20)
fig = decomposition.plot()
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/nflx_ts_decomposition.png" align="center" width="450" alt="time series decomposition">

#### ARIMA

Build the model:

```python
arima_model = StockModeler.arima(nflx, 10, 1, 5)
```

Check the residuals:

```python
StockModeler.plot_residuals(arima_model)
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/arima_residuals.png" align="center" width="650" alt="ARIMA residuals">

Plot the predictions:

```python
arima_ax = StockModeler.arima_predictions(
    arima_model, start=start, end=end, df=nflx, ax=axes[0], title="ARIMA"
)
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/arima_predictions.png" align="center" width="450" alt="ARIMA predictions">

#### Linear regression

Build the model:

```python
X, Y, lm = StockModeler.regression(nflx)
```

Check the residuals:

```python
StockModeler.plot_residuals(lm)
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/lm_residuals.png" align="center" width="650" alt="linear regression residuals">

Plot the predictions:

```python
linear_reg = StockModeler.regression_predictions(
    lm, start=start, end=end, df=nflx, ax=axes[1], title="Linear Regression"
)
plt.show()
```

<img src="https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/lm_predictions.png" align="center" width="450" alt="linear regression predictions">

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/themagicalmammal/stock-analyser",
    "name": "stock-analyser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "python,stock,analysis",
    "author": "Stefanie Molin",
    "author_email": "d19cyber@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/02/36/adca62c367fa13c7d6944eff3096d2eee696615b836f169f5124e883ad30/stock-analyser-0.3.7.tar.gz",
    "platform": null,
    "description": "\n# Stock Analysis\n\nPackage for making elements of technical analysis of a stock easier. This package is meant to be a starting point for you to develop your own. As such, all the instructions for installing/setup will be assuming you will continue to develop on your end.\n\n## Usage\n\nThis section will show some of the functionality of each class; however, it is by no means exhaustive.\n\n### Getting data\n\n```python\nfrom stock_analysis import StockReader\n\nreader = StockReader(\"2017-01-01\", \"2018-12-31\")\n\n# get bitcoin data in USD\nbitcoin = reader.get_bitcoin_data(\"USD\")\n\n# get faang data\nfb, aapl, amzn, nflx, goog = (\n    reader.get_ticker_data(ticker) for ticker in [\"FB\", \"AAPL\", \"AMZN\", \"NFLX\", \"GOOG\"]\n)\n\n# get S&P 500 data\nsp = reader.get_index_data(\"S&P 500\")\n```\n\n### Grouping data\n\n```python\nfrom stock_analysis import group_stocks, describe_group\n\nfaang = group_stocks(\n    {\"Facebook\": fb, \"Apple\": aapl, \"Amazon\": amzn, \"Netflix\": nflx, \"Google\": goog}\n)\n\n# describe the group\ndescribe_group(faang)\n```\n\n### Building a portfolio\n\nGroups assets by date and sums columns to build a portfolio.\n\n```python\nfrom stock_analysis import make_portfolio\n\nfaang_portfolio = make_portfolio(faang)\n```\n\n### Visualizing data\n\nBe sure to check out the other methods here for different plot types, reference lines, shaded regions, and more!\n\n#### Single asset\n\nEvolution over time:\n\n```python\nimport matplotlib.pyplot as plt\nfrom stock_analysis import StockVisualizer\n\nnetflix_viz = StockVisualizer(nflx)\n\nax = netflix_viz.evolution_over_time(\n    \"close\", figsize=(10, 4), legend=False, title=\"Netflix closing price over time\"\n)\nnetflix_viz.add_reference_line(\n    ax,\n    x=nflx.high.idxmax(),\n    color=\"k\",\n    linestyle=\":\",\n    label=f\"highest value ({nflx.high.idxmax():%b %d})\",\n    alpha=0.5,\n)\nax.set_ylabel(\"price ($)\")\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/netflix_line_plot.png\" align=\"center\" width=\"600\" alt=\"line plot with reference line\">\n\nAfter hours trades:\n\n```python\nnetflix_viz.after_hours_trades()\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/netflix_after_hours_trades.png\" align=\"center\" width=\"800\" alt=\"after hours trades plot\">\n\nDifferential in closing price versus another asset:\n\n```python\nnetflix_viz.fill_between_other(fb)\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/nflx_vs_fb_closing_price.png\" align=\"center\" width=\"600\" alt=\"differential between NFLX and FB\">\n\nCandlestick plots with resampling (uses `mplfinance`):\n\n```python\nnetflix_viz.candlestick(\n    resample=\"2W\", volume=True, xrotation=90, datetime_format=\"%Y-%b -\"\n)\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/candlestick.png\" align=\"center\" width=\"600\" alt=\"resampled candlestick plot\">\n\n*Note: run `help()` on `StockVisualizer` for more visualizations*\n\n#### Asset groups\n\nCorrelation heatmap:\n\n```python\nfrom stock_analysis import AssetGroupVisualizer\n\nfaang_viz = AssetGroupVisualizer(faang)\nfaang_viz.heatmap(True)\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/faang_heatmap.png\" align=\"center\" width=\"450\" alt=\"correlation heatmap\">\n\n*Note: run `help()` on `AssetGroupVisualizer` for more visualizations. This object has many of the visualizations of the `StockVisualizer` class.*\n\n### Analyzing data\n\nBelow are a few of the metrics you can calculate.\n\n#### Single asset\n\n```python\nfrom stock_analysis import StockAnalyzer\n\nnflx_analyzer = stock_analysis.StockAnalyzer(nflx)\nnflx_analyzer.annualized_volatility()\n```\n\n#### Asset group\n\nMethods of the `StockAnalyzer` class can be accessed by name with the `AssetGroupAnalyzer` class's `analyze()` method.\n\n```python\nfrom stock_analysis import AssetGroupAnalyzer\n\nfaang_analyzer = AssetGroupAnalyzer(faang)\nfaang_analyzer.analyze(\"annualized_volatility\")\n\nfaang_analyzer.analyze(\"beta\")\n```\n\n### Modeling\n\n```python\nfrom stock_analysis import StockModeler\n```\n\n#### Time series decomposition\n\n```python\ndecomposition = StockModeler.decompose(nflx, 20)\nfig = decomposition.plot()\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/nflx_ts_decomposition.png\" align=\"center\" width=\"450\" alt=\"time series decomposition\">\n\n#### ARIMA\n\nBuild the model:\n\n```python\narima_model = StockModeler.arima(nflx, 10, 1, 5)\n```\n\nCheck the residuals:\n\n```python\nStockModeler.plot_residuals(arima_model)\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/arima_residuals.png\" align=\"center\" width=\"650\" alt=\"ARIMA residuals\">\n\nPlot the predictions:\n\n```python\narima_ax = StockModeler.arima_predictions(\n    arima_model, start=start, end=end, df=nflx, ax=axes[0], title=\"ARIMA\"\n)\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/arima_predictions.png\" align=\"center\" width=\"450\" alt=\"ARIMA predictions\">\n\n#### Linear regression\n\nBuild the model:\n\n```python\nX, Y, lm = StockModeler.regression(nflx)\n```\n\nCheck the residuals:\n\n```python\nStockModeler.plot_residuals(lm)\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/lm_residuals.png\" align=\"center\" width=\"650\" alt=\"linear regression residuals\">\n\nPlot the predictions:\n\n```python\nlinear_reg = StockModeler.regression_predictions(\n    lm, start=start, end=end, df=nflx, ax=axes[1], title=\"Linear Regression\"\n)\nplt.show()\n```\n\n<img src=\"https://raw.githubusercontent.com/themagicalmammal/stock-analyser/main/images/lm_predictions.png\" align=\"center\" width=\"450\" alt=\"linear regression predictions\">\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Classes for technical analysis of stocks.",
    "version": "0.3.7",
    "split_keywords": [
        "python",
        "stock",
        "analysis"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ac2805d2c45945faafc8c343ebf020c5e7b05ceae53c41836bc560e37ee2b06b",
                "md5": "5b51e4932aabc46a10f8de5710dbc47d",
                "sha256": "e38c410aed300eab86f76b74da8c87f0621ca9c939f189b8abec6f1fb5e2f4a0"
            },
            "downloads": -1,
            "filename": "stock_analyser-0.3.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5b51e4932aabc46a10f8de5710dbc47d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 17533,
            "upload_time": "2023-01-03T16:03:26",
            "upload_time_iso_8601": "2023-01-03T16:03:26.446472Z",
            "url": "https://files.pythonhosted.org/packages/ac/28/05d2c45945faafc8c343ebf020c5e7b05ceae53c41836bc560e37ee2b06b/stock_analyser-0.3.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0236adca62c367fa13c7d6944eff3096d2eee696615b836f169f5124e883ad30",
                "md5": "87930d1c9e35ad5047d1a697dcb25613",
                "sha256": "a782f5147d3ef31adffd953dd3f1168fbbb556f5ee19c1e3452cf8c5757b077e"
            },
            "downloads": -1,
            "filename": "stock-analyser-0.3.7.tar.gz",
            "has_sig": false,
            "md5_digest": "87930d1c9e35ad5047d1a697dcb25613",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 17411,
            "upload_time": "2023-01-03T16:03:27",
            "upload_time_iso_8601": "2023-01-03T16:03:27.625788Z",
            "url": "https://files.pythonhosted.org/packages/02/36/adca62c367fa13c7d6944eff3096d2eee696615b836f169f5124e883ad30/stock-analyser-0.3.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-03 16:03:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "themagicalmammal",
    "github_project": "stock-analyser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "matplotlib",
            "specs": [
                [
                    ">=",
                    "3.0.2"
                ]
            ]
        },
        {
            "name": "numpy",
            "specs": [
                [
                    ">=",
                    "1.15.2"
                ]
            ]
        },
        {
            "name": "pandas",
            "specs": [
                [
                    ">=",
                    "0.23.4"
                ]
            ]
        },
        {
            "name": "pandas-datareader",
            "specs": [
                [
                    ">=",
                    "0.7.0"
                ]
            ]
        },
        {
            "name": "seaborn",
            "specs": [
                [
                    ">=",
                    "0.11.0"
                ]
            ]
        },
        {
            "name": "statsmodels",
            "specs": [
                [
                    ">=",
                    "0.11.1"
                ]
            ]
        },
        {
            "name": "mplfinance",
            "specs": [
                [
                    ">=",
                    "0.12.7a4"
                ]
            ]
        }
    ],
    "lcname": "stock-analyser"
}
        
Elapsed time: 0.06118s