I am learning how to analyze stock market. I followed this website:

https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1/

Here are problems as of April 10, 2018 that I encountered and how I resolved them.

 

1-reading data: the problem was that google and yahoo discontinued their services and pandas.io.data no more works and I received this error:

... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.

 After some searches I found the solution from https://github.com/rsvp/fecon235/issues/7:

The new code is

 

  1. import pandas_datareader.data   as web
  2. apple = web.DataReader(symbol, 'quandl', start_date, end_date)

Next challenge was the number of records that one can fetch which is limited. For this you can create a database and store data. The following code checks the local database and if the data for the period is not available, it fetches the data and puts in the database. 

  1. import sqlite3
  2. start_date = datetime.datetime(2017, 1, 1)
  3. end_date = datetime.datetime(2018, 1, 1)
  4. symbol = 'WIKI/AAPL'
  5. db = sqlite3.connect("database\stock.db")
  6. c = db.cursor()
  7. sssymb = symbol.split('/')[1]
  8. sql = "SELECT * FROM " + sssymb + " WHERE date BETWEEN '" + \
  9.       "{:%Y-%m-%d} 00:00:00".format(start_date) + "' AND '" + "{:%Y-%m-%d} 00:00:00".format(end_date) + "'"
  10. c.execute(sql)
  11. dataExist = len(c.fetchall())
  12. if dataExist < 2:
  13.     apple = web.DataReader(symbol, 'quandl', start_date, end_date)
  14.     query = ''' insert or replace into AAPL (date, Open,High,Low,Close,Volume,ExDividend,SplitRatio,AdjOpen,AdjHigh,AdjLow ,  AdjClose ,  AdjVolume ) values (?,?,?,?,?,?,?,?,?,?,?,?,?) '''
  15.     c.executemany(query, apple.to_records(index=True))
  16.     db.commit()

 

The next step is candlestickplots. A goodway is to install the plotpy offline and  then use the following code. However, you also can use the code from the aforementioned website that I modified and stored it in a file named candlestickplot.py. The latter requires installation of mpl_finance.

  1. import plotly.offline as offline
  2. trace = go.Candlestick(x=apple.index,open=apple.Open,high= apple.High, low=apple.Low,close=apple.Close)
  3. data=[trace]
  4. offline.plot(data,image="png")