Skip to content

Commit c80da1a

Browse files
authored
Merge pull request #22 from pierrelouisp/main
replace pandas datareader with yfinance
2 parents 7ad09c4 + c549f17 commit c80da1a

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dimod>=0.10.7
22
dwave-system>=1.10.0
33
pandas-datareader>=0.10.0
44
matplotlib>=3.3.4
5+
yfinance>=0.2.3

single_period.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from dimod import ConstrainedQuadraticModel, DiscreteQuadraticModel
2525
from dwave.system import LeapHybridDQMSampler, LeapHybridCQMSampler
2626

27+
import yfinance as yf
28+
2729

2830
class SinglePeriod:
2931
"""Define and solve a single-period portfolio optimization problem.
@@ -134,8 +136,8 @@ def load_data(self, file_path='', dates=None, df=None, num=0):
134136
self.stocks = random.sample(list(symbols_df.loc[:,'Symbol']), num)
135137

136138
# Read in daily data; resample to monthly
137-
panel_data = DataReader(self.stocks, 'yahoo',
138-
self.dates[0], self.dates[1])
139+
panel_data = yf.download(self.stocks,
140+
start=self.dates[0], end=self.dates[1])
139141
panel_data = panel_data.resample('BM').last()
140142
self.df_all = pd.DataFrame(index=panel_data.index,
141143
columns=self.stocks)
@@ -152,14 +154,13 @@ def load_data(self, file_path='', dates=None, df=None, num=0):
152154
self.stocks = list(self.df_all.columns)
153155

154156
# Read in baseline data; resample to monthly
155-
index_df = DataReader(self.baseline, 'yahoo',
156-
self.dates[0], self.dates[1])
157+
index_df = yf.download(self.baseline,
158+
start=self.dates[0], end=self.dates[1])
157159
index_df = index_df.resample('BM').last()
158-
self.df_baseline = pd.DataFrame(index=index_df.index,
159-
columns=self.baseline)
160-
160+
self.df_baseline = pd.DataFrame(index=index_df.index)
161+
161162
for i in self.baseline:
162-
self.df_baseline[i] = index_df[[('Adj Close', i)]]
163+
self.df_baseline[i] = index_df[[('Adj Close')]]
163164

164165
self.df = self.df_all
165166
else:

0 commit comments

Comments
 (0)