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"
#16
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)  
Reply


Messages In This Thread
How to change a name of database is a loop? - by Alberto - Jan-29-2018, 02:03 PM

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