Jan-29-2018, 02:03 PM
Dear Python Users,
I am struggling to change a name of database in a loop (lines 47-49). What the program is doing - it prompts a user to enter a country and automatically download the data of a stock index from Quandl database. However, once I do this, I want to call data frame like "data_country". Please, advise me this issue.
I am struggling to change a name of database in a loop (lines 47-49). What the program is doing - it prompts a user to enter a country and automatically download the data of a stock index from Quandl database. However, once I do this, I want to call data frame like "data_country". Please, advise me this issue.
import quandl #To extract the data from Quandl website from quandl.errors.quandl_error import NotFoundError #for error handling from datetime import date, timedelta # to define the date and time, while "timedelta" - for 1 month back import matplotlib.pyplot as plt #Plotting library import pandas as pd #Time series working library from scipy import interpolate #Scipy library for interpolation import numpy as np from scipy.optimize import leastsq def MainFormula(): exchange1, exchange2, exchange3, exchange4, exchange5 = [None]*5 while exchange1 is None or exchange2 is None or exchange3 is None or exchange4 is None or exchange5 is None: exchange1 = input('Please, choose a country from Germany, France, USA, HongKong, India, England, Japan or China: ') exchange2 = input('Please, choose a second country for comparison: ') exchange3 = input('Please, choose a third country for comparison: ') exchange4 = input('Please, choose a fourth country for comparison: ') exchange5 = input('Please, choose a fifth country for comparison: ') exchange1 = str(exchange1) #ensure that entered exchage is string type exchange2 = str(exchange2) #ensure that entered exchage is string type exchange3 = str(exchange3) #ensure that entered exchage is string type exchange4 = str(exchange4) #ensure that entered exchage is string type exchange5 = str(exchange5) #ensure that entered exchage is string type exchanges = [exchange1, exchange2, exchange3, exchange4, exchange5] if any(e.lower() == 'germany' for e in exchanges): ticker = "CHRIS/EUREX_FDAX1" elif any(e.lower() == 'france' for e in exchanges): ticker = "CHRIS/LIFFE_FCE1" elif any(e.lower() == 'usa' for e in exchanges): ticker = "MULTPL/SP500_REAL_PRICE_MONTH" elif any(e.lower() == 'hong kong' for e in exchanges): ticker = "CHRIS/HKEX_HSI1" elif any(e.lower() == 'india' for e in exchanges): ticker = "NSE/CNX_NIFTY" elif any(e.lower() == 'japan' for e in exchanges): ticker = "NIKKEI/ALL_STOCK" elif any(e.lower() == 'england' for e in exchanges): ticker = "CHRIS/LIFFE_Z1" elif any(e.lower() == 'china' for e in exchanges): ticker = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX" try: date_time = date.today() #define today's date ten_years = date_time - timedelta(days=365*10) # define the date one month ago end = date_time.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format start = ten_years.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format global data for i in exchanges: global data data[i] = quandl.get(ticker, start_date=start, end_date=end) except (SyntaxError, NotFoundError): " " print('Incorrect arguments. Please, try another country.') exchange3 = None return "Here are the results" program = MainFormula() print(program)