0

Below is the python script i'm working on to pick out the stocks that meet certain price criteria (as written, tickerlist=[]collects tickers of the stocks whose max price and min price were >30 and <2 respectively.)

import matplotlib.pyplot as plt
import math
import csv
import pandas as pd
import datetime
import pandas.io.data as web
from filesortfunct import filesort
from scipy import stats
from scipy.stats.stats import pearsonr
import numpy as np
import math

dataname= 'NASDAQ.csv'    #csv file from which to extract stock tickers
df = pd.read_csv(dataname, sep=',')
df = df[['Symbol']]
df.to_csv(new+dataname, sep=',', index=False)
x=open(new+dataname,'rb')    #convert it into a form more managable
f = csv.reader(x) # csv is binary

Symbol = zip(*f) 

print type(Symbol)   #list format
Symbol=Symbol[0]   #pick out the first column
Symbol = Symbol[1:len(Symbol)]  #remove the first row "symbol" header
Symbol= Symbol[0:2]   #slicing to coose which stocks to look at

#decide the two dates between which to look at stock prices
start = datetime.datetime.strptime('2/10/2016', '%m/%d/%Y')
end = datetime.datetime.strptime('2/24/2016', '%m/%d/%Y')

#intended to collect indeces and min/max prices
tickerlist=[]
maxpricelist = []
minpricelist =[]


for item in Symbol:

    serious=web.DataReader([item], 'yahoo', start, end)['Adj Close']
    serious2=serious.loc[:, item].tolist()   #extract the column of 'Adj Close' 

    plt.figure()
    ap = plt.plot(serious2)
    indexmax, valuemax = max(enumerate(serious2))
    indexmin, valuemin = min(enumerate(serious2))

    if valuemax>30 and valuemin<2:
        tickerlist.append(item)
        maxpricelist.append(valuemax)
        minpricelist.append(valuemin)

plt.show()

The issue that i have right now is that some of the stocks on the list are discontinued? or YAHOO does not have their stock prices listed I suppose. So, when those stock tickers are included in the slicing, i get the following error message.

RemoteDataError: No data fetched using '_get_hist_yahoo'

Is there a way to bypass that?

Thanks in advance!

-------------Add------------------------ I added except RemoteDataError: as suggested but i get either invalid syntax or unexpected indentation..

for item in Symbol:
    print item

    serious=web.DataReader([item], 'yahoo', start, end)['Adj Close']
    except RemoteDataError:


    serious2=serious.loc[:, item].tolist()   #extract the column of 'Adj Close' 

    plt.figure()
    ap = plt.plot(serious2)
    indexmax, valuemax = max(enumerate(serious2))
    indexmin, valuemin = min(enumerate(serious2))

    if valuemax>30 and valuemin<100:
        tickerlist.append(item)
        maxpricelist.append(valuemax)
        minpricelist.append(valuemin)

plt.show()

print tickerlist
stratofortress
  • 453
  • 1
  • 9
  • 21

0 Answers0