Skip to content

Commit

Permalink
Update code to reflect Yahoo Finance changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JerBouma committed Feb 15, 2024
1 parent 6bf2930 commit 6270fa9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
4 changes: 3 additions & 1 deletion thepassiveinvestor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Modules
"""The Passive Investor Initialization"""

# flake8: noqa
from .create_report import create_ETF_report
from .collect_data import collect_data
from .utils import data_placer, graph_placer, image_placer
Expand Down
9 changes: 6 additions & 3 deletions thepassiveinvestor/collect_data.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Data Collection Module"""
from datetime import datetime
import pandas as pd

import pandas as pd
from yahooquery import Ticker

from .config import DEFAULT_KEY_STATISTICS_CHOICES, DEFAULT_SUMMARY_DETAIL_CHOICES

# pylint: disable=too-many-locals,broad-exception-caught


def collect_data(tickers, comparison=False, surpress_print=False):
"""
Expand Down Expand Up @@ -125,8 +128,8 @@ def collect_data(tickers, comparison=False, surpress_print=False):
ticker_dataframe = pd.DataFrame.from_dict(
{
(i, j): ticker_data[ticker][i][j]
for i in ticker_data[ticker].keys()
for j in ticker_data[ticker][i].keys()
for i in ticker_data[ticker]
for j in ticker_data[ticker][i]
},
orient="index",
columns=[ticker],
Expand Down
23 changes: 13 additions & 10 deletions thepassiveinvestor/create_report.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import pandas as pd
from yahooquery import Ticker
"""Create Report Module"""

import os

import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment
from openpyxl.styles import Alignment, Font
from openpyxl.utils.dataframe import dataframe_to_rows
from yahooquery import Ticker

from .collect_data import collect_data
from .config import (
DEFAULT_KEY_STATISTICS_CHOICES,
DEFAULT_SUMMARY_DETAIL_CHOICES,
SECTOR_CATEGORY_MAPPING,
EMPTY_RISK_STATISTICS,
RISK_STATISTICS_CATEGORY_MAPPING,
SECTOR_CATEGORY_MAPPING,
)
from .utils import data_placer, image_placer, graph_placer
from .utils import data_placer, graph_placer, image_placer

# pylint: disable=too-many-locals


def create_ETF_report(tickers, filename, folder=None):
Expand All @@ -31,7 +36,7 @@ def create_ETF_report(tickers, filename, folder=None):
filename (string)
The name and location of the file you wish to save the data to.
folder (string, default is None)
If prefered, you can seperate filename and folder.
If preferred, you can separate filename and folder.
Output
----
Expand All @@ -40,7 +45,7 @@ def create_ETF_report(tickers, filename, folder=None):
workbook = Workbook()
stock_data = Ticker(tickers, asynchronous=True).history(period="10y")["adjclose"]
stock_data = stock_data.unstack(level=0)

stock_data.index = pd.to_datetime(stock_data.index)
stock_data.index = stock_data.index.tz_localize(None)

Expand Down Expand Up @@ -280,10 +285,8 @@ def create_ETF_report(tickers, filename, folder=None):
min_col += 1
max_col += 1

try:
if "Sheet" in workbook:
workbook.remove(workbook["Sheet"])
except KeyError:
pass

stock_sheet.sheet_state = "hidden"
workbook.save(filename)
12 changes: 8 additions & 4 deletions thepassiveinvestor/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Utilities Module"""

import io

import urllib3
from openpyxl.chart import Reference, LineChart
from openpyxl.chart import LineChart, Reference
from openpyxl.chart.axis import DateAxis
from openpyxl.drawing.image import Image as ExcelImage
from openpyxl.styles import Alignment, Font
from openpyxl.styles.numbers import FORMAT_PERCENTAGE_00

# pylint: disable=too-many-locals,broad-exception-caught


def data_placer(
data,
Expand Down Expand Up @@ -62,15 +66,15 @@ def data_placer(
for key, value in data.items():
if value_percentage:
try:
value = float(value[:-1]) / 100
value = float(value[:-1]) / 100 # noqa
sheet[
f"{column_value}{starting_row}"
].number_format = FORMAT_PERCENTAGE_00
except ValueError:
pass
if key_number:
try:
key = int(key)
try: # noqa
key = int(key) # noqa
except ValueError:
pass

Expand Down

0 comments on commit 6270fa9

Please sign in to comment.