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"
#15
I tried to re-write the code. Once, I enter the countries specified in the code (England, USA, etc.) everything works (Except for Hong Kong, the python does not provide any bug). However, once I enter a country name out of the list (e.g. Canada) I expect that the code provides "'Incorrect arguments. Please, try another country.') However, it does not. Please, advise me on 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, 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
            exchanges = [exchange1, exchange2, exchange3, exchange4, exchange5]
            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
            if any(e.lower() == 'germany' for e in exchanges):
                ticker = "CHRIS/EUREX_FDAX1"
                global data_germany   #definre dataframe as global
                data_germany = quandl.get(ticker, start_date=start, end_date=end)               
            if any(e.lower() == 'france' for e in exchanges):
                ticker2 = "CHRIS/LIFFE_FCE1"
                global data_france   #definre dataframe as global
                data_france = quandl.get(ticker2, start_date=start, end_date=end)                 
            if any(e.lower() == 'usa' for e in exchanges):
                ticker3 = "MULTPL/SP500_REAL_PRICE_MONTH"
                global data_usa   #definre dataframe as global
                data_usa = quandl.get(ticker3, start_date=start, end_date=end)               
            if any(e.lower() == 'hong kong' for e in exchanges):
                ticker4 = "CHRIS/HKEX_HSI1"
                global data_hong   #definre dataframe as global
                data_hong = quandl.get(ticker4, start_date=start, end_date=end)               
            if any(e.lower() == 'india' for e in exchanges):
                ticker5 = "NSE/CNX_NIFTY"
                global data_india   #definre dataframe as global
                data_india = quandl.get(ticker5, start_date=start, end_date=end)                  
            if any(e.lower() == 'japan' for e in exchanges):
                ticker6 = "NIKKEI/ALL_STOCK"
                global data_japan   #definre dataframe as global
                data_japan = quandl.get(ticker6, start_date=start, end_date=end)                 
            if any(e.lower() == 'england' for e in exchanges):
                ticker7 = "CHRIS/LIFFE_Z1"
                global data_england   #definre dataframe as global
                data_england = quandl.get(ticker7, start_date=start, end_date=end)                
            if any(e.lower() == 'china' for e in exchanges):
                ticker8 = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX"
                global data_china   #definre dataframe as global
                data_china = quandl.get(ticker8, 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)  
Reply


Messages In This Thread
RE: "Use proper inputs to download stock data" - by Alberto - Jan-28-2018, 10:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Stock market data in LibreOffice Calc Pitone 3 4,357 Jan-14-2021, 04:34 AM
Last Post: DrinkinBeer
Photo How to do a graph in Python? (and proper terminology) jpy 2 2,113 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