diff --git a/.github/workflows/auto-format.yml b/.github/workflows/auto-format.yml new file mode 100644 index 00000000..4b1e0632 --- /dev/null +++ b/.github/workflows/auto-format.yml @@ -0,0 +1,49 @@ +name: AutoUpdateFormatting +on: + push: + branches: + - master + - develop + pull_request: + branches: + - master + - develop + +jobs: + code-formatting: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.10' # Specify the desired Python version + + - name: Install dependencies + run: | + pip install -r requirements_test.txt + + - name: Updating README files + id: update_readme + run: | + bash scripts/update-readme.sh + bash scripts/auto-commit.sh "Updating README files" + continue-on-error: true + + - name: Code formatting and committing changes + id: code_format + run: | + bash scripts/auto-format.sh + bash scripts/auto-commit.sh "Automated formatting changes" + continue-on-error: true + + - name: Push changes + if: ${{ steps.update_readme.outcome == 'success' || steps.code_format.outcome == 'success' }} + uses: ad-m/github-push-action@master + with: + branch: ${{ github.head_ref }} diff --git a/README.md b/README.md index e6232e3e..41d29515 100644 --- a/README.md +++ b/README.md @@ -268,4 +268,3 @@ Furthermore, it is also shown how the entire *Efficient Frontier* and the optima Also, the optimisation of a portfolio and its visualisation based on a *Monte Carlo* is shown. Finally, *FinQuant*'s visualisation methods allow for overlays, if this is desired. Thus, with only the following few lines of code, one can create an overlay of the *Monte Carlo* run, the *Efficient Frontier*, its optimised portfolios for *Minimum Volatility* and *Maximum Sharpe Ratio*, as well as the portfolio's individual stocks. - diff --git a/example/Example-Analysis.py b/example/Example-Analysis.py index b012961c..ba0f4682 100644 --- a/example/Example-Analysis.py +++ b/example/Example-Analysis.py @@ -9,10 +9,11 @@ # +import datetime import pathlib + import matplotlib.pyplot as plt import pandas as pd -import datetime # importing FinQuant's function to automatically build the portfolio from finquant.portfolio import build_portfolio diff --git a/example/Example-Build-Portfolio-from-file.py b/example/Example-Build-Portfolio-from-file.py index 1bc856b9..1f61118e 100644 --- a/example/Example-Build-Portfolio-from-file.py +++ b/example/Example-Build-Portfolio-from-file.py @@ -9,9 +9,10 @@ # +import datetime import pathlib + import pandas as pd -import datetime # importing FinQuant's function to automatically build the portfolio from finquant.portfolio import build_portfolio diff --git a/example/Example-Build-Portfolio-from-web.py b/example/Example-Build-Portfolio-from-web.py index 18f2e701..f2406c24 100644 --- a/example/Example-Build-Portfolio-from-web.py +++ b/example/Example-Build-Portfolio-from-web.py @@ -9,9 +9,10 @@ # -import pandas as pd import datetime +import pandas as pd + # importing some custom functions/objects from finquant.portfolio import build_portfolio diff --git a/example/Example-Optimisation.py b/example/Example-Optimisation.py index 566d54bb..5646e300 100644 --- a/example/Example-Optimisation.py +++ b/example/Example-Optimisation.py @@ -26,11 +26,12 @@ # +import datetime import pathlib + import matplotlib.pyplot as plt import numpy as np import pandas as pd -import datetime # importing FinQuant's function to automatically build the portfolio from finquant.portfolio import build_portfolio diff --git a/finquant/asset.py b/finquant/asset.py index 7baba64b..ea51c50d 100644 --- a/finquant/asset.py +++ b/finquant/asset.py @@ -45,7 +45,9 @@ class Asset: """ - def __init__(self, data: pd.Series, name: str, asset_type: str = 'Market index') -> None: + def __init__( + self, data: pd.Series, name: str, asset_type: str = "Market index" + ) -> None: """ :Input: :data: ``pandas.Series``, of asset prices @@ -119,4 +121,4 @@ def properties(self): def __str__(self): # print short description string = f"Contains information about {self.asset_type}: {self.name}." - return string \ No newline at end of file + return string diff --git a/finquant/market.py b/finquant/market.py index b4652591..426bbe20 100644 --- a/finquant/market.py +++ b/finquant/market.py @@ -14,6 +14,7 @@ import numpy as np import pandas as pd + from finquant.asset import Asset from finquant.returns import daily_returns, historical_mean_return @@ -43,4 +44,4 @@ def comp_daily_returns(self) -> pd.Series: """Computes the daily returns (percentage change) of the market index. See ``finquant.returns.daily_returns``. """ - return daily_returns(self.data) \ No newline at end of file + return daily_returns(self.data) diff --git a/finquant/portfolio.py b/finquant/portfolio.py index 210ca307..1401de60 100644 --- a/finquant/portfolio.py +++ b/finquant/portfolio.py @@ -49,8 +49,8 @@ - Individual stocks of the portfolio (Expected Return over Volatility) """ -from typing import List import datetime +from typing import List import matplotlib.pylab as plt import numpy as np diff --git a/finquant/stock.py b/finquant/stock.py index a0f2da37..c47b32b7 100644 --- a/finquant/stock.py +++ b/finquant/stock.py @@ -25,6 +25,7 @@ import numpy as np import pandas as pd + from finquant.asset import Asset from finquant.returns import daily_returns, historical_mean_return diff --git a/requirements_dev.txt b/requirements_dev.txt index d50a4b6d..e4d01882 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,4 +1,4 @@ -black==23.1.0 +black mypy isort jupyter diff --git a/requirements_test.txt b/requirements_test.txt index da5b1675..5260e7d3 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,2 +1,5 @@ pytest>=7.3.2 +black +mypy +isort pylint \ No newline at end of file diff --git a/scripts/auto-commit.sh b/scripts/auto-commit.sh new file mode 100644 index 00000000..84d57b84 --- /dev/null +++ b/scripts/auto-commit.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +echo "Running auto commit" + +COMMITMSG=$1 + +if [ -z "$COMMITMSG" ]; then + COMMITMSG="Automated formatting changes" +fi + +# Stage changes +git add --update + +# Check Git diff-index +git diff-index --quiet HEAD -- + +if [ $? -eq 0 ]; then + echo "No changes found, nothing to see/do here." + exit 1 +else + echo "Changes found. Preparing commit." + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git commit -m "${COMMITMSG}" +fi diff --git a/scripts/auto-format.sh b/scripts/auto-format.sh new file mode 100644 index 00000000..ed107031 --- /dev/null +++ b/scripts/auto-format.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# Code formatting with isort and black +echo "Code formatting with isort and black:" +isort $(git ls-files '*.py') +black $(git ls-files '*.py') diff --git a/scripts/update-readme.sh b/scripts/update-readme.sh new file mode 100644 index 00000000..6f92f16a --- /dev/null +++ b/scripts/update-readme.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +update_version_readme() { + local version_file="version" + local readme_md="$1" + + # Read the current version from the "version" file + local current_version=$(grep -Eo 'version=([0-9]+\.){2}[0-9]+' "$version_file" | cut -d'=' -f2) + + update_file() { + local file=$1 + sed -i "s/pypi-v[0-9]\+\.[0-9]\+\.[0-9]\+/pypi-v$current_version/" "$file" + echo "Version updated to $current_version in $file" + } + + # Update version in README.md + update_file "$readme_md" +} + +update_readme_tex() { + local file_path="$1" + + # Copy README.md to README.tex.md + cp README.md "$file_path" + + # Read the contents of README.tex.md + local content=$(<"$file_path") + + # Replace patterns + content=$(echo "$content" | sed -E "s//\$\\\\displaystyle\\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_0} + \\\\text{dividend}}{\\\\text{price}_{t_0}}\$/") + + content=$(echo "$content" | sed -E "s//\$\\\\displaystyle\\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_{i-1}}}{\\\\text{price}_{t_{i-1}}}\$/") + + content=$(echo "$content" | sed -E "s//\$\\\\displaystyle\\\\log\\\\left(1 + \\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_{i-1}}}{\\\\text{price}_{t_{i-1}}}\\\\right)\$/") + + # Write the updated contents back to README.tex.md + echo "$content" > "$file_path" +} + +# Update both readme files: +echo "Updating README files:" +update_version_readme "README.md" +update_readme_tex "README.tex.md" diff --git a/scripts/update_readme.tex.md.sh b/scripts/update_readme.tex.md.sh deleted file mode 100644 index 441d0347..00000000 --- a/scripts/update_readme.tex.md.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -replace_patterns() { - local file_path="$1" - - # Copy README.md to README.tex.md - cp README.md "$file_path" - - # Read the contents of README.tex.md - local content=$(<"$file_path") - - # Replace patterns - content=$(echo "$content" | sed -E "s//\$\\\\displaystyle\\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_0} + \\\\text{dividend}}{\\\\text{price}_{t_0}}\$/") - - content=$(echo "$content" | sed -E "s//\$\\\\displaystyle\\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_{i-1}}}{\\\\text{price}_{t_{i-1}}}\$/") - - content=$(echo "$content" | sed -E "s//\$\\\\displaystyle\\\\log\\\\left(1 + \\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_{i-1}}}{\\\\text{price}_{t_{i-1}}}\\\\right)\$/") - - # Write the updated contents back to README.tex.md - echo "$content" > "$file_path" -} - -# Specify the file path for README.tex.md -tex_file_path="README.tex.md" - -# Call the function to perform the replacements -replace_patterns "$tex_file_path" \ No newline at end of file diff --git a/scripts/update_version_readme.sh b/scripts/update_version_readme.sh deleted file mode 100644 index 0f1b3357..00000000 --- a/scripts/update_version_readme.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -update_version_readme() { - local version_file="version" - local readme_md="README.md" - - # Read the current version from the "version" file - local current_version=$(grep -Eo 'version=([0-9]+\.){2}[0-9]+' "$version_file" | cut -d'=' -f2) - - update_file() { - local file=$1 - sed -i "s/pypi-v[0-9]\+\.[0-9]\+\.[0-9]\+/pypi-v$current_version/" "$file" - echo "Version updated to $current_version in $file" - } - - # Update version in README.md - update_file "$readme_md" -} - -# Call the update_version function -update_version_readme \ No newline at end of file diff --git a/setup.py b/setup.py index 578803f0..bb76ab19 100644 --- a/setup.py +++ b/setup.py @@ -21,10 +21,10 @@ def read_requirements(file_path): install_requires = read_requirements("requirements.txt") extras_require = { - "test": read_requirements("requirements_test.txt"), + "cd": read_requirements("requirements_cd.txt"), "dev": read_requirements("requirements_dev.txt"), "docs": read_requirements("requirements_docs.txt"), - "cd": read_requirements("requirements_cd.txt"), + "test": read_requirements("requirements_test.txt"), } setuptools.setup(