Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/yfinance in testing #81

Merged
merged 23 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2684678
pandas.DataFrame.append is not supported anymore. Using pandas.concat…
fmilthaler Jul 5, 2023
d720291
Using Python >= 3.10
fmilthaler Jul 5, 2023
2335de3
removing quandl from setup.py
fmilthaler Jul 5, 2023
6de5221
Revert "removing quandl from setup.py"
fmilthaler Jul 5, 2023
054c035
fixing tests: moving_average
fmilthaler Jul 5, 2023
83448f7
tests: updating common test values
fmilthaler Jul 5, 2023
27357b3
updating first passing test
fmilthaler Jul 5, 2023
51e6b9f
commenting out all tests
fmilthaler Jul 5, 2023
212bab9
updating 2nd passing test
fmilthaler Jul 5, 2023
ac51544
updating tests for default option quandl
fmilthaler Jul 5, 2023
ad2a7fb
removing addition test for quandl (due to limited quota)
fmilthaler Jul 5, 2023
f892361
re-enabling remaining passing tests
fmilthaler Jul 5, 2023
61008b9
updating failing tests
fmilthaler Jul 5, 2023
01d41d4
removing plt.legend() to remove warning
fmilthaler Jul 5, 2023
4662210
re-adding tests for moving average that checks the plot
fmilthaler Jul 5, 2023
570b694
expected failing tests to be done in a loop
fmilthaler Jul 5, 2023
a837228
re-adding tests for MC optimisation, EF optimisation, and plots
fmilthaler Jul 5, 2023
145df98
when running tests, automagically insert plt.switch_backend("Agg") in…
fmilthaler Jul 5, 2023
688bc37
black formatting
fmilthaler Jul 5, 2023
920ecf8
gitignore idea files
fmilthaler Jul 7, 2023
b3deb45
updating examples with seaborn-v0_8-darkgrid
fmilthaler Jul 8, 2023
b4864fc
also set the quandl api key for examples
fmilthaler Jul 8, 2023
bd9a8cb
formatting
fmilthaler Jul 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ FinQuant.egg-info/*
*.aux
*.out
*.pdf

#########
# other #
#########
.idea
2 changes: 1 addition & 1 deletion example/Example-Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# <codecell>

# plotting style:
plt.style.use("seaborn-darkgrid")
plt.style.use("seaborn-v0_8-darkgrid")
# set line width
plt.rcParams["lines.linewidth"] = 2
# set font size for titles
Expand Down
6 changes: 5 additions & 1 deletion example/Example-Build-Portfolio-from-web.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
# e.g. high, low, close, etc, FinQuant will extract the column "Adj. Close" ("Adj Close" if using yfinance).

pf = build_portfolio(
names=names, pf_allocation=pf_allocation, start_date=start_date, end_date=end_date
names=names,
pf_allocation=pf_allocation,
start_date=start_date,
end_date=end_date,
data_api="quandl",
)

# <markdowncell>
Expand Down
2 changes: 1 addition & 1 deletion example/Example-Optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# <codecell>

# plotting style:
plt.style.use("seaborn-darkgrid")
plt.style.use("seaborn-v0_8-darkgrid")
# set line width
plt.rcParams["lines.linewidth"] = 2
# set font size for titles
Expand Down
3 changes: 1 addition & 2 deletions finquant/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@


class Portfolio(object):
"""Object that contains information about a investment portfolio.
"""Object that contains information about an investment portfolio.
To initialise the object, it does not require any input.
To fill the portfolio with investment information, the
function ``add_stock(stock)`` should be used, in which ``stock`` is
Expand Down Expand Up @@ -579,7 +579,6 @@ def plot_stocks(self, freq=252):
textcoords="offset points",
label=i,
)
plt.legend()

def properties(self):
"""Nicely prints out the properties of the portfolio:
Expand Down
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
"Intended Audience :: Other Audience",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
Expand All @@ -45,7 +44,7 @@
"quantitative",
"quant",
],
python_requires=">=3.5",
python_requires=">=3.10",
install_requires=[
"quandl",
"yfinance",
Expand Down
8 changes: 7 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ default: test
test:rempltshow
@pytest -v .

rempltshow:
rempltshow:plt_agg_backend
@-sed -i 's/plt.show()//g' test_Example*.py

plt_agg_backend:set_api_key_example
@-sed -i '/^import matplotlib\.pyplot as plt$$/a \plt.switch_backend("Agg")' test_Example*.py

set_api_key_example:
@-sed -i '/^from finquant\.portfolio import build_portfolio$$/a \import os;import quandl;quandl\.ApiConfig\.api_key = os\.getenv("QUANDLAPIKEY")' test_Example-Build-Portfolio-from-web.py

clean:
@-rm -rf *.pyc test_Example*.py __pycache__

16 changes: 9 additions & 7 deletions tests/test_moving_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from finquant.moving_average import compute_ma, sma, ema, sma_std, ema_std
from finquant.moving_average import plot_bollinger_band

plt.switch_backend("Agg")


def test_sma():
orig = np.array(
Expand All @@ -14,7 +16,7 @@ def test_sma():
)
dforig = pd.DataFrame({"0": orig[0], "1": orig[1]}).dropna()
l1 = range(10)
l2 = [i ** 2 for i in range(10)]
l2 = [i**2 for i in range(10)]
df = pd.DataFrame({"0": l1, "1": l2})
res = sma(df, span=2).dropna()
assert all((dforig == res).all())
Expand Down Expand Up @@ -51,7 +53,7 @@ def test_ema():
)
dforig = pd.DataFrame({"0": orig[0], "1": orig[1]}).dropna()
l1 = range(10)
l2 = [i ** 2 for i in range(10)]
l2 = [i**2 for i in range(10)]
df = pd.DataFrame({"0": l1, "1": l2})
res = ema(df, span=2).dropna()
assert all((abs(dforig - res) <= 1e-15).all())
Expand Down Expand Up @@ -88,7 +90,7 @@ def test_sma_std():
)
dforig = pd.DataFrame({"0": orig[0], "1": orig[1]}).dropna()
l1 = range(10)
l2 = [i ** 2 for i in range(10)]
l2 = [i**2 for i in range(10)]
df = pd.DataFrame({"0": l1, "1": l2})
res = sma_std(df, span=2).dropna()
assert all((abs(dforig - res) <= 1e-15).all())
Expand Down Expand Up @@ -125,7 +127,7 @@ def test_ema_std():
)
dforig = pd.DataFrame({"0": orig[0], "1": orig[1]}).dropna()
l1 = range(10)
l2 = [i ** 2 for i in range(10)]
l2 = [i**2 for i in range(10)]
df = pd.DataFrame({"0": l1, "1": l2})
res = ema_std(df, span=2).dropna()
assert all((abs(dforig - res) <= 1e-15).all())
Expand Down Expand Up @@ -168,12 +170,12 @@ def test_compute_ma():
)
x = np.sin(np.linspace(1, 10, 100))
df = pd.DataFrame({"Stock": x})
ma = compute_ma(df, ema, spans=[10, 30])
ma = compute_ma(df, ema, spans=[10, 30], plot=False)
assert all(abs((dforig - ma.describe()) <= 1e-15).all())


def test_plot_bollinger_band():
labels_orig = ["Stock", "15d", "Bollinger Band"]
labels_orig = ["Bollinger Band", "Stock", "15d"]
xlabel_orig = "Days"
ylabel_orig = "Price"
title_orig = (
Expand All @@ -187,7 +189,7 @@ def test_plot_bollinger_band():
# get data from axis object
ax = plt.gca()
# ax.lines[0] is the data we passed to plot_bollinger_band
# ax.lines[1] is the moving average (tested already)
# ax.lines[1] is the moving average (already tested)
# not sure how to obtain the data of the BollingerBand from
# the plot.
# only checking if input data matches data of first line on plot,
Expand Down