Posts: 8,160
Threads: 160
Joined: Sep 2016
(Jan-28-2018, 08:40 PM)buran Wrote: for start, read https://python-forum.io/Thread-Multiple-...or-keyword if exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "france": do you see the connection?
Even after you fix this, you will get always data for one country, because of the if/elifs.
Posts: 4,794
Threads: 76
Joined: Jan 2018
I don't think lines like
elif exchange1.lower() or exchange2.lower() or exchange3.lower() or exchange4.lower() or exchange5.lower() is "germany": do what you expect they do. I would define first
exchanges = [exchange1, exchange2, exchange3, exchange4, exchange5] and write for example
elif any(e.lower() == 'germany' for e in exchanges):
Posts: 42
Threads: 20
Joined: May 2017
Thank you for the answers. However, I am still struggling to get the data for all indeces
Posts: 1,150
Threads: 42
Joined: Sep 2016
If you can provide more details, someone may be able to assist you.
Posts: 42
Threads: 20
Joined: May 2017
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)
Posts: 42
Threads: 20
Joined: May 2017
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.
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)
|