5
$\begingroup$

I recently got flung into the world of quantum computing and I'm a beginner at coding. I was assigned to do the Portfolio Optimization tutorial of the Qiskit Finance Tutorials and input real data. Truth be told, I'm clueless. It's my understanding that I have to replace the "TICKER" and "RandomDataProvider" parts of the code in order to generate a real-life portfolio.

# Generate expected return and covariance matrix from (random) time-series
stocks = [("TICKER%s" % i) for i in range(num_assets)]
data = RandomDataProvider(tickers=stocks,
                 start=datetime.datetime(2016,1,1),
                 end=datetime.datetime(2016,1,30))
data.run()
mu = data.get_period_return_mean_vector()
sigma = data.get_period_return_covariance_matrix()

I've imported Quandl and WikipediaDataProvider. I want to keep the number of assets the same, using Microsoft "MSFT", Disney "DIS", Nike "NKE", and Home Depot "HD" stocks. How might I apply this financial from Quandl to the tutorial? I've tried this so far:

num_assets = 4

# Generate expected return and covariance matrix from (random) time-series
stocks = [("MSFT%s" , "DIS%s" , "NKE%s" , "HD%s" % i) for i in range(num_assets)]
data = WikipediaDataProvider(tickers=stocks,
                 token="xeesvko2fu6Bt9jg-B1T",
                 start=datetime.datetime(2016,1,1),
                 end=datetime.datetime(2016,1,30))
data.run()
mu = data.get_period_return_mean_vector()
sigma = data.get_period_return_covariance_matrix()

But get the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-19e4d9cde1e3> in <module>
      3 # Generate expected return and covariance matrix from (random) time-series
      4 stocks = [("MSFT%s" , "DIS%s" , "NKE%s" , "HD%s" % i) for i in range(num_assets)]
----> 5 data = WikipediaDataProvider(tickers=stocks,
      6                  token="xeesvko2fu6Bt9jg-B1T",
      7                  start=datetime.datetime(2016,1,1),

TypeError: Can't instantiate abstract class WikipediaDataProvider with abstract methods run

I apologize for my limited coding skills - I'm very new to all of this! Thank you in advance.

$\endgroup$
  • $\begingroup$ Welcome to the community @Lana! Could you also link the QISKit tutorial you're referencing? At first glance, this seems to be more of a Quandl/Wikipedia problem than Qiskit question $\endgroup$ – C. Kang Aug 12 at 17:01
  • $\begingroup$ Thank you @C.Kang! I used the link qiskit.org/documentation/tutorials/finance/… which was generated from github.com/Qiskit/qiskit-tutorials/blob/master/tutorials/… . $\endgroup$ – Lana Aug 12 at 18:21
  • $\begingroup$ Could you update the links? Those are broken $\endgroup$ – C. Kang Aug 12 at 19:35
  • $\begingroup$ I actually got it all amended. Thank you for your willingness to help! :) $\endgroup$ – Lana Aug 12 at 20:16
  • $\begingroup$ Great! Could you also post your solution? A2As help for future people with similar questions :D $\endgroup$ – C. Kang Aug 12 at 21:13
2
$\begingroup$

I changed the stock parameter to a list of strings and added the line stockmarket = StockMarket.NASDAQ as such:

num_assets = 4

# Generate expected return and covariance matrix from (random) time-series
stocks = ['MSFT', 'DIS', 'NKE', 'HD']
data = WikipediaDataProvider(
                             token="xeesvko2fu6Bt9jg-B1T",
                             tickers = stocks,
                             stockmarket = StockMarket.NASDAQ,
                             start=datetime.datetime(2016,1,1),
                             end=datetime.datetime(2016,1,30))
data.run()
mu = data.get_period_return_mean_vector()
sigma = data.get_period_return_covariance_matrix()
print(mu)
print(sigma)```
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.