Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeboute committed Jan 19, 2025
1 parent ccaeca1 commit d7c92c7
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 26 deletions.
Empty file removed app/__init__.py
Empty file.
13 changes: 0 additions & 13 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import pystock

project = "pystock"
copyright = "2025, Martin Debouté"
author = "Martin Debouté"
release = pystock.__version__

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
Expand All @@ -25,7 +15,4 @@

exclude_patterns = []

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "pydata_sphinx_theme"
7 changes: 1 addition & 6 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
.. pystock documentation master file, created by
sphinx-quickstart on Sun Jan 19 14:52:42 2025.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
pystock documentation
=====================

.. toctree::
:maxdepth: 2
:maxdepth: 1
:caption: Contents:

pystock
Expand Down
2 changes: 1 addition & 1 deletion docs/source/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ pystock
=======

.. toctree::
:maxdepth: 4
:maxdepth: 2

pystock
7 changes: 7 additions & 0 deletions pystock/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
__version__ = "0.1.0"

import asset
import portfolio
import quantitative
from asset import *
from portfolio import *
from quantitative import *
6 changes: 6 additions & 0 deletions pystock/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class Asset:
def __init__(self, symbol: str, listed: bool = True):
"""Asset object.
Args:
symbol (str): Bloomberg ticker of the asset.
listed (bool, optional): If the asset is listed on Yahoo Finance or not. Default to True.
"""
self.symbol = symbol
if listed:
self.ticker = yf.Ticker(self.symbol)
Expand Down
12 changes: 7 additions & 5 deletions pystock/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@


class Portfolio:
def __init__(
self,
assets: List[Asset],
weights: np.ndarray,
):
def __init__(self, assets: List[Asset], weights: List[float] | np.ndarray):
"""Portfolio object.
Args:
assets (List[Asset]): The list of assets composing the portfolio.
weights (List[float] | np.ndarray): The proportion of each asset in the portfolio.
"""
self.assets = assets
self.weights = weights
self._cov_matrix = None
Expand Down
13 changes: 12 additions & 1 deletion pystock/quantitative.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import pyomo.environ as pyo
from joblib import Parallel, delayed
import plotly.graph_objects as go

import pystock.constants as cst
from pystock.portfolio import Portfolio


class PortfolioOptimizer:
def __init__(self, portfolio: Portfolio):
"""An optimizer of your Portfolio.
Args:
portfolio (Portfolio): The portfolio that you want to optimize.
"""
self.portfolio = portfolio
self.model = self._build_core_model()
self.solver = pyo.SolverFactory(cst.DEFAULT_SOLVER)
Expand Down Expand Up @@ -63,6 +69,11 @@ def _link_constraint_rule(model, i):

class MonteCarloSimulator:
def __init__(self, portfolio: Portfolio):
"""A Monte Carlo Simulator for your portfolio.
Args:
portfolio (Portfolio): The portfolio you want to work on.
"""
self.portfolio = portfolio

def _calculate_metrics(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_asset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest

from pystock.asset import Asset


Expand All @@ -8,3 +9,8 @@ def setUp(self):

def test_asset_name(self):
self.assertEqual(self.asset.name, "Apple Inc.")

def test_historical_data(self):
self.asset.fetch_historical_data()
self.assertIsNotNone(self.asset._historic_data)
self.assertEqual(self.asset._period_in_day, self.asset._historic_data.shape[0]) # type: ignore
16 changes: 16 additions & 0 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest

from pystock import Asset, Portfolio


class TestPortfolio(unittest.TestCase):
def setUp(self):
self.appl_stock = Asset("AAPL")
self.tsla_stock = Asset("TSLA")
self.msft_stock = Asset("MSFT")

def test_bad_weighted_portfolio(self):
with self.assertRaises(ValueError):
Portfolio(
[self.appl_stock, self.tsla_stock, self.msft_stock], [0.3, 0.3, 0.3]
)

0 comments on commit d7c92c7

Please sign in to comment.