"Use proper inputs to download stock data" - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: "Use proper inputs to download stock data" (/thread-7882.html) Pages:
1
2
|
"Use proper inputs to download stock data" - Alberto - Jan-28-2018 I have a code below that tries to ask a user to enter 5 countries so that in return she gets the data for stock index price of that country (Say if she enters USA in return she gets info for S&P 500). I use error-handling properties so that if there is no country with the index it asks the user to re-enter the country. The problem is that the code does not work and I am stuck. Can you please, help me with it. 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 while exchange1 is None or exchange2 is None or exchange3 is None or exchange4 is None or exchange5 is None: exchange1, exchange2, exchange3, exchange4, exchange5 = input('Please, enter 5 countries for obtaining corresponding stock indices (with a comma in between): ').split(',') try: 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 if exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "France": ticker = "CHRIS/LIFFE_FCE1" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "USA": ticker = "MULTPL/SP500_REAL_PRICE_MONTH" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Germany": ticker = "CHRIS/EUREX_FDAX1" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Hong_Kong": ticker = "CHRIS/HKEX_HSI1" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "India": ticker = "NSE/CNX_NIFTY" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "Japan": ticker = "NIKKEI/ALL_STOCK" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "England": ticker = "CHRIS/LIFFE_Z1" elif exchange1 or exchange2 or exchange3 or exchange4 or exchange5 is "England": ticker = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX" date_time = date.today() #define today's date one_month_ago = date_time - timedelta(years=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 = one_month_ago.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format global data #definre dataframe as global data = quandl.get(ticker, start_date=start, end_date=end) except (SyntaxError, NotFoundError): " " print('Incorrect arguments. Please, try another country.') ticker = None return "Here are the results" program = MainFormula() print(program) RE: "Use proper inputs to download stock data" - j.crater - Jan-28-2018 Can you be more specific about what doesn't work? If you get an error post full traceback message in error tags. If the output is not as expected, show the output you get as well as desired output. From what I see the only possible result is "Here are the results" . Because no matter what the MainFormula() does, you return this fixed string on line 46.return "Here are the results" RE: "Use proper inputs to download stock data" - Alberto - Jan-28-2018 Thank you for the reply. The error that I get is as follows: The expected result is that once the "user" inputs 5 countries the code will download 5 indexes from Quandl.
RE: "Use proper inputs to download stock data" - j.crater - Jan-28-2018 Which line produces this error? It would be great if you copied full traceback message. RE: "Use proper inputs to download stock data" - Alberto - Jan-28-2018
RE: "Use proper inputs to download stock data" - j.crater - Jan-28-2018 Assigning value to the 5 exchange variables expect an iterable (e.g. tuple or list) of 5 elements to be unpacked. You provide only 1 (None) element. One solution is: exchange1, exchange2, exchange3, exchange4, exchange5 = [None]*5 RE: "Use proper inputs to download stock data" - buran - Jan-28-2018 there are SO MANY problems with this code... for start, read https://python-forum.io/Thread-Multiple-expressions-with-or-keyword one_month_ago = date_time - timedelta(years=10) # define the date one month agoone month or 10 years ago? rest is issues with style and logic, but anyway... RE: "Use proper inputs to download stock data" - Alberto - Jan-28-2018 Thank you! I have changed it. However, I still do not get 5 datasets. For example, the user enters: USA, Germany, France, China, England, but I get one dataset. How can I ensure that there are 5 datasets uploaded associated with these countries? RE: "Use proper inputs to download stock data" - j.crater - Jan-28-2018 What is the new code? And what is the new error or undesired output vs desired? Beside that, I encourage you to take your time and do as buran suggested you. Your debugging and correcting code will be much easier that way, and less time will be wasted on figuring why things don't work as expected. RE: "Use proper inputs to download stock data" - Alberto - Jan-28-2018 Thank you, I changed the problem with potential input values. The thing is that I get "data" - line 41 of only one stock index. However, I need five datasets as the user introduces 5 countries. Here is the modified code: 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, exchange2, exchange3, exchange4, exchange5 = input('Please, enter 5 countries for obtaining corresponding stock indices (with a comma in between): ').split(',') try: 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 if exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "france": ticker = "CHRIS/LIFFE_FCE1" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "usa": ticker = "MULTPL/SP500_REAL_PRICE_MONTH" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "germany": ticker = "CHRIS/EUREX_FDAX1" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "hong_kong": ticker = "CHRIS/HKEX_HSI1" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "india": ticker = "NSE/CNX_NIFTY" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "japan": ticker = "NIKKEI/ALL_STOCK" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "england": ticker = "CHRIS/LIFFE_Z1" elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "china": ticker = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX" 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 #definre dataframe as global data = quandl.get(ticker, start_date=start, end_date=end) except (SyntaxError, NotFoundError): " " print('Incorrect arguments. Please, try another country.') ticker = None return "Here are the results" program = MainFormula() print(program) |