Python Forum
"Use proper inputs to download stock data"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Use proper inputs to download stock data"
#1
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)  
Reply
#2
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"
Reply
#3
Thank you for the reply. The error that I get is as follows:
Error:
exchange1, exchange2, exchange3, exchange4, exchange5 = None TypeError: 'NoneType' object is not iterable
The expected result is that once the "user" inputs 5 countries the code will download 5 indexes from Quandl.
Reply
#4
Which line produces this error? It would be great if you copied full traceback message.
Reply
#5
Error:
File "C:/Users/Barca/Documents/Project/Courses/Python/c1/Project/project.py", line 11, in MainFormula exchange1, exchange2, exchange3, exchange4, exchange5 = None TypeError: 'NoneType' object is not iterable
Reply
#6
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
Reply
#7
there are SO MANY problems with this code... for start, read https://python-forum.io/Thread-Multiple-...or-keyword

one_month_ago = date_time - timedelta(years=10) # define the date one month ago
one month or 10 years ago?

rest is issues with style and logic, but anyway...
Reply
#8
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?
Reply
#9
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.
Reply
#10
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)  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Stock market data in LibreOffice Calc Pitone 3 4,240 Jan-14-2021, 04:34 AM
Last Post: DrinkinBeer
Photo How to do a graph in Python? (and proper terminology) jpy 2 2,061 Dec-23-2020, 01:07 PM
Last Post: codeto

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020