pystock
A small python library for stock market analysis. Especially for portfolio optimization.
Installation
pip install pystock0
Note: You will need to call
pip install pystock0to install the library. However, you can import the library asimport pystock. The library is still in development, so, a lot of changes will be made to the code.
After installation, you can import the library as follows:
import pystock
Usage
The end goal of the library is to provide a simple interface for portfolio optimization. The library is still in development, so the interface is not yet stable. For now, this is how you can use the library to optimize a portfolio of stocks.
from pystock.portfolio import Portfolio
from pystock.models import Model
# Creating the benchmark and stocks
benchmark_dir = "Data/GSPC.csv"
benchmark_name = "S&P"
stock_dirs = ["Data/AAPL.csv", "Data/MSFT.csv", "Data/GOOGL.csv", "Data/TSLA.csv"]
stock_names = ["AAPL", "MSFT", "GOOGL", "TSLA"]
# Setting the frequency to monthly
frequency = "M"
# Creating a Portfolio object
pt = Portfolio(benchmark_dir, benchmark_name, stock_dirs, stock_names)
start_date = "2012-01-01"
end_date = "2022-12-20"
# Loading the data
pt.load_benchmark(
columns=["Close"],
start_date=start_date,
end_date=end_date,
frequency=frequency,
)
pt.load_all(
columns=["Close"],
start_date=start_date,
end_date=end_date,
frequency=frequency,
)
# Creating a Model object and adding the portfolio
model = Model(frequency=frequency, risk_free_rate=0.33)
model.add_portfolio(pt, weights="equal")
# Optimizing the portfolio using CAPM
risk = 0.5
model_ = "capm"
res = model.optimize_portfolio(risk=risk, model=model_)
print(res)
Optimized successfully.
Expected return: 1.1159%
Risk: 0.5000%
Expected weights:
--------------------
AAPL : 47.40%
MSFT : 0.00%
GOOGL : 35.83%
TSLA : 16.77%
{'weights': array([0.474 , 0. , 0.3583, 0.1677]), 'expected_return': 1.115892062822632, 'variance': 0.5000278422222152, 'std': 0.707126468336616}
More Examples
For more examples, please refer to the notebook Working_With_pystock.ipynb. Also have a look at Downloading_Data.ipynb. Please also have a look at Working_With_frontier.ipynb to see how to use the frontier module to plot efficient frontiers.
Documentation
The documentation is available at https://hari31416.github.io/pystock/.
Metadata
Release files for pystock0 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pystock0-0.3.0.tar.gz | 4.8 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pystock0-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 4.8 MB
Release files / pystock0-0.3.0.tar.gz
| Download URL | pystock0-0.3.0.tar.gz |
|---|---|
| Size | 4.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
452c38a721162e4d86a3078762d05954bc397d6d071d36cde3153a51d9b02405
|
|
BLAKE2b-256 checksum How to use checksums |
1a0edb3dce245a1fc2b596f5572c8c62f8f0cda3ddfe86eaf454002f9757eb07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.8
|
Release files / pystock0-0.3.0-py3-none-any.whl
| Download URL | pystock0-0.3.0-py3-none-any.whl |
|---|---|
| Size | 23.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c624e296d59722e3d69f6384fa49ca2526408836dc6c15e6623c21271ffaabe
|
|
BLAKE2b-256 checksum How to use checksums |
cf3f8c2e4bc39f32455b69ac1da2d8bb926d5eac1addc300753aed2c49daf4ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.8
|