def getPredFromDB(): predData = pred.query.order_by(desc(pred.date)).limit(61).all() date, predPrice = [], [] for i in predData: date.insert(0, i.date) predPrice.insert(0, i.closePrice) closePrice = getPriceFromDB() return (date, closePrice, predPrice)
def getCryptoHist(coin): dates = three_months_back() print(dates) url = 'https://coinmarketcap.com/currencies/' + coin + '/historical-data/?start=%s&end=%s' % ( dates[1], dates[0]) print(url) resp = requests.get(url) soup = bs.BeautifulSoup(resp.text) sevenDay = soup.find("div", {"id": "historical-data"}) tab = sevenDay.find('table') body = tab.find('tbody') rows = body.findAll('tr') date = [] openPrice = [] high = [] low = [] close = [] volume = [] marketCap = [] for row in rows: #pack lists col = row.findAll('td') col0 = col[0].text.strip() date.insert(0, col0) col1 = col[1].text.strip() openPrice.insert(0, float(col1)) col2 = col[2].text.strip() high.insert(0, float(col2)) col3 = col[3].text.strip() low.insert(0, float(col3)) col4 = col[4].text.strip() close.insert(0, float(col4)) col5 = col[5].text.strip() col5 = col5.replace(',', '') volume.insert(0, int(col5)) col6 = col[6].text.strip() col6 = col6.replace(',', '') marketCap.insert(0, int(col6)) tableDict = { 'date': date, 'open': openPrice, 'high': high, 'low': low, 'close': close, 'volume': volume, 'marketCap': marketCap } #df = pd.DataFrame(tableDict) return tableDict
def prediction(): # load model json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("model.h5") # Get Latest Historical Data testData = hp.query.order_by(desc(hp.date)).limit(121).all() # get only close price date, testPrice = [], [] for i in testData: date.insert(0, i.date) testPrice.insert(0, i.closePrice) date.append(nextTradingDay()) # Change to np array to reshape testPrice = np.asarray(testPrice) # reshape array to apply sc transform testPrice = testPrice.reshape(-1, 1) sc = load('std_scaler.bin') testPrice = sc.transform(testPrice) # prepare data for prediction X_test = [] for i in range(60, testPrice.shape[0]): X_test.append(testPrice[i - 60:i, 0]) X_test = np.array(X_test) X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1)) # perform prediction result = loaded_model.predict(X_test) # Return actual result normal_result = sc.inverse_transform(result) normal_result = np.array(normal_result).tolist() predPrice = [] for i in normal_result: predPrice.append(round(i[0], 4)) return (date[61:122], predPrice)
def country_wise(): url_cases = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv' url_death = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv' url_recovered = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv" df_cases = pd.read_csv(url_cases) df_death = pd.read_csv(url_death) df_recovered = pd.read_csv(url_recovered) #year = (datetime.now()).strftime('%Y') data_cases = df_cases.groupby(['Country/Region']).sum() data_death = df_death.groupby(['Country/Region']).sum().reset_index() data_recovered = df_recovered.groupby(['Country/Region']).sum().reset_index() data_cases.drop(columns=['Lat','Long'],inplace=True) data_death.drop(columns=['Lat','Long'],inplace=True) data_recovered.drop(columns=['Lat','Long'],inplace=True) date = [] for i in data_cases.columns: a = i.split('/') b = a[1]+ ' '+ calendar.month_abbr[int(a[0])] + ' 20'+ a[2] date.append(b) date.insert(0,'Country') data_cases = data_cases.reset_index() X_cases = data_cases.iloc[:,:].values X_deaths = data_death.iloc[:,:].values X_rec = data_recovered.iloc[:,:].values new_cases = pd.DataFrame(X_cases,columns = [date]) new_deaths = pd.DataFrame(X_deaths,columns = [date]) new_rec = pd.DataFrame(X_rec,columns = [date]) new_cases.to_csv('data/country_wise_cases.csv') new_deaths.to_csv('data/country_wise_deaths.csv') new_rec.to_csv('data/country_wise_recovered.csv')
def formatDate(dates): date = dates.split() if len(date) == 1: # if it only contains the year date.insert(0, '1') date.insert(1, 'JAN') if len(date) == 2: # if it only contains month and year date.insert(0, '1') if (int(date[0]) in range(1, 10)): date[0] = "0" + date[0] month = months[date[1]] year = date[2] return (year + '-' + month + '-' + date[0])