def stupid(): start = datetime(2017, 5, 24) end = datetime(2018, 5, 24) e = open("NYSE.txt") d = open("NASD.txt") b = open("shittystocks.txt",'w') c = open("volatility.txt", "w") a = [] for stock in e: a.append(stock) for stock in d: a.append(stock) for stock in a: try: df = get_historical_data(stock[:-1], start=start, end=end, output_format='pandas') except: b.write(stock) b.flush() print(stock[:-1] + "wroooooong") continue if len(df['close']) < 3: continue c.write(stock[:-1] + ": " + str(Stocks.findVolatility(df['close'])) + "\n") c.flush() print(stock[:-1])
def getStockData(self): # get StockPrice from startMonth-startDay-startYear to today's date now = datetime.now() month = now.month day = now.day year = now.year # get stock price startDate = datetime(self.startYear, self.startMonth, self.startDay) endDate = datetime(self.endYear, self.endMonth, self.endDay) data = get_historical_data(self.company, start=startDate, end=endDate, output_format='json') data = data[self.company] # dump json data into file print("\nStart saving data saved to file.") with open('stockData.json', 'w') as file: json.dump(data, file, indent=4) print("Data saved to file.") # store data into list, format: [year-month-day, open, high, low, close, volume] for date, dataSet in data.items(): newList = [date] for key, value in dataSet.items(): newList.append(value) self.dataList.append(newList)
def get_ticker_update(symb): # start = datetime(2013,11, 13) # end = datetime(2017, 5, 24) start = datetime.today() - timedelta(days=100) end = datetime.today() result = [] try: for symb1 in symb: df = get_historical_data(symb1, start=start, end=end, output_format='pandas') #df.reset_index(level=0,inplace=True) df1 = {} df1['data'] = df.to_dict('list') df1['t'] = df.index.tolist() df1['symbol'] = symb result.append(df1) io.savemat('result.mat', {'tsPY': result}) print('download sucessful') except: df1 = {} df1['data'] = [] df1['t'] = [] df1['symbol'] = symb print(symb + 'download failed')
def extract_stock_data(request): stocks = Stock.objects.all() end = date.today() count = 0 for stock in stocks: stock_id = stock.stock_id symbol = stock.symbol last_date_row = StockData.objects.filter( symbol=symbol).order_by('-date')[0] last_date = last_date_row.date start = last_date + datetime.timedelta(days=1) if (start < end): df = get_historical_data(symbol, start=start, end=end, output_format='pandas') template = loader.get_template('stocks/index.html') df.columns = [ 'open_price', 'high_price', 'low_price', 'close_price', 'volume' ] df['stock_id'] = stock_id df['symbol'] = symbol df['date'] = df.index StockData.objects.bulk_create( StockData(**vals) for vals in df.to_dict('records')) count += 1 if count > 0: return HttpResponse("Done Extracting Data") else: return HttpResponse( "You have the most recent data, no data to be extracted")
def test_batch_historical_json(self): f = get_historical_data(["AAPL", "TSLA"], self.good_start, self.good_end, output_format="json") assert isinstance(f, dict) assert len(f) == 2 assert sorted(list(f)) == ["AAPL", "TSLA"] a = f["AAPL"] t = f["TSLA"] assert len(a) == 73 assert len(t) == 73 expected1 = a["2017-02-09"] assert expected1["close"] == pytest.approx(132.42, 3) assert expected1["high"] == pytest.approx(132.445, 3) expected2 = a["2017-05-24"] assert expected2["close"] == pytest.approx(153.34, 3) assert expected2["high"] == pytest.approx(154.17, 3) expected1 = t["2017-02-09"] assert expected1["close"] == pytest.approx(269.20, 3) assert expected1["high"] == pytest.approx(271.18, 3) expected2 = t["2017-05-24"] assert expected2["close"] == pytest.approx(310.22, 3) assert expected2["high"] == pytest.approx(311.0, 3)
def test_batch_historical_pandas(self): f = get_historical_data(["AMZN", "TSLA"], self.good_start, self.good_end, output_format="pandas") assert isinstance(f, dict) assert len(f) == 2 assert sorted(list(f)) == ["AMZN", "TSLA"] a = f["AMZN"] t = f["TSLA"] assert len(a) == 73 assert len(t) == 73 expected1 = a.loc["2017-02-09"] assert expected1["close"] == pytest.approx(821.36, 3) assert expected1["high"] == pytest.approx(825.0, 3) expected2 = a.loc["2017-05-24"] assert expected2["close"] == pytest.approx(980.35, 3) assert expected2["high"] == pytest.approx(981.0, 3) expected1 = t.loc["2017-02-09"] assert expected1["close"] == pytest.approx(269.20, 3) assert expected1["high"] == pytest.approx(271.18, 3) expected2 = t.loc["2017-05-24"] assert expected2["close"] == pytest.approx(310.22, 3) assert expected2["high"] == pytest.approx(311.0, 3)
def test_batch_historical_pandas(self): f = get_historical_data(["AAPL", "TSLA"], self.good_start, self.good_end, output_format="pandas") assert isinstance(f, dict) assert len(f) == 2 assert sorted(list(f)) == ["AAPL", "TSLA"] a = f["AAPL"] t = f["TSLA"] assert len(a) == 73 assert len(t) == 73 expected1 = a.loc["2017-02-09"] assert expected1["close"] == 132.42 assert expected1["high"] == 132.445 expected2 = a.loc["2017-05-24"] assert expected2["close"] == 153.34 assert expected2["high"] == 154.17 expected1 = t.loc["2017-02-09"] assert expected1["close"] == 269.20 assert expected1["high"] == 271.18 expected2 = t.loc["2017-05-24"] assert expected2["close"] == 310.22 assert expected2["high"] == 311.0
def get_list_data(ticker_list): data = pd.DataFrame() repeat = 0 error_list = [] while len(ticker_list) > 0 and repeat <= 5: num_companies = len(ticker_list) for i in range(num_companies): ticker = ticker_list[i] try: temp = get_historical_data(ticker, start=start_date, end=last_date, output_format='pandas') temp['ticker'] = ticker data = pd.concat([data, temp]) # data['ticker'] = ticker print('{0} done! {1} done, {2} error, {3} left!'.format( ticker, i + 1 - len(error_list), len(error_list), num_companies - i - 1)) except: error_list.append(ticker) print('{0} Error! {1} done, {2} error, {3} left!'.format( ticker, i + 1 - len(error_list), len(error_list), num_companies - i - 1)) # time.sleep(1) if len(ticker_list) == len(error_list): repeat += 1 ticker_list = error_list error_list = [] print('Restart!') print('Done') return data
def get_price_history(ticker): """ Loads the price history of a stock for the last 252 days. Note, 252 days is a limit of iexfinance function. :param ticker: the stock ticker symbol :type: String :return: list of price objects :rtype: list """ today = datetime.date.today() start = today - datetime.timedelta(days=365) data = iexfinance.get_historical_data(ticker, start=start, end=today, output_format="json") quotes = data.get(ticker) history = [] for date in quotes.keys(): quote = quotes.get(date) history.append({ "date": date, "open": quote.get("open"), "close": quote.get("close"), "high": quote.get("high"), "low": quote.get("low") }) return history
def run(self, dispatcher, tracker, domain): company_x = tracker.get_slot('company') start_time = tracker.get_slot('start_time') end_time = tracker.get_slot('end_time') pattern = re.compile(r'\.|-|/') start_x = re.split(pattern, start_time) end_x = re.split(pattern, end_time) start = datetime(int(start_x[0]), int(start_x[1]), int(start_x[2])) end = datetime(int(end_x[0]), int(end_x[1]), int(end_x[2])) symbols = get_available_symbols() for i in range(len(symbols)): if company_x.lower() in symbols[i]["name"].lower(): company = symbols[i]["symbol"] f = get_historical_data(company, start, end, output_format='pandas') plt.plot(f["close"]) plt.title('Time series chart for {}'.format(company)) fig1 = plt.gcf() plt.draw() fig1.savefig("1.jpg", dpi=100) dispatcher.utter_template("utter_plot", tracker) plt.show() return [ SlotSet('company', company), SlotSet('start_time', start), SlotSet('end_time', end) ]
def dataframe( company ): #defining a function with parameter company(for company value to get data) & title(for mentioning on title) start = entry_field1.get() start = datetime.strptime(start, '%Y-%m-%d') end = entry_field2.get() end = datetime.strptime(end, '%Y-%m-%d') df = get_historical_data( company, start, end, output_format='pandas') #getting data from iexfinance library to df c = df[ 'open'] #assigning opening stocks prices to varibale c likewise for b,d,a b = df['high'] d = df['low'] a = df['close'] sns.set( ) #showing graph of values passed from the func dataframe from seaborn (hereafter we will be using many func from seaborn library) sns.set_style("darkgrid") sns.set(rc={'figure.figsize': (14.7, 8.27)}) #setting size of graph ax = plt.subplot(111) #plotting subplot for mentioning legends c.plot( label='Opening Price', color='blue') #plotting graph from matplotlib library likewise b,d,a b.plot(label='Highest Price', color='g') d.plot(label='Lowest Price', color='r') a.plot(label='Closing Price', color='c') plt.title( 'Stock Prices of :' + str(company) ) #plotting title name and value(argument) is getting from title parameter what we defined in func plt.xlabel(entry_field1.get() + " to " + entry_field2.get()) #setting labels x and y names plt.ylabel('Price in USD(US Dollars)') ax.legend() #calling legend to mention in graph plt.show()
def get_stock_data(stock_name="AAPL", normalized=0): df = get_historical_data(stock_name, start="2015-10-10", end="2017-10-10", output_format='pandas') # pd.DataFrame(stocks) # print(df) df.drop(df.columns[[2, 4]], axis=1, inplace=True) return df
def price_graph(start_date, end_date, ticker, price_type="close"): # Import stock data in the desired date range df = get_historical_data(ticker, start=start_date, end=end_date, output_format='pandas') df.index.name = 'date' df.reset_index(inplace=True) plt.plot(df['date'], df[price_type])
def get_history(sym="SPY", days=10): end = datetime.now() start = datetime.now() - timedelta(days=days) data = get_historical_data(sym, start=start, end=end, output_format='pandas') return data
def get_data(ticker, start_date='2016-01-01', end_date='2017-01-01'): try: df = quandl.get('WIKI/' + ticker, start_date=start_date, end_date=end_date, api_key=get_quantinsti_api_key()) df['Source'] = 'Quandl Wiki' return d[['Open', 'High', 'Low', 'Close', 'Volume', 'Source']] except AuthenticationError as a: print(a) print( "Please replace the line no. 13 in quantrautil.py file with your Quandl API Key" ) except: try: df = quandl.get('NSE/' + ticker, start_date=start_date, end_date=end_date, api_key=get_quantinsti_api_key()) df['Source'] = 'Quandl NSE' return df[['Open', 'High', 'Low', 'Close', 'Volume', 'Source']] except: try: start_date = pd.to_datetime(start_date) end_date = pd.to_datetime(end_date) df = iex.get_historical_data(ticker, start=start_date, end=end_date, output_format='pandas') df.index.name = 'Date' df = df.rename( columns={ 'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close', 'volume': 'Volume', }) df['Source'] = 'IEX' return df[['Open', 'High', 'Low', 'Close', 'Volume', 'Source']] except: try: df = nsepy.get_history(symbol=ticker, start=start_date, end=end_date) df['Source'] = 'nsepy' return df[[ 'Open', 'High', 'Low', 'Close', 'Volume', 'Source' ]] except: try: df = yf.download(ticker, start_date, end_date) df['Source'] = 'Yahoo' return df[[ 'Open', 'High', 'Low', 'Close', 'Volume', 'Source' ]] except: print(traceback.print_exc())
def get_ticker_update(symbs): # start = datetime(2013,11, 13) # end = datetime(2017, 5, 24) start = datetime.today() - timedelta(days=100) end=datetime.today() result=[] n=0 k=0 failed=[] t0=time() CRED = '\033[91m' CEND = '\033[0m' for symb in symbs: try: df = get_historical_data(symb, start=start, end=end, output_format='pandas') #df.reset_index(level=0,inplace=True) df1={} df1['data']=df.to_dict('list') df1['t']=df.index.tolist() df1['symbol']=symb result.append(df1) n=n+1 print('TK'+str(n) +' :: '+symb+' loaded sucessfully') except: df1={} df1['data']=[] df1['t']=[] df1['symbol']=symb failed.append(symb) n=n+1 k=k+1 print(CRED +'TK'+str(n) +' :: '+symb + ' download failed','red'+CEND) continue io.savemat('result.mat',{'tsPY':result}) print('All done Total time '+str(int(time()-t0))+' Seconds') print('Total ticker tried: '+str(n) ) print('Sucessfully loaded: '+str(n-k) ) print('Failed loaded: '+str(k) ) print(*failed,sep=',') # else: # print(symb + 'download sucessfully') # df1=df.to_dict('list') # scipy.io.savemat('test.mat',{'struct':df.to_dict('list')}) # df2={} # df2['Symbol']='MMM' # df2['data']=df1 # # # # df3={} # df3['Symbol']='MMM' # df3['data']=df1 # df4=[df2,df3] # scipy.io.savemat('test.mat',{'ts':df4})
def getData(ticker): start = datetime(2013, 2, 9) end = datetime(2017, 5, 9) stock_data = get_historical_data(ticker, start=start, end=end, output_format='pandas') print(stock_data.describe) return stock_data
def sma_graph(N, start_date, end_date, ticker, price_type="close"): df = get_historical_data(ticker, start=start_date, end=end_date, output_format='pandas') df.index.name = 'date' df.reset_index(inplace=True) df['SMA'] = df['date'].apply(lambda x: N_day_sma(N, df, datetime.strptime(str(x), "%Y-%m-%d"), start_date, price_type)) plt.plot(df['date'], df['SMA'])
def fetch_benchmark_returns(start_date: datetime, end_date: datetime) -> pd.DataFrame: benchmark = 'SPY' benchmark_price = \ get_historical_data(benchmark, start=start_date, end=end_date, output_format='pandas')['close'] benchmark_price.index = pd.to_datetime(benchmark_price.index, utc=True) benchmark_price.name = benchmark benchmark_rets = benchmark_price.pct_change(1) return benchmark_rets
def getData(self): start = dt.datetime(2013, 2, 9) end = dt.datetime(2017, 5, 9) self.stock_data = get_historical_data(self.ticker, start=start, end=end, output_format='pandas') self.stock_data.to_csv(self.fname) print("Saved to", self.fname) return self.stock_data
def run_iex(): start_time = datetime.now() - timedelta(days=365*5) for n in Market.select(): try: path = join(STORAGE_PATH, '{}.p'.format(n.symbol)) data = get_historical_data(n.symbol, start=start_time, output_format='pandas') data.rename(columns={ 'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close', 'volume': 'Volume' }, inplace=True) data.to_pickle(path) print(colored.green(n.symbol)) except Exception as err: print(colored.red(err))
def rsi_graph(period, start_date, end_date, sell_bound, buy_bound, ticker): df = get_historical_data(ticker, start=start_date, end=end_date, output_format='pandas') df.index.name = 'date' df.reset_index(inplace=True) df['RSI'] = df['date'].apply(lambda x: relative_strength_index(period, df, datetime.strptime(str(x), "%Y-%m-%d"), start_date)) plt.plot(df['date'], df['RSI']) plt.axhline(y = sell_bound) plt.axhline(y = buy_bound)
def collect_data_from_iex(start, end, *symbols): symbols = list(map(str.upper, symbols)) for s in symbols: try: time.sleep(2) df = get_historical_data(s, start, end, 'pandas') df.to_csv('./iexdata/{}'.format(s)) except: print('*** Error in the symbol {}: {}'.format( s, sys.exc_info()[0]))
def getIndexInformationOnDate(self, index, date): # Make sure date is an instance of datetime if not isinstance(date, datetime): date = datetime.strptime(date, '%d %b %Y') # Get the historical data on this day d = get_historical_data(index, start=date, end=date, output_format='pandas') print(d) return d
def test_single_historical_json(self): f = get_historical_data("AMZN", self.good_start, self.good_end) assert isinstance(f, dict) assert len(f["AMZN"]) == 73 expected1 = f["AMZN"]["2017-02-09"] assert expected1["close"] == pytest.approx(821.36, 3) assert expected1["high"] == pytest.approx(825.0, 3) expected2 = f["AMZN"]["2017-05-24"] assert expected2["close"] == pytest.approx(980.35, 3) assert expected2["high"] == pytest.approx(981.0, 3)
def getStockHistoricalData(stock, start_date, end_date): ''' The function getStockHistoricalData takes in following parameters stock - Name of the company start_date - The start date of the historical data end_date - The end date of the historical data ''' df = get_historical_data(stock, start=start_date, end=end_date, output_format='pandas') df.to_csv('out.csv', sep='\t', encoding='utf-8')
def test_single_historical_json(self): f = get_historical_data("AAPL", self.good_start, self.good_end) assert isinstance(f, dict) assert len(f["AAPL"]) == 73 expected1 = f["AAPL"]["2017-02-09"] assert expected1["close"] == 132.42 assert expected1["high"] == 132.445 expected2 = f["AAPL"]["2017-05-24"] assert expected2["close"] == 153.34 assert expected2["high"] == 154.17
def HistoricalData(ticker): try: historical = get_historical_data('%s' % (ticker), start=start, end=end, output_format='pandas') historical['100ma'] = historical['close'].rolling(window=100).mean() historical[['close', 'open', '100ma']].plot() plt.xlabel('Date') plt.ylabel('$') plt.show() except: print('That ticker is not valid, choose another one!')
def __init__(self, ticker): self.ticker = ticker self.price = None self.prices = None from iexfinance import get_historical_data from datetime import datetime start = datetime(2014, 1, 1) end = datetime(2018, 1, 1) df = get_historical_data(self.ticker, start=start, end=end, output_format='pandas') self.prices = df['open'] print("Opening: ", self.prices[0]) print("Closing: ", self.prices[-1]) print("Days: ", len(self.prices)) self.counter = 0
def getHistoricalData(ticker, daterange): ''' Will download the historical data for the given ticker and return it as a pandas dataframe. Daterange must be a 2 element list, in the following format: [[<start date>], [<end date>]], date format = int YYYYMMDD ''' startYear, startMonth, startDay = utils.convertDate(daterange[0]) endYear, endMonth, endDay = utils.convertDate(daterange[1]) start = datetime(startYear, startMonth, startDay) end = datetime(endYear, endMonth, endDay) dataframe = iex.get_historical_data(ticker, start=start, end=end, output_format='pandas') dataframe.head() return dataframe
def get_ma(ticker): stock = Stock(ticker) start = now - timedelta(days=25) try: keyStats = stock.get_key_stats() if (stock.get_price() < 5): return -1,-1,-1 df_hist = get_historical_data(ticker, start=start, end=now, output_format='pandas') dma50 = keyStats['day50MovingAvg'] dma10 = df_hist.tail(10)['close'].mean() dma20 = df_hist.tail(20)['close'].mean() except: return -1,-1,-1 return dma10, dma20, dma50
Date: 8/17/2018 Description: Creates a dataframe with moving averages and MACD oscillator ''' import matplotlib.pyplot as plt import numpy as np import pandas as pd from datetime import datetime, timedelta from iexfinance import get_historical_data moving_avg1 = 10 moving_avg2 = 20 ticker = "BABA" now = datetime.now() start = now - timedelta(days=90) df = get_historical_data(ticker, start=start, end=now, output_format='pandas') def macd(dat): dat['10dma'] = dat['close'].rolling(window=moving_avg1, min_periods=1).mean() dat['20dma'] = dat['close'].rolling(window=moving_avg2, min_periods=1).mean() return dat def add_macd(df): df = macd(df) df['position'] = 0 df['position'][moving_avg1:] = np.where(df['10dma'][moving_avg1:] >= df['20dma'][moving_avg1:], 1, 0)