def validate_ticker(ticker): """Function that validates ticker from yahoo_historical api""" now = arrow.now() now = get_date_array_for_fetcher(now) try: fetcher = Fetcher(ticker, now, now) fetcher.getHistorical() except KeyError: return False return True
def getHistory(stockCode): stockCode = str(stockCode).zfill(4) os.makedirs(f'C:\\Users\\Fung\\Downloads\\Financial Reports {stockCode} {compNameDict[stockCode]}', exist_ok=True) dst = f'C:\\Users\\Fung\\Downloads\\Financial Reports {stockCode} {compNameDict[stockCode]}\\historical price {stockCode}.xlsx' #result destination now = datetime.datetime.now() year= int(now.year) month = int(now.month) date = int(now.day) stockData = Fetcher(stockCode+'.hk', [1990,4,1], [year,month,date]) df = stockData.getHistorical() df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d') #change date column from string to date df.Date=df.Date.dt.strftime('%Y') #change date format to Year for pivot table pt= pd.pivot_table(df, values='Close', index=['Date'], #Get Max and low stock price group by each year aggfunc={'Close':[min, max]}) sortedPT = pt.sort_values('Date',axis=0,ascending=False) writer = ExcelWriter(dst) sortedPT.T.to_excel(writer,'Sheet1') writer.save() #reformat result to desire format wb = openpyxl.load_workbook(dst) ws = wb.active lastCol = ws.max_column for i in range(lastCol,2,-1): ws.insert_cols(i,2) wb.save(dst) os.startfile(f'C:\\Users\\Fung\\Downloads\\Financial Reports {stockCode} {compNameDict[stockCode]}') #open report folder
def Y3_range(loop_holdings_range): data_3ylow = Fetcher(loop_holdings_range, three_year_ago, now_year) df_3y_range = data_3ylow.getHistorical() year3_low = 100000 year3_high = 0 for index, row in df_3y_range.iterrows(): if row['Open'] < year3_low: year3_low = row['Open'] if row['High'] < year3_low: year3_low = row['High'] if row['Low'] < year3_low: year3_low = row['Low'] if row['Close'] < year3_low: year3_low = row['Close'] for index, row in df_3y_range.iterrows(): if row['Open'] > year3_high: year3_high = row['Open'] if row['High'] > year3_high: year3_high = row['High'] if row['Low'] > year3_high: year3_high = row['Low'] if row['Close'] > year3_high: year3_high = row['Close'] return(year3_low, year3_high)
def yahoo_finance(stok, m=0, y=0): 'Gets the data from Yahoo finance' today = date.today() df = pd.DataFrame(data={}) dif = pd.DataFrame(data={}) 'Fix for months' if y == 0 and m > 0: y = 0 while m > today.month: y += 1 m2 = today.month - m m = 12 * y + m2 else: m = today.month 'Gets the data from yahoo finance' try: stock_prices = Fetcher(stok, [today.year - y, m, today.day], [today.year, today.month, today.day]) dif = stock_prices.getHistorical() df[stok] = dif['Adj Close'].to_list() if len(dif) < 55: print('We need more data for: ' + stock_ticker) except: print( 'Oops! that was no valid ticker. Please delete or correct it from the Stock List file: ', stok) 'Puts the date in a column' df['Date'] = dif['Date'].to_list() return df
def post(): K.clear_session() namasaham = request.form['namasaham'] model = joblib.load('model' + namasaham) #split tanggal,bulan,tahun date = request.form['tanggal'] date = datetime.datetime.strptime(date, '%Y-%m-%d') tanggal = date.strftime('%d') if tanggal[0] == str(0): tanggal1 = int(tanggal[1]) else: tanggal1 = int(tanggal) print(tanggal1) bulan = date.strftime('%m') if bulan[0] == str(0): bulan1 = int(bulan[1]) else: bulan1 = int(bulan) tahun = date.strftime('%Y') tahun1 = int(tahun) tanggal_predict = str(tahun + '-' + bulan + '-' + tanggal) #import data stock price real_saham = Fetcher(namasaham + ".JK", [2018, 1, 1], [tahun1, bulan1, tanggal1], interval="1d") real_saham = real_saham.getHistorical() real_saham = real_saham.iloc[:, 0:2] real_saham = real_saham.dropna() #set index and drop data in predict_date real_saham = real_saham.set_index("Date") if tanggal_predict in real_saham.index.values: real_saham = real_saham.drop(tanggal_predict, axis=0) real_saham = real_saham.tail(10) #transform from sklearn.preprocessing import MinMaxScaler sc = MinMaxScaler() saham = sc.fit_transform(real_saham) window = 3 predictX = dataset_predict.createDataset(saham, window) predictX = predictX.reshape(len(saham) - window + 1, window, 1) predictY = model.predict(predictX) #denormalisasi predictY = sc.inverse_transform(predictY) predict_next_day = "Rp. {}".format(int(predictY[-1][0])) price_previous_day = "Rp. {}".format(int(real_saham['Open'][-1])) previous_date = real_saham.index[-1] return render_template('predict.html', predict=predict_next_day, price_previous=price_previous_day, tanggal_predict=tanggal_predict, previous_date=previous_date, namasaham=namasaham)
def histdata(): a = datetime.datetime.now() a = a.strftime('%Y-%m-%d') namasaham = request.form['namasaham'] tgl1 = request.form['tanggal1'] tgl2 = request.form['tanggal2'] def convert_date(date): date = datetime.datetime.strptime(date, '%Y-%m-%d') tanggal = date.strftime('%d') if tanggal[0] == str(0): tanggal1 = int(tanggal[1]) else: tanggal1 = int(tanggal) bulan = date.strftime('%m') if bulan[0] == str(0): bulan1 = int(bulan[1]) else: bulan1 = int(bulan) tahun = date.strftime('%Y') tahun1 = int(tahun) tanggal_lengkap = str(tahun + '-' + bulan + '-' + tanggal) return tanggal1, bulan1, tahun1, tanggal_lengkap tgl1_1, bulan1_1, tahun1_1, tgl_lengkap1 = convert_date(tgl1) tgl2_1, bulan2_1, tahun2_1, tgl_lengkap2 = convert_date(tgl2) #import data stock price real_saham = Fetcher(namasaham + ".JK", [tahun1_1, bulan1_1, tgl1_1], [tahun2_1, bulan2_1, tgl2_1], interval="1d") real_saham = real_saham.getHistorical() real_saham = real_saham.dropna() #plot data plt.plot(real_saham['Date'], real_saham['Open'], 'blue') plt.title('Plot Stock Price of {}'.format(namasaham)) plt.xlabel('Date') plt.ylabel('Price (Rp)') plt.xticks(real_saham['Date'], rotation=90) plt.tight_layout() #filename b = datetime.datetime.now() namafile = (str(b).split('.'))[-1] addressplot = './storage/{}.png'.format(namafile) urlplot = '/fileupload/{}.png'.format(namafile) plt.savefig(addressplot) plot = urlplot plt.close() return render_template('histdata.html', tables=real_saham.to_html(), a=a, tgl1=tgl_lengkap1, tgl2=tgl_lengkap2, namasaham=namasaham, plot=plot)
def get_asx(self, tick, start_date, end_date): ''' get prices for date range ''' tick_code = '%s.ax' % tick ticker = Fetcher(tick_code, start_date, end_date) time_series = ticker.getHistorical() time_series['Tick'] = tick time_series.rename(columns=lambda x: x.strip(), inplace=True) return time_series
def today_open(loop_holdings_today): data_today = Fetcher(loop_holdings_today, now_year) df_today = data_today.getHistorical() x = 0 for index, row in df_today.iterrows(): if row['Open'] > x: x = row['Open'] return(x)
def __init__(self, ticker): from yahoo_historical import Fetcher import datetime now = datetime.datetime.now() raw = Fetcher(ticker, [2018,1,1], [now.year,now.month,now.day]) self.data = raw.getHistorical() self.name = ticker self.features = [] #The data -- raw and unpruned. Needed for passing data into functions self.r_opens = self.data['Open'] self.r_highs = self.data['High'] self.r_lows = self.data['Low'] self.r_closes = self.data['Close'] self.r_adj_close = self.data['Adj Close'] self.r_volumes = self.data['Volume'] self.m_opens = self.data['Open'][2:-1] self.m_highs = self.data['High'][2:-1] self.m_lows = self.data['Low'][2:-1] self.m_closes = self.data['Close'][2:-1] self.m_adj_close = self.data['Adj Close'][2:-1] self.m_volumes = self.data['Volume'][2:-1] self.pc_open_close = self.percentChange(self.r_opens, self.r_closes) self.pc_high_low = self.percentChange(self.r_lows, self.r_highs) self.rv_opens = self.relVelocity(self.r_opens) self.rv_highs = self.relVelocity(self.r_highs) self.rv_lows = self.relVelocity(self.r_lows) self.rv_closes = self.relVelocity(self.r_closes) self.rv_adj_closes = self.relVelocity(self.r_adj_close) self.rv_volumes = self.relVelocity(self.r_volumes) self.rp_opens = self.relPos(self.r_opens) self.rp_highs = self.relPos(self.r_highs) self.rp_lows = self.relPos(self.r_lows) self.rp_closes = self.relPos(self.r_closes) self.rp_adj_closes = self.relPos(self.r_adj_close) self.rp_volumes = self.relPos(self.r_volumes) self.features.append(self.m_adj_close) self.features.append(self.m_closes) self.features.append(self.m_highs) self.features.append(self.m_lows) self.features.append(self.m_opens) self.features.append(self.m_volumes) self.y = self.percentChangeY(self.r_closes, self.r_closes)#r_opens
def get_daily_returns(symbol, from_date, to_date): # from and to date format in list [yyyy,mm,dd] data = Fetcher(symbol, from_date, to_date) close = data.getHistorical()["Close"].tolist() returns = [None] for close2, close1 in zip(close[1:], close): returns.append(close2 / close1 - 1) return returns
def fill_quote_history(stock): """ Given a stock, it fills in the last 10 years of historical data """ now = arrow.now() ten_back = now.replace(years=-10) now = get_date_array_for_fetcher(now) ten_back = get_date_array_for_fetcher(ten_back) fetcher = Fetcher(stock.ticker, ten_back, now) history = fetcher.getHistorical() save_stock_quote_from_fetcher(history, stock.id)
def get_ticker_update(symbs, ndays): # start = datetime(2013,11, 13) # end = datetime(2017, 5, 24) start = datetime.today() - timedelta(days=ndays) start1 = [ int(start.strftime('%Y')), int(start.strftime('%m')), int(start.strftime('%d')) ] end = datetime.today() end1 = [ int(end.strftime('%Y')), int(end.strftime('%m')), int(end.strftime('%d')) ] result = {} n = 0 k = 0 failed = [] t0 = time() CRED = '\033[91m' CEND = '\033[0m' for symb in symbs: try: data = Fetcher(symb, start1, end1) df = data.getHistorical() #df.reset_index(level=0,inplace=True) # df1={} # df['Adj_Close']=df['Adj Close'] # del df['Adj Close'] # df1['data']=df.to_dict('list') # df1['t']=df1['data']['Date'] # df1['symbol']=symb result.update({symb: df}) n = n + 1 print('TK' + str(n) + ' :: ' + symb + ' loaded sucessfully') except: failed.append(symb) n = n + 1 k = k + 1 print(CRED + 'TK' + str(n) + ' :: ' + symb + ' download failed', 'red' + CEND) continue # sio.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=',') return (result) #ts=get_ticker_update(['C','A'],100)
def data_fetcher(etflist, start, end): """ Takes in a list of etf symbols returns pandas dataframe of price details and dividends """ price_df = pd.DataFrame() errors = [] for e in etflist: try: data = Fetcher(e, start, end) pricedata = data.getHistorical() if 'Date' in data.getHistorical().columns.tolist(): pricedata['ticker'] = e price_df = price_df.append(pricedata) except KeyError: errors.append(e) else: pass return price_df, errors
def find_price(SYB): now = datetime.datetime.now() time_now = [now.year, now.month, now.day] data = Fetcher(SYB, time_now) data_get = data.getHistorical() #print (data_get) open = data_get['Open'] open_price = float(open[0]) #print (SYB, open_price) return open_price
def fill(): """Function that fills stock data for missing days""" stock_id_field = 'stock_id' stock_ticker = 'stock__ticker' date = 'date' now = arrow.now() data = DailyStockQuote.objects.values( stock_ticker, stock_id_field).annotate(date=Max(date)) for stock in data: last_date = arrow.get(stock[date]).replace(days=+1) last_date = get_date_array_for_fetcher(last_date) now_fetcher = get_date_array_for_fetcher(now) ticker = stock[stock_ticker] stock_id = stock[stock_id_field] fetcher = Fetcher(ticker, last_date, now_fetcher) history = fetcher.getHistorical() save_stock_quote_from_fetcher(history, stock_id)
def run_Fetcher(stock_name, start, end, cycle): AAPL = Fetcher(stock_name, start , end , cycle) date_close = AAPL.getHistorical() #print(date_close) close = date_close.ix[:,[1,2,3]] #print(close) ar = np.empty for i in range(len(close)): typical = (close.ix[i][0] + close.ix[i][1] + close.ix[i][2]) / 3.0 ar = np.append(ar, typical) ar = ar[1:] return ar
def loadCsv(symbols: list, output_dir: str, start_date=[2015, 5, 24], end_date=[2020, 10, 9]): import time for symbol in symbols: print("generate csv to symbol ", symbol, "...") time.sleep(2) data = Fetcher(symbol, start_date, end_date) try: with open(output_dir + '/' + symbol + '.csv', 'w') as csvfile: dataf = pd.DataFrame(data.getHistorical()) if dataf.empty: raise Exception('dataframe is empty') dataf.to_csv(path_or_buf=csvfile, mode='w') print("success generate csv to symbol ", symbol) except: print("FAILD!!!! generate csv to symbol ", symbol)
def yahoo_api(tkr, reset): poor_data = False if not reset: return pd.read_csv('stocks.csv'), poor_data data = Fetcher(tkr, [2000, 1, 1]) df = data.getHistorical() df = df.drop(['Close'], axis=1) df = df.rename(columns={ 'High': 'Max', 'Low': 'Min', 'Adj Close': 'Close', 'Volume': 'Vol' }) df.iloc[:] = df.iloc[::-1].values df.iloc[:, 0] = list(map(lambda x: x.replace('-', '.'), df.iloc[:, 0])) df['Date'] = df['Date'].map(lambda x: '.'.join(x.split('.')[::-1])) df['last_close'] = df['Close'].shift(-1) df['Var'] = list( map(lambda x, y: (x / y) - 1, df['Close'], df['last_close'])) df[['Open', 'Max', 'Min', 'Close', 'Vol']] = df[['Open', 'Max', 'Min', 'Close', 'Vol']].applymap(lambda x: x if x != 0 else None) if df.isnull().sum().max() / len(df.index) > 0.3 or len(df.index) < 50: poor_data = True df[['Open', 'Max', 'Min', 'Close', 'Vol']] = df[['Open', 'Max', 'Min', 'Close', 'Vol']].fillna(method='bfill') df['last_close'] = df['Close'].shift(-1) df['Var'] = list( map(lambda x, y: (x / y) - 1, df['Close'], df['last_close'])) df = df[['Date', 'Open', 'Close', 'Min', 'Max', 'Vol', 'Var']] #if df[['Vol']].sum(axis=0)[0] == 0: df = df.drop(columns=['Vol']) df = df.dropna() df.to_csv('stocks.csv') return df, poor_data
def fill_stocks(request): """Function that fills stock data for missing days""" if request.method == "GET": stock_id_field = 'stock_id' stock_ticker = 'stock__ticker' date = 'date' data = DailyStockQuote.objects.values( stock_ticker, stock_id_field).annotate(date=Max(date)) for stock in data: last_date = arrow.get(stock[date]).replace(days=+1) last_date = get_date_array_for_fetcher(last_date) now = get_date_array_for_fetcher(arrow.now()) ticker = stock[stock_ticker] stock_id = stock[stock_id_field] fetcher = Fetcher(ticker, last_date, now) history = fetcher.getHistorical() save_stock_quote_from_fetcher(history, stock_id) msg = "Filling data for {0} stocks".format(len(data)) return HttpResponse(msg, status=200) return HttpResponse("405 Method Not Allowed", status=405)
def getDataSet(stockTicker): startYear = 1980 currentDate = [ int(strNum) for strNum in datetime.now().strftime("%Y-%m-%d").split('-') ] while (startYear < int(datetime.now().year)): try: data = Fetcher(stockTicker, [startYear, 1, 1], currentDate) break except ValueError: startYear += 5 parsedData = list() dataFrame = data.getHistorical() for _, row in dataFrame.iterrows(): try: parsedData.append([ parseTime(row['Date']), round(float(row['Close']) + 0.005, 2) ]) except ValueError: continue return parsedData
from yahoo_historical import Fetcher data = Fetcher("AAPL", [2007, 1, 1], [2017, 1, 1]) df = data.getHistorical() print(df)
tickers = pd.read_csv(ticker_file)["Symbol"].values #DEBUG tickers = ["ALR", "ECOM ", "MITT^A"] tickers = [t.strip() for t in tickers] # Keep track of the saved tickers and unsaved tickers saved_tickers = [] failed_tickers = [] for ticker in tickers: # output file name output_file = os.path.join(sub_output_dir, ticker + ".csv") try: data = Fetcher(ticker, START, END) if data.getHistorical().empty: failed_tickers.append(ticker) else: data.getHistorical().to_csv(output_file, index=False) saved_tickers.append(ticker) except: failed_tickers.append(ticker) # If there are tickers failed to download, # report the name list of tickers. if failed_tickers: saved_tickers_output = os.path.join(sub_output_dir, "saved_tickers.txt") failed_tickers_output = os.path.join(sub_output_dir, "failed_tickers.txt") # Write successful ticker list
# load model with joblib import numpy as np import pandas as pd namasaham="bbri" namasaham=namasaham.upper() import joblib model=joblib.load('model{}'.format(namasaham)) #dataset from yahoo finance from yahoo_historical import Fetcher real_saham= Fetcher(namasaham+".JK", [2019,6,18], [2019,7,18], interval="1d") real_saham=real_saham.getHistorical() date_and_open=real_saham.iloc[:,0:2] real_saham=real_saham.iloc[:,1:2] real_saham=real_saham.dropna() print(real_saham) from sklearn.preprocessing import MinMaxScaler sc=MinMaxScaler() saham=sc.fit_transform(real_saham) print(saham) def createDataset(data, window): dataX= [] for i in range(len(data)-window): temp = [] for j in range(i, i+window): temp.append(data[j,0]) dataX.append(temp) return np.array(dataX)
f = open("/Users/ahsentob/stock_predictions/own_prediction/data_collect/complete_stock_data.txt",'r') message = [line.rstrip('\n') for line in f] fetch_list.extend(message) store_file_list.extend(message) start_pos = 0 for i in range(len(fetch_list)): store_path = '/Users/ahsentob/stock_predictions/own_prediction/data_collect/historical_data/'+store_file_list[i]+'.csv' if os.path.exists(store_path): start_pos = i now = datetime.datetime.now() #time_list = [now.year-10,now.month,now.day] time_list = [2005,1,1] for i in range(start_pos,len(fetch_list)): store_path = '/Users/ahsentob/stock_predictions/own_prediction/data_collect/historical_data/'+store_file_list[i]+'.csv' if os.path.exists(store_path): continue if i % 1 == 0: print(i) print(fetch_list[i]) try: data = Fetcher(fetch_list[i], time_list) hist = data.getHistorical() hist.to_csv(store_path) except: print("Could not get data for "+fetch_list[i])
def __init__(self, tick, url="google", folder="cache", begin=None, end=None, sep=",", intern=False, use_dtime=False): """ @param tick tick name, ex ``NASDAQ:MSFT`` @param url if yahoo, downloads the data from there if it was not done before url is possible, ``'google'``, ``'yahoo_new'``, ``'quandl'`` are predefined values @param folder cache folder (created if it does not exists @param begin first day (datetime), see below @param end last day (datetime), see below @param sep column separator @param intern do not use unless you know what to do (see :meth:`__getitem__ <pyensae.finance.astock.StockPrices.__getitem__>`) @param use_dtime if True, use DateTime instead of string """ if isinstance(url, pandas.DataFrame): self.datadf = url self.tickname = tick if "Date" not in url.columns: raise StockPricesHTTPException( "the dataframe does not contain any column 'Date': {0}". format(",".join(_ for _ in url.columns))) elif isinstance(tick, str) and is_file_string(tick) and os.path.exists(tick): self.tickname = os.path.split(tick)[-1] with open(tick, "r") as f: for line in f.readlines(): if line.startswith('<!DOCTYPE html PUBLIC'): raise StockPricesHTTPException( "pandas cannot parse the file, check your have access to internet: " + str(tick)) break try: self.datadf = pandas.read_csv(tick, sep=sep) except Exception as e: with open(tick, "r") as t: content = t.read() if "Firewall Authentication" in content: raise StockPricesException( "pandas cannot parse the file, check your have access to internet: " + str(tick)) from e raise else: if not os.path.exists(folder): try: os.mkdir(folder) except PermissionError as e: raise StockPricesException(( "PermissionError, unable to create directory '{0}', " + "check you execute the program in a folder you have " + "permission to modify ({1})").format( folder, os.getcwd())) from e self.tickname = tick if begin is None: begin = datetime.datetime(2000, 1, 3) if end is None: now = datetime.datetime.now() end = now - datetime.timedelta(1) sbeg = begin.strftime("%Y-%m-%d") send = end.strftime("%Y-%m-%d") name = os.path.join( folder, tick.replace(":", "_").replace("/", "_").replace("\\\\", "_") + ".{0}.{1}.txt".format(sbeg, send)) date_format = None if not os.path.exists(name): if url == "google": use_url = True url_string = "https://finance.google.com/finance/historical?q={0}".format( self.tickname) url_string += "&startdate={0}&enddate={1}&output=csv".format( begin.strftime('%b %d, %Y'), end.strftime('%b %d, %Y')) url = url_string.replace(" ", "+").replace(",", "%2C") date_format = "%b-%d-%Y" elif url == "quandl": import quandl # pylint: disable=C0415 df = quandl.get("EURONEXT/BNP", start_date=begin.strftime('%Y-%m-%d'), end_date=end.strftime('%Y-%m-%d')) df.reset_index(drop=False).to_csv(name, sep=sep, index=False) use_url = False elif url == 'yahoo_new': from yahoo_historical import Fetcher data = Fetcher(tick, [begin.year, begin.month, begin.day], [end.year, end.month, end.day]) df = data.getHistorical() df.to_csv(name, sep=sep, index=False) use_url = False elif url in ("yahoo", "google", "fred", "famafrench"): import pandas_datareader.data as web # pylint: disable=C0415 df = web.DataReader(self.tickname, url, begin, end).reset_index(drop=False) df.to_csv(name, sep=sep, index=False) use_url = False else: raise StockPricesHTTPException( "Unable to download data '{0}' from the following website '{1}'" .format(tick, url)) if use_url: self.url_ = url try: u = urllib.request.urlopen(url) text = u.read() u.close() except urllib.error.HTTPError as e: raise StockPricesHTTPException( "HTTPError, unable to load tick '{0}'\nURL: {1}". format(tick, url)) from e if len(text) < 10: raise StockPricesHTTPException( "nothing to download for '{0}' less than 10 downloaded bytes" .format(tick)) try: f = open(name, "wb") f.write(text) f.close() except PermissionError as e: raise StockPricesException(( "PermissionError, unable to create directory '{0}', " + "check you execute the program in a folder you have " + "permission to modify ({1})").format( folder, os.getcwd())) from e else: self.url_ = name try: self.datadf = pandas.read_csv(name, sep=sep) except Exception as e: with open(tick, "r") as t: content = t.read() if "Firewall Authentication" in content: raise StockPricesException( "pandas cannot parse the file, check your have access to internet '{0}'" .format(tick)) from e raise if date_format is not None: self.datadf["Date"] = pandas.to_datetime(self.datadf["Date"]) self.datadf["Date"] = self.datadf["Date"].apply( lambda x: x.strftime('%Y-%m-%d')) self.datadf.to_csv(name, sep=sep, index=False) if use_dtime: self.datadf["Date"] = pandas.to_datetime(self.datadf["Date"]) if not intern: try: self.datadf = self.datadf.sort_values("Date") except ValueError as e: if "'Date' is both an index level and a column label" in str( e): vals = self.datadf['Date'] ind = self.datadf.index if numpy.array_equal(vals, ind): self.datadf = self.datadf.sort_index() else: raise StockPricesException( "Columns Date and index are different.") from e else: raise except AttributeError: self.datadf = self.datadf.sort("Date") except KeyError as e: raise StockPricesException("schema: {}".format(",".join( self.datadf.columns))) from e self.datadf.reset_index(drop=True, inplace=True) self.datadf.set_index("Date", drop=False, inplace=True)
import numpy as np import pandas as pd import matplotlib.pyplot as plt ##Dataset from yahoo finance from yahoo_historical import Fetcher namasaham="bmri" namasaham=namasaham.upper() saham= Fetcher(namasaham+".JK", [2012,7,12], [2019,7,12], interval="1d") saham=saham.getHistorical() #Open Price saham=saham.iloc[:,1:2] # print(saham) ##Data Preprocessing #delete na values saham=saham.dropna() #split dataset into training and testing percent_train=0.75 trainingset=saham.iloc[:int(len(saham)*percent_train),:] testset=saham.iloc[int(len(saham)*percent_train):,:] # Scale the features from sklearn.preprocessing import MinMaxScaler sc=MinMaxScaler() trainingset=sc.fit_transform(trainingset) testset=sc.fit_transform(testset)
#List of tickers Symbols = data1['Symbol'].append(data2['Symbol']).drop_duplicates() #Calculate time for run import time t0 = time.time() # Create Empty Dataframe stock_final = pd.DataFrame() #loop through each ticker and download for i in Symbols: print(i, sep=',', end=',', flush=True) try: stock = [] data = Fetcher(i, [2019, 5, 16], [2019, 7, 16]) stock = data.getHistorical() if len(stock) == 0: None else: stock['Name'] = i stock_final = stock_final.append(stock, sort=False) except Exception: # Replace Exception with something more specific. None t1 = time.time() #total time taken total = t1 - t0
import numpy as np from pandas_datareader import data as wb import fix_yahoo_finance as yf yf.pdr_override() #DJ = wb.get_data_yahoo("DJI", start="2015-05-01", end="2018-05-01") from yahoo_historical import Fetcher data = Fetcher("VZ", [2015, 5, 1], [2018, 5, 2], '1mo') print(data.getHistorical().ix[:, [0, 5]]) data.getHistorical().ix[:, [0, 5]].to_csv( "/Users/adityaprasann/IdeaProjects/PythonProj/downloaddata/temp.csv") import csv with open("/Users/adityaprasann/IdeaProjects/PythonProj/downloaddata/temp.csv", "rt") as source: rdr = csv.reader(source) with open( "/Users/adityaprasann/IdeaProjects/PythonProj/downloaddata/VZ.csv", "wt") as result: wtr = csv.writer(result) for r in rdr: wtr.writerow((r[1], r[2])) #print(DJ.head())
import pandas as pd from yahoo_historical import Fetcher df = pd.read_csv('ind_nifty50list.csv', parse_dates=True, index_col=0) -- print(df) data = Fetcher("AAPL", [2007,1,1], [2017,1,1]) data = Fetcher() print(data.getHistorical())