Skip to main content

pystock

PyPI version Downloads example event parameter

A small python library for stock market analysis. Especially for portfolio optimization.

Installation

pip install pystock0

Note: You will need to call pip install pystock0 to install the library. However, you can import the library as import 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/.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystock0-0.3.0.tar.gz (4.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pystock0-0.3.0-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file pystock0-0.3.0.tar.gz.

File metadata

  • Download URL: pystock0-0.3.0.tar.gz
  • Upload date:
  • Size: 4.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for pystock0-0.3.0.tar.gz
Algorithm Hash digest
SHA256 452c38a721162e4d86a3078762d05954bc397d6d071d36cde3153a51d9b02405
MD5 6d8dcb498b6cb41fb379197f317407f5
BLAKE2b-256 1a0edb3dce245a1fc2b596f5572c8c62f8f0cda3ddfe86eaf454002f9757eb07

See more details on using hashes here.

File details

Details for the file pystock0-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pystock0-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for pystock0-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c624e296d59722e3d69f6384fa49ca2526408836dc6c15e6623c21271ffaabe
MD5 a5c01b0efb86bc5f410d72c58341041d
BLAKE2b-256 cf3f8c2e4bc39f32455b69ac1da2d8bb926d5eac1addc300753aed2c49daf4ac

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page