Skip to content

Commit

Permalink
Simplify dependencies, add github workflows, black/isort code.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdz committed Feb 8, 2023
1 parent 0a6be82 commit d975078
Show file tree
Hide file tree
Showing 21 changed files with 457 additions and 417 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Push
on: [push]

jobs:
test:
strategy:
fail-fast: false
matrix:
python-version: ['3.10']
poetry-version: ['1.3.1']
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Run image
uses: abatilo/[email protected]
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Install dependencies
run: poetry install
- name: Run tests
run: poetry run python -m unittest
# run: poetry run pytest --cov=./ --cov-report=xml
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v2
code-quality:
strategy:
fail-fast: false
matrix:
python-version: ['3.10']
poetry-version: ['1.3.1']
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Run image
uses: abatilo/[email protected]
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Install dependencies
run: poetry install --with dev
- name: Run black
run: poetry run black . --check
- name: Run isort
run: poetry run isort . --check-only --profile black
# - name: Run flake8
# run: poetry run flake8 .
# - name: Run bandit
# run: poetry run bandit .
# - name: Run saftey
# run: poetry run safety check
6 changes: 6 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ build:
os: ubuntu-22.04
tools:
python: "3.10"

jobs:
post_install:
- pip install poetry==1.3.1
- poetry config virtualenvs.create false
- poetry install --with doc
19 changes: 10 additions & 9 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"name": "Python: Unittest",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"env": {"PYTHONPATH": "."}
"module": "unittest",
"justMyCode": true
},
{
"name": "Test Strat Runner",
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/tests/test_strategy_runner.py",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"env": {"PYTHONPATH": "."}
}
"env": {
"PYTHONPATH": "."
}
},

]
}
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ There are some basic tests but use at your own peril. It's not production level

## Core dependencies

The core module uses pandas.

## Other dependencies

The portfolio optimisation module uses scipy.
The core module uses pandas and scipy.

## Installation

Expand Down
19 changes: 9 additions & 10 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

import sys
import os
import sys

# ensure project path avaliable to sphinx
sys.path.insert(0, os.path.abspath(".."))
Expand All @@ -13,22 +13,21 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'yabte'
copyright = '2023, Blair Azzopardi'
author = 'Blair Azzopardi'
project = "yabte"
copyright = "2023, Blair Azzopardi"
author = "Blair Azzopardi"

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

extensions = ['sphinx.ext.autodoc', 'sphinx.ext.mathjax', 'sphinx.ext.viewcode']

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
extensions = ["sphinx.ext.autodoc", "sphinx.ext.mathjax", "sphinx.ext.viewcode"]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


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

html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']
html_theme = "sphinx_rtd_theme"
html_static_path = ["_static"]
23 changes: 16 additions & 7 deletions notebooks/Portfolio_Optimization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
"import numpy.linalg as la\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from yabte.backtest import Strategy, StrategyRunner, PositionalBasketOrder, OrderSizeType, Book\n",
"from yabte.backtest import (\n",
" Strategy,\n",
" StrategyRunner,\n",
" PositionalBasketOrder,\n",
" OrderSizeType,\n",
" Book,\n",
")\n",
"import yabte.utilities.pandas_extension\n",
"from yabte.portopt.hierarchical_risk_parity import hrp\n",
"from yabte.portopt.minimum_variance import minimum_variance\n",
Expand Down Expand Up @@ -53,6 +59,7 @@
"source": [
"# run strategy for each portfolio optimization scheme\n",
"\n",
"\n",
"class PortfolioOptimizationStrat(Strategy):\n",
" def init(self):\n",
" data = self.data\n",
Expand Down Expand Up @@ -84,11 +91,11 @@
" p[\"weights\"] = None\n",
" if cd:\n",
" closes = self.data.loc[:, (slice(None), \"Close\")].droplevel(axis=1, level=1)\n",
" returns = closes.price.log_returns\n",
" returns = closes.prc.log_returns\n",
" Sigma = returns.cov()\n",
" R = returns.corr()\n",
" sigma = returns.std()\n",
" mu = closes.price.capm_returns()\n",
" mu = closes.prc.capm_returns()\n",
"\n",
" match p.weight_scheme:\n",
" case \"HRP\":\n",
Expand Down Expand Up @@ -178,13 +185,13 @@
" + df_combined.loc[:, (slice(None), \"Close\")]\n",
" .mean(axis=1)\n",
" .to_frame()\n",
" .price.log_returns.loc[first_date:, :]\n",
" .prc.log_returns.loc[first_date:, :]\n",
" .iloc[1:, :]\n",
" ).cumprod(),\n",
" ]\n",
")\n",
"market.columns = [\"MARKET\"]\n",
"pd.concat([df.loc[first_date:, :], market], axis=1).plot()\n"
"pd.concat([df.loc[first_date:, :], market], axis=1).plot()"
]
},
{
Expand All @@ -205,9 +212,11 @@
],
"source": [
"# plot the weightings for each scheme\n",
"fig, axs = plt.subplots(3, 1, figsize=(1*6, 3*4), sharex=True)\n",
"fig, axs = plt.subplots(3, 1, figsize=(1 * 6, 3 * 4), sharex=True)\n",
"for ix, scheme in enumerate(schemes):\n",
" srs[ix].trade_history.pivot_table(index=\"ts\", values=\"quantity\", columns=\"asset_name\").cumsum().plot(ax=axs[ix], title=scheme)"
" srs[ix].trade_history.pivot_table(\n",
" index=\"ts\", values=\"quantity\", columns=\"asset_name\"\n",
" ).cumsum().plot(ax=axs[ix], title=scheme)"
]
},
{
Expand Down
9 changes: 7 additions & 2 deletions notebooks/Readme_Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"\n",
"data_dir = Path(inspect.getfile(Strategy)).parents[2] / \"tests/data/nasdaq\"\n",
"\n",
"\n",
"class SMAXO(Strategy):\n",
" def init(self):\n",
" # enhance data with simple moving averages\n",
Expand Down Expand Up @@ -97,8 +98,12 @@
"for symbol, scol, lcol in [(\"GOOG\", \"red\", \"green\"), (\"MSFT\", \"blue\", \"yellow\")]:\n",
" long_ix = th.query(f\"asset_name == '{symbol}' and quantity > 0\").ts\n",
" short_ix = th.query(f\"asset_name == '{symbol}' and quantity < 0\").ts\n",
" bvh.loc[long_ix].rename(columns={\"PrimaryBook\": f\"{symbol} Short\"}).plot(color=scol, marker=\"v\", markersize=5, linestyle=\"None\", ax=ax)\n",
" bvh.loc[short_ix].rename(columns={\"PrimaryBook\": f\"{symbol} Long\"}).plot(color=lcol, marker=\"^\", markersize=5, linestyle=\"None\", ax=ax)\n"
" bvh.loc[long_ix].rename(columns={\"PrimaryBook\": f\"{symbol} Short\"}).plot(\n",
" color=scol, marker=\"v\", markersize=5, linestyle=\"None\", ax=ax\n",
" )\n",
" bvh.loc[short_ix].rename(columns={\"PrimaryBook\": f\"{symbol} Long\"}).plot(\n",
" color=lcol, marker=\"^\", markersize=5, linestyle=\"None\", ax=ax\n",
" )"
]
},
{
Expand Down
Loading

0 comments on commit d975078

Please sign in to comment.